Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.

First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p_1, the glass from the second position to position p_2 and so on. That is, a glass goes from position i to position p_{i}. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.

After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.


-----Input-----

The first line contains three integers: n, s, t (1 ≤ n ≤ 10^5; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n) — the shuffling operation parameters. It is guaranteed that all p_{i}'s are distinct.

Note that s can equal t.


-----Output-----

If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.


-----Examples-----
Input
4 2 1
2 3 4 1

Output
3

Input
4 3 3
4 1 3 2

Output
0

Input
4 3 4
1 2 3 4

Output
-1

Input
3 1 3
2 1 3

Output
-1

Difficulty: interview

Starter code:

Solutions:
rd = lambda: list(map(int, input().split()))

n, s, t = rd()
p = rd()
for i in range(n):
  if s == t : print(i); return
  s = p[s-1]
print(-1)

N,s,t=list(map(int,input().split()))

A=list(map(int,input().split()))

s-=1
t-=1
ans=0
Taken=[False]*(N+1)
while(Taken[s]==False and s!=t):
    Taken[s]=True
    s=A[s]-1
    ans+=1

if(s==t):
    print(ans)
else:
    print(-1)
    


import sys
f = sys.stdin
#f = open("input.txt", "r")
l = [int(i) for i in f.readline().strip().split()]
n, s, t = l[0], l[1], l[2]
p = [int(i) for i in f.readline().strip().split()]
def solve():
    count = 0
    b = s
    if (s == p.index(s)+1 or t == p.index(t)+1) and t != s:
        count = -1
    else:
        while b != t:
            b = p[b-1]
            count += 1
            if count >= n:
                count = -1
                break
    print(count)
solve()

import sys

values = [int(x) for x in sys.stdin.readline().split()]
n_glasses, start, end = values[0], values[1], values[2]
positions = [int(x) for x in sys.stdin.readline().split()]
used = {}
min_swaps = 0

start -= 1

while start != end-1:
    if start in used:
        print("-1")
        return
    min_swaps += 1
    used[start] = True
    start = positions[start]-1
    
print(min_swaps)



n, a, b = map(int, input().split())
t = [0] + list(map(int, input().split()))

if a == b: print(0)
else:
    x, s = t[a], 1

    while x != b and x != a:
        x = t[x]
        s += 1
    if x == a: print(-1)
    else: print(s)

n, s, t = list(map(int, input().split()))
p = [0]
p.extend(list(map(int, input().split())))
f = [0]*(n+1)

f[s] = 1
cnt = 0
while s != t:
    s = p[s]
    cnt += 1
    if f[s] == 1: break

if s == t:
    print(cnt)
else:
    print(-1)



import sys
import math

n, s, t = [int(x) for x in (sys.stdin.readline()).split()]
p = [int(x) for x in (sys.stdin.readline()).split()]

res = 0
k = s
while(s != t and res < n):
    s = p[s - 1]
    res += 1
    
if(t != s and res >= n):
    print(-1)
else:    
    print(res)

import sys
import math

n, s, t = [int(x) for x in (sys.stdin.readline()).split()]
p = [int(x) for x in (sys.stdin.readline()).split()]

res = 0
k = s
while(s != t):
    s = p[s - 1]
    res += 1
    if(k == s):
        print(-1)
        return
    
print(res)

n, s, t = (int(x) for x in input().split())
p = [0] + [int(x) for x in input().split()]
val = 0
while s > 0 and s != t:
    nxt = p[s]
    p[s] = 0
    s = nxt
    val += 1
print(val if s else -1)

n, s, t = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
s -= 1
t -= 1
if s == t:
	print(0)
	return
cnt = 0
for i in range(n):
	next = p[s]
	if next == -1:
		break
	p[s] = -1
	s = next - 1
	cnt += 1
	if s == t:
		print(cnt)
		return
print(-1)


def main():
    n, s, t = map(int, input().split())
    l = [None]
    l.extend(map(int, input().split()))
    for i in range(n):
        if s == t:
            print(i)
            return
        s = l[s]
    print(-1)


def __starting_point():
    main()
__starting_point()

# coding: utf-8
n, s, t = [int(i) for i in input().split()]
p = [0] + [int(i) for i in input().split()]
if s == t:
    print(0)
    return
pos = p[s]
ans = 1
while pos != t:
    pos = p[pos]
    ans += 1
    if pos == s:
        print('-1')
        break
else:
    print(ans)


