CodeEff/ECCO huggingface dataset:
# Original solution
*a, = list(map(int, open(0).read().split()))
b = a[1]
l = []
for i in range(a[0]):
    if b in l:
        print((-1))
        exit()
    elif b == 2:
        print((i+1))
        exit()
    l += [b]
    b = a[b]

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

pushed = [0]
ans = 0
current = 0
while True:
    ans += 1
    current = a[current] - 1
    if current in pushed:
        ans = -1
        break
    if current == 1:
        break
    pushed.append(current)
print(ans)


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

pushed = [False] * N
ans = 0
current = 0
while True:
    ans += 1
    current = a[current] - 1
    if pushed[current]:
        ans = -1
        break
    if current == 1:
        break
    pushed[current] = True
print(ans)
# Original solution
def I(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LMI(): return list(map(int, input().split()))

N = I()
A = [I() for _ in range(N)]

seen = {1}
i = 0
light = 1
while light != 2:
    light = A[light - 1]
    if light in seen:
        print((-1))
        exit(0)
    i += 1
print(i)


# Optimized solution
def I(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LMI(): return list(map(int, input().split()))

N = I()
A = [I() for _ in range(N)]

seen = set()
i = 0
light = 1
while light != 2:
    seen.add(light)
    light = A[light - 1]
    if light in seen:
        print((-1))
        exit(0)
    i += 1
print(i)
# Original solution
# -*- coding:utf-8 -*-
N=int(eval(input()))
button=[]
for i in range(N):
  button.append(int(eval(input())))

number=0
count=0
footprint=[0]
while number!=1:
  number=button[number]-1
  count+=1
  if number in footprint:
    print((-1))
    exit()
  footprint.append(number)
print(count)

# Optimized solution
# -*- coding:utf-8 -*-
import sys

a=[int(i)-1 for i in sys.stdin]
N=a[0]+1
a=a[1:]

done=[False]*N

ans=0
i=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()))
button_list =[]
for i in range(1,N+1):
  button_list.append([i,int(eval(input()))])
bond_list = [button_list[0][0]]
dest = 0
for i in range(N):
  bond_list.append(button_list[button_list[dest][1]-1][0])
  bond_list.append(button_list[button_list[dest][1]-1][1])
  dest = button_list[button_list[dest][1]-1][1]-1
  if 2 in bond_list:
    print((bond_list.index(2)))
    break;
  if len(bond_list) >=3 and bond_list[len(bond_list)-1] == bond_list[len(bond_list)-3]:
    print((-1))
    break;
  
  

# Optimized solution
N = int(eval(input()))
button_list =[]
for i in range(1,N+1):
  button_list.append(int(eval(input())))

count = 0
dest = 1
for i in range(N):
    dest = button_list[dest - 1]
    count +=1
    if dest == 2:
        break;
if count == N:
    print((-1))
else:
    print(count)
# Original solution
n = int(eval(input()))
x = [0] + [int(eval(input())) for i in range(n)]
cnt = 1
idx = min(x[1:])
while True:
    if x[idx] == 2:
        ans = cnt
        break
    elif x[idx] == x.index(x[idx]):
        ans = -1
        break
    elif x.index(x[idx]) == x.index(min(x[1:])):
        ans = -1
        break
    idx = x[idx]
    cnt += 1
print(ans)

# Optimized solution
def train(n):
    x =[0] + [int(eval(input())) for i in range(n)]
    idx = 1
    for i in range(n+1):
        if x[idx] == 2:
            return i + 1
        idx = x[idx]
    return -1
n = int(eval(input()))
print((train(n)))
# Original solution
N=int(eval(input()))
a=[int(eval(input())) for _ in range(N)]

visited=[1]
old=1
count=0
while 1:
  new = a[old-1]
  count += 1
  if new in visited:
    print((-1))
    exit()
  if new == 2:
    print(count)
    exit()
  old = new
  visited.append(new)

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

visited={1,}
old=1
count=0
while 1:
  new = a[old-1]
  count += 1
  if new in visited:
    print((-1))
    exit()
  if new == 2:
    print(count)
    exit()
  old = new
  visited.add(new)
