CodeEff/ECCO huggingface dataset:
# Original solution
n=int(eval(input()))
a=[]
ans=1
for i in range(n):
    b=int(eval(input()))
    a.append(b)
c=a[0]
if(c==2):
    print(ans)
else:
    while(c!=2):
        ans+=1
        c=a[c-1]
        if(ans>len(a)):
            print((-1))
            exit()
    print(ans)

# Optimized solution
a=[int(eval(input())) for _ in range(int(eval(input())))]
ans=1
c=a[0]
if(c==2):
    print(ans)
else:
    while(c!=2):
        ans+=1
        c=a[c-1]
        if(ans>len(a)):
            print((-1))
            exit()
    print(ans)
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]

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

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

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


# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
L = [0] * n
cnt = 0
i = 0
while True:
    if L[i] == 1:
        print((-1))
        break
    else:
        L[i] = 1
        i = a[i] - 1
        cnt += 1
        if i == 1:
            print(cnt)
            break
# Original solution
n = int(eval(input()))
arr = []
for i in range(n):
    arr.append(int(eval(input())))
visited = []
i = 0
c = 0
while i != 1 and i not in visited:
    visited.append(i)
    i = arr[i]-1
    c += 1
if i == 1:
    print(c)
else:
    print((-1))

# Optimized solution
n = int(eval(input()))
arr = []
for i in range(n):
    arr.append(int(eval(input()))-1)
now = 0
c = 0
while True:
    if now == 1:
        print(c)
        break
    if c >= n:
        print((-1))
        break
    c += 1
    now = arr[now]
# Original solution
n=int(eval(input()))
a=[int(eval(input()))for i in range(n)]
vis=[0]*n
i=0
cnt=0
while True:
    next=a[i]
    cnt+=1
    if vis[next-1]==1:
        print((-1))
        exit()
    vis[next-1]=1
    if next==2:
        break
    i=next-1
print(cnt)

# Optimized solution
n=int(eval(input()))
a=[int(eval(input()))for i in range(n)]
vis=[0]*n
i=0
cnt=0
while True:
    next=a[i]
    cnt+=1
    if vis[next-1]==1:#一度来たことがある点であればbreak
        print((-1))
        exit()
    vis[next-1]=1#一度光った点は記録
    if next==2:
        break
    i=next-1
print(cnt)
# Original solution
n=int(eval(input()))
a=[int(eval(input()))for i in range(n)]
vis=[0]*n
i=0
cnt=0
while True:
    next=a[i]
    cnt+=1
    if vis[next-1]==1:#一度来たことがある点であればbreak
        print((-1))
        exit()
    vis[next-1]=1#一度光った点は記録
    if next==2:
        break
    i=next-1
print(cnt)


# Optimized solution
n=int(eval(input()))
a=[int(eval(input()))for i in range(n)]
a.insert(0,-1)
cnt=0
pos=1
for _ in range(n):
    now=a[pos]
    cnt+=1
    if now==2:
        print(cnt)
        exit()
    pos=now
print((-1))
# Original solution
n = int(eval(input()))
ls = []
for i in range(n):
  ls.append(int(eval(input())) - 1)
count = 0
if (1 not in ls):
  count = -1
else:
  used = []
  x = 0
  count = 0
  while (x != 1):
    used.append(x)
    x = ls[x]
    count += 1
    if (x in used):
      count = -1
      break
print(count)


# Optimized solution
n = int(eval(input()))
ls = []
for i in range(n):
  ls.append([int(eval(input()))-1,False])
count = 0
x = 0
while (x != 1):
  if ls[x][1]:
    count = -1
    break
  else:
    count += 1
    ls[x][1] = True
    x = ls[x][0]
print(count)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
i = a[0]
cnt = 1
while cnt<=n and i!=2:
    i = a[i-1]
    cnt += 1
if cnt==n+1:print((-1))
else:print(cnt)


# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
used = [False]*n
f = True
cur = 0
cnt = 0
while f:
    cnt += 1
    cur = a[cur]-1
    if cur == 1:
        break
    if used[cur]:
        f = False
        cnt = -1
    else:
        used[cur] = True
