CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))

btn_list = {i:int(eval(input())) for i in range(1,n+1)}

count = 0
btn = 1

while True:
    if btn == 2:
        print(count)
        break
    elif btn == 1 and count != 0:
        print((-1))
        break
    else:
        count += 1
        btn = btn_list[btn]

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

btn_list = {i:int(eval(input())) for i in range(1,n+1)}

count = 1
right = btn_list[1]

while True:
    if right == 2:
        print(count)
        break
    elif count == n:
        print((-1))
        break
    else:
        count += 1
        right = btn_list[right]
# Original solution
n = int(eval(input()))
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 0
for i in range(len(L)):
  if L[next] == 2:
    print((i+1))
    quit()
  next = L[next]-1
print((-1))

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print((-1))
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1
# Original solution
n = int(eval(input()))
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
while True:
  if next == 2:
    print(count)
    exit()
  elif count > n:
    print((-1))
    exit()
  else:
    next = L[next-1]
    count += 1

# Optimized solution
n = int(eval(input()))
L = [int(eval(input())) for _ in range(n)]
next = 1
count = 0
while True:
  if next == 2:
    print(count)
    exit()
  elif count > n:
    print((-1))
    exit()
  else:
    next = L[next-1]
    count += 1
# Original solution
n = int(eval(input()))
L = [int(eval(input())) for _ in range(n)]
count = 0
next = 0
for i in range(len(L)):
  if L[next] == 2:
    print((count+1))
    quit()
  next = L[next]-1
  count += 1
print((-1))

# Optimized solution
n = int(eval(input()))
L = [int(eval(input())) for _ in range(n)]
next = 1
for i in range(n):
  if next == 2:
    print(i)
    quit()
  next = L[next-1]
print((-1))
# Original solution
n=int(eval(input()))
a=[]
for _ in range(n):
    a.append(int(eval(input())))

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

# Optimized solution
n=int(eval(input()))
a=[int(eval(input()))for _ in range(n)]
b=0
for i in range(1,n+1):
    b=a[b]-1
    if b==1:
        print(i)
        exit()
else:
    print((-1))
# Original solution
n = int(eval(input()))
l = [0 for _ in range(n)]
already = []
next = 1
c = 0
for i in range(n):
  a = int(eval(input()))
  l[i] = a
while c <= n:
  next = l[next-1]
  c += 1
  if next == 2:
    print(c)
    break
  elif next in already:
    print((-1))
    break
  already.append(next)

# Optimized solution
n = int(eval(input()))
l = [0 for _ in range(n)]
already = set()
next = 1
c = 0
for i in range(n):
  a = int(eval(input()))
  l[i] = a
while c <= n:
  next = l[next-1]
  c += 1
  if next == 2:
    print(c)
    break
  elif next in already:
    print((-1))
    break
  already.add(next)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
A = [0]*n
i = 0
ok = False
cnt = 0
for _ in range(n):
  cnt += 1
  A[i] = 1
  if a[i] == 2: 
    ok = True
    break
  i = a[i]-1
  if A[i] == 1: break
print((cnt if ok else -1))

# Optimized solution
n = int(eval(input()))
a = [0]+[int(eval(input())) for _ in range(n)]
check = [0] * (n+1)
now = 1
cnt = 0
while now != 2:
    if check[now] == 1: break
    cnt += 1
    check[now] = 1
    now = a[now]
print((cnt if now == 2 else -1))
# Original solution
n=int(eval(input()))
s=list(int(eval(input())) for _ in range(n))

push=[0 for _ in range(n)]
index=0
cnt=1
flag=True

while flag:
    push[index]+=1
    if s[index]==2:
        print(cnt)
        flag=False
    else:
        if push.count(2)!=0:
            print((-1))
            flag=False
        else: 
            index=s[index]-1
            cnt+=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
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
count = [0]*N
i = 0

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

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

while(True):
    count[i]+=1
    i = a[i]-1
    if i == 1:
        print((sum(count)))
        break
    if count[i] == 1:
        print((-1))
        break
# Original solution
#2019/10/16
N, *a = list(map(int, open(0).read().split()))
#print(N, a)
i=0
cnt=1
button=[]
while(True):
    #print("i=",i, "a[i]=",a[i])
    #print("button=", button)
    i=a[i]-1
    if i==1:
        print(cnt)
        break
    elif i in button:
        print("-1")
        break
    else:
        button.append(i)
        cnt+=1