# Original solution
N = int(eval(input()))
dic = {}
for i in range(N):
  a = int(eval(input()))
  dic[i + 1] = a
l = [1]
n = dic[1]
i = 1
flg = False
if n == 2:
  print((1))
else:
  while n not in l:
    l.append(n)
    n = dic[n]
    i += 1
    if n == 2:
      flg = True
      ans = i
      break
  if flg:
    print(ans)
  else:
    print((-1))
  


# Optimized solution
n = int(eval(input()))
dic = {}
push = [True] * n
for i in range(1, n+1):
  a = int(eval(input()))
  dic[i] = a
i = 1
cnt = 1
push[0] = False
while True:
  if dic[i] == 2:
    break
  else:
    k = dic[i]
    if push[k-1]:
      i = k
      cnt += 1
      push[k-1] = False
    else:
      cnt = -1
      break
print(cnt)
# Original solution
# https://atcoder.jp/contests/abc065/tasks/abc065_b

def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))

N = INT()
a = []
for _ in range(N):
    a.append(INT() - 1)

light_idx = 0

ans = 0
while True:
    light_idx = a[light_idx]
    ans += 1

    if light_idx == 1:
        break

    if light_idx == 0:
        ans = -1
        break

print(ans)


# Optimized solution
# https://atcoder.jp/contests/abc065/tasks/abc065_b

def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))

N = INT()
a = []
for _ in range(N):
    a.append(INT() - 1)

light_idx = 0

ans = 0
while True:
    light_idx = a[light_idx]
    ans += 1

    if light_idx == 1:
        break

    if ans > N:
        ans = -1
        break

print(ans)
# Original solution
from functools import reduce

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

rm = {}
for i, x in enumerate(a):
  rm.setdefault(x, set()).add(i + 1)

p = set()
for i in range(n):
  if 1 in rm.get(2, []):
    print((i + 1))
    exit(0)
  d = rm.get(2, set()) - p
  if len(d) == 0: break
  n = reduce(lambda a, x: a.union(rm.get(x, set())), d, d)
  rm[2] = d.union(n)
  p = p.union(d)
print((-1))

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

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

print((-1))
# Original solution
N=int(eval(input()))
a=[int(eval(input())) for i in range(N)]
b=[]
j=1
k=1
if 2 not in a:
    print((-1))
else:
    while j in range(1,N+1):
      if a[k-1] not in b:
         b.append(a[k-1])
         if a[k-1]==1:
                 break
         elif a[k-1]==2:
                 break
         else:
            j += 1
            k = a[k-1]
      else:
          break
    if 2 not in b:
        print((-1))
    else:
        print((len(b)))

# Optimized solution
N=int(eval(input()))
a=[int(eval(input())) for i in range(N)]
b=[]
j=1
k=1
c=1
if 2 not in a:
    print((-1))
else:
    while j in range(1,N+1):
         c += 1
         if a[k-1]==1:
                 print((-1))
                 break
         elif a[k-1]==2:
                 print((c-1))
                 break
         else:
            j += 1
            k = a[k-1]
    if c>=(N+1):
        print((-1))
# Original solution
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
 
n = ni()
a = [int(ns()) for i in range(n)]
pushed = []

next_b = 1
count = 0
for _ in range(0,n):
  if next_b == 2:
    break
  count += 1
  next_b = a[next_b-1]
  if next_b in pushed:
    count = -1
    break
  pushed.append(next_b)
  
if count == n:
  count = -1
print(count)
  

# Optimized solution
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
 
n = ni()
a = [int(ns()) for i in range(n)]
pushed = {}

next_b = 1
count = 0
for _ in range(0,n):
  if next_b == 2:
    break
  count += 1
  next_b = a[next_b-1]
  if pushed.get(next_b) != None:
    count = -1
    break
  pushed[next_b] = True
  
if count == n:
  count = -1
print(count)
# Original solution
def main():
    N = int(eval(input()))
    a = [int(eval(input())) for _ in range(N)]

    ans = 0
    nxt = 1
    while True:
        if ans > N:
            print((-1))
            exit()
        elif nxt == 2:
            break
        nxt = a[nxt-1]
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()


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

    tmp = 0

    for i in range(N):
        if a[tmp] == 2:
            print((i+1))
            exit()
        else:
            tmp = a[tmp]-1

    print((-1))


