Code Submission Evaluation System Login

CSES Problem Set

Teleporters


Task | Statistics


CSES - Teleporters

Time limit:1.00 s Memory limit:512 MB

You are playing a game which consists of $n$ rooms. Each room has a teleporter to some other room (or the room itself).

You have to process $q$ queries of the form: You are now in room $a$ and want to reach room $b$. What is the minimum number of teleportations?

Input

The first input line contains two integers $n$ and $q$: the number of rooms and queries. The rooms are numbered $1,2,\ldots,n$.

The second line contains $n$ integers $t_1,t_2,\ldots,t_n$: for each room, the destination of the teleporter in that room.

Finally, there are $q$ lines that describe the queries. Each line has two integers $a$ and $b$: you are now in room $a$ and want to reach room $b$.

Output

For each query, print the minimum number of teleportations. If it is not possible to reach the destination, print $-1$.

Constraints
Example

Input:
5 3
2 3 2 3 2
1 2
1 3
1 4


Output:
1
2
-1