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: EASY
Tags: Implementation

Starter code:

Solutions:
def find_marble(n, s, t, x):
	step = 0
	while s != t and x[s - 1] != 0:
		temp = x[s - 1]
		x[s - 1] = 0
		s = temp
		step += 1
	if s == t:
		return step
	else:
		return -1
(n, s, t) = map(int, input().split())
x = list(map(int, input().split()))
print(find_marble(n, s, t, x))


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)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = len(b)
d = a[1]
if a[1] == a[2]:
	print('0')
else:
	l = 0
	while 1:
		if l == c:
			print('-1')
			break
		if d == a[2]:
			print(l)
			break
		l += 1
		d = b[d - 1]


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
(n, s, t) = rinput()
p = get_list()
pos = s
visited = [0 for _ in range(n)]
flag = ans = 0
while pos != t:
	pos = p[pos - 1]
	ans += 1
	if visited[pos - 1] == 1:
		break
	visited[pos - 1] = 1
if pos == t:
	print(ans)
else:
	print(-1)


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
original_s = s
flag = 0
while flag == 0:
	if s == t:
		print(ans)
		flag = 1
	elif l[s - 1] == t:
		ans += 1
		print(ans)
		flag = 1
	elif l[s - 1] != t and l[s - 1] != original_s:
		ans += 1
		s = l[s - 1]
	else:
		print(-1)
		flag = 1


def main():
	(n, s, t) = map(int, input().split(' '))
	s -= 1
	t -= 1
	if s == t:
		return 0
	nums = list(map(lambda i: int(i) - 1, 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())


(n, s, t) = 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


from collections import deque

def bfs(s, t):
	vis = [0] * n
	q = deque()
	q.append(s)
	count = 0
	while len(q) != 0:
		e = q.popleft()
		vis[e] = 1
		if not vis[graph[e]]:
			q.append(graph[e])
			count += 1
			if graph[e] == t:
				return count
		else:
			return -1
(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
if s == t:
	print(0)
	exit()
graph = [-1] * n
for i in range(n):
	graph[i] = a[i] - 1
(s, t) = (s - 1, t - 1)
print(bfs(s, t))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def main():
	(n, s, t) = LI_()
	a = LI_()
	u = set()
	r = 0
	while 1:
		if s == t:
			return r
		u.add(s)
		s = a[s]
		if s in u:
			return -1
		r += 1
	return -1
print(main())


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
if s == t:
	print(0)
else:
	ci = l[s - 1] - 1
	count = 1
	x = 0
	i = ci
	if ci == t - 1:
		print(count)
		x += 1
	else:
		T = 0
		while T < n:
			ci = l[ci] - 1
			count += 1
			if ci == t - 1:
				print(count)
				x += 1
				break
			T += 1
	if x == 0:
		print(-1)


import sys
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())

def solve(n, s, t, arr):
	c = 0
	for i in range(len(arr)):
		if s == t:
			print(c)
			return
		else:
			s = arr[s - 1]
			c += 1
	print(-1)
(n, s, t) = get_int()
arr = int_arr()
solve(n, s, t, arr)


import sys
input = sys.stdin.readline
(n, s, t) = map(int, input().split())
ar = list(map(int, input().split()))
k = 0
d = {s}
while True:
	if s == t:
		print(k)
		exit()
	s = ar[s - 1]
	if s in d:
		break
	k += 1
print(-1)


(n, s, e) = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 0)
count = 0
ok = True
start = s
while s != e:
	s = a[s]
	if s == start:
		ok = False
		break
	count += 1
print({True: count, False: -1}[ok])


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


inp = lambda : [*map(int, input().split())]
(n, s, t) = inp()
a = inp()
current = s
i = 0
while i <= n:
	if current == t:
		print(i)
		quit()
	current = a[current - 1]
	i += 1
print(-1)


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


ins = input()
ins = ins.split()
map(int, ins)
(n, s, t) = map(int, ins)
ins = input()
ins = ins.split()
ins = 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) = list(map(int, input().split()))
p = list(map(int, input().split()))
path = [s]
path_d = {s: 0}
while True:
	try:
		check = path_d[p[path[-1] - 1]] == 0
		break
	except:
		pass
	path_d[p[path[-1] - 1]] = 0
	path.append(p[path[-1] - 1])
	if path[-1] == t:
		break
if s == t:
	print(0)
elif path[-1] == t:
	print(len(path) - 1)
else:
	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)
main()


(n, i, f) = map(int, input().split())
l = list(map(int, input().split()))
if i == f:
	print(0)
elif l[i - 1] == i or l[f - 1] == f:
	print(-1)
else:
	x = i
	flag = 0
	flag2 = 0
	c = 1
	while True:
		if l[x - 1] == f:
			flag = 1
			break
		elif l[x - 1] == i:
			flag2 = 1
			break
		x = l[x - 1]
		c = c + 1
	if flag == 1:
		print(c)
	elif flag2 == 1:
		print(-1)
	else:
		print(-1)


(n, s, t) = map(int, input().split())
swaps = list(map(int, input().split()))
pathLen = 0
while swaps[s - 1] != 0 and s != t:
	temp = swaps[s - 1]
	swaps[s - 1] = 0
	s = temp
	pathLen += 1
if s == t:
	print(pathLen)
else:
	print(-1)


(a, b, c) = map(int, input().split())
p = list(map(int, input().split()))
for i in range(a):
	if b == c:
		print(i)
		exit()
	b = p[b - 1]
else:
	print(-1)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
count = 0
while s != t and p[s - 1] != 0:
	temp = p[s - 1]
	p[s - 1] = 0
	s = temp
	count += 1
if s == t:
	print(count)
else:
	print(-1)


from sys import stdin, stdout
(n, s, t) = map(int, stdin.readline().split())
w = [int(x) for x in stdin.readline().split()]
for k in range(n):
	if s == t:
		stdout.write(str(k))
		exit()
	s = w[s - 1]
stdout.write('-1')


def arr_inp():
	return [int(x) for x in stdin.readline().split()]
from sys import stdin
from collections import defaultdict
(n, s, t) = arr_inp()
(p, mem) = (arr_inp(), defaultdict(int))
p.insert(0, 0)
(ans, root) = (0, s)
while root != t:
	if mem[root]:
		exit(print(-1))
	else:
		mem[root] = 1
		root = p[root]
		ans += 1
print(ans)


(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, ini, fi) = list(map(int, input().split()))
ops = list(map(int, input().split()))
this_that = {i + 1: index for (i, index) in enumerate(ops)}
visited = set()
curr = ini
sh = 0
bad = False
while curr != fi:
	if this_that[curr] in visited:
		bad = True
		break
	else:
		curr = this_that[curr]
		visited.add(curr)
		sh += 1
if bad:
	print(-1)
else:
	print(sh)


(n, s, t) = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
s = s - 1
t = t - 1
for i in range(n):
	p[i] -= 1
count = 0
pos = s
visited = [0] * n
visited[s] = 1
if s == t:
	print('0')
	exit(0)
for i in range(n):
	pos = p[pos]
	if visited[pos] == 1:
		print('-1')
		exit(0)
	visited[pos] = 1
	count += 1
	if pos == t:
		print(count)
		exit(0)
print('-1')


l1 = [int(x) for x in input().split()]
n = l1[0]
s = l1[1]
si = s
t = l1[2]
l2 = [int(x) for x in input().split()]
step = 0
check1 = 0
check2 = 0
if s == t:
	print('0')