print(cnt)
# Original solution
n = int(eval(input()))
alist = [int(eval(input()))-1 for _ in range(n)]

visitedlist = [0]
from_ = 0
for _ in range(n):
    to = alist[from_]
    if to in visitedlist:
        print((-1))
        break
    elif to == 1:
        print((len(visitedlist)))
        break
    else:
        visitedlist.append(to)
        from_ = to

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

now = 0
count = 0
while True:
    if now == 1:
        print(count)
        break
    elif count >= n:
        print((-1))
        break
    else:
        count += 1
        now = alist[now]
# Original solution
def process(a):
    tmp = a[0]
    pushed = []
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.append(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1

if __name__ == '__main__':
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print((process(a)))

# Optimized solution
def process(a):
    tmp = a[0]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1

if __name__ == '__main__':
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print((process(a)))
# Original solution
def process(a):
    tmp = a[0]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1

if __name__ == '__main__':
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print((process(a)))

# Optimized solution
import sys

def process(a):
    tmp = a[1]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp]
        cnt += 1
    return -1

if __name__ == '__main__':
    # a = [int(input().strip()) for _ in range(int(input().strip()))]
    a = list(map(int, sys.stdin))
    print((process(a)))
# Original solution
n = int(eval(input()))
a = [0] + [int(eval(input())) for i in range(n)]

match = []
counter = 0
now = 1
for i in range(n):
    if now == 2:
        print(counter)
        exit()

    elif now not in match:
        match += [now]
        now = a[now]
        counter += 1

    else:
        print("-1")
        exit()

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

match = []
counter = 0
now = 1
for i in range(n):
    if now == 2:
        print(counter)
        exit()

    else:
        now = a[now]
        counter += 1

print("-1")
# Original solution
n=int(eval(input()))
data=[int(eval(input())) for i in range(n)]
x=1
was=set()
now=1
while x==1:
    if now==2:
        print((len(was)))
        break
    if now in was:
        print('-1')
        break
    was|={now,}
    now=data[now-1]
    

# Optimized solution
N=int(eval(input()))
A=[int(eval(input())) for i in range(N)]
cnt=0
now=1
for i in range(N+1):
    if now==2:
        break
    cnt+=1
    now=A[now-1]
if cnt>N:print((-1))
else:print(cnt)
# Original solution
N = int(eval(input()))
A = [0]
for i in range(N):
    A.append(int(eval(input())))
old = []
bo = 1
ind = 1
while True:
    if A[bo] == 2:
        print(ind)
        exit()
    else:
        ind += 1
        if bo in old:
            print((-1))
            exit()
        old.append(bo)
        bo = A[bo]


# Optimized solution
N = int(eval(input()))
A = [0]
for i in range(N):
    A.append(int(eval(input())))
old = []
bo = 1
ind = 1
while ind <= N:
    if A[bo] == 2:
        print(ind)
        exit()
    else:
        ind += 1
        bo = A[bo]
print((-1))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
v = [False for i in range(n)]
v[0] = True
cd = 0
count = 0
while(True) :
    cd = a[cd] - 1
    count += 1
    if cd == 1 :
        print(count)
        break
    if v[cd] :
        print((-1))
        break

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
cd = 0
count = 0
while(True) :
    cd = a[cd] - 1
    count += 1
    if cd == 1 :
        print(count)
        break
    if count >= n :
        print((-1))
        break
# Original solution
# -*- coding: utf-8 -*-


n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())))
if 2 not in a:
    print((-1))
else:
    cnt = 0
    before_num = 1
    lighted_bottons = []
    while True:
        cnt += 1
        if a[before_num - 1] == 2:
            print(cnt)
            break
        else:
            if a[before_num - 1] in lighted_bottons:
                print((-1))
                break
            before_num = a[before_num - 1]
            lighted_bottons.append(before_num)

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


n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())))
if 2 not in a:
    print((-1))
else:
    cnt = 0
    before_num = 1
    lighted_bottons = [0 for i in range(n + 1)]
    while True:
        cnt += 1
        if a[before_num - 1] == 2:
            print(cnt)
            break
        else:
            if lighted_bottons[a[before_num - 1]] == 1:
                print((-1))
                break
            before_num = a[before_num - 1]
            lighted_bottons[before_num] = 1