rd=lambda: list(map(int, input().split()))
n,s,t=rd()
A=rd()
cur=s
ans=0
for i in range(n):
   if(cur==t):
      print(ans);
      return
   cur=A[cur-1]
   ans=ans+1
print(-1)

n, s, t = list(map(int, input().split()))
p = list(map(int, input().split()))
if s == t:
    print(0)
else:
    k = set(); ans = 0
    while 1:
        s = p[s-1]
        if s in k:
            print(-1)
            break
        k.add(s)
        ans += 1
        if s == t:
            print(ans)
            break


# your code goes here
rd = lambda : list(map(int,input().strip().split()))
n,s,t = rd()
A = rd()
for i in range(n):
    if s == t:
        print(i);return
    s = A[s-1]
print(-1)   


n,s,t = input().split()
n=int(n)
s=int(s)
t=int(t)
l=input().split()
for i in range(n):
    l[i]=int(l[i])
j=0
i=s
flag=0
if(s==t):
    flag=1
while(j<n and flag!=1):
    if(i==l[i-1]):
        if(i==t):
            flag=1
            break
        break
    i=l[i-1]
    if(i==t):
        flag=1
        j+=1
        break
    j+=1

if(flag):
    print(j)
else:
    print(-1)



n,s,t = map(int,input().split())
l = []
mp = map(int,input().split())
for i in mp:
    l.append(i)
a = [-1 for i in range(n)]
cur=0
next=s
for i in range(n):
    cur = next
    if a[cur-1]==-1:
        a[cur-1] = i
    else:
        break
    next = l[cur-1]
    #print(a)

print(a[t-1])