# Optimized solution
#2019/10/16
N, *a = list(map(int, open(0).read().split()))
#print(N, a)
i=0
cnt=0
button=[]
while(True):
    #print("i=",i, "a[i]=",a[i])
    #print("button=", button)
    if i==1:
        print(cnt)
        break
    if cnt >= N:
        print("-1")
        break
    cnt+=1
    i=a[i]-1
# Original solution
import sys
n = int(eval(input()))
ls = [int(eval(input())) for _ in range(n)]
if 2 not in ls:
    print((-1))
    sys.exit()
i = 1
k = 0
di = []
for j in range(n):
    i = ls[i-1]
    if i in di:
        print((-1))
        break
    di.append(i)
    k += 1
    if i == 2:
        print(k)
        break

# Optimized solution
import sys
n = int(eval(input()))
ls = [int(eval(input())) for _ in range(n)]
if 2 not in ls:
    print((-1))
    sys.exit()
i = 1
k = 0
for j in range(n):
    i = ls[i-1]
    k += 1
    if i == 2:
        print(k)
        sys.exit()
print((-1))
# Original solution
n=int(eval(input()))
a=[int(eval(input())) for i in range(n)]
tmp=[]
i=1
cnt=0
while cnt<=n:
  i=a[i-1]
  cnt+=1
  if i==2:
    print(cnt)
    break
else:
  print((-1))

# Optimized solution
n=int(eval(input()))
a=[int(eval(input())) for i in range(n)]
i=1
cnt=0
while cnt<=n:
  i=a[i-1]
  cnt+=1
  if i==2:
    print(cnt)
    break
else:
  print((-1))
# Original solution
# -*- coding:utf-8 -*-
import sys
nyuryoku = [int(i) for i in sys.stdin]

N = nyuryoku[0]
a_list = nyuryoku[1:]

"""
a_list=[]

for _ in range(N):
    a_list.append(int(input()))
"""


cnt_push = 1
button_list = []
cur_but_posi = a_list[0]

while True:
    #cur_but_posi = a_list[cur_but_posi - 1]
    if cur_but_posi == 2:
        print(cnt_push)
        break
    elif cur_but_posi in button_list:
        print((-1))
        break
    else:
        button_list.append(cur_but_posi)
        cur_but_posi = a_list[cur_but_posi - 1]    
        cnt_push += 1    



# 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 = []
for _ in range(n):
    a.append(int(eval(input())))

a.insert(0,0)
ans = 0
visited = []
i = 1

while True:
    if a[i] == 2:
        ans += 1
        break
    elif not i in visited:
        ans += 1
        visited.append(i)
        i = a[i]
    else:
        ans = -1
        break
print(ans)

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

a.insert(0,0)
ans = 0
i = 1

for x in range(n+1):
    if a[i] == 2:
        ans += 1
        break
    elif x == n:
        ans = -1
        break
    else:
        ans += 1
        i = a[i]
print(ans)
# Original solution
# import sys
# input = sys.stdin.readline

N, *a = [int(x) for x in open(0)]
a.insert(0, None)

first = 1
pres = first
count = 0
nex =  a[pres] # press the button
count += 1
for i in range(N-1): # operations are at most N-1 times.
    if nex == 2: break
    prev = pres
    pres = nex
    nex = a[pres] # press the button
    count += 1
    if nex == prev or nex == pres: break

if nex == 2: print(count)
else: print((-1))

# Optimized solution
import sys

n, *b = list(map(int, sys.stdin.read().split()))

def main():
    on = 1
    lightened = set([1])
    for i in range(1, n):
        on = b[on-1]
        if on == 2:
            return i
        if on in lightened:
            return -1
        lightened.add(on) 

if __name__ == '__main__':
    ans = main()
    print(ans)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
now = 1
ans = 0
while True:
    now = a[now-1]
    ans += 1
    if (now==2):
        print(ans)
        break
    elif (now==1):
        print((-1))
        break


# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
now = 1
ans = 0
while True:
    now = a[now-1]
    ans += 1
    if (now==2):
        print(ans)
        break
    elif (now==1):
        print((-1))
        break
    elif (ans>len(a)):
        print((-1))
        break
# Original solution
N = int(eval(input()))

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

lst = [0] * N

