CodeEff/ECCO huggingface dataset:
# Original solution
n=int(eval(input()))
a=[int(eval(input())) for _ in range(n)]
       
cnt=0
tmp=1
already=[]
while True:
    cnt+=1
    already.append(tmp)
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        break
    elif tmp in already[::-1]:
        print((-1))
        break

# Optimized solution
import sys
n=int(eval(input()))
a=[int(eval(input())) for _ in range(n)]
       
cnt=0
tmp=1

for i in range(n):
    cnt+=1
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        sys.exit()
print((-1))
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
count = 0
i = 1
osita = []
while 2 not in osita:
    osita.append(i)
    i = a[i-1]
    count += 1
    if count > len(a):
        print((-1))
        exit()
print((count-1))

# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
count = 0
i = 1
x = 0
osita = [0]*(N+1)
for b in range(N+1):
    osita[b] = i
    i = a[i-1]
    
if 2 in osita:
    print((osita.index(2)))
else:
    print((-1))
# Original solution
import sys

l = [int(i) for i in sys.stdin]

N = l[0]

a = [int(i) - 1 for i in l[1:]]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)


# Optimized solution
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
on = 1
count = 0
on_li = [1]
while True:
    on = a[on-1]
    count += 1
    if on == 2:
        print(count)
        exit()

    if on in on_li:
        print((-1))
        exit()
    else:
        on_li.append(on)

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

is_visited = [ False for _ in range(n) ]
is_visited[0] = True

cnt = 0
now = 0
while True:
    if now == 1:
        print(cnt)
        break
    nxt = table[now]
    if is_visited[nxt]:
        print((-1))
        break
    now = nxt
    cnt += 1

# Optimized solution
n = int(eval(input()))
table = {}
for i in range(n):
    a = int(eval(input()))
    table[i] = a-1

is_visited = [ False for _ in range(n) ]
is_visited[0] = True

cnt = 0
now = 0
while True:
    is_visited[now] = True
    if now == 1:
        print(cnt)
        break
    nxt = table[now]
    if is_visited[nxt]:
        print((-1))
        break
    now = nxt
    cnt += 1
# Original solution
n = int(eval(input()))
btn = []
checked = []
flag = True

for i in range(n):
  btn.append(int(eval(input())))

def find(n):
  global btn, checked
  j = 1
  count = 0
  for i in range(n):
    if btn[j - 1] == 2:
      count += 1
      return count
    elif j - 1 in checked:
      return -1
    else:
      checked.append(j - 1)
      j = btn[j - 1]
      count += 1
  return -1

print((find(n)))

# Optimized solution
n = int(eval(input()))
btn = []
checked = set()

for i in range(n):
  btn.append(int(eval(input())))

def find(n):
  global btn, checked
  j = 1
  for i in range(n):
    if btn[j - 1] == 2:
      # count += 1
      return len(checked) + 1
    elif j - 1 in checked:
      return -1
    else:
      checked.add(j - 1)
      j = btn[j - 1]
  return -1

print((find(n)))
# Original solution
n = int(eval(input()))
a = [0] + [int(eval(input())) for i in range(n)]

end = {}

end[1] = True
to = a[1]

cnt = 0
while True:
    cnt += 1
    if to == 2:
        print(cnt)
        __import__("sys").exit()
    if end.get(to) == None:
        to = a[to]
    else:
        break

print((-1))


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

end = {}

end[1] = True
to = a[1]

cnt = 0
while True:
    cnt += 1
    if to == 2:
        print(cnt)
        __import__("sys").exit()
    if end.get(to) == None:
        end[to] = True
        to = a[to]
    else:
        break

print((-1))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
i = 1
count = 0

while True:
    i = a[i-1]
    count += 1
    if i == 1:
        print((-1))
        break
    elif i == 2:
        print(count)
        break

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

while True:
    i = a[i-1]
    count += 1
    if i == 1 or count == n+1:
        print((-1))
        break
    elif i == 2:
        print(count)
        break
# Original solution
n=int(eval(input()))
lst=[int(eval(input())) for i in range(n)]

lst = list([x-1 for x in lst])
counts = 0
lst_p = [0]
tmp = 0
while True:
    tmp = lst[tmp]
    counts += 1
    if tmp == 1:
        print(counts)
        break
    if tmp in lst_p:
        print((-1))
        break
    lst_p.append(tmp)

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

