CodeEff/ECCO huggingface dataset:
# Original solution
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : list(map(int, input().split()))
mod=1000000007

#+++++

def main():
	a = int(eval(input()))
	al=[]
	for i in range(a):
		b = int(eval(input()))
		al.append(b)
		
	opened_list=set()
	st=0
	opened_list.add(st)
	count=0
	while st != 1:
		st = al[st]-1
		count+=1
		if st in opened_list:
			return -1
			
	return count
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

# Optimized solution
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : list(map(int, input().split()))
mod=1000000007

#+++++

def main():
	a = int(eval(input()))
	al=[]
	for i in range(a):
		b = int(eval(input()))
		al.append(b)
		
	opened_list=set()
	st=0
	opened_list.add(st)
	count=0
	while st != 1:
		st = al[st]-1
		count+=1
		if st in opened_list:
			return -1
		opened_list.add(st)
			
	return count
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)
# Original solution
n = int(eval(input()))
butnum = {}
for i in range(1,n+1):
    butnum[i] = int(eval(input()))
pushbut = 1
pushedbut =[]
jud = 0
for j in range(1,n+1):
    pushedbut.append(pushbut)
    pushbut = butnum[pushbut]
    if pushbut == 2:
        print(j)
        jud = 1
        break
    elif pushbut in pushedbut:
        print((-1))
        jud = 1
        break
if jud == 0:
    print((-1))

# Optimized solution
n = int(eval(input()))
butnum = {}
for i in range(1,n+1):
    butnum[i] = int(eval(input()))
pushbut = 1
pushedbut =[]
jud = 0
for j in range(1,n+1):
    pushbut = butnum[pushbut]
    if pushbut == 2:
        print(j)
        jud = 1
        break
if jud == 0:
    print((-1))
# Original solution
N=int(eval(input()))
L=[]
for i in range(N):
	L.append(int(eval(input())))
count=0
ans=0
i=0
while(1):
	count+=1
	if count==N+100:
		print((-1))
		exit()
	i=L[i]-1
	ans+=1
	if i==1:
		print(ans)
		exit()

# Optimized solution
N=int(eval(input()))
L=[]
count=0
t=0
for i in range(N):
	L.append(int(eval(input())))
while(1):
	count+=1
	if count>N+10:
		print((-1))
		exit()
	if L[t]==2:
		print(count)
		exit()
	else:
		t=L[t]-1
# Original solution
import sys

N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]

l = 1
p = a[0] - 1

if a[0] == 2:
    print(l)
    sys.exit()

while l < int(1e6):
    l += 1
    if a[p] == 2:
        print(l)
        break
    else:
        p = a[p] - 1
else:
    print((-1))

# Optimized solution
import sys

N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]

l = 1
p = a[0] - 1

if a[0] == 2:
    print(l)
    sys.exit()

while l < int(2e5):
    l += 1
    if a[p] == 2:
        print(l)
        break
    else:
        p = a[p] - 1
else:
    print((-1))
# Original solution
n = int(eval(input()))
a = []
b = []
for i in range(n):
   a.append(int(eval(input())))
   b.append(0)

# 1: now, -1: visited, 0: none
b[0] = 1
now = 0
count = 0
while (b[1] != 1):
   next = a[now] - 1

   if (b[next] == -1):
      print((-1))
      exit()

   b[now] = -1
   b[next] = 1

   now = next
   count += 1

print(count)


# Optimized solution
n = int(eval(input()))
a = []
b = []
for i in range(n):
   a.append(int(eval(input())))
   b.append(0)

# 1: now, -1: visited, 0: none
b[0] = 1
now = 0
count = 0
while (b[1] != 1 and count <= n):
   next = a[now] - 1

   if (b[next] == -1):
      print((-1))
      exit()

   b[now] = -1
   b[next] = 1

   now = next
   count += 1

if (b[1] == 1):
   print(count)
else:
   print((-1))
# Original solution
N = int(eval(input()))
A=[]
l=[1]
ans=0
for i in range(N):
    A.append(int(eval(input())))
for i in range(N):
    l.append(A[l[i]-1])
    if l[i+1]==2:
        ans=i+1
        break
    if l[i+1]in l[:i]:
        break
if ans!=0:
    print(ans)
else:
    print((-1))

# Optimized solution
N = int(eval(input()))
A=[]
l=[1]
ans=0
for i in range(N):
    A.append(int(eval(input())))