flag = True
i = 0
count = 0
while flag:
    if lst[i] == 1:
        print((-1))
        exit()
    elif a[i] == 2:
        print((count + 1))
        exit()
    else:
        lst[i] = 1
        i = a[i] - 1
        count += 1


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

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

check = [True] * (N + 1)

count = 0
tmp = 1
while check[tmp]:
    count += 1
    check[tmp] = False
    if A[tmp - 1] == 2:
        print (count)
        exit()
    tmp = A[tmp - 1]

print((-1))
# Original solution
import sys
sys.setrecursionlimit(500000)
N=int(eval(input()))
A=[int(eval(input()))-1 for _ in range(N)]
result=[]
app=result.append
def next(k):
	newlight=A[k]
	if k==1:
		return(1)
	elif k in result:
		return(0)
	else:
		app(k)
		return(next(newlight))
if not next(0):
	print((-1))
else:print((len(result)))

# Optimized solution
import sys
sys.setrecursionlimit(500000)
N=int(eval(input()))
A=[int(eval(input()))-1 for _ in range(N)]
result=[False]*N
def next(k):
	newlight=A[k]
	if k==1:
		return(1)
	elif result[k]:
		return(0)
	else:
		result[k]=True
		return(next(newlight))
if not next(0):
	print((-1))
else:print((sum(result)))
# Original solution
n = int(eval(input()))
light = 1
count = 0
a = []
his = []
for i in range(n):
    a.append(int(eval(input())))
while light not in his:
    his.append(light)
    light = a[light-1]
    count += 1
    if light == 2:
        print(count)
        quit()
print((-1))


# Optimized solution
n = int(eval(input()))
light = 1
count = 0
a = []
for i in range(n):
    a.append(int(eval(input())))
while count < n:
    light = a[light-1]
    count += 1
    if light == 2:
        print(count)
        quit()
print((-1))
# Original solution
N = int(eval(input()))
a = [int(eval(input()))-1 for i in range(N)]

hist = []
next_i = 0
ans = 0
for i in range(N):
    ans += 1
    hist.append(next_i)
    next_i = a[next_i]
    if next_i == 1:
        break
    elif next_i in hist:
        ans = -1
        break

print(ans)

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

hist = set()
next_i = 0
ans = 0
for i in range(N):
    ans += 1
    hist.add(next_i)
    next_i = a[next_i]
    if next_i == 1:
        break
    elif next_i in hist:
        ans = -1
        break

print(ans)
# Original solution
n = int(eval(input()))

buttons = [0] * 10**5

for i in range(n):
    buttons[i] = int(eval(input()))

next = 1
i = 0
while next != 2:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()
    if i >= 10**5:
        print((-1))
        break


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

buttons = []

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

next = 1
i = 0
while i <= 10**5:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()

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

buttons = [0] * 10**5

for i in range(n):
    buttons[i] = int(eval(input()))

next = 1
i = 0
while i <= 10**5:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()

print((-1))


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

next = 0
ans = 0
while ans <= n:
    ans += 1
    if A[next] == 2:
        print(ans)
        exit()
    next = A[next]-1

print((-1))
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
check = [True for _ in range(N)]

ai = 1
count = 1
while check[ai-1]:
  if A[ai-1] == 2:
    print(count)
    exit()
  check[ai-1] = False
  ai = A[ai-1]
  count += 1
print((-1))


# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
order = A[0]
for i in range(1,N):
  if order == 2:
    print(i)
    exit()
  order = A[order-1]
print((-1))
# Original solution
import sys

N = int(eval(input()))
button = [0]*N

for i in range(N):
    button[i] = int(eval(input()))-1
course = []
count, now = 0, 0

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


# Optimized solution
import sys

N = int(eval(input()))
button = [0]*N

for i in range(N):
    button[i] = int(eval(input()))-1
count, now = 0, 0

while count <= N:
    now = button[now]
    count += 1
    if now == 1:
        print(count)
        sys.exit()
print((-1))
# Original solution
from collections import defaultdict, deque


N = int(eval(input()))
d = defaultdict(int)
for i in range(N):
    d[i+1] = int(eval(input()))

#print(d)

q = []
q.append(1)

point = 1
count = 1

while True:
    if d[point] in q:
        print((-1))
        break
    if d[point] == 2:
        print(count)
        break
        
    q.append(d[point])
    point = d[point]
    count += 1

