A satellite relay network is stored as nodes numbered 0 to n-1. Node 0 is the head. Each node has exactly one outgoing link, given as a next index (or -1 if it links to nothing). Starting at the head and repeatedly following next, you either eventually reach a node whose link is -1 (no cycle), or you loop forever inside a cycle. Report the number of distinct nodes that make up that cycle, or 0 if the path from the head terminates without cycling.
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 but describe the nodes.
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 number of nodes in the cycle reachable from the head, or 0 if there is no cycle.
Constraints
- 0 <= n <= 100000
- Each next[i] is -1 or a valid node index in [0, n-1].