In the JOI country, there are $N$ post offices, numbered from $1$ to $N$. Each post office has an assigned ”destination,” and the destination of post office $i$ is post office $P_i$. Note that it is possible for $P_i = i$. If a package is sent from post office $i$ at time $t$, it will arrive at post office $P_i$ at time $t + 1$. However, a post office cannot send another package while it is in the process of sending a package. Each post office can store an unlimited number of packages at any given time.
Now, $M$ packages need to be delivered in the JOI country. The $j$-th package arrives at post office $A_j$ at time $0$, and it must eventually be delivered to the assigned post office $B_j$. Given the information about the post offices and the packages, write a program to determine whether it is possible to deliver all the packages to their assigned post offices, and if so, find the smallest possible time at which the last package arrives at its assigned post office.
Read the following data from the standard input.
$N$
$P_1$ $P_2$ $\cdots$ $P_N$
$M$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_M$ $B_M$
Output a single line to the standard output. If it is possible to deliver all the packages to their assigned post offices, output the smallest possible time at which the last package arrives at its assigned post office. Otherwise, output -1 instead.