if __name__ == "__main__":
    main()
# Original solution
n = int(eval(input()))
a = [0] * n
for i in range(n):
    a[i] = int(eval(input()))
ans = 1
num = a[0]
path = []
while num != 2:
    if len({num} & set(path)) > 0:
        ans = -1
        break
    path.append(num)
    num = a[num-1]
    ans += 1
print(ans)

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

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

ans = 0
check = []
tmp = 1
check.append(tmp)
flag = False
while(1):
    tmp = a[tmp][0]
    ans += 1
    if tmp==2:
        flag = True
        break
    if tmp in check:
        break
    check.append(tmp)
    if ans >N:
        break
if flag:
    print(ans)
else:
    print((-1))

# Optimized solution
N = int(eval(input()))
a = []
for i in range(N):
    b = int(eval(input()))
    a.append(b)
ans = 0
tmp = 1
flag = False
while(1):
    tmp = a[tmp-1]
    ans += 1
    if tmp==2:
        flag = True
        break
    if ans > N:
        break
if flag:
    print(ans)
else:
    print((-1))
# Original solution
N = int(eval(input()))
a = []
for i in range(N) :
    a.append(int(eval(input())))

index = 1
cnt = 0

while True:
    index = a[index - 1]
    cnt += 1
    if index == 2 :
        break
    if index == 1 :
        cnt = -1
        break

print(cnt)


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

index = 1
cnt = 0

for i in range(N):
    index = a[index - 1]
    cnt += 1
    if index == 2 :
        break
    if index == 1 :
        cnt = -1
        break
    if i == N - 1 :
        cnt = -1

print(cnt)
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]

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


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

i = 1
c = 0
ans = -1
for _ in range(N):
    i = a[i - 1]
    c += 1
    if i == 2:
        ans = c
        break
print(ans)
# Original solution
n = int(eval(input()))
s = [int(eval(input())) for _ in range(n)]
t = list(range(1,n+1))
i = 1
count = 0

while(i in t):
    count += 1
    t.remove(i)
    i = s[i-1]
    if i==2:
        print(count)
        break
else:
    print((-1))

# Optimized solution
n = int(eval(input()))
s = [int(eval(input())) for _ in range(n)]
t = set()
i = 1
count = 0

while(i not in t):
    count += 1
    t.add(i)
    i = s[i-1]
    if i==2:
        print(count)
        break
else:
    print((-1))
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

done_list = [1]
ans = 0
tmp = 1
for _ in range(N):
    if A[tmp-1] in done_list:
        print((-1))
        exit(0)
    done_list.append(A[tmp-1])
    tmp = A[tmp-1]
    ans += 1
    if tmp == 2:
        print(ans)
        exit(0)

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

done = {}
ans = 0
now = A[1]
while True:
    ans += 1
    if now == 2:
        print(ans)
        exit(0)
    if done.get(now) == None:
        done[now] = True
        now = A[now]
    else:
        break
print((-1))
# Original solution
n = int(eval(input()))
l = []
o = set()
t = 0
c = 0
ans = -1
for i in range(n):
    l.append(int(eval(input()))-1)

for i in range(n):
    if l[t] == 1:
        ans = c+1
        break
    else:
        if t in o:
            break
        else:
            c += 1
            o.add(t)
            t = l[t]

print(ans)


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

button = [0]
on = 0
ans = 0

while True:
    now = a[on]-1
    ans += 1
    if now not in button:
        if now == 1:
            break
        else:
            on = now
            button.append(now)
    else:
        ans = -1
        break

print(ans)

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

on, ans, flg = 0, 0, 0

for i in range(N):
    now = a[on]-1
    ans += 1
    if now == 1:
        flg = 1
        break
    else:
        on = now

if flg == 1:
    print(ans)
else:
    print((-1))
# Original solution
#ABC065

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

# Optimized solution
#ABC065

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

i = 1
for _ in range(n):
    
    b.append(i)
    i = a[i-1]
    
    
        