else:
	if l2[s - 1] == s:
		print('-1')
	else:
		while True:
			s = l2[s - 1]
			step += 1
			if s == t:
				check1 = 1
				break
			elif s == si:
				check2 = 1
				break
	if check1 == 1:
		print(step)
	elif check2 == 1:
		print('-1')


(n, s, t) = map(int, input().split())
list1 = list(map(int, input().split()))
list1 = [0] + list1
c = 0
if s == t:
	print(0)
else:
	x = list1[s]
	c += 1
	while x != t and x != s:
		c += 1
		x = list1[x]
	if x == t:
		print(c)
	else:
		print(-1)


(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)


from sys import stdin, stdout
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
(n, s, t) = iia()
arr = iia()
for i in range(n):
	if s == t:
		print(i)
		break
	s = arr[s - 1]
else:
	print(-1)


def main():
	(n, s, t) = map(int, input().split())
	p = [0] + list(map(int, input().split()))
	(ans, visited) = (0, [0] * (n + 1))
	while not visited[s] and s != t:
		visited[s] = 1
		s = p[s]
		ans += 1
	print(ans if s == t else -1)
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
readint = lambda : int(sys.stdin.readline().rstrip('\r\n'))
readints = lambda : map(int, sys.stdin.readline().rstrip('\r\n').split())
readstr = lambda : sys.stdin.readline().rstrip('\r\n')
readstrs = lambda : map(str, sys.stdin.readline().rstrip('\r\n').split())
readarri = lambda : [int(_) for _ in sys.stdin.readline().rstrip('\r\n').split()]
readarrs = lambda : [str(_) for _ in sys.stdin.readline().rstrip('\r\n').split()]

def solve():
	(n, s, t) = readints()
	arr = readarri()
	if s == t:
		print(0)
		return
	adj = [[] for _ in range(n)]
	for i in range(n):
		adj[i].append(arr[i] - 1)
	qu = [s - 1]
	vis = [0 for _ in range(n)]
	distance = [0] * n
	vis[s - 1] = 1
	ans = 1
	while qu:
		par = qu.pop(0)
		if par + 1 == t:
			print(distance[t - 1])
			return
		for i in adj[par]:
			if not vis[i]:
				distance[i] = distance[par] + 1
				qu.append(i)
				vis[i] = 1
	print(-1)

def main():
	t = 1
	for _ in range(t):
		solve()
main()


(n, start, end) = map(int, input().split())
L = list(map(int, input().split()))
count = 0
pos = start
co = True
while pos != end and co:
	if count > 10 and pos == start:
		co = False
	else:
		pos = L[pos - 1]
	count += 1
if co:
	print(count)
else:
	print('-1')


(n, s, t) = map(int, input().split())
lst = list(map(int, input().split()))
pos = s
chk = pos
cnt = 0
while True:
	if pos == t:
		break
	pos = lst[pos - 1]
	cnt += 1
	if chk == pos:
		print(-1)
		exit()
print(cnt)


(n, s, t) = map(int, input().split(' '))
l = list(map(int, input().split(' ')))
a = s
temp = 0
flag = 0
while True:
	a = l[a - 1]
	temp = temp + 1
	if a == s:
		flag = 1
		break
	elif a == t:
		break
	else:
		continue
if s == t:
	print(0)
elif flag == 1:
	print(-1)
else:
	print(temp)


(n, s, t) = map(int, input().split())
p = [0] + list(map(int, input().split()))
d = {s: 0}
curr = s
nex = p[curr]
while nex not in d:
	d[nex] = d[curr] + 1
	curr = nex
	nex = p[curr]
if t in d:
	print(d[t])
else:
	print(-1)


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
(n, s, t) = int_array()
a = int_array()
count = 0
f = 0
for i in range(n + 5):
	if s == t:
		f = 1
		break
	else:
		count += 1
		s = a[s - 1]
if f == 1:
	print(count)
else:
	print(-1)


(n, f, l) = map(int, input().split())
li = list(map(int, input().split()))
if f == l and li[f - 1] == l:
	print(0)
	exit()
if f == l:
	print(0)
	exit()
posOfMarble = f
shuff = 0
for i in range(n):
	posOfMarble = li[posOfMarble - 1]
	shuff += 1
	if posOfMarble == l:
		print(shuff)
		exit()
print(-1)


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')
		sys.exit(0)
	min_swaps += 1
	used[start] = True
	start = positions[start] - 1
print(min_swaps)


