A shuttle route is stored as nodes numbered 0 to n-1, with node 0 the head. Each node has exactly one outgoing link, given as a next index (or -1 for no link). Following links from the head, if the path enters a cycle, there is a unique first node of that cycle — the junction where the tail path meets the loop. Report that node's index. If following links from the head reaches a -1 link (no cycle), report -1.
Input format
Line 1: an integer n, the number of nodes.
Line 2: n space-separated integers, the node values (empty line when n is 0). These do not affect the answer.
Line 3: n space-separated integers, where the i-th is next[i] in the range [-1, n-1] (empty line when n is 0).
Output format
A single integer: the 0-indexed node index where the cycle begins, or -1 if there is no cycle.
Constraints
- 0 <= n <= 100000
- Each next[i] is -1 or a valid node index in [0, n-1].