for i in range(N):
    l.append(A[l[i]-1])
    if l[i+1]==2:
        ans=i+1
        break
if ans!=0:
    print(ans)
else:
    print((-1))
# Original solution
# Author: cr4zjh0bp
# Created: Sun Mar 22 23:25:40 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
a = nin(n)

ans = 0
flag = True
i = 0
p = 0
while i != 1:
    if p == a[i] - 1:
        flag = False
        break
    p = i
    i = a[i] - 1
    ans += 1

print((ans if flag else -1))

# Optimized solution
# Author: cr4zjh0bp
# Created: Sun Mar 22 23:25:40 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
a = nin(n)

i = 0
ans = 0
flag = True
while i != 1:
    if ans > n:
        flag = False
        break
    i = a[i] - 1
    ans += 1

print((ans if flag else -1))
# Original solution
from collections import deque
n=int(eval(input()))
blist=deque([int(eval(input())) for a in range(n)])
vlist=deque([1])
while True:
    if 2 == vlist[-1]:
        print((len(vlist)-1))
        break
    a=blist[vlist[-1]-1]
    if a in deque([a for a in vlist]):
        print((-1))
        break
    else:
        vlist.append(a)

# Optimized solution
from collections import deque
n=int(eval(input()))
blist=deque([int(eval(input())) for a in range(n)])
vlist=1
count=0
while True:
    if count>n:
        print((-1))
        break
    if 2 == vlist:
        print(count)
        break
    vlist=blist[vlist-1]
    count+=1
# Original solution
N=int(eval(input()))
x=[int(eval(input())) for _ in range(N)]

#x=dict(zip(range(1,N+1),a))
#print(x)
route=[1,]
#for _ in range(N):
while 2 not in route:
  #print(x[route[-1]])
  if route[-1]==2:
    break
  elif x[route[-1]-1] not in route:
    route.append(x[route[-1]-1])
  else:
    print((-1))
    exit()
    
print((len(route)-1))

# Optimized solution
N=int(eval(input()))
x=[int(eval(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))
# Original solution
a = int(eval(input()))
b = [[int(eval(input())), False]for i in range(a)]
c = 1
d = 0
while True:
    d += 1
    if b[c-1][0] == 2:
        print(d)
        exit()
    else:
        b[c-1][1] = not b[c-1][1]
        if b[c-1][1]:
            c = b[c-1][0]
        else:
            print((-1))
            exit()


# Optimized solution
a = int(eval(input()))
b = [int(eval(input())) for _ in range(a)]
c = 1
d = 0
e = {}
while True:
    d += 1
    if b[c-1] == 2:
        print(d)
        exit()
    else:
        if (c-1) not in e:
            e.setdefault((c-1), "")
        else:
            print((-1))
            exit()
        c = b[c - 1]
# Original solution
N = int(eval(input()))
array = []
for i in range(N):
    a = int(eval(input()))-1
    array.append(a)

c = 0
now = 0
for i in range(N):
    now = array[now]
    c += 1
    if now == 1:
        break
if c == N and now != 1:
    print((-1))
else:
    print(c)

# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
nexta = a[0]
ans = -1
for i in range(1,N):
    if nexta == 2:
        ans = i
        break
    nexta = a[nexta-1]
print(ans)
# Original solution
def training():
	N = int(eval(input()))

	A = [0]

	for i in range(N):
		A.append(int(eval(input())))

	counter = 1
	index = 1
	judge = [1]
	if A[index] == 2:
		print(counter)
		return
	counter += 1
	index = A[index]
	for i in range(N):
		if A[index] in judge:
			print("-1")
			return
		if A[index] == 2:
			print(counter)
			return
		counter += 1
		judge.append(A[index])
		index = A[index]
	

training()
	

# Optimized solution
def training():
	N = int(eval(input()))

	A = [0]

	for i in range(N):
		A.append(int(eval(input())))

	counter = 1
	index = 1
	#judge = [1]
	if A[index] == 2:
		print(counter)
		return
	counter += 1
	index = A[index]
	for i in range(N):
		#if A[index] in judge:
			#print("-1")
			#return
		if A[index] == 2:
			print(counter)
			return
		counter += 1
		#judge.append(A[index])
		index = A[index]
	print("-1")
	

training()
# Original solution
n = int(eval(input()))
a = list(map(int, list(eval(input()) for _ in range(n))))
visited = list(False for _ in range(n))

if (2 in a) == False:
    print((-1))
    exit()
    
idx = 1
cnt = 0
while True:
    cnt += 1
    if a[idx - 1] == 2:
        print(cnt)
        exit()
    else:
        if visited[idx - 1] == True:
            print((-1))
            exit()
        else:
            visited[idx - 1] = True
            idx = a[idx - 1] ##次のボタン

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline

    N = int(eval(input()))
    a = [int(eval(input())) - 1 for _ in range(N)]

    cur = 0
    cnt = [-1] * N
    cnt[0] = 0
    while cur != 1:
        c = cnt[cur]

        cur = a[cur]
        if ~cnt[cur]: break
        cnt[cur] = c + 1

    print((cnt[1] if ~cnt[1] else -1))


if __name__ == '__main__':
    main()
# Original solution
# -*- coding: utf-8 -*-

n = int(eval(input()))
a = [0] * n
for i in range(n):
    a[i] = int(eval(input()))

cnt = 0
lit = 0

while lit != 1 and (lit != 0 or cnt == 0):
    lit = a[lit] - 1
    cnt += 1

if lit == 1:
    print(cnt)
else:
    print((-1))


# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
i = 0
done = {a - 1: False for a in A}
num = 0
while True:
    i = A[i] - 1
    num += 1
    if i == 1:
        print(num)
        exit()
    if done[i]:
        print((-1))
        exit()
    done[i] = True
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
c,s = 1,a[0]
while s != 2 and c < n:c,s = c+1,a[s-1]
print((c if c < n else "-1"))

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

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

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]