# Original solution
N = int(eval(input()))
light_rule = {n:int(eval(input()))-1 for n in range(N)}

light = "1"+"0"*(N-1)
target = "0"+"1"+"0"*(N-2)
searched = {light:1}

flag = 0
num = 0
bottun = 0
while True:
    if light == target:
        flag = 1
        break
    num += 1
    idx = bottun
    light = light[:idx]+"0"+light[idx+1:]
    idx = light_rule[bottun]
    light = light[:idx]+"1"+light[idx+1:]
    bottun = light_rule[bottun]
    if searched.get(light):
        break
    else:
        searched[light] = 1
    
if flag:
    print(num)
else:
    print((-1))

# Optimized solution
N = int(eval(input()))
light_rule = {n:int(eval(input()))-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)
target = ["0"]+["1"]+["0"]*(N-2)
searched = {"".join(light):1}

flag = 0
num = 0
bottun = 0
while True:
    if light == target:
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    if searched.get("".join(light)):
        break
    else:
        searched["".join(light)] = 1
    
if flag:
    print(num)
else:
    print((-1))
# Original solution
N = int(eval(input()))
light_rule = {n:int(eval(input()))-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)
target = ["0"]+["1"]+["0"]*(N-2)
searched = {"".join(light):1}

flag = 0
num = 0
bottun = 0
for _ in range(N):
    if light == target:
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    if searched.get("".join(light)):
        break
    else:
        searched["".join(light)] = 1
    
if flag:
    print(num)
else:
    print((-1))

# Optimized solution
N = int(eval(input()))
light_rule = {n:int(eval(input()))-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)

flag = 0
num = 0
bottun = 0
for _ in range(N):
    if light[1] == "1":
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    
if flag:
    print(num)
else:
    print((-1))
# Original solution
# ABC 65, B - trained
n = int(eval(input()))
A = [int(eval(input())) for i in range(n)]
went = []
now = 1
for i in range(n):
    if now in went:
        print((-1))
        break
    if now == 2:
        print(i)
        break
    went.append(now)
    now = A[now-1]


# Optimized solution
# ABC 65, B - trained
n = int(eval(input()))
A = [int(eval(input())) for i in range(n)]
now = 1
went = set()
for i in range(n):
    if now == 2:
        print(i)
        break
    if now in went:
        print((-1))
        break
    went.add(now)
    now = A[now-1]
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]

x = 1
y = []

while x not in y and x != 2:
    y += [x]
    x = a[x-1]

if x == 2:
    print((len(y)))
else:
    print((-1))


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

x = 1
y = [0] * n

while y[x-1] != 2 and x != 2:
    y[x-1] += 1
    x = a[x-1]

if x == 2:
    print((y.count(1)))
else:
    print((-1))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]

x = 1
y = [0] * n

while y[x-1] != 2 and x != 2:
    y[x-1] += 1
    x = a[x-1]

if x == 2:
    print((y.count(1)))
else:
    print((-1))