lst = list([x-1 for x in lst])
lst_p = [0]
tmp = 0
flag =True
for i in range(n+1):
    tmp = lst[tmp]
    if tmp == 1:
        print((i+1))
        flag = False
        break
if flag:
    print((-1))
# Original solution
N = int(eval(input()))

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

ans = 1
button = a[0]
for i in range(N):
    if button == 2:
        break
    button = a[button-1]
    ans += 1
    
if ans >= N+1:
    print((-1))
else:
    print(ans)

# Optimized solution
N = int(eval(input()))
a = []
for _ in range(N):
    a.append(int(eval(input()))-1)

tmp = a[0]
ans = 1
for i in range(N+1):
    if tmp == 1:
        print(ans)
        break
    tmp = a[tmp]
    ans += 1
else:
    print((-1))
# Original solution
n = eval(input())
b = []
b_old = []
for i in range(int(n)):
    tmp = eval(input())
    b.append(int(tmp))

count = 1
s = b[0]
if s == 2:
    print(count)
else:
    b_old.append(s)
    n_b = s
    flag = 0
    while(True):
        count += 1
        n_b = b[n_b-1]
        if n_b == 2:
            print(count)
            flag = 1
            break
        elif n_b in b_old:
            print("-1")
            flag = 1
            break
        elif count == int(n):
            print("-1")
            break
        else:
            b_old.append(n_b)


# Optimized solution
n = int(eval(input()))
b = []
b_old = []
for i in range(n):
    tmp = int(eval(input()))
    b.append(tmp)
n_b = 1
count = 0
flag = 0
while(count < n):
    n_b = b[n_b-1]
    count += 1
    if n_b == 2:
        print(count)
        flag = 1
        break
if flag == 0:
    print("-1")
# Original solution
n = int(eval(input()))
a = []
for i in range(n):
	a.append(int(eval(input())))

past = [0 for _ in range(n)]

cnt = 0
now = 1

while(True):
	if past[now-1] == 1:
		print((-1))
		exit(0)
	if now == 2:
		print(cnt)
		exit(0)
	past[now-1] = 1
	cnt += 1
	now = a[now-1]

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

checked = [0 for _ in range(n)]

cnt = 0
light = 0

while(True):
    if checked[light]:
        print((-1))
        break
    checked[light] = True
    light = inp[light] - 1
    cnt += 1
    if light == 1:
        print(cnt)
        break
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N) ]
hist = [1]
bottun = 0
i = 0

while True:
    # print(hist)
    bottun = a[hist[i]-1]
    # print(bottun)
    for j in hist:
        if bottun == j:
            print((-1))
            # print("hoge")
            exit()
    
    if bottun == 2:
        print((len(hist)))
        exit()
    
    hist.append(bottun)
    i += 1


# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
hist = [0] * N
bottun = a[0]
cnt = 1
hist[0] = 1

while True:
    # print(hist)
    # print(bottun)
    if hist[bottun-1] == 1:
        print((-1))
        exit()
    
    if bottun == 2:
        print(cnt)
        exit()
    
    hist[bottun-1] = 1
    bottun = a[bottun-1]
    cnt += 1
# Original solution
N = int(eval(input()))
a = tuple(int(eval(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)

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

now = 1
flag = 1
memo = []
cnt = 0
for i in range(N):
    next = a_lis[now-1]
    if next in memo:
        print((-1))
        flag = 0
        break
    else:
        memo.append(next)
        now = next
    cnt+=1
    if next ==2:
        print(cnt)
        break


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

now = 1
flag = 1
cnt = 0
while cnt<N and next!=2:
    cnt+=1
    next = a_lis[now-1]
    now = next
if next==2:print(cnt)
else:print((-1))
# Original solution
import sys
n=int(eval(input()))
a=[]
for i in range(n):
    a.append(int(eval(input())))
count =1 #ボタンを押した回数用 
s=0 #スタートするボタン　これを操作ボタンにしていく
while True:
    if a[s]==2:
        print(count)
        sys.exit()
    elif count>1 and s==0:
        print((-1))
        sys.exit()
    else:
        count+=1
        s=a[s]-1



# Optimized solution
import sys
n=int(eval(input()))
a=[]
for i in range(n):
    a.append(int(eval(input())))
count =1 #ボタンを押した回数用 
s=0 #スタートするボタン　これを操作ボタンにしていく
while True:
    if a[s]==2:
        print(count)
        sys.exit()
    elif count>n:
        print((-1))
        sys.exit()
    else:
        count+=1
        s=a[s]-1
# Original solution
N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())))

