A factory conveyor is modeled as a singly linked list of item codes, front to back. Instead of stopping, the LAST item's next-hop may point back to an EARLIER item, forming a loop (instead of ending). You are given the item codes and, separately, the 0-indexed position that the last item's next-hop points to (or -1 if there is no loop). Print the ITEM CODE of the first item that belongs to the loop — the loop's entry checkpoint. If there is no loop, print -1.
Input format
Line 1: an integer n — the number of items.
Line 2: n space-separated integers — the item codes, front to back.
Line 3: an integer pos — the 0-indexed item that the last item's next-hop points to, or -1 if there is no loop.
Output format
A single integer: the item code at the loop's entry checkpoint, or -1 if there is no loop.
Constraints
- 1 ≤ n ≤ 100000
- -1 ≤ pos ≤ n-1
- -1000000000 ≤ item code ≤ 1000000000