# Optimized solution
from collections import defaultdict, deque
import sys

N = int(eval(input()))
d = defaultdict(int)
for i in range(N):
    d[i+1] = int(eval(input()))

#print(d)

q = deque()
q.append(1)

point = 1
count = 1

while N > 0:
    if d[point] == 2:
        print(count)
        sys.exit()
        
    q.append(d[point])
    point = d[point]
    count += 1
    N -= 1

print((-1))
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]
cnt = 1

if 2 not in A:
  print((-1))
  exit()

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


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

if 2 not in a:
  print((-1))
  exit()

x = 1
for i in range(n-1):
 
  x = a[x-1]
  cnt += 1

            
  if x == 2:
    print(cnt)
    exit()

print((-1))
# Original solution
n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())))
log_l = [0]
i = 0
count = 0
flag = False
while i != 1: 
    i = a[i]-1
    count +=1
    if i in log_l: break
    else: log_l.append(i)
    if i == 1:
        flag = True
        break
if flag: print(count)
else: print((-1))

# Optimized solution
n = int(eval(input()))
a = []
for i in range(n):
    a.append(int(eval(input())))
i = 0
j = 0
count = -1
while i < n:
    j = a[j]-1
    i += 1
    if j == 1:
        count = i
        break
print(count)
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]

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

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

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


a = []

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

#loopの定義→同じインデックスを使用したらloop判定
#in の処理を爆速で実行するためにsetを使用する
#loop=[]
loop=set()

tmp=1
while True:  
  tmp=a[tmp-1]
  count+=1
  if tmp==2:
    print(count)
    exit()
  if tmp-1 in loop:
    print((-1))
    exit()
  #loop.append(tmp-1)
  loop.add(tmp-1)

# Optimized solution
N=int(eval(input()))
#複数行の入力を高速化
#a=[0]*N
#for i in range(N):
#  a[i]=int(input())
import sys
a=[int(i) for i in sys.stdin]

count=0

#loopの定義→同じインデックスを使用したらloop判定
#in の処理を爆速で実行するためにsetを使用する
#loop=[]
loop=set()

tmp=1
while True:  
  tmp=a[tmp-1]
  count+=1
  if tmp==2:
    print(count)
    exit()
  if tmp-1 in loop:
    print((-1))
    exit()
  #loop.append(tmp-1)
  loop.add(tmp-1)
# Original solution
N = int(eval(input()))
a = [""]
for i in range(N):
    a.append(int(eval(input())))

button = 1
i = 0

while True:
    if button == 2:
        print(i)
        break
    if i > N:
        print((-1))
        break

    button = a[button]
    i += 1


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

ai = 1
for i in range(N):
    if ai == 2:
        print(i)
        exit()
    ai = a[ai]
else:
    print((-1))
# Original solution
from sys import stdin


def main() -> int:
    n = next_int()
    a = [next_int() for _ in range(n)]

    j = 1
    dic = dict()

    for i in range(n):
        dic[j] = True
        j = a[j - 1]
        if j in dic:
            break
        if j == 2:
            print((i + 1))
            return 0

    print((-1))
    return 0


def next_int() -> int:
    return int(next_str())


def next_str() -> str:
    result = ""
    while True:
        tmp = stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()

# Optimized solution
from sys import stdin


def main() -> int:
    n = next_int()
    a = [next_int() for _ in range(n)]

    j = 1

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

    print((-1))
    return 0


def next_int() -> int:
    return int(next_str())


def next_str() -> str:
    result = ""
    while True:
        tmp = stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()
# Original solution
N = int(eval(input()))
a = {}
for i in range(N):
    a[i + 1] = int(eval(input()))

i = 1
before_i = None
cnt = 0
while True:
    next_i = a[i]
    cnt += 1
    if next_i in [1, i, before_i]:
        print((-1))
        break
    elif next_i == 2:
        print(cnt)
        break

    before_i = i
    i = next_i

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

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

    a[i] = 0
    i = next_i
# Original solution
n = int(eval(input()))
a_list = [0]
for _ in range(n):
    a_list.append(int(eval(input())))
counter = 0
current_number = 1
already_pushed_list = []

while(True):
    counter += 1
    already_pushed_list.append(current_number)
    current_number = a_list[current_number]
    if current_number in already_pushed_list:
        print((-1))
        break
    if current_number == 2:
        print(counter)
        break