(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
x = s - 1
c = 0
f = 0
while c <= n:
	c += 1
	q = a[x]
	x = q - 1
	if q == a[t - 1]:
		f = 1
		print(c - 1)
		break
if f == 0:
	print(-1)


class CodeforcesTask285BSolution:

	def __init__(self):
		self.result = ''
		self.n_s_t = []
		self.p = []

	def read_input(self):
		self.n_s_t = [int(x) for x in input().split(' ')]
		self.p = [int(x) for x in input().split(' ')]

	def process_task(self):
		(n, s, t) = self.n_s_t
		if s == t:
			self.result = '0'
		else:
			connected = True
			pos = s
			steps = 0
			while pos != t:
				steps += 1
				pos = self.p[pos - 1]
				if steps > n:
					connected = False
					break
			if not connected:
				self.result = '-1'
			else:
				self.result = str(steps)

	def get_result(self):
		return self.result
Solution = CodeforcesTask285BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


(n, s, t) = map(int, input().strip().split())
P = list(map(int, input().strip().split()))
arr = []
for i in range(n):
	arr.append(i)
if s == t:
	print(0)
else:
	count = 0
	while s != t and P[s - 1] != 0:
		temp = P[s - 1]
		P[s - 1] = 0
		s = temp
		count += 1
	if s == t:
		print(count)
	else:
		print(-1)


def marble(n, s, t, l):
	if s == t:
		return 0
	count = 0
	l.insert(0, 0)
	arr = [0] * (n + 1)
	scopy = s
	while arr[t] != 1:
		arr[s] = 0
		arr[l[s]] = 1
		s = l[s]
		count += 1
		if arr[scopy] == 1:
			return -1
	return count
(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
print(marble(n, s, t, l))


import sys

def get_ints():
	return list(map(int, sys.stdin.readline().strip().split()))
curarr = get_ints()
arr = get_ints()
n = curarr[0]
arr.insert(0, 0)
startd = curarr[1]
endd = curarr[2]
if startd == endd:
	print(0)
elif arr[endd] == endd:
	print(-1)
else:
	repeat = [0 for _ in range(n + 1)]
	flag = 0
	ans = 0
	while True:
		if repeat[startd] == 1:
			flag = 1
			break
		repeat[startd] = 1
		startd = arr[startd]
		ans += 1
		if startd == endd:
			break
	if flag:
		print(-1)
	else:
		print(ans)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
shuf = {}
for (i, pos) in enumerate(p):
	shuf[i + 1] = pos
steps = 0
visited = {}
visited[s] = 1
if s == t:
	print(steps)
	exit()
while shuf[s] not in visited:
	visited[shuf[s]] = 1
	s = shuf[s]
	steps += 1
	if s == t:
		print(steps)
		exit()
print(-1)


(n, s, t) = map(int, input().split())
arr = list(map(int, input().split()))
if s == t:
	print(0)
	exit()
visited = set()
count = 0
flag = 0
i = arr[s - 1]
while i not in visited:
	if i == t:
		flag = 1
		break
	count += 1
	visited.add(i)
	i = arr[i - 1]
if flag == 1:
	print(count + 1)
else:
	print(-1)


t = input()
temp = t.split()
n = int(temp[0])
s = int(temp[1])
t = int(temp[2])
positionList = []
o = input()
temp = o.split()
positionList = [int(i) for i in temp]
shuffle = 0
tempVar = s - 1
while True:
	if shuffle > n:
		break
	if positionList[tempVar] == t:
		shuffle += 1
		break
	elif positionList[tempVar] != t:
		shuffle += 1
		tempVar = positionList[tempVar] - 1
if shuffle > n:
	print(-1)
elif s == t:
	print(0)
else:
	print(shuffle)


(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
if s == t:
	print(0)
	exit()
vis = [0] * (n + 1)
a = [0] + a
x = a[s]
vis[x] = 1
vis[s] = 1
ans = 1
while x != t:
	ans += 1
	x = a[x]
	if vis[x]:
		print(-1)
		exit()
	vis.append(x)
print(ans)


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)


from collections import Counter
import string
import math
import sys

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
mod = 1000000007
(n, s, t) = vary(3)
num = array_int()
count = 0
while 1:
	if count > 2 * n:
		print(-1)
		break
	if s == t:
		print(count)
		break
	s = num[s - 1]
	count += 1


from collections import defaultdict
(n, s, t) = map(int, input().split())
a = [int(x) for x in input().split()]
p1 = s
d = {}
d = defaultdict(lambda : -1, d)
for i in range(len(a)):
	d[i + 1] = a[i]
count = 0
while p1 != t:
	p1 = d[p1]
	if p1 == s:
		count = -1
		break
	count += 1
if count == -1:
	print(-1)
else:
	print(count)


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)
		exit()
	cur = A[cur - 1]
	ans = ans + 1
print(-1)


def cs(f, l, v):
	s = set()
	(t, c) = (f, 0)
	while 1:
		if t == l:
			return c
		t = v[t]
		if t in s:
			return -1
		s.add(t)
		c += 1
(n, f, l) = [int(c) - 1 for c in input().split()]
v = [int(c) - 1 for c in input().split()]
print(cs(f, l, v))


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


(n, s, t) = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
c = 0
flag = False
if s == t:
	print(0)
else:
	state = p[s - 1]
	while state != t:
		c += 1
		if state == s:
			flag = True
			break
		state = p[state - 1]
	if flag == False:
		print(c + 1)
	else:
		print(-1)


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
x = s - 1
count = 0
done = 0
while x != t - 1:
	if count != n:
		x = l[x] - 1
		count += 1
	else:
		done = 1
		break
if done == 1:
	print(-1)
else:
	print(count)


(n, st, f) = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
i = st
shuffles = 0
while i != f:
	if i == st and shuffles > 0:
		shuffles = -1
		break
	else:
		i = p[i - 1]
		shuffles += 1
print(shuffles)


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


(n, s, t) = list(map(int, input().rstrip().split()))
p = list(map(int, input().rstrip().split()))
flag = 0
count = 0
ind = s
if s == t:
	flag = 1
elif p[t - 1] == t or p[s - 1] == s:
	flag = 0
else:
	while count <= n:
		count += 1
		ind = p[ind - 1]
		if ind == t:
			flag = 1
			break
if flag == 0:
	print(-1)
else:
	print(count)


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
c = 0
l1 = []
if s == t:
	print(0)
elif t == l[t - 1] or s == l[s - 1]:
	print(-1)
else:
	while 1:
		l1.append(s)
		s = l[s - 1]
		c += 1
		if s == t:
			print(c)
			break
		if c > n:
			print(-1)
			break


(n, start, end) = map(int, input().split())
arr = list(map(int, input().split()))
arr.insert(0, 0)
i = start
s = set()
while i != end:
	s.add(i)
	i = arr[i]
	if i in s:
		print(-1)
		exit(0)
print(len(s))


(n, s, e) = list(map(int, input().split()))
s -= 1
e -= 1
a = list(map(int, input().split()))
a = [x - 1 for x in a]
i = s
found = False
if s == e:
	print(0)
	found = True
count = 0
while not (i == s and count > 0) and (not found):
	i = a[i]
	count += 1
	if i == e:
		print(count)
		found = True
if not found:
	print(-1)


ln0 = input()
ln1 = input()
(n, s, t) = map(int, ln0.split(' '))
P = list(map(lambda x: int(x) - 1, ln1.split(' ')))
if s == t:
	print(0)
	exit(0)
s -= 1
t -= 1
static_ps = set()
p_map = {}
for (i, p) in enumerate(P):
	if p == i:
		static_ps.add(p)
	p_map[i] = p
if s in static_ps or t in static_ps:
	print(-1)
	exit(0)
processed = set()
groups = []
curr_group = [next(iter(p_map.keys()))]
while p_map:
	k = curr_group[-1]
	if k in p_map:
		curr_group.append(p_map[k])
		del p_map[k]
	else:
		groups.append(curr_group)
		if p_map:
			curr_group = [next(iter(p_map.keys()))]
groups.append(curr_group)
for group in groups:
	group = group[1:]
	group_set = set(group)
	if s in group_set and t in group_set:
		si = group.index(s)
		ti = group.index(t)
		gn = len(group)
		res = (gn + ti - si) % gn
		print(res)
		exit(0)
print(-1)
exit(0)


w = input().split(' ')
(n, s, t) = (int(w[0]), int(w[1]), int(w[2]))
k = s
p = list(map(int, input().split(' ')))
cnt = 0
while s != t:
	s = p[s - 1]
	cnt = cnt + 1
	if s == k:
		cnt = -1
		break
print(cnt)


(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
k = 0
temp = s
while temp != t:
	temp = a[temp - 1]
	if temp == s:
		print(-1)
		exit()
	k += 1
print(k)


(n, s, t) = map(int, input().split())
l = [int(i) - 1 for i in input().split()]
if s == t:
	print(0)
	exit()
vis = [0] * n
s -= 1
t -= 1
p = s
cnt = 0
while p != t:
	p = l[p]
	cnt += 1
	if p == s:
		print(-1)
		exit()
print(cnt)


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)


def zip_sorted(a, b):
	(a, b) = zip(*sorted(zip(a, b)))
	sorted(zip(a, b), key=lambda x: x[1])
	return (a, b)
(n, s, t) = [int(n1) for n1 in input().split()]
p = [int(n1) for n1 in input().split()]
p = [0] + p
reached = [0] * (n + 1)
reached[s] = 1
if s == t:
	print(0)
else:
	curr = p[s]
	nex = p[curr]
	count = 1
	count1 = 0
	while reached[curr] != 1:
		if curr == t:
			count1 = 1
			break
		reached[curr] = 1
		count = count + 1
		curr = nex
		nex = p[curr]
	if count1 == 0:
		print(-1)
	else:
		print(count)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
path = dict()
vis = [0] * n
for i in range(n):
	path[i] = p[i] - 1
i = s - 1
while path[i] != t - 1 and vis[i] != 1:
	vis[i] = 1
	i = path[i]
if s == t:
	print(0)
elif path[i] == t - 1:
	print(vis.count(1) + 1)
else:
	print('-1')


(n, s, t) = map(int, input().split())
arr = [int(i) for i in input().split()]
arr.insert(0, 0)
if s == t:
	print(0)
else:
	temp = 0
	q = s
	c = 0
	while temp != t and temp != s:
		temp = arr[q]
		q = arr[q]
		c += 1
	if temp == s:
		print(-1)
	else:
		print(c)


import sys
import math as m

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def lts(l):
	s = ''.join(map(str, l))
	return s

def stl(s):
	l = list(s)
	return l
mod = int(1000000000.0) + 7

def sinp():
	return sys.stdin.readline().strip()

def iinp():
	return int(input())

def ninp():
	return map(int, sys.stdin.readline().strip().split())

def linp():
	return list(map(int, sys.stdin.readline().strip().split()))

def p(a):
	print(a)

def p2(a, b):
	print(a, b)
(n, s, t) = ninp()
p = linp()
ind = s - 1
a = []
a.append(s)
for i in range(n - 1):
	a.append(p[ind])
	ind = p[ind] - 1
cnt = 0
f = 0
for i in a:
	if i == t:
		print(cnt)
		f = 1
		break
	else:
		cnt += 1
if f == 0:
	print(-1)


(n, i, f) = map(int, input().split())
moves = 0
L = [int(i) for i in input().split()]
for j in range(n):
	if i == f:
		print(moves)
		exit(0)
	i = L[i - 1]
	moves += 1
print('-1')


(n, s, t) = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
start = s
amount = 0
while s != t and amount <= n:
	s = p[s - 1]
	if s == start:
		amount = -1
		break
	amount += 1
print(amount)


(n, s, t) = map(int, input().split())
A = [int(i) for i in input().split()]
c = 0
if t == s:
	print(0)
else:
	a = [s]
	while c < n:
		if a[-1] == t:
			a.append(t)
			break
		if A[s - 1] == s:
			break
		else:
			a.append(A[a[-1] - 1])
			s = A[a[-1] - 1]
		c += 1
	if t in a:
		print(len(a) - 2)
	else:
		print(-1)


c = []
a = []
c = input().split()
b = []
b = input().split()
n = int(c[0])
s = int(c[1])
t = int(c[2])
for i in range(n):
	ele = int(b[i])
	a.append(ele)
if s == t:
	print(0)
	exit()
j = 0
v = []
count = 0
v.append(int(a[s - 1]))
for j in range(n - 1):
	v.append(int(a[int(v[j] - 1)]))
for i in range(n):
	if t == v[i]:
		print(i + 1)
		exit()
print(-1)


(n, s, t) = list(map(int, input().split()))
p = list(map(int, input().split()))
st = s
c = 0
while s != t:
	c = c + 1
	s = p[s - 1]
	if s == st:
		c = -1
		break
print(c)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
if s == t:
	print(0)
else:
	ans = 0
	k = p[s - 1]
	ans = 1
	b = 1
	for i in range(n + 1):
		if k == t:
			print(ans)
			b = 0
			break
		else:
			ans += 1
			k = p[k - 1]
	if b:
		print(-1)


(n, s, t) = map(int, input().split())
x = [int(q) for q in input().split()]
for i in range(len(x)):
	if s == t:
		print(i)
		break
	s = x[s - 1]
else:
	print(-1)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
seen = set()
count = 0
while s != t and s not in seen:
	seen.add(s)
	s = p[s - 1]
	count += 1
if s == t:
	print(count)
else:
	print(-1)


def isPossible(n, s, t, a):
	visited = [False for i in range(n)]
	visited[s - 1] = True
	pos = s
	count = 0
	while pos != t:
		pos = a[pos - 1]
		count += 1
		if visited[pos - 1]:
			break
		visited[pos - 1] = True
	if pos == t:
		return count
	else:
		return -1
(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
print(isPossible(n, s, t, a))


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
ind = s
count = 0
while True:
	if ind == t:
		break
	ind = p[ind - 1]
	count += 1
	if ind == s:
		break
if ind == t:
	print(count)
else:
	print(-1)


import sys
import bisect
(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
maxn = 10 ** 5 + 5
l.insert(0, -10000)
vis = [0] * maxn
if s == t:
	print('0')
else:
	ans = 0
	while vis[s] != 1 and s != t:
		vis[s] = 1
		s = l[s]
		ans += 1
	if s != t:
		print('-1')
	else:
		print(ans)


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
l1 = [0] * (n + 1)
i = 1
for i in range(1, n + 1):
	l1[i] = l[l[i - 1] - 1]
cnt = 0
i = 0
if s != t:
	while s != t:
		cnt += 1
		s = l[s - 1]
		i += 1
		if i > n:
			cnt = -1
			break
print(cnt)


mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
(n, ip, fp) = f()
l = il()
el = l[ip - 1]
c = 1
if ip == fp:
	print(0)
else:
	while el != ip:
		if el == fp:
			exit(print(c))
		el = l[el - 1]
		c += 1
	print(-1)


(n, s, t) = map(int, input().split())
operation = [int(x) for x in input().split()]
positions = [s]
nCount = 0
if t == s:
	print(nCount)
else:
	while True:
		s = operation[s - 1]
		nCount += 1
		if t == s:
			print(nCount)
			break
		if nCount > n:
			print(-1)
			break


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
		elif 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()


(n, s, t) = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
flag = 0
cnt = 0
prev = s
for i in range(n):
	if prev == t:
		flag = 1
		break
	prev = l[prev - 1]
	cnt += 1
if flag:
	print(cnt)
else:
	print(-1)


(n, s, t) = list(map(int, input().split()))
a = list(map(int, input().rstrip().split()))
a.insert(0, 0)
p = dict()
curr = s
flag = 0
if s == t:
	print(0)
	exit()
count = 1
while 1:
	if curr in p:
		flag = 1
		break
	else:
		p[curr] = 1
		curr = a[curr]
		if curr == t:
			break
		count += 1
if flag == 1:
	print(-1)
else:
	print(count)


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)
	exit()
pos = s
moves = 0
while pos != t:
	pos = p[pos - 1]
	moves = moves + 1
	if pos == t:
		print(moves)
		exit()
	if pos == s:
		print(-1)
		exit()


while True:
	try:

		def soln(n, s, d, num):
			flg = True
			ans = 0
			for i in range(n):
				if s == d:
					print(ans)
					flg = False
					break
				s = num[s - 1]
				ans += 1
			if flg:
				print('-1')

		def read():
			(n, s, t) = map(int, input().split())
			num = list(map(int, input().split()))
			soln(n, s, t, num)
		if __name__ == '__main__':
			read()
	except EOFError:
		break


a = list(map(int, input().split()))
n = a[0]
s = a[1]
g = a[2]
a = [0]
a = a + list(map(int, input().split()))
c = 0
curr = s
l = []
for x in range(n + 1):
	l.append(0)
while curr != g:
	c += 1
	l[curr] += 1
	if l[curr] > 1:
		c = -1
		break
	curr = a[curr]
print(c)


(N, s, t) = 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)


(n, s, t) = map(int, input().split())
p = [*map(int, input().split())]
x = p[s - 1] - s
c = 0
if s == t:
	print(0)
	exit(0)
elif s != t and p[t - 1] == t or x == 0:
	print(-1)
	exit(0)
while s != t and c < n:
	s = p[s - 1]
	c += 1
if s == t:
	print(c)
else:
	print(-1)


(n, s, t) = map(int, input().split())
P = list(map(int, input().split()))
c = 0
x = s - 1
while x != t - 1:
	x = P[x] - 1
	c = c + 1
	if c > n:
		c = -1
		break
print(c)


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()


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
visited = set()
i = s
ct = 0
while i not in visited and t not in visited:
	visited.add(i)
	i = p[i - 1]
	ct += 1
if t in visited:
	print(ct - 1)
else:
	print(-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)


(n, s, f) = map(int, input().split())
(s, f) = (s - 1, f - 1)
arr = [int(i) - 1 for i in input().split()]
if s == f:
	print(0)
else:
	cnt = 0
	temp = arr[s]
	while cnt < n + 3:
		cnt += 1
		if temp == f:
			break
		else:
			temp = arr[temp]
	if cnt > n:
		print(-1)
	else:
		print(cnt)


def main():
	(n, i, f) = list(map(int, input().split()))
	lst = [0]
	lst.extend(list(map(int, input().split())))
	isdone = [0 for i in range(n + 1)]
	counter = 0
	indexx = i
	if i == f:
		print(0)
	else:
		while 1:
			if isdone[indexx] == 1:
				print(-1)
				break
			isdone[indexx] = 1
			indexx = lst[indexx]
			counter += 1
			if indexx == f:
				print(counter)
				break
t = 1
while t != 0:
	main()
	t -= 1


(n, start, end) = map(int, input().split())
arr = list(map(int, input().split()))
arr.insert(0, 0)
i = start
c = 0
while i != end:
	i = arr[i]
	c += 1
	if i == start:
		print(-1)
		exit(0)
print(c)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
flag = 0
while s != t and flag < n:
	flag += 1
	s = p[s - 1]
if flag >= n:
	print(-1)
else:
	print(flag)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
a = list(range(1, n + 1))
m = s
x = 0
if s == t:
	print(0)
else:
	while True:
		tmp = p[s - 1]
		x += 1
		if tmp == t:
			print(x)
			break
		elif tmp == m:
			print(-1)
			break
		s = tmp


(n, s, t) = [int(x) for x in input().split(' ')]
l = [int(x) for x in input().split(' ')]
d = {}
ans = 0
for i in range(n):
	d[i + 1] = l[i]
while s != t:
	if ans >= n:
		ans = -1
		break
	ans += 1
	s = d[s]
print(ans)


def main():
	(n, s, t) = [int(c) for c in input().split()]
	positions = [int(c) - 1 for c in input().split()]
	s -= 1
	t -= 1
	if s == t:
		print(0)
		return
	i = 1
	cur = s
	while i < n:
		cur = positions[cur]
		if cur == t:
			print(i)
			return
		i += 1
	print(-1)
main()


import sys
input = sys.stdin.buffer.readline
(n, s, t) = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
ipos = -1
pos = s
while pos != t and ipos != pos and (ans <= n):
	ans += 1
	ipos = pos
	pos = arr[ipos - 1]
if pos == t:
	print(ans)
else:
	print('-1')


(n, s, t) = [int(x) for x in input().split()]
p = [int(x) for x in input().split()]
if s != t:
	i = 0
	m = p[s - 1]
	flag = 0
	while i < n:
		if t == m:
			flag = 1
			break
		else:
			m = p[m - 1]
			i = i + 1
	if flag == 1:
		print(i + 1)
	else:
		print('-1')
else:
	print('0')


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
c = 0
while c < n + 1:
	if s == t:
		print(c)
		quit()
	else:
		s = l[s - 1]
	c += 1
print(-1)


(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)


(n, s, t) = map(int, input().split())
shufl = list(map(int, input().split()))
c = 0

def test():
	global c, s, t
	while c < n:
		if s == t:
			return True
		else:
			s = shufl[s - 1]
		c += 1
	return False
if test():
	print(c)
else:
	print('-1')


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
(n, initial, final) = rinput()
initial -= 1
final -= 1
A = rlinput()
moves = 0
visited = [False] * n
cur = initial
flag = True
while cur != final:
	moves += 1
	visited[cur] = True
	if not visited[A[cur] - 1]:
		cur = A[cur] - 1
	else:
		flag = False
		break
if flag:
	print(moves)
else:
	print(-1)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return map(int, input().split())

def array():
	return [int(i) for i in input().split()]

def bfs(adj, s, k):
	vis = [False] * len(adj)
	vis[s - 1] = True
	qu = [s]
	c = 0
	while len(qu) != 0:
		e = qu.pop(0)
		if e == k:
			return c
		c += 1
		for i in adj[e]:
			if vis[i - 1] != True:
				qu.append(i)
				vis[i - 1] = True
	return -1
(n, s, t) = value()
adj = defaultdict(list)
a = array()
for i in range(n):
	adj[i + 1].append(a[i])
print(bfs(adj, s, t))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def binary_search(lb, ub, val, cnt):
	mid = (lb + ub) // 2
	if val < mid:
		ub = mid - 1
		binary_search(lb, ub, val, cnt - 1)
	elif val > mid:
		lb = mid + 1
		binary_search(lb, ub, val, cnt - 1)
	else:
		print(cnt)

def abs(x):
	return x if x >= 0 else -x
for t in range(1):
	(n, s, t) = list(map(int, input().split()))
	check = list(map(int, input().split()))
	check.insert(0, -1)
	start = s
	cnt = 0
	li = [0] * (n + 1)
	while start != t and li[start] == 0:
		cnt += 1
		li[start] = 1
		start = check[start]
	if start == t:
		print(cnt)
	else:
		print(-1)


(n, s, t) = map(int, input().split())
a = [0] + [int(x) for x in input().split()]
if s == t:
	print('0')
else:
	count = 0
	found = False
	temp = s
	while a[temp] != s:
		count += 1
		temp = a[temp]
		if temp == t:
			found = True
			break
	if found:
		print(count)
	else:
		print('-1')


[n, s, t] = [int(y) for y in input().split()]
s -= 1
t -= 1
p = []
for x in input().split():
	p.append(int(x) - 1)
op = 0
pointer = p[s]
flag = False
if pointer != s:
	op = 1
while op <= n:
	if s == t:
		print(0)
		flag = True
		break
	if pointer == t:
		flag = True
		print(op)
		break
	pointer = p[pointer]
	op += 1
if flag == False:
	print(-1)


(n, s, t) = map(int, input().split())
arr = [0] + list(map(int, input().split()))
if s == t:
	print(0)
else:
	ans = 0
	vis = {s}
	while s != t:
		ans += 1
		s = arr[s]
		if s in vis:
			print(-1)
			exit(0)
		vis.add(s)
	print(ans)


(n, s, t) = map(int, input().split())
arr = list(map(int, input().split()))
current = s
f = 0
count = 0
if s == t:
	print(0)
else:
	while True:
		count += 1
		current = arr[current - 1]
		if current == t:
			f = 1
			break
		if current == s:
			f = -1
			break
	if f == 1:
		print(count)
	if f == -1:
		print(-1)


(n, s, t) = map(int, input().split())
x = list(map(int, input().split()))
if s == t:
	print('0')
else:
	pos = s
	k = 0
	f = 0
	for i in range(0, n):
		if pos == t:
			f = 1
			break
		pos = x[pos - 1]
		k = k + 1
		if pos > n:
			pos = pos % n
	if f == 1:
		print(k)
	else:
		print('-1')


(n, s, t) = map(int, input().split())
k = list(map(int, input().split()))
arr = []
arr.append(0)
for i in k:
	arr.append(i)
vi = [False for i in range(n + 1)]
cnt = 0
flag = 0
i = s
if s == t:
	print(0)
else:
	while True:
		if arr[i] == t:
			print(cnt + 1)
			flag = 1
			break
		else:
			vi[i] = True
			i = arr[i]
			if vi[i]:
				break
		cnt += 1
	if flag == 0:
		print(-1)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
graph = dict()
for x in range(n):
	u = x + 1
	v = p[x]
	graph[u] = v
seen = set()
current = s
while current not in seen:
	seen.add(current)
	if current == t:
		break
	current = graph[current]
if t in seen:
	print(len(seen) - 1)
else:
	print(-1)


(n, s, t) = list(map(int, input('').split(' ')))
A = list(map(int, input('').split(' ')))
initial = s
moves = 0
found = True
while s != t:
	s = A[s - 1]
	if s == initial:
		found = False
		break
	moves += 1
if found == True:
	print(moves)
else:
	print(-1)


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
step = 0
while s != t and l[s - 1] != 0:
	tmp = l[s - 1]
	l[s - 1] = 0
	s = tmp
	step += 1
if s == t:
	print(step)
else:
	print(-1)


(n, s, t) = map(int, input().split())
p = [int(x) for x in input().split()]
for i in range(n):
	if s == t:
		print(i)
		exit()
	s = p[s - 1]
print(-1)


import sys
f = sys.stdin
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 math
import bisect
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
(n, s, t) = inlt()
P = inlt()
out = 0
seen = {}
while s != t and s not in seen:
	seen[s] = True
	s = P[s - 1]
	out += 1
if s != t:
	print(-1)
else:
	print(out)


(n, s, t) = map(int, input().split(' '))
p = list(map(int, input().split(' ')))
d = {}
for (i, x) in enumerate(p):
	d[x] = i + 1
vis = [0] * (n + 1)
ans = 0
while not vis[t] and s != t:
	vis[t] = 1
	t = d[t]
	ans += 1
if s != t:
	print(-1)
else:
	print(ans)


def mi():
	return map(int, input().split(' '))
(n, s, t) = mi()
li = list(mi())
if s == t:
	print(0)
	exit()
temp = li[s - 1]
count = 1
while temp != t:
	temp = li[temp - 1]
	count += 1
	if temp == s:
		print(-1)
		exit()
print(count)


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


from collections import deque

def main():
	(n, s, t) = map(int, input().split())
	move = list(map(int, input().split()))
	graph = {}
	for i in range(1, n + 1):
		graph[i] = move[i - 1]
	visited = [False] * n
	swaps = 0
	d = deque()
	flag = True
	count = 0
	d.append(s)
	while d:
		s = d.popleft()
		if s == t:
			break
		if visited[s - 1]:
			flag = False
			break
		else:
			count += 1
			visited[s - 1] = True
			d.append(graph[s])
	if flag:
		print(count)
	else:
		print(-1)
main()


import sys
input = sys.stdin.buffer.readline
(n, s, t) = list(map(int, input().split()))
p = list(map(int, input().split()))
nxt = s
cnt = 0
while nxt != t:
	nxt = p[nxt - 1]
	cnt += 1
	if nxt == s:
		cnt = -1
		break
print(cnt)


import sys
(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
i = 0
while i < n:
	if s == t:
		print(i)
		sys.exit()
	s = l[s - 1]
	i = i + 1
if s == t:
	print(i)
else:
	print('-1')


(n, s, t) = [int(i) for i in input().split()]
p = [int(i) - 1 for i in input().split()]
i = s - 1
h = 0
t -= 1
while h < n and i != t - 1 + 1:
	i = p[i]
	h += 1
if i == t:
	print(h)
else:
	print(-1)


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
l.insert(0, 0)
if s == t:
	print(0)
else:
	count = 0
	flag = 1
	s1 = l[s]
	count = count + 1
	while s1 != t:
		if s1 == s:
			print(-1)
			flag = 0
			break
		s1 = l[s1]
		count = count + 1
	if flag == 1:
		print(count)


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


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


(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)


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
cur = s
flag = True
i = 0
while cur != t:
	cur = l[cur - 1]
	i += 1
	if i > 10 ** 5:
		flag = False
		break
if flag:
	print(i)
else:
	print('-1')


(a, b, c) = map(int, input().split())
l = list(map(int, input().split()))
z = 0
while b != c:
	if l[b - 1] != 0:
		a = b - 1
		b = l[b - 1]
		l[a] = 0
		z += 1
	else:
		print('-1')
		exit(0)
print(z)


from sys import stdin, stdout
from collections import OrderedDict

def solve(n, s, t, positions):
	count = 0
	if s == t:
		return count
	while count < n:
		if positions[s - 1] == t:
			return count + 1
		if positions[s - 1] == s:
			return -1
		s = positions[s - 1]
		count += 1
	if count == n:
		count = -1
	return count
(n, s, t) = list(map(int, stdin.readline().rstrip().split()))
positions = list(map(int, stdin.readline().rstrip().split()))
stdout.write(str(solve(n, s, t, positions)))


n = input().split(' ')
s = int(n[1])
t = int(n[2])
n = int(n[0])
p = input().split(' ')
p = list(map(int, p))
pos = s
time = 0
while 1:
	if s == t:
		print('0')
		break
	pos = p[pos - 1]
	time += 1
	if pos == t:
		print(time)
		break
	elif pos == s:
		print('-1')
		break


(n, s, t) = map(int, input().split())
p = [int(i) for i in input().split()]
dp = [s]
for i in range(n - 1):
	s = p[s - 1]
	dp.append(s)
time = -1
if t in dp:
	time = dp.index(t)
print(time)


b = list(map(int, input().split()))
a = list(map(int, input().split()))
n = b[0]
s = b[1]
t = b[2]
anshu = 0
if s == t:
	print(0)
elif a[s - 1] == s or a[t - 1] == t:
	print(-1)
else:
	temp = -1
	count = 0
	while temp != t:
		temp = a[s - 1]
		s = temp
		count = count + 1
		if count > n:
			anshu = 1
			break
	if anshu == 0:
		print(count)
	else:
		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) = (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) = map(int, input().split())
seq = list(map(int, input().split()))
for i in range(n):
	seq[i] -= 1
s -= 1
t -= 1
ind = s
count = 0
f = 0
ff = 1
while True:
	if ind == s and count != 0:
		ff = 0
	if ff == 1:
		if ind == t:
			f = 1
			break
		else:
			ind = seq[ind]
			count += 1
	else:
		f = 0
		break
if f == 0:
	print(-1)
else:
	print(count)


index = list(map(int, input().split()))
tags = [0]
tags.extend(list(map(int, input().split())))
pointer = index[1]
if index[1] == index[2]:
	print(0)
else:
	steps = 0
	while True:
		if tags[pointer] == index[2]:
			if tags[pointer] == pointer:
				print(steps)
				break
			else:
				steps += 1
				print(steps)
				break
		elif tags[pointer] == index[1]:
			print(-1)
			break
		else:
			pointer = tags[pointer]
			steps += 1


from collections import defaultdict
(n, s, t) = map(int, input().split())
li = list(map(int, input().split()))
if s == t:
	print(0)
	exit()
g = defaultdict(int)
for i in range(n):
	g[i + 1] = li[i]
visited = defaultdict(bool)
c = 0
f = 1
while not visited[t]:
	if visited[s]:
		f = 0
		break
	visited[s] = True
	s = g[s]
	if not visited[t]:
		c += 1
if f:
	print(c)
else:
	print(-1)


(n, s, t) = map(int, input().split())
s = s - 1
t = t - 1
lis = list(map(int, input().split()))
l = 0
while True:
	if l == n:
		print(-1)
		break
	if s == t:
		print(l)
		break
	l = l + 1
	s = lis[s] - 1


(n, s, t) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
marble = s
chance = 0
p = list(range(1, n + 1))
while marble != t:
	chance = chance + 1
	p[marble - 1] = -1
	marble = a[marble - 1]
	if p[marble - 1] < 0:
		chance = -1
		break
	else:
		pass
print(chance)


import os
import sys
from io import BytesIO, IOBase

def main():
	(n, s, t) = map(int, input().split())
	p = list(map(int, input().split()))
	check = [0] * (n + 1)
	p = [0] + p
	ans = 0
	while True:
		if s == t:
			break
		elif check[s]:
			print(-1)
			exit()
		else:
			check[s] = 1
			s = p[s]
			ans += 1
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
(n, s, t) = mp()
l = mp()
ans = 0
if s == t:
	print(0)
else:
	for i in range(n):
		if s == t:
			print(i)
			break
		s = l[s - 1]
	else:
		print(-1)


(n, s, t) = map(int, input().split())
A = [0] + [int(x) for x in input().split()]
if s == t:
	print(0)
else:
	start = s
	cnt = 0
	while start != t:
		start = A[start]
		cnt += 1
		if start == s:
			print(-1)
			exit()
	print(cnt)


(n, s, t) = map(int, input().split())
l = [int(x) for x in input().split()]
initial = s
final = t
c = 0
f = 0
if s == t:
	print(0)
else:
	for _ in range(n):
		if s == final:
			f = 1
			break
		else:
			temp = l[s - 1]
			s = temp
			c += 1
	if f == 0:
		print(-1)
	else:
		print(c)


(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
z = []
d = [0] * (n + 1)
if s == t:
	print(0)
	exit()
else:
	i = 0
	while i < n:
		if d[a[s - 1]] != 0:
			break
		z.append(a[s - 1])
		d[a[s - 1]] = 1
		if t == a[s - 1]:
			print(z.index(t) + 1)
			exit()
		i += 1
		s = a[s - 1]
print(-1)


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
l = [0] + l
vis = [0] * (n + 1)
vis[0] = 1
i = s
if s == t:
	print('0')
	exit()
summ = 0
while True:
	if l[i] == t and i != t:
		print(summ + 1)
		exit()
	elif l[i] == t and i == t:
		print(summ)
		exit()
	elif vis[i] != 1:
		summ += 1
		vis[i] = 1
		i = l[i]
	else:
		print('-1')
		exit()


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
k = s
c = 0
if s == t:
	print(0)
else:
	while True:
		k = l[k - 1]
		c += 1
		if k == t or k == s:
			break
	if k == t:
		print(c)
	else:
		print(-1)


(n, s, t) = map(int, input().split())
p = list(map(int, input().split()))
s -= 1
t -= 1
visited = [0 for i in range(n)]
i = s
ans = 0
if s == t:
	print(ans)
else:
	while not visited[i]:
		visited[i] = 1
		ans += 1
		i = p[i] - 1
		if i == t:
			break
	if i == t:
		print(ans)
	else:
		print(-1)


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

def fn(a, n, s, t):
	if s == t:
		return 0
	ans = 0
	cur = s - 1
	while True:
		cur = a[cur] - 1
		ans += 1
		if cur == t - 1:
			return ans
		if cur == s - 1:
			return -1
print(fn(a, n, s, t))


import re
in1 = [int(x) for x in re.split('\\s', input())]
in2 = [int(x) for x in re.split('\\s', input())]
done = False
x = 0
while not done and x < in1[0]:
	if in1[1] == in1[2]:
		print(x)
		done = True
	in1[1] = in2[in1[1] - 1]
	x += 1
if not done:
	print(-1)


import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
(n, s, t) = invr()
a = [0] + inlist()
curr = s
move = 0
while True:
	if curr == t:
		print(move)
		break
	curr = a[curr]
	if curr == s:
		print(-1)
		break
	move += 1


(n, s, t) = [int(x) for x in input().strip().split()]
p = [int(x) for x in input().strip().split()]
count = 0
flag = True
curr = s
while curr != t:
	if curr == t:
		break
	curr = p[curr - 1]
	count += 1
	if s == curr:
		flag = False
		break
if flag:
	print(count)
else:
	print(-1)


from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest
from math import ceil, floor, log, log2, sqrt, gcd, factorial, pow, pi
from bisect import bisect_left, bisect_right

def binNumber(n, size=4):
	return bin(n)[2:].zfill(size)

def iar():
	return list(map(int, input().split()))

def ini():
	return int(input())

def isp():
	return map(int, input().split())

def sti():
	return str(input())

def par(a):
	print(' '.join(list(map(str, a))))

def tdl(outerListSize, innerListSize, defaultValue=0):
	return [[defaultValue] * innerListSize for i in range(outerListSize)]

def sts(s):
	s = list(s)
	s.sort()
	return ''.join(s)

class pair:

	def __init__(self, f, s):
		self.fi = f
		self.se = s

	def __lt__(self, other):
		return (self.fi, self.se) < (other.fi, other.se)
(n, s, f) = isp()
p = iar()
s -= 1
f -= 1
for i in range(n):
	p[i] -= 1
if s == f:
	print(0)
	quit()
x = s
cnt = 1
while p[s] != f and cnt < n:
	s = p[s]
	cnt += 1
if cnt == n:
	print(-1)
	quit()
print(cnt)


nst = input().split()
(n, s, t) = (int(nst[0]), int(nst[1]), int(nst[2]))
arr = list(map(int, input().split()))
c = 0
x = s
while x != t:
	x = arr[x - 1]
	c += 1
	if x == s:
		print(-1)
		break
if x != s or c == 0:
	print(c)


(n, s, t) = map(int, input().split())
l = [int(i) for i in input().split()]
initial = s - 1
final = t - 1
G = True
i = 0
if s == t:
	print(i)
	G = False
else:
	while i < n:
		if l[initial] == t:
			print(i + 1)
			G = False
			break
		else:
			i += 1
			initial = l[initial] - 1
if G:
	print(-1)


g = input()
(n, s, t) = [int(x) for x in g.split()]
r = input()
dic = {}
i = 1
l = 0
for x in r.split():
	dic[i] = int(x)
	i += 1
joker = s
while l >= 0:
	l += 1
	if dic[joker] == t:
		if s == t:
			print('0')
		else:
			print(l)
		break
	elif dic[joker] == s:
		print('-1')
		break
	else:
		joker = dic[joker]


import sys
input = sys.stdin.readline
(n, s, t) = map(int, input().split())
pos = list(map(int, input().split()))
count = 0
seen = set()
while s != t:
	if s not in seen:
		seen.add(s)
	else:
		print(-1)
		break
	count += 1
	s = pos[s - 1]
else:
	print(count)


import sys
INT_MAX = sys.maxsize
INT_MIN = -sys.maxsize - 1
sys.setrecursionlimit(10 ** 6)
mod = 1000000007

def IOE():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	(n, s, t) = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	c = 0
	for i in range(n):
		if s == t:
			print(i)
			break
		s = a[s - 1]
	else:
		print(-1)
try:
	IOE()
except:
	pass
main()


(n, s, t) = map(int, input().split(' '))
pos = list(map(int, input().split(' ')))
c = 0
if s == t:
	print(0)
else:
	i = pos[s - 1]
	while 1:
		if i == t:
			print(c + 1)
			break
		elif i == s:
			print(-1)
			break
		i = pos[i - 1]
		c += 1


(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
f = 0
for i in range(n):
	if s == t:
		print(i)
		f = 1
		break
	s = a[s - 1]
if f == 0:
	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[t - 1])


enter = input().split()
n = int(enter[0]) - 1
s = int(enter[1]) - 1
t = int(enter[2]) - 1
fix = s
arr_b = [int(i) - 1 for i in input().split()]
arr_a = [i for i in range(1, n + 2)]
target = arr_a[s]
flag = 0
i = 0
while arr_a[t] != target:
	correct_pos = arr_b[s]
	(arr_a[correct_pos], arr_a[s]) = (arr_a[s], arr_a[correct_pos])
	s = correct_pos
	i = i + 1
	if target == arr_a[fix]:
		flag = 1
		break
if flag == 0:
	print(i)
else:
	print(-1)


(n, s, t) = map(int, input().split())
input_ara = list(map(int, input().strip().split()))
ara = []
for i in range(1, n + 1):
	ara.append((i, input_ara[i - 1]))
dictionary = {}
i = flag = 0
while 1:
	flag = 0
	if s not in dictionary:
		dictionary[s] = 1
	else:
		print(-1)
		flag = 1
		break
	if s == t:
		break
	s = ara[s - 1][1]
	i += 1
if flag != 1:
	print(i)


(n, s, t) = map(int, input().rstrip().split())
op = list(map(int, input().rstrip().split()))
ans = 0
curr = s - 1
while curr != t - 1:
	if ans > n:
		ans = -1
		break
	curr = op[curr] - 1
	ans += 1
print(ans)


from sys import stdin
import sys
import math

class PriorityQueueBase:

	class _Item:
		__slots__ = ('_key', '_value')

		def __init__(self, k, v):
			self._key = k
			self._value = v

		def __lt__(self, other):
			return self._key < other._key

	def is_empty(self):
		return len(self) == 0

def queue_time(customers, n):
	for i in range(1, len(customers)):
		i = i % n
		while i % n < n and customers[i % n] > customers[(i + 1) % n]:
			i += 1
			customers[i % n] = customers[i % n] + customers[i]
			customers[i] = 0
	return customers

def phi(n):
	result = n
	for i in range(2, int(pow(n, 0.5)) + 1):
		while n % i == 0:
			n /= i
			result -= result / i
	if n > 1:
		result -= result / n
	return result

class heap:

	def __init__(self, data):
		self.data = data

	def parent(self, j):
		return (j - 1) // 2

	def left(self, j):
		return 2 * j + 1

	def right(self, j):
		return 2 * j + 2

	def hasr(self, j):
		return self.right(j) < len(self.data)

	def hasl(self, j):
		return self.left(j) < len(self.data)

	def swap(self, i, j):
		(self.data[i], self.data[j]) = (self.data[j], self.data[i])

	def upheap(self, j):
		parent = self.parent(j)
		if j > 0 and self.data[j] < self.data[parent]:
			self.swap(j, parent)
			self.upheap(parent)

	def downheap(self, j):
		if self.hasl(j):
			left = self.left(j)
			small = left
			if self.hasr(j):
				right = self.right(j)
				if self.data[left] > self.data[right]:
					small = right
			if self.data[small] < self.data[j]:
				self.swap(j, small)
				self.downheap(small)

	def __str__(self):
		return ' '.join(map(str, self.data))
(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
move = 0
while s != t:
	move += 1
	s = a[s - 1]
	if move > n:
		print(-1)
		sys.exit()
print(move)


import sys
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')
(n, s, t) = map(int, input().split())
s = s - 1
t = t - 1
p = list(map(int, input().split()))
p = [i - 1 for i in p]
ans = -1
for i in range(n):
	if s == t:
		ans = i
		break
	else:
		s = p[s]
print(ans)


(n, s, t) = list(map(int, input().split()))
p = [-1] + list(map(int, input().split()))
vis = [False for i in range(n + 1)]
ans = 0
cur = s
while cur != t:
	if vis[cur] == True:
		ans = -1
		break
	vis[cur] = True
	cur = p[cur]
	ans += 1
print(ans)


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


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


str1 = list(map(int, input().split()))
n = str1[0]
s = str1[1]
t = str1[2]
p = list(map(int, input().split()))
check = [0] * n
for x in range(n):
	check[x] = False
prev = s
if s == t:
	print('0')
else:
	ct = 0
	flag = False
	check[prev - 1] = True
	for i in range(n):
		cur = p[prev - 1]
		check[cur - 1] = True
		prev = cur
		ct += 1
		if cur == t:
			print(ct)
			flag = True
			break
	if flag is False:
		for x in check:
			if x is False:
				print('-1')
				break


def traverse(arr, curr, dest):
	accumulator = 0
	visited = set()
	stack = [curr]
	while stack:
		curr = stack.pop()
		if curr == dest:
			return accumulator
		elif curr in visited:
			return -1
		visited.add(curr)
		accumulator += 1
		stack.append(arr[curr])
(n, s, t) = [int(i) - 1 for i in input().split()]
arr = [int(i) - 1 for i in input().split()]
print(traverse(arr, s, t))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.setrecursionlimit(int(pow(10, 6)))
	sys.stdin = open('input.txt', 'r')
except:
	pass
(a, b, c) = map(int, input().split())
p = list(map(int, input().split()))
for i in range(a):
	if b == c:
		print(i)
		exit()
	b = p[b - 1]
else:
	print(-1)
endtime = time.time()


(n, s, t) = map(int, input().split())
l = list(map(int, input().split()))
flag = 0
d = dict()
for k in range(n):
	d[l[k]] = k + 1
count = 0
for p in range(n):
	if s == t:
		flag = 1
		break
	else:
		t = d[t]
		count += 1
if flag == 1:
	print(count)
else:
	print(-1)


import math

def main():
	(n, s, t) = map(int, input().split())
	arr = list(map(int, input().split()))
	if s == t:
		return 0
	c = 1
	ind = s - 1
	while c <= n + 1:
		if ind == t - 1:
			return c - 1
		else:
			ind = arr[ind] - 1
			c += 1
	return -1
print(main())


(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
count = 0
d = 1
b = s - 1
for i in range(len(a)):
	if a[b] == t:
		if s == t:
			print(0)
			d = 0
			break
		else:
			count += 1
			print(count)
			d = 0
			break
	else:
		b = a[b] - 1
		count += 1
if d == 1:
	print(-1)


(n, s, t) = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
	a[i] -= 1
s -= 1
t -= 1
x = s
ans = 0
flag = 0
for i in range(n):
	if x == t:
		break
	else:
		ans += 1
		if x == a[x]:
			flag = 1
			break
		x = a[x]
if flag == 1 or x != t:
	print(-1)
else:
	print(ans)