l = [1]
bot = 1
flag = True
for i in range(n):
    bot = a[bot - 1]
    if bot == 2:
        print((i + 1))
        flag = False
        break

if flag:
    print((-1))
# Original solution
n = int(eval(input()))
a = []
for i in range(n):
  a_ = int(eval(input()))
  a.append(a_)
res = []
i = 1
for _ in range(n):
  res.append(a[i-1])
  i = a[i-1]
  if i == 2:
    break

if 2 in res:
  print((len(res)))
else:
  print((-1))

# Optimized solution
n = int(eval(input()))
a = []
for _ in range(n):
  a_ = int(eval(input()))
  a.append(a_)

ans = 0
c = 1
for i in range(n):
  c = a[c-1]
  ans += 1
  if c == 2:
    print(ans)
    exit()
print((-1))
# Original solution
n = int(eval(input()))
a = []
for _ in range(n):
  a_ = int(eval(input()))
  a.append(a_)

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

# Optimized solution
n=int(eval(input()))
a=[int(eval(input())) for i in range(n)]
ans=1
res=1
for i in range(n):
  if a[res-1]==2:
    print(ans)
    exit()
  else:
    ans+=1
    res=a[res-1]
print((-1))
# Original solution
n = int(eval(input()))
a = [0] + [int(eval(input())) for _ in range(n)]

pos = 1
cnt = 0
while True:
    cnt += 1
    if a[pos] == 1:
        print((-1))
        break
    elif a[pos] == 2:
        print(cnt)
        break
    pos = a[pos]


# Optimized solution
n = int(eval(input()))
a = [0] + [int(eval(input())) for _ in range(n)]

pos = 1
for i in range(n):
    pos = a[pos]
    if pos == 2:
        print((i+1))
        break
else:
    print((-1))
# Original solution
def main():
  n = int(eval(input()))
  a = [0]*n
  for i in range(n):
    a[i] += int(eval(input()))

  s = 1
  for i in range(n):
    if a[s-1] == 2:
      print((i+1))
      exit()
    else:
      s = a[s-1]
  print((-1))
  
if __name__=='__main__':
  main()