if 2 in b:
    print((b.index(2)))
else:
    print((-1))
# Original solution
n = int(eval(input()))
a = []
for _ in range(n):
    a.append(int(eval(input())))
r = []
idx = 0
cnt = 0
for i in range(n):
    w = a[idx]
    cnt += 1
    if w in r:
        cnt = -1
        break
    elif w == 2:
        break
    elif i == n - 1:
        cnt = -1
        break
    else:
        r.append(w)
        idx = w - 1
print(cnt)

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
cnt = idx = 0
for i in range(n):
    cnt += 1
    if a[idx] == 2:
        break
    if a[a[idx] - 1] == idx + 1:
        cnt = -1
        break
    if i == n - 1:
        cnt = -1
        break
    idx = a[idx] - 1
print(cnt)
# Original solution
N = int(eval(input()))
B = [0] + [int(eval(input())) for _ in range(N)]

x = 1
cnt = 0
while True:
  x = B[x]
  cnt += 1
  if x == 2:
    break
  if x == 1:
    cnt = -1
    break
print(cnt)


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

x = 1
cnt = 0
dic = {x}
while True:
  x = B[x]
  cnt += 1
  if x == 2:
    break
  if {x} <= dic:
    cnt = -1
    break
  dic.add(x)
print(cnt)
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]
i = 0
cnt = 1
cmpr = [0]
while A[i] != 2:
    if A[i] == i + 1:
        print((-1))
        exit()
    i = A[i] - 1
    if i in cmpr:
        print((-1))
        exit()
    cmpr.append(i)
    cnt += 1
print(cnt)

# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]
i = 0
cnt = 1
cmpr = [0]
while A[i] != 2:
    if A[i] == i + 1:
        print((-1))
        exit()
    i = A[i] - 1
    cmpr.append(i)
    cnt += 1
    if cnt > N:
        print((-1))
        exit()
print(cnt)
# Original solution
n = int(eval(input()))
a_list = [int(eval(input())) for i in range(n)]

count = 1
now_button = 0
for i in range(1, len(a_list)+1):
    if a_list[now_button] == 2:
        print(count)
        exit()
    else:
        now_button = a_list[now_button]-1
        count += 1

print('-1')

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

count = 1
now_button = 0
for i in range(len(a_list)):
    if a_list[now_button] == 2:
        print(count)
        exit()
    else:
        now_button = a_list[now_button]-1
        count += 1

print('-1')
# Original solution
import sys
input = sys.stdin.readline
n=int(eval(input()))
A = [0]*n
B = [int(eval(input()))-1 for i in range(n)]
C = [0]

def fun(k):
    if B[k] in C:
        print((-1))
        sys.exit()                      
    else:
        C.append(B[k])
        if B[k] == 1:
            return False                                                    
        else:
            return B[k]
ans = 0
cnt = 0
for i in range(n):
    cnt = fun(cnt)
    ans +=1
    if cnt ==False:
        print(ans)
        sys.exit() 


        

# Optimized solution
import sys
input = sys.stdin.readline
n=int(eval(input()))

B = [int(eval(input()))-1 for i in range(n)]


def fun(k):
    if B[k] == 1:
     	return -5                                                    
    else:
        return B[k]
ans = 0
cnt = 0
for i in range(n+1):
    cnt = fun(cnt)
    ans +=1
    
    if cnt ==-5:
        print(ans)
        sys.exit() 
print((-1))
sys.exit()
# Original solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
buttons = [int(eval(input()))-1 for n in range(n)]
light = 0
count = 0
while light != 1:
    if buttons[light] == -1:
        print((-1))
        break
    num = light
    light = buttons[num]
    buttons[num] = -1
    count += 1
else:
    print(count)

# Optimized solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
buttons = [int(eval(input())) for n in range(n)]
count,light = 1,buttons[0]
while light != 2 and count<n:
    count,light = count+1,buttons[light-1]
print((count if count<n else -1))
# Original solution
import sys
input = sys.stdin.readline

buttons = list(map(int,sys.stdin))
n = buttons[0]
count,light = 1,buttons[1]
while light!=2 and count<n:
    count,light = count+1,buttons[light]