pushed = [0] * N
pushed[0] = 1

current_bottom = a[0]
ans = 1
if current_bottom == 2:
    print(ans)
    exit()

for _ in range(N):
    current_bottom = a[current_bottom-1]
    if pushed[current_bottom-1] == 1:
        print((-1))
        exit()
    else:
        pushed[current_bottom-1] = 1
    ans += 1
    if current_bottom == 2:
        print(ans)
        exit()

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

current_bottom = a[0]
ans = 1
if current_bottom == 2:
    print(ans)
    exit()

for _ in range(N):
    current_bottom = a[current_bottom-1]
    ans += 1
    if current_bottom == 2:
        print(ans)
        exit()
print((-1))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
count = 0
visited = []
ans = -1

btn = 0
for _ in range(n):
  if btn in visited:
    break
  visited.append(btn)
  btn = a[btn]-1
  count += 1
  if btn==1:
    ans = count
    break
    
print(ans)
  

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

ans = -1
count = 0
btn = 0
for i in range(n):
  btn = a[btn]-1
  count += 1
  if btn == 1:
    ans = count
    break

print(ans)
# Original solution
N=int(eval(input()))
A=[]
for i in range(N):
    A.append(int(eval(input())))

light=1
count=0
while True:
    if light==2:
        print(count)
        quit()
    elif count>0 and light==1:
        print((-1))
        quit()
    else:
        light=A[light-1]
        count+=1

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

light=1
count=0
for i in range(N):
    if light==2:
        print(count)
        quit()
    elif count>0 and light==1:
        print((-1))
        quit()
    else:
        light=A[light-1]
        count+=1
print((-1))
# Original solution
n=int(eval(input()))
a=[int(eval(input())) for i in range(n)]

b=a[0]
cnt=1
while b!=2:
  b=a[b-1]
  cnt+=1
  if b==a[0]:
    print((-1))
    exit()
    
print(cnt)




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

b=a[0]
cnt=1
if b==2:
  print(cnt)
else:
  
  while cnt<=n+2:
    b=a[b-1]
    cnt+=1
    if b==2:
      print(cnt)
      exit()
      
  print((-1))
# Original solution
n = int(eval(input()))
A = [int(eval(input())) for i in range(n)]
ans = 1
light = 0
list = []

while True:
    if A[light] in list:
        ans = -1
        break
    elif A[light] == 2:
        break
    else:
        ans = ans + 1
        list.append(light + 1)
        light = A[light] - 1

print(ans)

# Optimized solution
n = int(eval(input()))
A = [int(eval(input())) for i in range(n)]
ans = 0
light = 1
list = [0] * n

while True:
    if light == 2:
        break
    elif list[light - 1] == 1:
        ans = -1
        break
    else:
        ans = ans + 1
        list[light - 1] = 1
        light = A[light - 1]

