AtCoder Beginner Contest 065 - Trained?

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.
The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.
Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.
Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

-----Constraints-----
 - 2 ≤ N ≤ 10^5
 - 1 ≤ a_i ≤ N

-----Input-----
Input is given from Standard Input in the following format:
N
a_1
a_2
:
a_N

-----Output-----
Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

-----Sample Input-----
3
3
1
2

-----Sample Output-----
2

Press Button 1, then Button 3.

Difficulty: EASY

Starter code:

Solutions:
n = int(input())
a = list(map(int, [input() for i in range(n)]))
ans = 1
hikari = 1
for i in range(n):
	hikari = a[hikari - 1]
	if hikari == 2:
		print(ans)
		return
	else:
		ans += 1
print(-1)


N = int(input())
A = []
for i in range(N):
	A.append(int(input()) - 1)
ans = -1
push = 0
for i in range(N + 1):
	if A[push] == 1:
		ans = i + 1
		break
	else:
		push = A[push]
print(ans)


n = int(input())
a = [int(input()) for i in range(n)]
c = 1
s = a[0]
while s != 2 and c < n:
	(c, s) = (c + 1, a[s - 1])
if c < n:
	print(c)
else:
	print(-1)


N = int(input())
A = [int(input()) for _ in range(N)]
A = [0] + A
i = 1
k = 1
while k <= N:
	if A[i] == 2:
		print(k)
		return
	i = A[i]
	k += 1
print(-1)