print((count if count<n else -1))

# Optimized solution
import sys
input = sys.stdin.readline

buttons = list(map(int,sys.stdin))
light = buttons[1]
for i in range(buttons[0]):
    if light==2:
        print((i+1))
        break
    light= buttons[light]
else:
    print((-1))
# Original solution
n = int(eval(input()))
nlist = [int(eval(input())) for i in range(n)]
xlist = []
c = 0
x = 1
while x != 2 and x not in xlist and c < n:
    xlist.append(x)
    x = nlist[x - 1]
    c += 1
if x == 2:
    print(c)
else:
    print((-1))


# Optimized solution
n = int(eval(input()))
nlist = [int(eval(input())) for i in range(n)]
c = 0
x = 1
while x != 2 and c < n:
    x = nlist[x - 1]
    c += 1
if x == 2:
    print(c)
else:
    print((-1))
# Original solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(eval(input()))
    A = list(int(eval(input())) for _ in range(n))

    i = 1
    res = 0
    for _ in range(n):
        i = A[i - 1]
        res += 1
        if i == 2:
            break
    else:
        print((-1))
        exit()

    print(res)


if __name__ == '__main__':
    resolve()


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(eval(input()))
    A = list(int(eval(input())) for _ in range(n))

    j = 1
    cnt = 0
    for i in range(n):
        if j == 2:
            break
        j = A[j - 1]
        cnt += 1
    else:
        print((-1))
        exit()

    print(cnt)


if __name__ == '__main__':
    resolve()
# Original solution
N = int(eval(input()))
B = [int(eval(input())) for i in range(N)]
A = [0]
A.extend(B)
past = [1]
next_l = 1
if not 2 in A:
  print((-1))
  exit(0)
for i in range(N):
  next_l = A[next_l]
  if next_l in past or next_l > N:
    print((-1))
    exit(0)
  if next_l == 2:
    print((len(past)))
    exit(0)
  past.append(next_l)
print((-1))


# Optimized solution
N = int(eval(input()))
B = [int(eval(input())) for i in range(N)]
A = [0]
A.extend(B)
past = [1]
next_l = 1
#if not 2 in A:
#  print(-1)
#  exit(0)
for i in range(N):
  next_l = A[next_l]
  #if next_l in past or next_l > N:
  #  print(-1)
  #  exit(0)
  if next_l == 2:
    print((len(past)))
    exit(0)
  past.append(next_l)
print((-1))
# Original solution
# ABC 065 B

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

i = 1
q = []
while i != 2:
    q.append(i)
    i = A[i-1]
    if i in q:
        print((-1))
        break
else:
    print((len(q)))

# Optimized solution
# ABC 065 B

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

i = 1
s = 0
while i != 2:
    s += 1
    A[i-1], i = 1, A[i-1]
    if i == 1:
        print((-1))
        break
else:
    print(s)
# Original solution
N = int(eval(input()))

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

b = a[0]
li = [0]
c = 1

while b != 2:
    li += [b-1]
    b = a[b-1]
    if b-1 in li:
        print((-1))
        break
    else:
        c += 1
else:
    print(c)


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

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

b = a[0]
li = [0]
c = 1

while b != 2:
    li += [b-1]
    b = a[b-1]
    c += 1
    if c > N:
        print((-1))
        break
else:
    print(c)
# Original solution
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    a_arr = [None]
    for i in range(N):
        _ = int(_in[i+1])  # type:int
        a_arr.append(_)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    memo = []
    now = 1
    cnt = 0
    while True:
        cnt += 1
        _ = a_arr[now]
        if _ in memo or cnt>N:
            cnt = -1
            break
        elif _ == 2:
            break
        else:
            memo.append(_)
            now = _
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)


# Optimized solution
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    a_arr = [None]
    for i in range(N):
        _ = int(_in[i+1])  # type:int
        a_arr.append(_)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    memo = []
    now = 1
    for i in range(N):
        _ = a_arr[now]
        if _ == 2:
            cnt = i+1
            break
        else:
            now = _
    else:
        cnt = -1
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)