print(ans)
# Original solution
N = int(eval(input()))
A = {i+1: int(eval(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))

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

b = 0
for i in range(N):
  if(b != 0):
    b -=1
  b = A[b]
  if(b == 2):
    print((i+1))
    break
else:
  print((-1))
# Original solution
# -*- coding:utf-8 -*-
import sys
l = [int(i) for i in sys.stdin]
used = []
add = used.append
now = 1
count = 0
while True:
  if now == 2:
    break
  if now in used:
    print((-1))
    exit()
  add(now)
  now = l[now]
  count += 1
print(count)

# Optimized solution
# -*- coding:utf-8 -*-
import sys
button = [int(i) for i in sys.stdin]
now = 1
count = 0
for i in range(button[0]):
  if now == 2:
    print(count)
    exit()
  now = button[now]
  count += 1
print((-1))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
j = 0
for k in range(n):
    j = a[j] - 1
    if j == 1:
        print((k+1))
        break
    elif k == n-1:
        print((-1))
    else:
        pass

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
j = 0
for k in range(n):
    j = a[j] - 1
    if j == 1:
        print((k+1))
        break
    else:
        pass
else:
    print((-1))
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
pikaa = 1
ans = 0
while 1:
    pikaa = a[pikaa - 1]
    ans += 1
    if pikaa == 1:
        print((-1))
        exit()
    elif pikaa == 2:
        print(ans)
        exit()


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

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

i = 0
count = 0

pushed = []

while(1):
  pushed.append(i)
  i = a[i]
  count += 1
  if i == 1:
    break
  elif i in pushed:
    count = -1
    break
    
print(count)

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

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

i = 0
count = 0

for _ in range(n+1):
  i = a[i]
  count += 1
  if i == 1:
    break

if count == n + 1:
  count = -1
  
print(count)
# Original solution
import sys

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

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        sys.exit()
print("-1")


# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
cnt = 0
tmp = 1
for i in A:
    tmp = A[tmp-1]
    cnt += 1
    if tmp == 2:
        print(cnt)
        exit()
print("-1")
# Original solution
N=int(input())
A=[]
I=[]
for n in range(N):
    a=int(input())
    A.append(a)
I.append(1)
def t(I):
    #while 2 not in I:
    while 1:
        a=A[I[-1]-1]
        if a in I:
            return print(-1)
        if a==2:
            return print(len(I))
        I.append(a)
t(I)


# Optimized solution
N=int(input())
A=[int(input()) for n in range(N)]
def t(N,A):
    i=1
    for n in range(1,N+1):
        a=A[i-1]
        if a==2:
            return print(n)
        i=a
    return print(-1)
t(N,A)
# Original solution
#Trained?
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
judge2 = 0
judgeloop = 0
now = 1
tugi = a[0]
rireki = []
n = 1

if tugi == 2:
    judge2 += 1

while judge2 == 0 and judgeloop == 0:
    n += 1
    rireki.append(now)
    now = tugi
    tugi = a[tugi-1]
    if tugi in rireki:
        judgeloop += 1
    elif tugi == 2:
        judge2 += 1

if judgeloop == 1:
    print((-1))
elif judge2 == 1:
    print(n)

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

now = 1
L = [0]*N
for i in range(N):
    L[i] = now
    now = a[now-1]

judge = 1
for i in range(N):
    if L[i] == 2:
        print(i)
        judge = 0
        break
    else:
        pass

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

# Optimized solution
n = int(eval(input()))
a=[0]
for i in range(n):
  a.append(int(eval(input())))
c = 1
k = a[1]
while c <= n:
  if k == 2:
    print(c)
    exit()
  k = a[k]
  c += 1
print((-1))
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
a = [0] + a
hist = [1, ]
count = 0
i = 1
while i != 2:
    i = a[i]
    count  += 1
    if i in hist:
        count = -1
        break
    hist.append(i)
print(count)

# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
a = [0] + a
count = 0
i = 1
while i != 2:
    j = i
    i = a[i]
    a[j] = -1
    count  += 1
    if i == -1:
        count = -1
        break
print(count)
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
a = [0] + a
count = 0
i = 1
while i != 2:
    j = i
    i = a[i]
    a[j] = -1
    count  += 1
    if i == -1:
        count = -1
        break
print(count)

# Optimized solution
n = int(eval(input()))
A = [int(eval(input())) for _ in range(n)]
count = 0
i = 1
for _ in range(n):
    i = A[i - 1]
    count += 1
    if i == 2:
        print(count)
        exit()
print((-1))
# Original solution
n=int(eval(input()))
a=[int(eval(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)   

# Optimized solution
n,*a=list(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))
# Original solution
n=int(eval(input()))
a=[]
for i in range(n):
	a.append(int(eval(input())))
b=['']*n
b[0]='hoge'
c=0
x=0
while True:
	c+=1
	x=a[x]-1
	if b[x]=='hoge':
		c=-1
		break
	else:
		b[x]='hoge'
		if x==1:
			break
print(c)



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

l=[]
for i in range(N):
  l.append(int(eval(input())))

# 押してない：0
# 押した：1
l2=[0]*N

# 1番目は「押した：1」にしておく
l2[0]=1

# 回数カウント
c=0

i=1
while True:
  # 回数をインクリメント
  c+=1

  # 次のボタン番号を取得
  i=l[i-1]

  # 2番目のライトが光る場合は回数を出力して終了
  if i==2:
    print(c)
    exit()

  if l2[i-1]==1:
    # 無限ループ確定で終了
    print((-1))
    exit()
  else:
    # 押した／押してない を更新
    l2[i-1]=1
# Original solution
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, a: "List[int]"):
    visited = [False]*N
    curr = 1
    counter = 0
    while curr != 2:
        curr = a[curr-1]
        counter += 1
        if curr == 1:
            print((-1))
            return
    print(counter)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)