'''
def main():
    from sys import stdin,stdout
def __starting_point():
    main()
'''
#118B
'''
def main():
    from sys import stdin,stdout
    n=int(stdin.readline())
    for i in range(n+1):
        stdout.write(' '*(2*(n-i)))
        for k in range(i+1):
            if k:
                if k==1:
                    stdout.write(' 1 ')
                else:
                    stdout.write(str(k)+' ')
            else:
                stdout.write('0')
        for k in range(i-1,-1,-1):
            if k:
                stdout.write(str(k)+' ')
            else:
                stdout.write('0')
        stdout.write('')
    for j in range(i-1,-1,-1):
        stdout.write(' '*2*(n-j))
        for k in range(j+1):
            if k:
                if k==1:
                    stdout.write(' 1 ')
                else:
                    stdout.write(str(k)+' ')
            else:
                stdout.write('0')
        for k in range(j-1,-1,-1):
            if k:
                stdout.write(str(k)+' ')
            else:
                stdout.write('0')
        stdout.write('')
def __starting_point():
    main()
'''
#499B
'''
def main():
    from sys import stdin,stdout
    n,m=map(int,stdin.readline().split())
    mydic={}
    for _ in range(m):
        a,b=map(str,stdin.readline().strip().split())
        mydic[a]=b
    for k in stdin.readline().strip().split():
        if len(mydic[k])<len(k):
            stdout.write(mydic[k]+' ')
        else:
            stdout.write(k+' ')
def __starting_point():
    main()
'''
#450A
'''
def main():
    from sys import stdin,stdout
    n,m=map(int,stdin.readline().split())
    k=list(range(1,n+1))
    a=list(map(int,stdin.readline().split()))
    while k!=[]:
        top=k[0]
        if a[top-1]<=m:
            k=k[1:]
            a[top-1]=0
        else:
            a[top-1]-=m
            k=k[1:]+[top]
    stdout.write(str(top))
def __starting_point():
    main()
'''
#550C
'''
def main():
    from sys import stdin,stdout
    n=stdin.readline().strip()
    i=0
    k=2
    t=''
    for j in n[::-1]:
        if j=='0':
            t='0'
            break
        elif j=='8':
            tz='8'
            break
        i+=1
        if k==16:
            break
        if int(j+t) & (k-1):
            continue
        else:
            t=j+t
            k<<=1
    #print(t,i)
    if t!='':
        if int(t)%8==0:
            if i!=len(n) and t!='0' and t!='8':
                stdout.write('YES\n'+n[:len(n)-i+1]+t)
            else:
                stdout.write('YES\n'+t)
        else:
            stdout.write('NO')
    else:
        stdout.write('NO')
def __starting_point():
    main()
'''
#476B
'''
def main():
    from sys import stdin,stdout
    a=stdin.readline().strip()
    b=stdin.readline().strip()
    apc=0
    anc=0
    bpc=0
    bnc=0
    bqc=0
    for i in a:
        if i =='+':
            apc+=1
        elif i=='-':
            anc+=1
    for i in b:
        if i=='+':
            bpc+=1
        elif i=='-':
            bnc+=1
        else:
            bqc+=1
    #print(apc,anc,bpc,bnc,bqc)
    if apc==bpc and anc==bnc and bqc==0:
        p=1
    elif bpc>apc or bnc>anc or len(a)!=len(b):
        p=0
    else:
        denom=(1<<bqc)
        numer=1
        maxim=max(apc-bpc,anc-bnc)
        minim=min(aspc-bpc,anc-bnc)
        while(bqc>maxim):
            #print(numer)
            numer*=bqc
            bqc-=1
        if minim>1:
            k=minim
            while(minim>=2):
            #print('k',k)
                k*=(minim-1)
                minim-=1
            numer/=k
        p=numer/denom
    stdout.write('{:.12f}'.format(p))
def __starting_point():
    main()
'''
'''
def main():
    from sys import stdin,stdout
    for _ in range(int(stdin.readline())):
        l1,l2,l3,n=stdin.readline().strip().split()
        L=l1+l2*int(n)+l3
        n=int(L,2)
        c=0
        while n>=0:
            n=(n&(n+1)) -1
            #print(n)
            c+=1
        stdout.write(str(c)+'\n')
def __starting_point():
    main()
'''
#230B
'''
def main():
	from sys import stdin,stdout
	s=[]
	for i in range(1000000):
		s.append(True)
	for i in range(2,1000000):
		if s[i-1]:
			j=2*i
			while j<=1000000:
				s[j-1]=False
				j+=i
	#print(s[:10])
	t=int(stdin.readline())
	for i in stdin.readline().strip().split():
		n=int(i)
		k=n**0.5
		if int(k)==k and s[int(k)-1] and n!=1:
			stdout.write('YES\n')
		else:
			stdout.write('NO\n')
def __starting_point():
	main()
'''
#489C
'''
def main():
	import sys
	a,b=map(int,sys.stdin.readline().split())
	if not a:
		maxim='-1'
		minim='-1'
	elif not b:
		if a==1:
			maxim='0'
			minim='0'
		else:
			maxim='-1'
			minim='-1'
	else:
		#finding the maximum number	
		maxim=0
		n=b
		while n:
			if 9>n:
				maxim=maxim*10+n
				n=0
			else:
				n-=9
				maxim=maxim*10+9
		s=str(maxim)
		diff=a-len(s)
		if diff<0:
			maxim='-1'
		elif diff>0:
			maxim=s+'0'*diff
		else:
			maxim=s
		#finding the minimum number
		if diff:
			if diff<0:
				minim='-1'
			else:
				s=maxim[::-1]
				for i in range(len(s)):
					if int(s[i]):
						break
				
				minim='1'+'0'*(i-1)+str(int(s[i])-1)+s[i+1:]
		else:
			minim=maxim[::-1]
	sys.stdout.write(minim+' '+maxim+'\n')
def __starting_point():
	main()
'''
#230B
'''
def main():	
	import sys
	import bisect
	C=int(1e6)
	l=[]
	for i in range(C):
		l.append(True)
	for i in range(3,C,2):
		if l[i-1]:
			j=3*i
			while j<=C:
				l[j-1]=False
				j+=2*i
	for i in range(3,C,2):
		l[i]=False
	l[0]=False
	s=[]
	for i in range(C):
		if l[i]:
			s.append((i+1)*(i+1))
	#print(s[:10])
	n=int(sys.stdin.readline())
	tup=tuple(map(int,sys.stdin.readline().split()))
	for i in tup:
		pos=bisect.bisect_left(s,i)
		if pos!=len(s) and s[pos]==i:
			sys.stdout.write('YES\n')
		else:
			sys.stdout.write('NO\n')
	#print(s[:10])
def __starting_point():
	main()
'''
#508B TLE
'''
def main():
	import sys
	flag=False
	minim=10
	minindex=-1
	maxindex=-1
	n=sys.stdin.readline().strip()
	for i in range(len(n)):
		if (n[i]=='2' or n[i]=='4' or n[i]=='6' or n[i]=='8'or n[i]=='0') and int(n[i])<=minim:
			if minindex<0:
				minindex=i
				minim=int(n[i])
				flag=True
			else:
				maxindex=i
	if flag:	
		s=''
		for i in range(len(n)):
			if i==minindex:
				s+=n[-1]
			elif i==len(n)-1:
				s+=str(minim)
			else:
				s+=n[i]
		if maxindex>0:
			t=''
			for i in range(len(n)):
				if i==maxindex:
					t+=n[-1]
				elif i==len(n)-1:
					t+=str(minim)
				else:
					t+=n[i]
			s=str(max(int(s),int(t)))
	else:
		s='-1'
	sys.stdout.write(s)
def __starting_point():
	main()
'''
#466C
'''
def main():
	import sys
	n=int(sys.stdin.readline())
	t=tuple(map(int,sys.stdin.readline().split()))
	acc=sum(t)
	if acc % 3:
		sys.stdout.write('0')
	else:
		cnt=[]
		const=acc//3
		ss=0
		for i in t:
			if acc-ss==const:
				cnt.append(1)
			else:
				cnt.append(0)
			ss+=i 
		for i in range(n-2,-1,-1):
			cnt[i]+=cnt[i+1];		
		#print(cnt)
		ans=0
		ss=0
		for i in range(n-2):
			ss+=t[i]
			if ss==const:
				ans+=cnt[i+2]
		sys.stdout.write(str(ans)+'\n')
def __starting_point():
	main()
'''
#285B
def main():
	from sys import stdin,stdout
	import collections
	stack=collections.deque()
	n,s,t=list(map(int,stdin.readline().split()))
	M=tuple(map(int,stdin.readline().split()))
	visited=list(0 for x in range(n))
	count=0
	stack.appendleft(s-1)
	while(len(stack)):
		top=stack.popleft()
		if top==t-1:
			break
		else:
			if not visited[M[top]-1]:
				stack.appendleft(M[top]-1)
				visited[M[top]-1]=1
				count+=1
	if top==t-1:
		stdout.write(str(count)+'\n')
	else:
		stdout.write('-1\n')