# Optimized solution
n = int(eval(input()))
a = [int(eval(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))
# Original solution
n = int(eval(input()))
a = [int(eval(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))

# Optimized solution
def main():
  n = int(eval(input()))
  a = [int(eval(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))
  
if __name__=='__main__':
  main()
# Original solution
n = int(eval(input()))
buttons = [eval(input()) for i in range(n)]
buttons_int = [int(b) - 1 for b in buttons]
button = 0
count = 0
history_list = []
i = 0
while i < n:
    if button == i:
        button = buttons_int[i]
        count += 1
        if button == 1:
            print(count)
            exit()
        if i in history_list:
            print((-1))
            exit()
        else:
            history_list.append(i)
        i = 0
    else:
        i += 1


# Optimized solution
n = int(eval(input()))
buttons = [eval(input()) for i in range(n)]
buttons_int = [int(b) - 1 for b in buttons]
button = 0
for count in range(1, n+1):
    button = buttons_int[button]
    if button == 1:
        print(count)
        exit()
print((-1))
# Original solution
n = int(eval(input()))
a = []
b = []
look = 1
end = 0
count = 0
for i in range(n):
    a.append(int(eval(input())))
while end == 0:
    count += 1
    b.append(look)
    look = a[look - 1]
    for i in range(len(b)):
        if b[i] == look:
            end = -1
            break
    else:
        if look == 2:
            end = count
print(end)

# Optimized solution
n = int(eval(input()))
a = []
b = [0]*n
look = 1
end = 0
count = 0
for i in range(n):
    a.append(int(eval(input())))
while end == 0:
    count += 1
    b[look-1] = 1
    look = a[look - 1]
    if b[look - 1] == 1:
        end = -1
    else:
        if look == 2:
            end = count
print(end)
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

pnext = 1
pvisited = []
while True:
	pvisited.append(pnext)
	pnext = A[pnext - 1]
	if pnext == 2:
		print((len(pvisited)))
		exit()
	if pnext in pvisited:
		print((-1))
		exit()


# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

count = 0
pnext = 1
while count < N:
	count += 1
	pnext = A[pnext - 1]
	if pnext == 2:
		print(count)
		break
else:
	print((-1))
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

a = []
i = 1
cnt = 0
while i != 2:
  a.append(i)
  cnt += 1
  i = A[i-1]
  if i == 2 or i in a:
    break

if i == 2:
  print(cnt)
else:
  print((-1))

# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

i = 1
cnt = 0
while i != 2 and cnt <= N:
  cnt += 1
  i = A[i-1]
  if i == 2:
    break

if i == 2:
  print(cnt)
else:
  print((-1))
# Original solution
N = int(eval(input()))
a = [0]
for _ in range(N):
    a.append(int(eval(input())))
memo = [1]
next = 1
cnt = 0
while True:
    cnt += 1
    next = a[next]
    if next == 2:
        print(cnt)
        break
    if next in memo:
        print((-1))
        break
    memo.append(next)

# Optimized solution
N = int(eval(input()))
a = [0]
for _ in range(N):
    a.append(int(eval(input())))
memo = [-1] + [1] + [0] * (N-1)
next = 1
cnt = 0
while True:
    cnt += 1
    next = a[next]
    if next == 2:
        print(cnt)
        break
    if memo[next] == 1:
        print((-1))
        break
    memo[next] = 1
# Original solution
n=int(eval(input()))
s=[0]+[int(eval(input()))for i in range(n)]
p=[1]
k=0
while n:
    if s[p[k]]==2:
        print((k+1))
        break
    elif k==len(set(p)):
        print((-1))
        break
    else:
        p.append(s[p[k]])
        k+=1

# Optimized solution
n=int(eval(input()))
s=[0]+[int(eval(input()))for i in range(n)]
t=s[1]
k=1
while n:
    if t==2:
        print(k)
        break
    elif t==0:
        print((-1))
        break
    else:
        a=s[t]
        s[t]=0
        t=a
        k+=1
# Original solution
N = int(eval(input()))
a = [int(eval(input())) - 1 for _ in [0]*N]
cur = 0
cnt = -1
for i in range(N):
    cur = a[cur]
    if cur == 1:
        cnt = i + 1
        break
print(cnt)

# Optimized solution
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))
# Original solution
N = int(eval(input()))
a = {i+1: int(eval(input())) for i in range(N)}

on = 1
ans = -1
count = 0
while True:
    on = a[on]
    count += 1
    if on == 2:
        ans = count
        break
    if on == 1:
        break
print(ans)


# Optimized solution
N = int(eval(input()))
a = {i+1: int(eval(input())) for i in range(N)}

on = 1
history = {1}
ans = -1
count = 0
while True:
    on = a[on]
    count += 1
    if on == 2:
        ans = count
        break
    if on in history:
        break
    history.add(on)
print(ans)
# Original solution
n = int(eval(input()))
a = []
b = 1
c = 1
d = []
for i in range(n):
    a.append(int(eval(input())))
while True:
    d.append(b-1)
    b = a[b-1]
    if b == 2:
        print(c)
        break
    if (b-1) in d:
        print((-1))
        break
    c += 1
    


# Optimized solution
n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())))