# Optimized solution
n = int(eval(input()))
a = [int(eval(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))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
if 2 not in a:
    print((-1))
    quit()
else:
    t = a.index(2)
    s = 1
    count = 0
    while count <= 10**6:
        s = a[s-1]
        count += 1
        if s == 2:
            print(count)
            quit()
print((-1))

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
if 2 not in a:
    print((-1))
    quit()
else:
    s = 1
    count = 0
    while count <= n:
        s = a[s-1]
        count += 1
        if s == 2:
            print(count)
            quit()
print((-1))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
s = [1,a[0]] #shone buttons
while True: #unitl 2 appears in s or a loop generates 
    if s[-1] == 2: #if 2 appears
        break
    if len(set(s)) != len(s): #if a loop generates: mathematical induction
        break 
    s += [a[s[-1]-1]]
# print(s)
if s[-1] == 2:
    print((len(s)-1))
else:
    print((-1))



# Optimized solution
n = int(eval(input()))
a = [int(eval(input()))-1 for i in range(n)]
#ボタンを押す=listから値を取得して-1にする
#取得した値=光っているボタンの番号
#光っているボタンの番号が1ならbreak
#取得した値が-1なら2を通らずループしてしまっている
sb = 0
ans = 0
while True:
    if sb == 1:
        flag = True
        break
    elif sb == -1:
        flag = False
        break
    tmp = a[sb]
    a[sb] = -1
    sb = tmp
    ans += 1
if flag:
    print(ans)
else:
    print((-1))
# Original solution
n=int(eval(input()))
a=[]
for i in range(n):
    b=int(eval(input()))
    a.append(b)

count=1
c=a[0]

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

print(count)    

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

count=1

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

print((-1))
# Original solution
N=int(eval(input()))

alist=[0]
for i in range(N):
  alist.append(int(eval(input())))
#print(alist)

answer=0
pos=1
set_pos=set()
while True:
  if pos==2:
    print(answer)
    break
  else:
    pos=alist[pos]
    answer+=1

  if pos in set_pos:
    print((-1))
    break
  else:
    set_pos.add(pos)
    

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

alist=[0]
for i in range(N):
  alist.append(int(eval(input())))
#print(alist)

answer=0
pos=1
while True:
  if answer>N:
    print((-1))
    break
  elif pos==2:
    print(answer)
    break
  else:
    pos=alist[pos]
    answer+=1
# Original solution
import sys

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

count = 0
saw = []
now = 1

while now not in saw:
    count += 1
    saw.append(now)
    now = a[now - 1]
    if now == 2:
        print(count)
        sys.exit()

print((-1))


# Optimized solution
import sys

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

count = 0
now = 1

while count < N:
    count += 1
    now = a[now - 1]
    if now == 2:
        print(count)
        sys.exit()

print((-1))
# Original solution
import sys
from collections import deque

n = int(eval(input()))
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

pushed = deque([1])
button = 1

for _ in range(n):
    if a[button] in pushed:
        print((-1))
        break
    pushed.append(a[button])
    button = a[button]
    if button == 2:
        print((len(pushed) - 1))
        break

# Optimized solution
import sys

n = int(eval(input()))
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

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

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

n = int(eval(input()))
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

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

if count == n:
    print((-1))

# Optimized solution
import sys

n = int(eval(input()))
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

for _ in range(n):
    if button == 2:
        print(count)
        break
    button = a[button]
    count += 1
else:
    print((-1))
# Original solution
import sys

n = int(eval(input()))
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1

for i in range(n):
    if button == 2:
        print(i)
        break
    button = a[button]
else:
    print((-1))

# Optimized solution
import sys

n = list(map(int, sys.stdin))

button = 1

for i in range(n[0]):
    if button == 2:
        print(i)
        break
    button = n[button]
else:
    print((-1))
# Original solution
N=int(eval(input()))
a=[]
for i in range(N):
  a.append(int(eval(input())))
flag=True
step=[]
pivot=0
for i in range(N):
  step.append(pivot)
  if a[pivot]==2:
    break
  pivot=a[pivot]-1
  if pivot in step:
    flag=False
    break
if flag:
  print((len(step)))
else:
  print((-1))

# Optimized solution
N=int(eval(input()))
a=[]
for i in range(N):
  a.append(int(eval(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)))
# Original solution
n=int(eval(input()))
num=[0]
ans=0
count=0
k=1
for i in range(n):
    a=int(eval(input()))
    num.append(a)

for i in range(n+1):
    k=num[k]
    count+=1
    if k==2:
        ans=1
        break
if ans==1:
    print(count)
else:
    print("-1")

# Optimized solution
#1
n = int(eval(input()))
a = [0]

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

cnt = 0
b = 1
ans = 0

for j in range(n+1):
    cnt += 1
    b = a[b]
    
    if b == 2:
        ans = 1
        break
if ans == 1:
    print(cnt)

else:
    print("-1")
# Original solution
N = int(eval(input()))
al = [[] for _ in range(N+1)]
for i in range(1,N+1):
    al[i] = int(eval(input()))
#print(al)

#初期値
n = 1
cnt = 1
visited = [0 for _ in range(N+1)]
visited[1] = 1
while al[n] != 2:
    cnt += 1
    n = al[n]
    if visited[n] == 1:
        print((-1))
        exit()
print(cnt)




# Optimized solution
N = int(eval(input()))
al = [[] for _ in range(N+1)]
for i in range(1,N+1):
    al[i] = int(eval(input()))
#print(al)

#初期値
n = 1
cnt = 1
visited = [0 for _ in range(N+1)]
visited[1] = 1
while al[n] != 2:
    cnt += 1
    n = al[n]
    if visited[n] == 1:
        print((-1))
        exit()
    else:
        visited[n] = 1
print(cnt)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
ans = False
used = set()
i = 1
count = 0
while ans == False:
    count += 1
    if a[i-1] == 2:
        ans = True
    elif i-1 in used:
        break
    else:
        used.add(i-1)
        i = a[i-1]
if ans == True:
    print(count)
else:
    print("-1")
        

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
count = 0
i = 0
ans = False
while count < n:
    count += 1
    if a[i] == 2:
        ans = True
        break
    else:
        i = a[i] - 1
if ans == True:
    print(count)
else:
    print("-1")
# Original solution
n = int(eval(input()))
rec = {}
for i in range(n):
    a = int(eval(input()))
    rec[i + 1] = a

i = 1
rec2  = [1]
num = 0
while i != 2 and num < n:
    i = rec[i]
    if i not in rec2:
        rec2.append(i)
        num += 1
    else:
        num = -1
        break

if num == n:
    print((-1))
else:
    print(num)
    

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

i = 1
rec2 = [1]
num = 0
while i != 2 and num < n:
    i = rec[i]
    num += 1

if num == n:
    print((-1))
else:
    print(num)
# Original solution
n = int(eval(input()))
rec = {}
for i in range(n):
    a = int(eval(input()))
    rec[i + 1] = a

i = 1
rec2 = [1]
num = 0
while i != 2 and num < n:
    i = rec[i]
    num += 1

if num == n:
    print((-1))
else:
    print(num)

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

i = 0
k = 0
s = set()
flag = True
while i != 1:
    if i in s:
        flag = False
        break
    s.add(i)
    i = A[i]
    k += 1

if flag:
    print(k)
else:
    print((-1))
# Original solution
n=int(eval(input()))
a=[int(eval(input())) for _ in range(n)]
b=[1]
buf=1
if 2 not in a:
    print((-1))
else:
    while True:
        a_buf=a[buf-1]
        if a_buf==2:
            print((len(b)))
            break
        elif a_buf in b:
            print((-1))
            break
        b.append(a_buf)
        buf=a_buf

# Optimized solution
n=int(eval(input()))
a=[int(eval(input())) for _ in range(n)]
b=[1]
buf=1
count=0
if 2 not in a:
    print((-1))
else:
    while count<n:
        a_buf=a[buf-1]
        #print("buf,a[buf-1],b=",buf,a[buf-1],b)
        if a_buf==2:
            #print("check_2")
            print((len(b)))
            break
        #elif a_buf in b:
            #print("check_loop")
            #print(-1)
            #break
        b.append(a_buf)
        buf=a_buf
        count+=1
    if count==n:
        print((-1))
# Original solution
n = int(input())
edges = [[0 for _ in range(n)] for _ in range(n)]
mem = [[0 for _ in range(n)] for _ in range(n)]
dist = []

for i in range(n):
    edges[i][int(input())-1] = 1

def has_edge(edges, v, t, d):
    targets = [i for i, e in enumerate(edges[v]) if e != 0]
    for target in targets:
        if mem[v][target] == 0:
            mem[v][target] = 1
            if target == t:
                dist.append(d)
                return True
            else:
                return has_edge(edges, target, t, d+1)
    return False


print(min(dist)+1) if has_edge(edges, 0, 1, 0) else print(-1)


# Optimized solution
n = int(eval(input()))
edges = {i: int(eval(input()))-1 for i in range(n)}


def push_buttom(v, goal):
    for i in range(n):
        v = edges[v]
        if v == goal:
            return i+1
    return -1


print((push_buttom(0, 1)))
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
b = [-1] * N
botan = a[0]

cnt = 0

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

print((-1))


# Optimized solution
from sys import stdin
input = stdin.readline
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
b = [-1] * N
botan = a[0]

cnt = 0

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

print((-1))