def __starting_point():
	main()

__starting_point()

ins = input()
ins = ins.split()
list(map(int,ins))
n,s,t = list(map(int,ins))
ins = input()
ins = ins.split()
ins = list(map(int,ins))
p = []
p.append(-1)
p += ins
ans = 0
while(s != t):
    ans += 1
    s = p[s]
    if(ans >= n):
        s = t
        ans = -1
print(ans)


n,s,t=map(int,input().split())
arr=list(map(int,input().split()))
for i in range (n):
    if(s==t):
        print(i)
        return
    s=arr[s-1]
print(-1)

"""http://codeforces.com/problemset/problem/285/B"""
def solve(n, s, t, p):
    p.insert(0, 0)
    for i in range(n + 1):
        if s == t:
            return i
        s = p[s]
    return -1

def __starting_point():
    n, s, t = list(map(int, input().split()))
    p = list(map(int, input().split()))
    res = solve(n, s, t, p)
    print(res)

__starting_point()

# Description of the problem can be found at http://codeforces.com/problemset/problem/285/B

n, s, t = map(int, input().split())
l_m = list(map(int, input().split()))

o_s = s
m_c = 0
while s != t:
    s = l_m[s - 1]
    m_c += 1
    
    if s == o_s:
        m_c = -1
        break
    
print(m_c)

string = input()
numbers = list(map(int, string.split()))
s, t = numbers[1], numbers[2]
string = input()
glasses = list(map(int, string.split()))
temp = s
a = 0
while temp != t:
    temp = glasses[temp - 1]
    if temp == s:
        print(-1)
        break
    a += 1
else:
    print(a)



import sys

line = input().split()
n = int(line[0])
s = int(line[1])
t = int(line[2])

line = input().split()
p = []
for i in line:
	p.append(int(i))

if s == t:
	print(0)
	return

pos = s
moves = 0
while pos != t:
	pos = p[pos - 1]
	moves = moves + 1
	if pos == t:
		print (moves)
		return
	if pos == s:
		print(-1)
		return


def main():
    (n, s, t) = list(map(int, input().split(' ')))
    s -= 1
    t -= 1
    if s == t:
        return 0
    nums = list([int(i) - 1 for i in input().split(' ')])
    slow = s
    fast = nums[slow]
    i = 0
    while slow != t:
        if slow == fast:
            i = -1
            break
        slow = nums[slow]
        fast = nums[nums[fast]]
        i += 1
    return i

print(main())