if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, a: "List[int]"):
    visited = [False]*N
    curr = 1
    counter = 0
    while curr != 2:
        if visited[curr-1]:
            print((-1))
            return
        visited[curr-1] = True
        curr = a[curr-1]
        counter += 1
    print(counter)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)


if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
T = [False for i in range(n)]
tmp = 1
T[0] = True
count = 1
while True:
    tmp = a[tmp-1]
    if T[tmp -1]:
        print((-1))
        exit()
    if tmp == 2:
        print(count)
        break
    count+=1

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
T = [False for i in range(n)]
tmp = 1
T[0] = True
count = 1
while True:
    tmp = a[tmp-1]
    if T[tmp -1]:
        print((-1))
        exit()
    T[tmp-1] = True
    if tmp == 2:
        print(count)
        break
    count+=1
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
b = [0]*n
b[0] = 1
count = 0
c = a[0]
for i in range(n):
  if b[c-1] == 0:
    b[c-1] = 1
    if c == 2:
      print((count+1))
      break
  else:
    print((-1))
    break
  c = a[c-1]
  count += 1

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i 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))
# Original solution
def solve():
    N = int(input())
    arr = []
    for i in range(N):
        arr.append(int(input()))
    turning_on = 1
    count = 0
    turned_on_set = [1]
    while turning_on != 2:
        count += 1
        turning_on = arr[turning_on - 1]
        if turning_on in turned_on_set:
            count = -1
            break
        turned_on_set.append(turning_on)
    print(str(count))

if __name__ == '__main__':
    solve()


# Optimized solution
def solve():
    N = int(input())
    arr = []
    for i in range(N):
        arr.append(int(input()))
    turning_on = 1
    count = 0
    while turning_on != 2:
        count += 1
        turning_on = arr[turning_on - 1]
        if count > N + 1:
            count = -1
            break
    print(str(count))

if __name__ == '__main__':
    solve()
# Original solution
n = int(eval(input()))
a = []
for i in range(n):
  a.append(int(eval(input())))
b = a[0]
c = []
while True:
  if b == 2:
    print((len(c)+1))
    exit()
  elif b in c:
    print((-1))
    exit()
  else:
    c.append(b)
    b = a[c[-1]-1]

# Optimized solution
n = int(eval(input()))
a = []
for i in range(n):
  a.append(int(eval(input())))
b = a[0]
c = []
while True:
  if b == 2:
    print((len(c)+1))
    exit()
  if len(c)> n:
    print((-1))
    exit()
  else:
    c.append(b)
    b = a[c[-1]-1]
# Original solution
# ABC065 B - Trained?
N = int(eval(input()))
A = [[int(eval(input())),False] for i in range(N)]
idx = 0
A[0][1] == True
cnt = 1

while cnt != N+1:
    if A[idx][1]:
        print((-1))
        exit()
    if A[idx][0] == 2:
        print(cnt)
        exit()
    A[idx][1] = True
    idx = A[idx][0]-1
    cnt += 1


# Optimized solution
n=int(eval(input()))
a=[int(eval(input())) for i in range(n)]
btn=[0]*n
ans=1
i=0
while True:
  if btn[i]==1:
    print((-1))
    exit()
  if a[i]==2:
    print(ans)
    exit()
  btn[i]=1
  i=a[i]-1
  ans+=1
# Original solution
n = int(eval(input()))
A = [int(eval(input())) for i in range(n)]
B = []
for i, a in enumerate(A):
    B.append([i + 1, a])
    
idx = 1
for i in range(n):
    x = B[idx - 1][1]
    if x == 2:
        print((i + 1))
        break
    else:
        idx = x
else:
    print((-1))

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

cnt = 1
x = A[0]
while x != 2 and cnt != n:
    x = A[x - 1]
    cnt += 1
    
if cnt == n:
    print((-1))
else:
    print(cnt)