n = int(input())
q = [int(input()) for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
	ind = q[ind] - 1
	cnt += 1
	if ind == 1:
		print(cnt)
		return
print(-1)


n = int(input())
a = [int(input()) for _ in range(n)]
s = a[0]
count = 1
while s != 2 and count < n:
	s = a[s - 1]
	count += 1
print(count if count < n else -1)


n = int(input())
A = [int(input()) for _ in range(n)]
now = 0
cnt = 0
while A[now] != 2 and cnt <= n:
	now = A[now] - 1
	cnt += 1
if A[now] == 2:
	print(cnt + 1)
else:
	print(-1)


n = int(input())
a = [int(input()) for i in range(n)]
botan = a[0]
ans = -1
for i in range(n):
	if botan == 2:
		ans = i + 1
		break
	botan = a[botan - 1]
print(ans)


(*a,) = map(int, open(0).read().split())
i = 1
for t in range(10 ** 5):
	i = a[i]
	if i == 2:
		print(t + 1)
		exit()
print(-1)


n = int(input())
a = [int(input()) for _ in range(n)]
now = 1
for i in range(n):
	now = a[now - 1]
	if now == 2:
		print(i + 1)
		exit()
print(-1)


n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
a.insert(0, 0)
ans = -1
push = 1
for i in range(1, n + 2):
	if a[push] == 2:
		ans = i
		break
	else:
		push = a[push]
print(ans)


n = int(input())
lst = []
for i in range(n):
	lst.append(int(input()))
a = 1
for i in range(n):
	a = lst[a - 1]
	if a == 2:
		print(i + 1)
		exit()
print(-1)


N = int(input())
x = [int(input()) for _ in range(N)]
on = 1
ans = 0
for _ in range(N):
	on = x[on - 1]
	ans = ans + 1
	if on == 2:
		print(ans)
		exit()
print(-1)


def f():
	n = int(input())
	a = [int(input()) for _ in range(n)]
	x = 1
	c = 0
	for i in range(n):
		if x != 2:
			x = a[x - 1]
			c += 1
			if c == n:
				return -1
		else:
			return c
print(f())


n = int(input())
a = {i + 1: int(input()) for i in range(n)}
v = 1
for c in range(n):
	v = a[v]
	if v == 2:
		print(c + 1)
		break
else:
	print(-1)


from collections import defaultdict
n = int(input())
A = [int(input()) for _ in range(n)]
dic = defaultdict()
for i in range(n):
	dic[i + 1] = A[i]
(cnt, idx) = (1, 0)
val = dic[1]
while idx <= n:
	if val == 2:
		print(cnt)
		return
	val = dic[val]
	cnt += 1
	idx += 1
print(-1)


import sys
n = int(input())
a = [int(input()) for _ in range(n)]
i = 0
for j in range(n):
	nbtn = a[i]
	i = nbtn - 1
	if i == 1:
		print(j + 1)
		sys.exit()
print(-1)


n = int(input())
A = [int(input()) for _ in range(n)]
now = 0
for ans in range(n):
	now = A[now] - 1
	if now == 1:
		print(ans + 1)
		break
else:
	print(-1)


(n, *a) = map(int, open(0))
(c, s) = (1, a[0])
while s != 2 and c < n:
	c += 1
	s = a[s - 1]
print(c * (c < n) or -1)


N = int(input())
a = [int(input()) - 1 for _ in range(N)]
x = 0
ans = 0
while True:
	ans += 1
	x = a[x]
	if x == 1:
		print(ans)
		break
	if ans > N:
		print(-1)
		break


n = int(input())
A = {x: int(input()) for x in range(1, n + 1)}
i = 0
b = 1
while i < n and b != 2:
	b = A[b]
	i += 1
if b == 2:
	print(i)
else:
	print(-1)


N = int(input())
a = [int(input()) for _ in range(N)]
(c, s) = (1, a[0])
while s != 2 and c < N:
	c += 1
	s = a[s - 1]
print(c if c < N else -1)


N = int(input())
K = []
for _ in range(N):
	K.append(int(input()))
L = [1]
for i in range(1, N):
	L.append(K[L[i - 1] - 1])
if 2 in L:
	print(L.index(2))
else:
	print(-1)


n = int(input())
lst = [int(input()) for _ in range(n)]
i = lst[0]
for c in range(n):
	if i == 2:
		print(c + 1)
		quit()
	i = lst[i - 1]
print(-1)


x = int(input())
a = []
for i in range(x):
	a.append(int(input()))
ans = 0
t = 1
while t != 2 and ans < 100001:
	ans = ans + 1
	t = a[t - 1]
if ans > 100000:
	print(-1)
else:
	print(ans)


n = int(input())
l = [int(input()) - 1 for i in range(n)]
ans = 0
j = 0
for i in range(n):
	j = l[j]
	ans += 1
	if j == 1:
		print(ans)
		exit()
print(-1)


import sys
N = int(input())
d = [int(input()) for i in range(N)]
cnt = 0
button = d[0]
while button != 2:
	button = d[button - 1]
	cnt += 1
	if cnt > 100000:
		print(-1)
		return
print(cnt + 1)


N = int(input())
A = [0] + [int(input()) for _ in range(N)]
j = 1
cnt = 0
check = False
for i in range(N):
	if j == 2:
		check = True
		break
	cnt += 1
	j = A[j]
print(cnt if check else -1)


n = int(input())
l = [int(input()) - 1 for i in range(n)]
a = l[0]
for i in range(n):
	if a == 1:
		print(i + 1)
		exit()
	a = l[a]
else:
	print(-1)


N = int(input())
a = []
m = 1
for i in range(N):
	t = int(input())
	a.append(t)
for j in range(N):
	m = a[m - 1]
	if m == 2:
		print(j + 1)
		break
else:
	print(-1)


(n, now, c) = (int(input()), 0, -1)
a = [int(input()) - 1 for i in range(n)]
for i in range(n):
	now = a[now]
	if now == 1:
		c = i + 1
		break
print(c)


import sys
n = int(input())
a = [int(input()) for i in range(n)]
count = 0
num = 1
while num != 2:
	num = a[num - 1]
	count += 1
	if count >= n:
		count = -1
		break
print(count)


n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
res = -1
cc = 0
count = 0
for i in range(2 * n):
	cc = a[cc] - 1
	count += 1
	if cc == 1:
		res = count
		break
print(res)


n = int(input())
al = [int(input()) for i in range(n)]
i = 0
for j in range(n):
	i = al[i] - 1
	if i == 1:
		print(j + 1)
		return
print(-1)


n = int(input())
a = [int(input()) for _ in range(n)]
pos = 0
for i in range(n):
	if a[pos] == 2:
		print(i + 1)
		exit()
	pos = a[pos] - 1
print(-1)


N = int(input())
a = [int(input()) - 1 for _ in range(N)]
p = 0
for i in range(N):
	if p == 1:
		print(i)
		exit()
	p = a[p]
print(-1)


n = int(input())
A = []
for i in range(n):
	A.append(int(input()))
l = 1
ans = 0
while l != 2 and ans <= n:
	l = A[l - 1]
	ans += 1
print(ans if ans <= n else -1)


n = int(input())
a = [int(input()) for i in range(n)]
ans = 1
res = 1
for i in range(n):
	if a[res - 1] == 2:
		print(ans)
		return
	else:
		ans += 1
		res = a[res - 1]
print(-1)


from typing import List

def answer(n: int, a: List[int]) -> int:
	next_value = 1
	count = 0
	for _ in range(n):
		next_value = a[next_value - 1]
		count += 1
		if next_value == 2:
			return count
	return -1

def main():
	n = int(input())
	a = [int(input()) for _ in range(n)]
	print(answer(n, a))

def __starting_point():
	main()
__starting_point()


n = int(input())
a = [int(input()) for _ in range(n)]
a.insert(0, 0)
pos = 1
for i in range(1, n + 1):
	pos = a[pos]
	if pos == 2:
		print(i)
		break
if pos != 2:
	print(-1)


import sys
N = int(input())
a = [int(input()) for _ in range(N)]
flag = [0] * N
k = 0
i = 0
flag[0] = 1
while k < N:
	k += 1
	i = a[i] - 1
	if flag[i] == 0:
		flag[i] = 1
	elif flag[i] == 1:
		print(-1)
		return
	if i == 1:
		print(k)
		return


n = int(input())
L = [int(input()) for _ in range(n)]
next = 1
for i in range(n):
	if next == 2:
		print(i)
		quit()
	next = L[next - 1]
print(-1)


n = int(input())
data = [int(input()) for i in range(n)]
count = 0
temp = 1
for i in range(n):
	temp = data[temp - 1]
	count += 1
	if temp == 2:
		print(count)
		break
else:
	print(-1)


n = int(input())
a = [int(input()) for _ in range(n)]
x = 1
for (k, i) in enumerate(a):
	x = a[x - 1]
	if x == 2:
		print(k + 1)
		break
else:
	print(-1)


N = int(input())
L = []
light = [0] * N
light[0] = 1
(cnt, now) = (0, 0)
for i in range(N):
	L.append(int(input()))
for i in range(N):
	now = L[now] - 1
	cnt += 1
	if now == 1:
		print(cnt)
		return
print(-1)


a = [int(input()) for _ in range(int(input()))]
i = a[0]
cnt = 1
while i != 2 and cnt <= 10 ** 5:
	i = a[i - 1]
	cnt += 1
if cnt <= 10 ** 5:
	print(cnt)
else:
	print(-1)


n = int(input())
a = [int(input()) for i in range(n)]
s = 0
for i in range(n):
	if a[s] == 2:
		print(i + 1)
		break
	s = a[s] - 1
else:
	print(-1)


n = int(input())
a = [0]
for i in range(n):
	a.append(int(input()))
b = 1
for i in range(n):
	b = a[b]
	if b == 2:
		print(i + 1)
		break
else:
	print(-1)


N = int(input())
A = [int(input()) - 1 for k in range(N)]
p = 0
for k in range(N):
	if p == 1:
		print(k)
		exit()
	else:
		p = A[p]
print(-1)


N = int(input())
A = [int(input()) - 1 for _ in range(N)]
k = 0
for i in range(N):
	k = A[k]
	if k == 1:
		print(i + 1)
		break
else:
	print(-1)


n = int(input())
a = [int(input()) - 1 for i in range(n)]
(c, x) = (1, 0)
for i in range(n):
	x = a[x]
	if x == 1:
		print(i + 1)
		break
else:
	print(-1)


import re
import copy

def accept_input():
	N = int(input())
	A = {}
	for i in range(1, N + 1):
		A[i] = int(input())
	return (N, A)

def process(s):
	if s == '#':
		return 1
	else:
		return 0
(N, A) = accept_input()
cur = 1
kaisu = 0
for _ in range(N):
	cur = A[cur]
	kaisu += 1
	if cur == 2:
		break
if cur == 2:
	print(kaisu)
else:
	print(-1)


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


N = int(input())
A = [int(input()) - 1 for _ in range(N)]
a = 0
for i in range(N):
	a = A[a]
	if a == 1:
		print(i + 1)
		quit()
print(-1)


N = int(input())
a = [int(input()) - 1 for _ in range(N)]
ans = 0
cur = 0
visit = [-1] * N
while True:
	cur = a[cur]
	ans += 1
	if visit[cur] == -1:
		visit[cur] = ans
	else:
		print(-1)
		return
	if cur == 1:
		print(ans)
		return


n = int(input())
buttons = [int(input()) for _ in range(n)]
done = [0] * (n + 1)
next_button = buttons[0]
for _ in range(n):
	buttons_status = done[next_button]
	if buttons_status != 0:
		print(-1)
		return
	done[next_button] = 1
	if next_button == 2:
		break
	next_button = buttons[next_button - 1]
print(done.count(1))


(n, *a) = map(int, open(0).read().split())
tmp = 0
for i in range(n + 1):
	tmp = a[tmp] - 1
	if tmp == 1:
		print(i + 1)
		exit()
print(-1)


N = int(input())
BU = [int(input()) - 1 for i in range(N)]
ans = -1
onid = 0
for i in range(1, N + 1):
	onid = BU[onid]
	if onid == 1:
		ans = i
		break
print(ans)


n = int(input())
a = [int(input()) for i in range(n)]
cnt = 0
if 2 not in a:
	print(-1)
	return
x = 1
for i in range(n - 1):
	x = a[x - 1]
	cnt += 1
	if x == 2:
		print(cnt)
		return
print(-1)


n = int(input())
a = [int(input()) for i in range(n)]
tmp = []
i = 1
cnt = 0
while cnt <= n:
	i = a[i - 1]
	cnt += 1
	if i == 2:
		print(cnt)
		break
else:
	print(-1)


n = int(input())
a = [None] + [int(input()) for _ in range(n)]
x = 1
ans = 0
while x != 2 and ans < n:
	x = a[x]
	ans += 1
print(ans if ans < n else -1)


n = int(input())
a = list((int(input()) - 1 for _ in [0] * n))
s = 0
c = 0
while s != 1 and c < n:
	c += 1
	s = a[s]
print(c if c < n else -1)


n = int(input())
li = [int(input()) for i in range(n)]
basyo = 1
res = -1
for i in range(2 * n):
	basyo = li[basyo - 1]
	if basyo == 2:
		res = i + 1
		break
print(res)


n = int(input())
a = [int(input()) for i in range(n)]
x = 1
for i in range(n):
	if a[x - 1] == 2:
		print(i + 1)
		exit()
	x = a[x - 1]
print(-1)


N = int(input())
a = [int(input()) for i in range(N)]
count = [0] * N
i = 1
min_num = 0
count[i - 1] += 1
while True:
	i = a[i - 1]
	min_num += 1
	if i == 2:
		print(min_num)
		return
	if count[i - 1] == 0:
		count[i - 1] += 1
	else:
		break
print(-1)


n = int(input())
A = list((int(input()) for i in range(n)))
a = 0
for x in range(n):
	a = A[a] - 1
	if a == 1:
		print(x + 1)
		break
else:
	print(-1)


n = int(input())
l = [0] + [int(input()) for _ in [0] * n]
a = 1
c = 0
while a != 2 and c < n:
	a = l[a]
	c += 1
if c == n and a != 2:
	print(-1)
else:
	print(c)


N = int(input())
a = [int(input()) - 1 for _ in range(N)]
now = 0
for i in range(N):
	now = a[now]
	if now == 1:
		print(i + 1)
		break
else:
	print(-1)


import math
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions import gcd
from fractions import gcd
n = int(input())
p = set()
p.add(0)
ai = [int(input()) for _ in range(n)]
s = 0
cnt = 0
while True:
	a = ai[s]
	if a == 2:
		print(cnt + 1)
		return
	if a - 1 not in p:
		p.add(a - 1)
		cnt += 1
		s = a - 1
	else:
		print(-1)
		return


n = int(input())
a = [input() for i in range(n)]
b = 0
cnt = 0
while cnt < n:
	if b == 1:
		print(cnt)
		exit()
	b = int(a[b]) - 1
	cnt += 1
print(-1)


n = int(input())
al = [int(input()) for _ in range(n)]
pos = 0
for i in range(n):
	pos = al[pos] - 1
	if pos == 1:
		print(i + 1)
		exit()
print(-1)


N = int(input())
a = [int(input()) for i in range(N)]
count = 1
button = a[0]
while button != 2 and count < N:
	count += 1
	button = a[button - 1]
print(count if count < N else -1)


N = int(input())
l = [int(input()) - 1 for _ in range(0, N)]
i = 0
flg = False
for n in range(0, N):
	if l[i] == 1:
		print(n + 1)
		flg = True
		break
	else:
		i = l[i]
if not flg:
	print('-1')


with open(0) as f:
	(N, *a) = map(int, f.read().split())
from itertools import count
p = 0
seen = {0}
for i in count(1, 1):
	p = a[p] - 1
	if p == 1:
		ans = i
		break
	if p in seen:
		ans = -1
		break
	seen.add(p)
print(ans)


n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
x = 1
for i in range(n):
	x = a[x - 1]
	if x == 2:
		print(i + 1)
		break
else:
	print(-1)


N = int(input())
l = []
for i in range(N):
	v = int(input())
	l.append(v)
v = l[0]
ans = 1
while v != 2 and v != -1:
	tmp = l[v - 1]
	l[v - 1] = -1
	v = tmp
	ans += 1
if v == 2:
	print(ans)
else:
	print(-1)


n = int(input())
a = [0] * (n + 1)
for i in range(n):
	a[i] = int(input())
k = 0
ans = -1
for i in range(n):
	k = a[k] - 1
	if k == 1:
		ans = i + 1
		break
print(ans)


n = int(input())
a = [int(input()) for i in range(n)]
b = a[0]
for i in range(n):
	if b == 2:
		print(i + 1)
		exit()
	b = a[b - 1]
print(-1)


import sys
n = int(input())
A = list(map(int, sys.stdin.readlines()))
index = 0
past_index = list()
found = False
for i in range(len(A)):
	if index == 1:
		print(i)
		found = True
		break
	index = A[index] - 1
if not found:
	print(-1)


n = int(input())
A = tuple((int(input()) for _ in range(n)))
a = 0
for i in range(n):
	a = A[a] - 1
	if a == 1:
		print(i + 1)
		break
else:
	print(-1)


N = int(input())
A = [int(input()) for _ in range(N)]
A = [a - 1 for a in A]
fin = set()
cnt = 0
i = 0
while True:
	if i in fin:
		cnt = -1
		break
	if i == 1:
		break
	cnt += 1
	fin.add(i)
	i = A[i]
print(cnt)


n = int(input())
a = []
s = [0] * n
x = 1
for i in range(n):
	a.append(int(input()))
if 2 not in a:
	print(-1)
else:
	while True:
		if a[x - 1] == 2:
			print(sum(s) + 1)
			return
		else:
			s[x - 1] += 1
			if s[x - 1] == 2:
				print(-1)
				return
			x = a[x - 1]


import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque, Counter
from decimal import Decimal
import functools

def v():
	return input()

def k():
	return int(input())

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

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

def X():
	return list(input())

def L():
	return list(input().split())

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

def lcm(a, b):
	return a * b // math.gcd(a, b)
sys.setrecursionlimit(10 ** 6)
mod = 10 ** 9 + 7
cnt = 0
ans = 0
inf = float('inf')
al = 'abcdefghijklmnopqrstuvwxyz'
AL = al.upper()
N = k()
A = []
for i in range(N):
	a = k()
	A.append(a)
aaa = 0
ans = A[0]
count = 1
for i in range(N):
	if ans == 2:
		aaa = 1
		break
	else:
		ans = A[ans - 1]
		count += 1
if aaa == 1:
	print(count)
else:
	print(-1)


n = int(input())
btn = [0]
for i in range(n):
	btn.append(int(input()))
see = [0 for i in range(n + 1)]
now = 1
cnt = 0
see[now] = 1
while True:
	now = btn[now]
	cnt += 1
	if now == 2:
		print(cnt)
		break
	if see[now] == 1:
		print(-1)
		break
	see[now] = 1


N = int(input())
a = [int(input()) for _ in range(N)]
X = 1
i = 1
while a[i - 1] != 2 and X <= N:
	X += 1
	i = a[i - 1]
if X > N:
	print(-1)
else:
	print(X)


N = int(input())
a = [int(input()) for _ in range(N)]
(ans, nex) = (1, a[0])
while ans < N and nex != 2:
	nex = a[nex - 1]
	ans += 1
print(ans if ans != N else -1)


n = int(input())
A = list((int(input()) for i in range(n)))
a = 0
count = 0
for i in range(n):
	if a != 1:
		a = A[a] - 1
		count += 1
print(count if a == 1 else -1)


N = int(input())
a = [int(input()) for i in range(N)]
cnt = 1
idx = a[0]
while idx != 2 and cnt < N:
	cnt += 1
	idx = a[idx - 1]
print(cnt if cnt < N else -1)


N = int(input())
A = []
for i in range(N):
	A.append(int(input()) - 1)
ans = -1
push = 0
count = [False] * N
for i in range(N + 1):
	if A[push] == 1:
		ans = i + 1
		break
	elif count[push]:
		break
	else:
		count[push] = True
		push = A[push]
print(ans)


n = int(input())
l = [0] + [int(input()) for i in range(n)]
now = 1
for i in range(n + 5):
	now = l[now]
	if now == 2:
		print(i + 1)
		break
else:
	print(-1)


N = int(input())
a = [0]
for i in range(N):
	a.append(int(input()))
idx = 1
for i in range(1, N + 1):
	idx = a[idx]
	if idx == 2:
		print(i)
		return
print(-1)


N = int(input())
A = [int(input()) - 1 for _ in range(N)]
cnt = 1
next_button = 0
while cnt <= len(A):
	next_button = A[next_button]
	if next_button == 1:
		print(cnt)
		return
	cnt += 1
print(-1)


N = int(input())
Buttons = []
for i in range(N):
	Buttons.append(int(input()))
pos = 0
count = 0
visit = [0] * N
flag = 1
while pos != 1:
	if visit[pos] != 0:
		flag = 0
		break
	else:
		visit[pos] = 1
		pos = Buttons[pos] - 1
		count = count + 1
if flag == 1:
	print(count)
else:
	print(-1)


n = int(input())
l = {i + 1: int(input()) for i in range(n)}
c = 1
i = 1
while i <= n:
	c = l.get(c)
	if c == 2:
		print(i)
		exit()
	i += 1
print(-1)


n = int(input())
done = [False for _ in range(n)]
A = [int(input()) - 1 for _ in range(n)]
tmp = 0
for i in range(n + 1):
	if A[tmp] == 1:
		print(i + 1)
		break
	if done[tmp]:
		print(-1)
		break
	else:
		done[tmp] = True
	tmp = A[tmp]
else:
	print(-1)


N = int(input())
a = [int(input()) for i in range(N)]
push = 1
count = 0
while count <= N:
	push = a[push - 1]
	count += 1
	if push == 2:
		print(count)
		break
else:
	print(-1)


n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
b = 0
s = 0
while s != 1:
	s = a[s] - 1
	b += 1
	if b == n:
		b = -1
		break
print(b)


N = int(input())
a = [int(input()) - 1 for _ in range(N)]
c = 0
for i in range(N):
	if c == 1:
		print(i)
		break
	c = a[c]
if c != 1:
	print(-1)


N = int(input())
A = [int(input()) for _ in range(N)]
nb = 1
for i in range(N):
	nb = A[nb - 1]
	if nb == 2:
		print(i + 1)
		exit()
print(-1)


(n, *a) = map(int, open(0).read().split())
i = ans = 1
while a[i - 1] != 2 and ans <= n:
	ans += 1
	i = a[i - 1]
print(ans if ans <= n else -1)


n = int(input())
l = []
for i in range(n):
	a = int(input())
	l.append(a)
x = l[0]
cnt = 1
for i in range(n):
	if x == 2:
		print(cnt)
		return
	else:
		cnt += 1
		x = l[x - 1]
print('-1')


n = int(input())
A = [int(input()) for _ in range(n)]
now = 0
for i in range(n):
	now = A[now] - 1
	if now == 1:
		print(i + 1)
		exit()
print(-1)


n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
total = 1
i = a[0] - 1
if a[0] == 2:
	print(total)
else:
	while True:
		if a[i] == 2:
			print(total + 1)
			break
		elif total > n:
			print('-1')
			break
		else:
			i = a[i] - 1
			total += 1


N = int(input())
a = []
for i in range(N):
	a.append(int(input()))
ans = pos = 0
flag = [0] * N
while True:
	ans += 1
	if a[pos] == 2:
		break
	pos = a[pos] - 1
	if flag[pos] > 0:
		ans = -1
		break
	else:
		flag[pos] = 1
print(ans)


N = int(input())
a = tuple((int(input()) for _ in range(N)))
log = set()
i = 0
count = 1
while a[i] != 2:
	i = a[i] - 1
	if i in log:
		print(-1)
		break
	else:
		log.add(i)
		count += 1
else:
	print(count)


def main():
	N = int(input())
	a = [int(input()) for _ in range(N)]
	cnt = 0
	tmp = 1
	while cnt <= 100001:
		tmp = a[tmp - 1]
		cnt += 1
		if tmp == 2:
			return cnt
		elif tmp == 1:
			return -1
	return -1
print(main())


N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
now = 1
for i in range(150000):
	now = A[now - 1]
	if now == 2:
		print(i + 1)
		exit()
print(-1)


n = int(input())
a = [int(input()) - 1 for _ in range(n)]
ans = -1
l = 0
for i in range(n):
	l = a[l]
	if l == 1:
		ans = i + 1
		break
print(ans)


n = int(input())
a = []
b = []
for i in range(n):
	a.append(int(input()))
next = a[0] - 1
b.append(next + 1)
for i in range(n):
	next = a[next] - 1
	b.append(next + 1)
print(b.index(2) + 1) if 2 in b else print(-1)


N = int(input())
A = [0] * N
for i in range(N):
	A[i] = int(input()) - 1
index = 0
trials = -1
for i in range(N):
	index = A[index]
	if index == 1:
		trials = i + 1
		break
print(trials)


n = int(input())
List = [int(input()) for _ in range(n)]
(c, s) = (1, List[0])
while s != 2 and c < n:
	c += 1
	s = List[s - 1]
print(-1 if c >= n else c)


n = int(input())
a = [int(input()) for _ in range(n)]
p = a[0]
if p == 2:
	print(1)
	return
cnt = 1
for _ in range(n):
	p = a[p - 1]
	cnt += 1
	if p == 2:
		print(cnt)
		return
print(-1)


import sys
import cProfile
input = sys.stdin.readline

def main():
	n = int(input())
	a = [int(input()) for _ in range(n)]
	l = [1]
	i = 0
	aa = 1
	while True:
		aa = a[aa - 1]
		if aa == 2:
			print(i + 1)
			return
		if i > n:
			print(-1)
			return
		i += 1
main()


N = int(input())
a = []
for _ in range(N):
	a.append(int(input()) - 1)
(i, count) = (0, 0)
success = False
for _ in range(N):
	i = a[i]
	count += 1
	if i == 1:
		success = True
		break
if not success:
	count = -1
print(count)


N = int(input())
a = [None] + [int(input()) for i in range(N)]
c = 1
for i in range(1, N + 1):
	if a[c] == 2:
		print(i)
		return
	else:
		c = a[c] + 0
print(-1)


n = int(input())
t = [int(input()) for i in range(n)]
x = 1
res = -1
for i in range(n):
	x = t[x - 1]
	if x == 2:
		res = i + 1
		break
print(res)


import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20

def I():
	return int(input())

def F():
	return float(input())

def S():
	return input()

def LI():
	return [int(x) for x in input().split()]

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

def LF():
	return [float(x) for x in input().split()]

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

def resolve():
	N = I()
	a = [I() - 1 for _ in range(N)]
	c = 0
	ans = 0
	is_ok = True
	while True:
		c = a[c]
		ans += 1
		if c == 0 or ans > N:
			is_ok = False
			break
		if c == 1:
			break
	if is_ok:
		print(ans)
	else:
		print(-1)

def __starting_point():
	resolve()
__starting_point()


N = int(input())
n = []
for i in range(N):
	n.append(int(input()))
a = 1
c = -1
for i in range(N):
	a = n[a - 1]
	if a == 2:
		if c < 0:
			c = i + 1
print(c)


N = int(input())
a = []
for i in range(N):
	a.append(int(input()) - 1)
flag = True
step = []
pivot = 0
for i in range(N + 1):
	step.append(pivot)
	if a[pivot] == 1:
		break
	pivot = a[pivot]
if len(step) > N:
	print(-1)
else:
	print(len(step))


n = int(input())
a = [int(input()) for i in range(n)]
c = 1
d = a[0]
while d != 2:
	d = a[d - 1]
	c += 1
	if c > n:
		print(-1)
		exit()
print(c)


n = int(input())
l = [int(input()) for i in range(n)]
b = 1
cnt = 0
for i in range(n):
	cnt += 1
	next = l[b - 1]
	if next == 2:
		print(cnt)
		exit()
	b = next
print(-1)


n = int(input())
d = {i: int(input()) for i in range(1, n + 1)}
f = True
s = 1
for i in range(n):
	if s == 2:
		print(i)
		f = False
		break
	s = d[s]
if f:
	print(-1)


n = int(input())
a = [int(input()) for _ in range(n)]
b = 0
for i in range(1, n + 1):
	b = a[b] - 1
	if b == 1:
		print(i)
		exit()
else:
	print(-1)


n = int(input())
a = [0] + [int(input()) for i in range(n)]
cnt = 1
next_a = a[1]
if a[1] == 2:
	print(cnt)
else:
	while cnt <= 10 ** 5:
		next_a = a[next_a]
		cnt += 1
		if next_a == 2:
			break
	if cnt <= 10 ** 5:
		print(cnt)
	else:
		print(-1)


N = int(input())
list = [int(input()) for i in range(N)]
c = 0
x = 1
while x != 2 and c < N:
	x = list[x - 1]
	c += 1
if x == 2:
	print(c)
else:
	print(-1)


a = [int(input()) for _ in range(int(input()))]
(c, s) = (1, a[0])
for i in range(len(a)):
	if s == 2:
		print(c)
		break
	(c, s) = (c + 1, a[s - 1])
else:
	print(-1)


n = int(input())
a = [int(input()) for i in range(n)]
bu = 0
end = -1
for i in range(n):
	if bu == 1:
		end = i
		break
	bu = a[bu] - 1
print(end)


n = int(input())
a = [int(input()) - 1 for _ in range(n)]
curr_node = 0
for i in range(n):
	curr_node = a[curr_node]
	if curr_node == 1:
		print(i + 1)
		return
print(-1)


n = int(input())
a = []
for _ in range(n):
	a.append(int(input()))
now = 0
cnt = 0
visit = [0 for _ in range(n)]
Flag = False
while now != 1:
	if visit[now] != 0:
		Flag = True
		break
	else:
		visit[now] = 1
		now = a[now] - 1
		cnt += 1
if Flag:
	print('-1')
else:
	print(cnt)


(n, ans) = (int(input()), -1)
(a, m) = ([int(input()) for i in range(n)], 1)
for i in range(n):
	m = a[m - 1]
	if m == 2:
		ans = i + 1
		break
print(ans)


N = int(input())
A = [int(input()) for i in range(N)]
i = 0
cnt = 1
cmpr = [0]
while A[i] != 2:
	if A[i] == i + 1:
		print(-1)
		return
	i = A[i] - 1
	cmpr.append(i)
	cnt += 1
	if cnt > N:
		print(-1)
		return
print(cnt)


import sys
nyuryoku = [int(i) - 1 for i in sys.stdin]
N = nyuryoku[0] + 1
a_list = nyuryoku[1:]
cnt_push = 0
button_list = [False] * N
i = 0
while True:
	if button_list[i] == False:
		button_list[i] = True
		cnt_push += 1
		if a_list[i] == 1:
			break
		i = a_list[i]
	else:
		cnt_push = -1
		break
print(cnt_push)


import sys
N = list(map(int, sys.stdin))
bot = N[1]
for i in range(N[0]):
	if bot == 2:
		print(i + 1)
		break
	bot = N[bot]
else:
	print(-1)


n = int(input())
a = [int(input()) for _ in range(n)]
(num, cnt) = (0, 1)
while a[num] != 2:
	num = a[num] - 1
	cnt += 1
	if cnt == n:
		print(-1)
		exit()
print(cnt)


n = int(input())
A = [int(input()) for _ in range(n)]
A = [0] + A
cnt = 1
for i in range(1, n + 1):
	cnt = A[cnt]
	if cnt == 2:
		print(i)
		exit()
print(-1)


n = int(input())
a = [0] * n
for i in range(n):
	a[i] = int(input())
visit = set()
now = 1
for i in range(1, n + 10):
	if now not in visit:
		visit.add(now)
		now = a[now - 1]
	else:
		print(-1)
		break
	if now == 2:
		print(i)
		break


n = int(input())
alis = [int(input()) for i in range(n)]
counter = 0
ans = 1
while counter < n and ans != 2:
	counter += 1
	ans = alis[ans - 1]
print(counter if ans == 2 else '-1')


n = int(input())
a = [int(input()) for _ in range(n)]
light = 0
ans = 0
for i in range(n):
	if a[light] == 2:
		ans += 1
		break
	else:
		light = a[light] - 1
		ans += 1
else:
	ans = -1
print(ans)


n = int(input())
l = [int(input()) for _ in range(n)]
t = 0
for i in range(1, n + 1):
	t = l[t] - 1
	if t == 1:
		print(i)
		break
else:
	print(-1)


N = int(input())
A = []
for i in range(N):
	A.append(int(input()) - 1)
ans = -1
push = 0
count = set()
for i in range(N + 1):
	if A[push] == 1:
		ans = i + 1
		break
	elif A[push] in count:
		break
	else:
		count.add(push)
		push = A[push]
print(ans)


(*a,) = map(int, open(0).read().split())
b = a[1]
l = []
for i in range(a[0]):
	if b == 2:
		print(i + 1)
		exit()
	b = a[b]
print(-1)


N = int(input())
a = list([int(input()) for _ in range(N)])
cur = 0
for n in range(N + 1):
	if cur == 1:
		print(n)
		break
	cur = a[cur] - 1
else:
	print(-1)


N = int(input())
a = [int(input()) for i in range(N)]
s = 1
for i in range(N):
	if s == 2:
		print(i)
		exit()
	s = a[s - 1]
print('-1')


N = int(input())
a = 1
A = list((int(input()) for i in range(N)))
for i in range(N):
	a = A[a - 1]
	if a == 2:
		print(i + 1)
		break
else:
	print(-1)


N = int(input())
li = [int(input()) for _ in range(N)]
i = 1
for n in range(N):
	i = li[i - 1]
	if i == 2:
		print(n + 1)
		break
else:
	print(-1)


n = int(input())
li = [0]
for i in range(n):
	li.append(int(input()))
count = 0
now = 1
while now != 2:
	if li[now] == 0:
		count = -1
		break
	else:
		a = li[now]
		li[now] = 0
		now = a
		count += 1
print(count)


N = int(input())
A = [int(input()) for _ in [0] * N]
r = 0
i = 0
for _ in [0] * N:
	i = A[i] - 1
	r += 1
	if i == 1:
		print(r)
		exit()
print(-1)


N = int(input())
l = [int(input()) for _ in range(N)]
k = 1
t = -1
for i in range(N):
	k = l[k - 1]
	if k == 2:
		t = i + 1
		break
print(t)


n = int(input())
a = [int(input()) - 1 for _ in range(n)]
i = 0
ct = 0
while i != 1:
	if a[i] == -1:
		print('-1')
		break
	k = i
	i = a[i]
	a[k] = -1
	ct += 1
else:
	print(ct)


N = int(input())
lis = [int(input()) for i in range(N)]
a = 1
for i in range(1, N + 1):
	a = lis[a - 1]
	if a == 2:
		print(i)
		break
if a != 2:
	print(-1)


n = int(input())
a = [int(input()) - 1 for _ in range(n)]
visited = [False] * n
now = 0
count = 0
while visited[now] != True:
	count += 1
	visited[now] = True
	now = a[now]
	if now == 1:
		print(count)
		return
print(-1)


n = int(input())
a = [int(input()) for i in range(n)]
x = 0
for i in range(1, n):
	y = a[x]
	if y == 2:
		print(i)
		exit()
	else:
		x = y - 1
print(-1)


LI = lambda : list(map(int, input().split()))
N = int(input())
A = [int(input()) for _ in range(N)]

def main():
	a = 1
	for i in range(N + 1):
		if a == 2:
			print(i)
			return 0
		a = A[a - 1]
	print(-1)

def __starting_point():
	main()
__starting_point()


import sys
e = list(map(int, sys.stdin))
cur = e[1]
for i in range(e[0]):
	if cur == 2:
		print(i + 1)
		break
	cur = e[cur]
else:
	print(-1)


n = int(input())
a = [0] * n
for i in range(n):
	a[i] = int(input())
counter = 0
now = 1
visit = [False] * n
visit[0] = True
while True:
	counter += 1
	if a[now - 1] == 2:
		print(counter)
		break
	elif visit[a[now - 1] - 1] == True:
		print(-1)
		break
	else:
		visit[a[now - 1] - 1] = True
	now = a[now - 1]


N = int(input())
A = [0] * (N + 1)
for i in range(1, N + 1):
	A[i] = int(input())
is_visited_list = [0] * (N + 1)
loop = False
now_visit = 1
cnt = 0
while True:
	if is_visited_list[now_visit] == 1:
		loop = True
		break
	else:
		is_visited_list[now_visit] = 1
	now_visit = A[now_visit]
	if now_visit == 2:
		cnt += 1
		break
	else:
		cnt += 1
if loop:
	print(-1)
else:
	print(cnt)


(n, *a) = map(int, open(0).read().split())
cnt = 1
next = a[0]
while cnt < n:
	if next == 2:
		break
	next = a[next - 1]
	cnt += 1
print([-1, cnt][next == 2])


import os
import sys

def main():
	N = int(input())
	A = [0]
	visited = [0] * (N + 1)
	for _ in range(N):
		A.append(int(input()))
	i = 1
	cnt = 0
	while visited[i] == 0:
		if i == 2:
			print(cnt)
			return
		cnt += 1
		visited[i] = 1
		i = A[i]
	print(-1)

def __starting_point():
	main()
__starting_point()


N = int(input())
d = []
visited = [False for _ in range(N + 1)]
for _ in range(N):
	d.append(int(input()) - 1)
i = 0
cnt = 0
while True:
	if i == 1:
		break
	elif cnt >= N:
		cnt = -1
		break
	elif visited[i]:
		cnt = -1
		break
	else:
		visited[i] = True
		cnt += 1
		i = d[i]
print(cnt)


import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce
rr = lambda : sys.stdin.readline().rstrip()
rs = lambda : sys.stdin.readline().split()
ri = lambda : int(sys.stdin.readline())
rm = lambda : list(map(int, sys.stdin.readline().split()))
rl = lambda : list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10 ** 9 + 7
n = ri()
a = [ri() for _ in range(n)]
idx = 1
cnt = 1
if a[0] == 2:
	print(1)
	return
while idx != -1:
	(a[idx - 1], idx) = (-1, a[idx - 1])
	cnt += 1
	if a[idx - 1] == 2:
		print(cnt)
		return
print(-1)


n = int(input())
l = [0 for _ in range(n)]
already = set()
next = 1
c = 0
for i in range(n):
	a = int(input())
	l[i] = a
while c <= n:
	next = l[next - 1]
	c += 1
	if next == 2:
		print(c)
		break
	elif next in already:
		print(-1)
		break
	already.add(next)


n = int(input())
a = [0] * n
for i in range(n):
	a[i] = int(input())
now = 1
for i in range(1, n + 10):
	now = a[now - 1]
	if now == 2:
		print(i)
		break
else:
	print(-1)


N = int(input())
buttons = []
for _ in range(N):
	buttons.append(int(input()))
s = 1
cnt = 0
while True:
	s = buttons[s - 1]
	cnt += 1
	if s == 2:
		break
	elif cnt > N:
		cnt = -1
		break
print(cnt)


def resolve():
	N = int(input())
	A = [int(input()) for _ in range(N)]
	next = 0
	cnt = 0
	for i in range(N):
		next = A[next] - 1
		cnt += 1
		if next == 1:
			break
	if cnt == N:
		print(-1)
	else:
		print(cnt)

def __starting_point():
	resolve()
__starting_point()


n = int(input())
a = [int(input()) - 1 for _ in range(n)]
visit_node = [0] * n
ans = 0
curr_node = 0
while visit_node[curr_node] == 0:
	visit_node[curr_node] = 1
	curr_node = a[curr_node]
	ans += 1
	if curr_node == 1:
		print(ans)
		return
print(-1)


import sys
N = int(input())
A = {}
for i in range(1, N + 1):
	A[i] = int(input())
ans = 1
x = 1
for j in range(N):
	if A[x] == 2:
		print(ans)
		return
	x = A[x]
	ans += 1
print(-1)


N = int(input())
A = [int(input()) for _ in range(N)]
order = A[0]
for i in range(1, N):
	if order == 2:
		print(i)
		exit()
	order = A[order - 1]
print(-1)


N = int(input())
a = [int(input()) for i in range(N)]
x = 1
for i in range(N):
	x = a[x - 1]
	if x == 2:
		print(i + 1)
		break
else:
	print(-1)


N = int(input())
lst = []
for i in range(N):
	lst.append(int(input()))
now = 1
count = 0
for i in range(N):
	now = lst[now - 1]
	count += 1
	if now == 2:
		print(count)
		return
print(-1)


n = int(input())
light = [0] * (n + 1)
for i in range(1, n + 1):
	light[i] = int(input())
s = 1
ans = 0
while light[s] != 2 and ans <= n:
	s = light[s]
	ans += 1
if light[s] == 2:
	print(ans + 1)
else:
	print(-1)


(N, *A) = map(int, open(0).read().split())
k = 0
for i in range(N):
	k = A[k] - 1
	if k == 1:
		print(i + 1)
		break
else:
	print(-1)


import sys
N = int(input())
a = [int(input()) for _ in range(N)]
s = 1
for i in range(N):
	if s == 2:
		print(i)
		sys.exit()
	s = a[s - 1]
print(-1)


n = int(input())
line = [0]
for i in range(n):
	line.append(int(input()))
check = [0 for i in range(n + 1)]
count = 0
check[1] = 1
a = 1
while True:
	count += 1
	if line[a] == 2:
		break
	else:
		a = line[a]
		check[a] += 1
	if check[a] > 1:
		count = -1
		break
print(count)


n = int(input())
a = [int(input()) for i in range(n)]
now = 0
for i in range(n):
	if a[now] == 2:
		print(i + 1)
		exit()
	else:
		now = a[now] - 1
print(-1)


n = int(input())
a = [int(input()) for i in range(n)]
b = [0] * n
i = 0
s = 0
while i != 1 and b[i] == 0:
	b[i] = 1
	i = a[i] - 1
	s += 1
if i == 1:
	print(s)
else:
	print(-1)


n = int(input())
a = [int(input()) - 1 for _ in range(n)]
v = [False] * n
i = 0
cnt = 0
while i != 1 and (not v[i]):
	v[i] = True
	cnt += 1
	i = a[i]
print(cnt if i == 1 else -1)


n = int(input())
li = [int(input()) for i in range(n)]
num = 1
for i in range(n):
	if li[num - 1] == 2:
		print(i + 1)
		exit()
	num = li[num - 1]
print(-1)


n = int(input())
A = []
bool = [True] * n
for i in range(n):
	a = int(input())
	a -= 1
	A.append(a)
j = A[0]
cnt = 1
for i in range(n):
	if j == 1:
		print(cnt)
		return
	if bool[A[j]] == True:
		bool[A[j]] = False
		j = A[j]
		cnt += 1
	else:
		print(-1)
		return


n = int(input())
a = [int(input()) for _ in range(n)]
b = a[0]
c = 1
ans = -1
for i in range(10 ** 5):
	if b == 2:
		ans = c
		break
	b = a[b - 1]
	c += 1
print(ans)


n = int(input())
a = [0]
for i in range(n):
	a.append(int(input()))
c = 1
k = a[1]
while c <= n:
	if k == 2:
		print(c)
		exit()
	k = a[k]
	c += 1
print(-1)


N = int(input())
V = [int(input()) for i in range(1, N + 1)]
cnt = 1
val = V[0]
while True:
	if val == 2:
		print(cnt)
		break
	if cnt >= N:
		print(-1)
		break
	cnt += 1
	val = V[val - 1]


N = int(input())
a = [0] * N
for i in range(N):
	a[i] = int(input())
M = 10 ** 6
b = 0
for i in range(M):
	b = a[b] - 1
	if b == 1:
		break
print(i + 1 if b == 1 else -1)


N = int(input())
a = list((int(input()) for _ in range(N)))
i = 0
cnt = 0
while i != 1:
	i = a[i] - 1
	cnt += 1
	if cnt > N:
		print(-1)
		break
else:
	print(cnt)


n = int(input())
a = [int(input()) - 1 for _ in range(n)]
id = [-1] * n
x = 0
for c in range(1, n + 1):
	if id[x] != -1:
		print(-1)
		return
	id[x] = c
	x = a[x]
	if x == 1:
		print(c)
		return


n = int(input())
btn = {b + 1: int(input()) for b in range(n)}
cur = 1
cnt = 0
while cnt <= n and cur != 2:
	cur = btn[cur]
	cnt += 1
	if cur == 2:
		print(cnt)
		return
print(-1)


N = int(input())
A = [int(input()) for _ in range(N)]
i = 1
count = 0
while i != 2 and count <= 100001:
	i = A[i - 1]
	count += 1
if count > 100000:
	print(-1)
else:
	print(count)


(n, *A) = map(int, open(0).read().split())
x = 0
for i in range(n):
	(A[x], x) = (0, A[x] - 1)
	if x == -1:
		print(x)
		exit()
	if x == 1:
		print(i + 1)
		exit()


n = int(input())
a = [int(input()) for _ in range(n)]
x = a[0]
for i in range(n):
	if x == 2:
		print(i + 1)
		break
	x = a[x - 1]
else:
	print(-1)


N = int(input())
a = []
for i in range(N):
	a.append(int(input()))
x = 1
Sum = 0
while x != 2 and Sum < N:
	x = a[x - 1]
	Sum += 1
if Sum >= N:
	print(-1)
else:
	print(Sum)


n = int(input())
l = [int(input()) for i in range(n)]
count = 0
for i in range(n):
	if l[count] == 2:
		print(i + 1)
		exit()
	else:
		count = l[count] - 1
print(-1)


(*A,) = map(int, open(0))
i = 1
t = [0] * A[0] + [0]
while i - 2 and t[i] == 0:
	t[i] = 1
	i = A[i]
print(-1 if i - 2 else sum(t))


N = int(input())
A = {i: int(input()) for i in range(1, N + 1)}
flag = 0
k = 1
for i in range(N):
	k = A[k]
	if k == 2:
		flag = 1
		break
if flag:
	print(i + 1)
else:
	print(-1)


n = int(input())
a = []
for i in range(n):
	a.append(int(input()) - 1)
i = 0
out = 0
kai = 0
while i != 1:
	kai += 1
	out += 1
	i = a[i]
	if kai > n:
		out = -1
		break
print(out)


n = int(input())
a = [int(input()) - 1 for i in range(n)]
on = 0
for i in range(n):
	on = a[on]
	if on == 1:
		print(i + 1)
		exit()
print(-1)


n = int(input())
a = list((int(input()) for _ in range(n)))
b = 1
for i in range(1, n):
	b = a[b - 1]
	if b == 2:
		print(i)
		exit()
print(-1)


n = int(input())
a = [0] * n
for i in range(n):
	a[i] = int(input())
ans = -1
memo = 0
light = 1
for i in range(n):
	if light == 2:
		ans = memo
		break
	light = a[light - 1]
	memo += 1
print(ans)


n = int(input())
a = [int(input()) for i in range(n)]
cnt = 0
if not a.count(2):
	print('-1')
else:
	i = 0
	while True:
		cnt += 1
		if a[i] == 2:
			print(cnt)
			break
		elif a[i] == i + 1 or cnt > n:
			print('-1')
			break
		i = a[i] - 1


n = int(input())
l = [int(input()) for i in range(n)]
num = l[0]
cnt = 0
for i in range(10 ** 5):
	if num == 2:
		cnt += 1
		break
	else:
		num = l[num - 1]
		cnt += 1
if cnt == 10 ** 5:
	cnt = -1
print(cnt)


n = int(input())
a = [int(input()) for i in range(n)]
x = 1
i = 0
while i < n and x != 2:
	x = a[x - 1]
	i += 1
if x == 2:
	print(i)
else:
	print(-1)


n = int(input())
l = [int(input()) for i in range(n)]
a = 1
ans = 0
while a != 2:
	a = l[a - 1]
	ans += 1
	if ans == n + 1:
		ans = -1
		break
print(ans)


N = int(input())
a = [int(input()) - 1 for _ in range(N)]
v = [False] * N
c = 0
ans = 0
while c != 1:
	v[c] = True
	c = a[c]
	if v[c]:
		ans = -1
		break
	ans += 1
print(ans)


N = int(input())
A = [int(input()) for _ in range(N)]
pos = 1
ans = 0
while pos != 2:
	pos = A[pos - 1]
	ans += 1
	if ans == 100000:
		ans = -1
		break
print(ans)


N = int(input())
A = [int(input()) for n in range(N)]
B = 0
for n in range(N):
	B = A[B] - 1
	if B == 1:
		print(n + 1)
		exit()
print(-1)


N = int(input())
A = []
for i in range(N):
	a = int(input())
	A.append(a)
b = 1
x = A[0]
while x != 2:
	x = A[x - 1]
	if b > N:
		print(-1)
		return
	b += 1
print(b)


N = int(input())
a = list((int(input()) for i in range(N)))
j = 0
i = 1
while j < N:
	i = a[i - 1]
	j += 1
	if i == 2:
		print(j)
		break
else:
	print('-1')


n = int(input())
ans = ''
cnt = 0
lst = list((int(input()) for _ in range(n)))
i = 1
for _ in range(n):
	cnt += 1
	if lst[i - 1] == 2:
		break
	else:
		i = lst[i - 1]
else:
	cnt = -1
print(cnt)


n = int(input())
a = [int(input()) for _ in range(n)]
i = 0
for m in range(n):
	i = a[i] - 1
	if i == 1:
		print(m + 1)
		exit()
print(-1)


N = int(input())
a = [int(input()) for _ in range(N)]
cnt = 0
on = 0
for _ in range(N):
	cnt += 1
	if a[on] == 2:
		print(cnt)
		return
	on = a[on] - 1
print(-1)


n = int(input())
al = list((int(input()) for _ in range(n)))
num = [0] * n
i = 0
cnt = 0
while True:
	if al[i] == 2:
		print(cnt + 1)
		break
	elif num[al[i] - 1] == 1:
		print(-1)
		break
	else:
		num[al[i] - 1] += 1
		i = al[i] - 1
		cnt += 1


(*A,) = map(int, open(0))
i = 1
t = 0
while i - 2 and t < A[0]:
	t += 1
	i = A[i]
print(t * (i == 2) or -1)


N = int(input())
A = [int(input()) - 1 for _ in range(N)]
j = 0
for i in range(N):
	j = A[j]
	if j == 1:
		print(i + 1)
		exit()
print(-1)


n = int(input())
x = [int(input()) for i in range(n)]
c = 0
a = 1
while True:
	if c == n + 1:
		print('-1')
		break
	a = x[a - 1]
	c += 1
	if a == 2:
		print(c)
		break


N = int(input())
A = {i + 1: int(input()) for i in range(N)}
v = 1
for cnt in range(N):
	v = A[v]
	if v == 2:
		print(cnt + 1)
		break
else:
	print(-1)


N = int(input())
num_list = [int(input()) for _ in range(N)]
num = 0
for i in range(N):
	if num_list[num] == 2:
		print(i + 1)
		return
	num = num_list[num] - 1
print(-1)


a = [input() for _ in range(int(input()))]
now = 1
cnt = 0
while cnt <= len(a):
	now = int(a[now - 1])
	cnt += 1
	if now == 2:
		break
print(cnt if now == 2 else '-1')


N = int(input())
A = [int(input()) - 1 for _ in range(N)]
light = 0
ans = -1
for n in range(1, N + 1):
	light = A[light]
	if light == 1:
		ans = n
		break
print(ans)


(n, *a) = map(int, open(0).read().split())
f = lambda x, y: x[y]
i = 1
c = 0
for _ in range(n):
	i = f(a, i - 1)
	c += 1
	if i == 2:
		print(c)
		exit()
print(-1)


n = int(input())
l = [int(input()) for i in range(n)]
c = l[0]
for i in range(n):
	if c == 2:
		print(i + 1)
		break
	else:
		c = l[c - 1]
else:
	print(-1)


def main():
	n = int(input())
	a = [int(input()) for _ in range(n)]
	cnt = 0
	x = 1
	if 2 in a:
		for _ in range(n - 1):
			x = a[x - 1]
			cnt += 1
			if x == 2:
				print(cnt)
				return
		else:
			print(-1)
	else:
		print(-1)
main()


N = int(input())
A = [int(input()) for i in range(N)]
x = 1
for i in range(N + 5):
	x = A[x - 1]
	if x == 2:
		print(i + 1)
		exit()
print(-1)


n = int(input())
a = [int(input()) for _ in range(n)]
b = a[0]
ans = 1
for i in range(len(a)):
	if b == 2:
		print(ans)
		exit()
	b = a[b - 1]
	ans += 1
print(-1)


n = int(input())
A = []
for i in range(n):
	A.append(int(input()))
k = 1
for i in range(n):
	if A[k - 1] == 2:
		print(i + 1)
		exit()
	k = A[k - 1]
print(-1)


n = int(input())
a = [int(input()) for _ in range(n)]
i = 1
s = 1
while i < n:
	if a[s - 1] == 2:
		print(i)
		exit()
	s = a[s - 1]
	i += 1
print(-1)


import sys
N = int(input())
A = [int(input()) for _ in range(N)]
x = 0
cnt = 0
for _ in range(N):
	x = A[x] - 1
	cnt += 1
	if x == 1:
		print(cnt)
		return
print(-1)


N = int(input())
a = [int(input()) for i in range(N)]
b = a[0]
c = 1
while b != 2 and c <= N:
	b = a[b - 1]
	c += 1
if b == 2:
	print(c)
else:
	print('-1')


n = int(input())
a = [int(input()) for i in range(n)]
s = 1
for i in range(n):
	if a[s - 1] == 2:
		print(i + 1)
		exit()
	else:
		s = a[s - 1]
print(-1)