# Optimized solution
n = int(eval(input()))
a_list = [0]
for _ in range(n):
    a_list.append(int(eval(input())))
counter = 0
current_number = 1
already_pushed_list = set()

while(True):
    counter += 1
    already_pushed_list.add(current_number)
    current_number = a_list[current_number]
    if current_number in already_pushed_list:
        print((-1))
        break
    if current_number == 2:
        print(counter)
        break
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

now = 0
count = 0
while True:
    if now == 0 and count != 0:
        print((-1))
        break

    if now == 1:
        print(count)
        break

    now = A[now] - 1
    count += 1


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

now = 0
count = 0
while True:
    if count >= N:
        print((-1))
        break

    if now == 1:
        print(count)
        break

    now = A[now] - 1
    count += 1
# Original solution
N = int(eval(input()))
a = [int(eval(input()))-1 for i in range(N)]
ans = 0
index = 0
b = [False]*N
b[0] = True
while index!=1:
    index = a[index]
    if b[index]:
        ans = -1
        break
    ans += 1
print(ans)

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

# if not 1 in a:
#     print(-1)

# hst = []
btn = 0
cnt = 0

while cnt <= 10**7:
    # hst.append(btn)
    btn = a[btn]
    cnt += 1
    if btn == 1:
        print(cnt)
        exit()

print((-1))

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

hst = [0] * n
btn = 0
cnt = 0

while hst[btn] != 1:
    hst[btn] = 1
    btn = a[btn]

    cnt += 1
    if btn == 1:
        print(cnt)
        exit()

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

while o != 2:
    if o in t:
        print((-1))
        exit(0)
    t.append(o)
    o = a[o-1]

print((len(t)))

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

while o != 2:
    if len(t) > n:
        print((-1))
        exit(0)
    t.append(o)
    o = a[o-1]

print((len(t)))
# Original solution
N = int(eval(input()))
l = []
istwo = False
for i in range(N):
    a = int(eval(input()))
    if a == 2 and i != 1: istwo = True
    l.append(a-1)
flag = [0]*N
flag[0] = 1
flag[1] = 2
cnt = 0
cur_b = 0
while istwo:
    cnt += 1
    next_b = l[cur_b]
    if flag[next_b] == 1:
        print((-1))
        break
    elif flag[next_b] == 2:
        print(cnt)
        break
    else:
        flag[next_b] == 1
        cur_b = next_b

if istwo == False:
    print((-1))

# Optimized solution
N = int(eval(input()))
l = [(int(eval(input()))-1) for _ in range(N)]
flag = [0]*N
flag[0] = 1
flag[1] = 2
cnt = 0
cur_b = 0
for i in range(N):
    cnt += 1
    next_b = l[cur_b]
    if flag[next_b] == 1:
        print((-1))
        break
    elif flag[next_b] == 2:
        print(cnt)
        break
    else:
        flag[next_b] == 1
        cur_b = next_b
else:print((-1))
# Original solution
N = int(eval(input()))

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

tmp = 0
count = 1
tmp = a[0]

while tmp != 2 and tmp != 1:
    count += 1
    tmp = a[tmp-1]

if tmp == 2:
    print(count)
else:
    print((-1))

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

a = [int(eval(input()))-1 for i in range(N)]
owari = [0]*N

tmp = 0
count = 0

while owari[tmp] == 0 and tmp != 1:
    count += 1
    owari[tmp]=1
    tmp = a[tmp]

if tmp == 1:
    print(count)
else:
    print((-1))
# Original solution
n = int(eval(input()))
botan = [int(eval(input())) for i in range(n)]
light = 1 # 1番目のボタンが初め光ってる
count = 1
exi = [1]

if 2 in botan:

    while True:
        light = botan[light - 1]
        if light in exi:
            print((-1))
            exit()
        exi.append(light)
    
        if 2 in exi:
            print(count)
            exit()
        count += 1

else:
    print((-1))

# Optimized solution
n = int(eval(input()))
botan = [int(eval(input())) for i in range(n)]
light = 0 # 1番目のボタンが初め光ってる
count = 0

for i in range(n):
    light = botan[light] - 1
    if light == 1:
        break
    
if i == n - 1:
    print((-1))
else:
    print((i + 1))