k = 1
p = False
for i in range(n):
    if a[k-1] == 2:
        print((i+1))
        p = True
        break
    k = a[k-1]
if not p:
    print((-1))
# Original solution
N = int(eval(input()))
A = [eval(input()) for i in range(N)]

prev = []
next = 0
count = 0
while(next not in prev and count <= N):
    prev.append(next)
    next = int(A[next])-1
    count += 1
    if(next == 1):
        print(count)
        break
else:
    print((-1))


# Optimized solution
N = int(eval(input()))
A = [eval(input()) for i in range(N)]

prev = []
next = 0
count = 0
while(count < N):
    next = int(A[next])-1
    count += 1
    if(next == 1):
        print(count)
        break
else:
    print((-1))
# Original solution
N = int(eval(input()))
a = [set() for _ in range(N)]

for i in range(N):
    id = int(eval(input())) - 1
    a[id].add(i)

step = -1
already = a[1]
now = a[1]
next = set()
for i in range(N-1):
    already = already.union(now)
    if 0 in now:
        step = i+1
        break
    for j in now:
        next = next.union(a[j])
    now = next
    next = set()

print(step)

# Optimized solution
N = int(eval(input()))
a = [set() for _ in range(N)]

for i in range(N):
    id = int(eval(input())) - 1
    a[id].add(i)

step = -1
#already = a[1]
now = a[1]
next = set()
for i in range(N-1):
    #already = already.union(now)
    if 0 in now:
        step = i+1
        break
    for j in now:
        next = next.union(a[j])
    now = next
    next = set()

print(step)
# Original solution
N=int(eval(input()))
a=[int(eval(input()))-1 for _ in range(N)]
x=a[0]
ans=1
while x!=0:
    if x==1:
        print(ans)
        break
    ans+=1
    x=a[x]
else:
    print((-1))


# Optimized solution
N=int(eval(input()))
a=[int(eval(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;
# Original solution
N = int(input())
A = [int(input()) for _ in range(N)]

B = [1]
i = 0
flag = False
for _ in range(N):
    if 2 in B:
        flag = True
        break
    elif A[i] in B:
        break
    B.append(A[i])
    i = A[i] - 1

print(len(B) - 1) if flag else print(-1)


# Optimized solution
N = int(input())
A = [int(input()) for _ in range(N)]

B = [1]
i = 0
cnt = 0
flag = False
for _ in range(N):
    i = A[i] - 1
    cnt += 1
    if i == 1:
        flag = True
        break

print(cnt) if flag else print(-1)
# Original solution
N = int(eval(input()))

jmps = []
for _ in range(N):
    jmps.append(int(eval(input())) - 1)

now = 0
ans = 0
for i in range(N):
    if now == 1:
        ans = i
        break
    now = jmps[now]

ans = ans or -1
print(ans)

# Optimized solution
N = int(eval(input()))

jmps = [int(eval(input())) - 1 for _ in range(N)]

now = 0
ans = 0
for i in range(N):
    if now == 1:
        ans = i
        break
    now = jmps[now]

ans = ans or -1
print(ans)
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
L = [0]
i = 0
cnt = 0
while True:
    if a[i]-1 == 1:
        cnt += 1
        break
    elif a[i]-1 in L :
        cnt = -1
        break
    else:
        L.append(a[i]-1)
        cnt += 1
        i = a[i]-1
print(cnt)

# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
L = set([0])
i = 0
cnt = 0
while True:
    if a[i]-1 == 1:
        cnt += 1
        break
    elif a[i]-1 in L:
        cnt = -1
        break
    else:
        L.add(a[i]-1)
        cnt += 1
        i = a[i]-1
print(cnt)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
b = [0] * n
ans = 0
c = 0
b[0] = 1
while True:
    c = a[c] - 1
    ans += 1
    if b[c] == 1:
        ans = -1
        break
    if c == 1:
        break
print(ans)

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
b = [0] * n
ans = 0
c = 0
b[c] = 1
while True:
    c = a[c] - 1
    ans += 1
    if b[c] == 1:
        ans = -1
        break
    if c == 1:
        break
    b[c] = 1
print(ans)