Problem:
 Score : 200 points    Problem Statement Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.  The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N .
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i .
When Button i is not lighten up, nothing will happen by pressing it.  Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.  Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.      Constraints  2 ≤ N ≤ 10^5  1 ≤ a_i ≤ N         Input Input is given from Standard Input in the following format:  N  a_1  a_2 : a_N       Output Print -1 if it is impossible to lighten up Button 2 .
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2 .        Sample Input 1 3
3
1
2      Sample Output 1 2  Press Button 1 , then Button 3 .       Sample Input 2 4
3
4
1
2      Sample Output 2 -1  Pressing Button 1 lightens up Button 3 , and vice versa, so Button 2 will never be lighten up.       Sample Input 3 5
3
3
4
2
4      Sample Output 3 3

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
i = 1
cnt = 0
for j in range(n):
    if i == 2:
        cnt = j
        break
    i = a[i-1]
else:
    cnt = -1
print(cnt)

Solution (Python):
n = int(input())
a = [int(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

Solution (Python):
N = int(input())
A = [0]
S = set()
tmp = 1
count = 0
for i in range(N):
    A.append(int(input()))
while True:
    S.add(tmp)
    tmp = A[tmp]
    count += 1
    if tmp == 2:
        print(str(count))
        break
    if tmp in S:
        print('-1')
        break

Solution (Python):
*a,=map(int,open(0).read().split());i=1
for t in range(10**5):
	i=a[i]
	if i==2:
		print(t+1)
		exit()
print(-1)

Solution (Python):
nums = int(input())
ary = list(range(nums))
result = -1
for i in range(nums):
    ary[i] = int(input())

check = ary[0]
for i in range(nums):
    if check  == 2:
        result = i+1
        break
    check = ary[check-1]
print(result)

Solution (Python):
N = int(input())
As = []
for i in range(N):
  A = int(input())
  A = A-1
  As.append(A)
r = 0
c = 0
M = [-1]*N
M[c] = 0
while True:
  n = As[c]
  if not(M[n] < 0):
    break
  r += 1
  M[n] = r
  c = n

if M[1] < 0:
  print(-1)
else:
  print(M[1])

Solution (Python):
import sys


def main():
    n = int(input())
    a = [0] * n
    for i in range(n):
        a[i] = int(sys.stdin.readline()) - 1
    sw = [False for _ in range(n)]
    nextsw = 0
    while True:
        if nextsw == 1:
            print(sw.count(True))
            return
        if sw[nextsw]:
            print(-1)
            return
        sw[nextsw] = True
        nextsw = a[nextsw]


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
index = 1
count = 0
while index != 2 and count < n:
  index = a[index-1]
  count += 1
if count != n:
  print(count)
else:
  print(-1)

Solution (Python):
N=int(input())
A=[0]*N
for i in range(N):A[i]=int(input())
l=[0]*(N+1)

i=1
cnt=0
while True:
    if i==2:
        print(cnt)
        break
    
    if l[i]==1:
        print(-1)
        break
        
    l[i]=1
    i=A[i-1]
    cnt+=1

Solution (Python):
N = int(input())
a = list([int(input()) for _ in range(N)])

cur = 0
for n in range(N+1):
  if cur==1:
    print(n)
    break
  cur = a[cur]-1
else:
  print(-1)

Solution (Python):
n=int(input())
a=[]
for _ in range(n):
    a.append(int(input())-1)

def check():
    loop=0
    next=0
    while True:
        loop+=1
        next=a[next]
        if next==1:
            print(loop)
            return
        if loop>n:
            print(-1)
            return 

    
check()

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append(int(input())-1)
'''

i=0
j=0
b=[]
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b:
        print(-1)
        break
    else:
        b.append(a[i])
        i=a[i]
'''
i=0
j=0
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif j==n:
        print(-1)
        break
    else:
        i=a[i]

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

ans = 0
pos = 0
while True:
    ans += 1
    if visited[pos] == 1:
        print(-1)
        exit()
    visited[pos] = 1
    pos = a[pos] - 1
    if pos == 1:
        print(ans)
        exit()

Solution (Python):
n = int(input())
a = {}
for i in range(n):
    a[i] = int(input())
cnt = 1
ans = 0
while True:
    if a[cnt-1] == 2:
        ans += 1
        print(ans)
        break
    cnt = a[cnt-1]
    ans += 1
    if ans > n:
        print('-1')
        break

Solution (Python):
n,*a=map(int,open(0).read().split())

cnt=1
next=a[0]
while cnt<n:
  if next==2:
    break
  next=a[next-1]
  cnt+=1
print([-1,cnt][next==2])

Solution (Python):
import sys
n = int(sys.stdin.readline())
a = [int(sys.stdin.readline()) for i in range(n)]
a = [-1] + a

def cond(a):
    if 2 not in a:
        return -1

    visited = set()
    current = 1
    count = 0

    while True:
        if current == 2:
            return count
        if current in visited:
            return -1
        count += 1
        visited.update((current,))
        current = a[current]

print(cond(a))

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]

num = a[0]
count = 1

while num != 2 and count < n:
    count += 1
    num = a[num - 1]

if count < n:
    print(count)
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
s = a[0]
count = 1
while s != 2 and count < n:
  s = a[s-1]
  count += 1
print(count if count < n else -1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
s = a[0]
count = 1
while s!=2 and count<n:
  s = a[s-1]
  count += 1
print(count if count<n else -1)

Solution (Python):
n = int(input())
li = []
for i in range(n):
    li.append(int(input())-1)
cnt = 0


p = 0
l = li[0]
i = 0
while i < n:

    cnt += 1
    p = l
    if p == 1:
        print(cnt)
        break
    l = li[l]
    i += 1
if p != 1:
    print(-1)

Solution (Python):
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
checked = set()
now = 1
while(True):
    if a[now-1] == 2:
        print(len(checked)+1)
        break
    elif a[now-1] in checked:
        print(-1)
        break
    else:
        checked.add(a[now-1])
        now = a[now-1]

Solution (Python):
n = int(input())
a = [int(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)

Solution (Python):
n=int(input())
A=list(int(input()) for i in range(n))
a=0
for x in range(n):
  a=A[a]-1
  if a==1:
    print(x+1)
    break
else:
  print(-1)

Solution (Python):
n = int(input())
a = [int(input())-1 for i in range(n)]


light = 0
count = 0
while count < n:
  
  light = a[light]
  count +=1  
  if light == 1:
   
    ans = count
    break
else:
  ans = -1

print(ans)

Solution (Python):
n = int(input())
a = dict()
for i in range(n):
    a[i+1] = int(input())
now = 1

ans = 0
for i in range(n+1):
    if now == 2:
        break
    else:
        now = a[now]
        ans +=1
print(ans if ans != n+1 else -1)

Solution (Python):
n = int(input())
an = [int(input()) for i in range(n)]
c, s = 1, an[0]
while s != 2 and c < n:
    c, s = c + 1, an[s - 1]
print(c if c < n else -1)

Solution (Python):
n = int(input())
start = 0
end = 1

a = []
for _ in range(n):
    a.append(int(input()) - 1)

pushed = {}
l = 0
cnt = 0
while True:
    pushed[l] = True
    l = a[l]
    cnt += 1
    if l in pushed:
        print(-1)
        exit()
    if l == end:
        break
print(cnt)

Solution (Python):
n=int(input())
a=[int(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)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
c = 1
s = a[0]
while c<n and s!=2:
    s = a[s-1]
    c += 1
print(c if c<n else -1)

Solution (Python):
N = int(input())
list = [int(input()) for i in range(N)]
c = 0
x = 1
while x != 2 and c < N:
    x = list[x - 1]
    c += 1
if x == 2:
    print(c)
else:
    print(-1)

Solution (Python):
N=int(input())
a=[]
for i in range(N):
    a.append(int(input()))
switch=1
flag=0
count=0
for i in range(N):
    switch=a[switch-1]
    if switch==2:
        flag=1
        count=i+1
        break
if flag==1:
    print(count)
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
c, s = 1, a[0]
while s != 2 and c < n:
    c, s = c+1, a[s-1]

print(c if c < n else -1)

Solution (Python):
N = int(input())
b_list = []
for _ in range(N):
  b_list.append(int(input())-1)

i = 0
cnt=0
keiyu_list = [0 for _ in range(N)]

while keiyu_list[i]==0:
  keiyu_list[i]=1
  i = b_list[i]
  cnt+=1
  if i == 1:
    cnt+1
    break
if i==1:
  print(cnt)
else:
  print(-1)

Solution (Python):



N = int(input())
a = [int(input()) for _ in range(N)]
shining = a[0]
flag = False
for _ in range(1, N):
    if shining == 2:
        flag = True
        break
    shining = a[shining-1]

if flag:
    print(_)
else:
    print(-1)

Solution (Python):
import sys
n=int(input())
a=[int(input())-1 for i in range(n)]
used=[0]*n
cur=0
cnt=0
while not used[cur]:
    if cur==1:
        print(cnt)
        sys.exit()
    used[cur]=1
    cnt+=1
    cur=a[cur]

print(-1)

Solution (Python):
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))

idx = 1
flag = False
for i in range(1, N+1):
  idx = A[idx - 1]
  if idx == 2:
    flag = True
    break

if flag:
  print(i)
else:
  print(-1)

Solution (Python):
n = int(input())
l = [int(input()) for _ in range(n)]

b = 0
c = 0
s = {0}
while b != 1:
  c += 1
  b = l[b] - 1
  if b == 1:
    print(c)
    exit()
  if b in s:
    break
  s.add(b)

print(-1)

Solution (Python):

n = int(input())
A = [int(input()) for _ in range(n)]
count = 1
next = 0
for i in range(n):
    if A[next] == 2:
        print(count)
        exit()

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

print(-1)

Solution (Python):
n = int(input())
nlist = [int(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)

Solution (Python):
n = int(input())

ali=[int(input()) for i in range(n)]

count=0
b=0
while True:
    b=ali[b]-1
    count+=1
    if(b==1):
        print(count)
        break
    elif(count>n):
        print(-1)
        break

Solution (Python):
N = int(input())

a = [int(input()) for _ in range(N)]
cnt = 0
b = 0
while True:
  b = a[b] - 1
  cnt += 1
  if b == 1:
    print (cnt)
    break
  if cnt > N: 
    print (-1)
    break

Solution (Python):
import sys
n=int(input())
a=[]
for i in range(n):
    a.append(int(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

Solution (Python):
N=int(input())
a=[int(input()) for i in range(N)]
j=[0]*N
ans=0
b=1
while(1):
    ans+=1
    b=a[b-1]
    j[b-1]+=1
    if j[b-1]==2:
        ans=-1
        break
    if b==2:break
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

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

pushed = [False] * N
now = 0
ret = 0
while True :
    if pushed[now] :
        print(-1)
        break
    if now == 1 :
        print(ret)
        break
    pushed[now] = True
    now = a[now]
    ret += 1

Solution (Python):

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))  
    a = [int(next(tokens)) for _ in range(N)]  
    solve(N, a)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
s = 1
for i in range(N):
    if s == 2:
        print(i)
        exit()
    s = a[s-1]
print("-1")

Solution (Python):
n=int(input())
li = []
for i in range(n):
  li += [int(input())]
x = li[0]
if x == 2:
    print(1)
    exit()
ans = -1
count=0
for i in range(n):
  x = li[x-1]
  count +=1
  if x == 2:
    print(count+1)
    exit()
print(-1)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
a_tuple = tuple(int(input()) for _ in range(n))
num = 0
count = 0

for i in range(n):
    num = a_tuple[num] - 1
    count += 1
    if num == 1:
        break

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

Solution (Python):
n = int(input())
a = []
for _ in range(n):
  a.append(int(input()))
now = 1
for i in range(n):
  now = a[now-1]
  
  if now == 2:
    print(i+1)
    break
else:
  print(-1)

Solution (Python):
n = int(input())
a = [int(input()) - 1 for _ in range(n)]

i = 0
ans = 1
while a[i] != -1:
    if a[i] == 1:
        print(ans)
        exit()
    _i = a[i]
    a[i] = -1
    i = _i
    ans += 1
print(-1)

Solution (Python):
N=int(input())
AN=[int(input()) for _ in range(N)]

ret=0
now=0
for i in range(N):
    ret+=1
    if AN[now]==2:
        print(ret)
        exit()
    else:
        now=AN[now]-1
print(-1)

Solution (Python):
n = int(input())
a = [int(input())-1 for i in range(n)]
 
light = [False for i in range(n)]
cnt = 1
botan = a[0]
if botan == 1:
    pass
else:
    while cnt <= n:
        cnt += 1
        botan = a[botan]
        light[botan] = True
        if light[1]:
            break
 
if cnt < n:
    print(cnt)
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
cnt = 1
now = 0
while True:
    if a[now] == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        now = a[now] - 1
        cnt += 1

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


count = 0
l = 0
while count <= n:
    l = a[l]-1
    count += 1
    if l == 1:
        break

if l == 1:
    print(count)
else:
    print("-1")

Solution (Python):
import sys 
sys.setrecursionlimit(10**6)

n = int(input())

memo= [0]*n

def f(i):
	global count

	if i ==1:
		return (1)
	elif memo[i]:
		return (0)
	else:
		count+=1
		memo[i]=1
		return f(a[i]-1)
	
 

a = [int(input()) for i in range(n)]
count=0
if f(0):
	print(count)
else:
	print(-1)

Solution (Python):
import sys

n = int(input())
aL = [int(input()) for i in range(n)]

count = 1
tmp_num = aL[0]
if tmp_num == 2:
    print(1)
    sys.exit()
for i in range(n-1):
    tmp_num = aL[tmp_num-1]
    count += 1
    if tmp_num == 2:
        print(count)
        sys.exit()
print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b = a[0]
if b == 2:
  print(1)
  exit()
for i in range(n):
  b = a[b-1]
  if b == 2:
    print(i+2)
    exit()
print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b = a[0]
if b == 2:
  print(1)
  exit()
for i in range(n):
  b = a[b-1]
  if b == 2:
    print(i+2)
    exit()
print(-1)

Solution (Python):
N = int(input())

a =[0]

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


f =[0]*(N+1)

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

Solution (Python):
N = int(input())
a = [int(input()) for _ in range(N)]

hantei = False
index = 0
cnt = 1
for i in range(N):
    
    if a[index] == 2:
        hantei = True
        break
    index = a[index]-1
    cnt += 1
if hantei:
    print(cnt)
else:
    print(-1)

Solution (Python):
N=int(input())
aN=[int(input()) for _ in range(N)]
c=0
i=1
while True:
  c+=1
  if aN[i-1]==2:
    print(c)
    break
  elif aN[i-1]==0:
    print(-1)
    break
  t=aN[i-1]
  aN[i-1]=0
  i=t

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
cnt = 1
i = 0
for j in range(n):
    if a[i] == 2:
        break
    if j == n-1 and a[i] != 2 :
        cnt = -1
        break
    i = a[i]-1
    cnt += 1
print(cnt)

Solution (Python):
n=int(input())
a=[int(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)

Solution (Python):
n=int(input())
a=[int(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)

Solution (Python):
a = int(input())
b = [int(input()) for _ in range(a)]
c = 0
d = 1
while True:
    c += 1
    if b[d-1] == 2:
        print(c)
        exit()
    elif c > a:
        print(-1)
        exit()
    else:
        d = b[d-1]

Solution (Python):
n = int(input())
a_list = [int(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')

Solution (Python):
n = int(input())
a_list = [int(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')

Solution (Python):
n=int(input())
a=[int(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)

Solution (Python):
n = int(input())
a = [int(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

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
count = 0
hantei = 0
s = a[0]

while count < N :
    if s == 2 :
        print(count + 1)
        hantei = 1
        break
    else :
        s = a[s - 1]
        count += 1

if hantei == 0 :
    print(-1)

Solution (Python):
N = int(input())
aLst = []
ans = -1
cnt = 0
for i in range(N):
    aLst.append(int(input()) - 1)

tmp = 0
index = 0
for i in range(N):
    tmp = aLst[tmp]
    aLst[index] = 0
    if tmp == 0:
        break
    elif tmp == 1:
        cnt += 1
        ans = cnt
        break
    else:
        index = tmp
        cnt += 1

print(ans)

Solution (Python):
def solve():
    n = int(input())
    buttons = [int(input()) for _ in range(n)]
    c = 1
    now = buttons[0]  
    while now != 2 and c < n:
        c += 1
        now = buttons[now - 1]
    return c if c < n else -1


if __name__ == "__main__":
    print(solve())

Solution (Python):
n=int(input())
a=[int(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)

Solution (Python):

n = int(input())
A = [int(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]

Solution (Python):
n = int(input())
l = [0]*n
for i in range(n):
  l[i] = int(input())
ans = 0
b = 1
rireki = set()
while True:
  ans += 1
  b = l[b-1]
  rireki.add(b)
  if b == 2:
    print(ans)
    break
  elif len(rireki) != ans:
    print(-1)
    break

Solution (Python):
N = int(input())
nums = [0]
for _ in range(N):
  nums.append(int(input()))
check_cnt = 1
_next = nums[1]
while True:
  if check_cnt >= N:
    break
  else:
    if _next == 2:
      print(check_cnt)
      exit()
    _next = nums[_next]
    check_cnt += 1
print(-1)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
b = set()
cur = 1
b.add(cur)
for i in range(n):
  cur = a[cur-1]
  if cur == 2:
    print(i+1)
    exit()
  elif cur in b:
    break
  else:
    b.add(cur)
print(-1)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
n = int(input())
a_list = [int(input()) for _ in range(n)]
def calc_ans(start_num, count, max_count, all_list):
    ans = all_list[start_num-1]
    if ans == 2:
        print(count)
    elif count == max_count:
        print('-1')
    else:
        calc_ans(ans, count+1, max_count, all_list)
calc_ans(1, 1, n, a_list)

Solution (Python):
N=int(input())
x=[int(input()) for _ in range(N)]

on=1
ans=0
for _ in range(N):
  on=x[on-1]
  ans=ans+1
  if on==2:
    print(ans)
    exit()
  
print(-1)

Solution (Python):

import sys, math, fractions, itertools


def solve(N: int, a: "List[int]"):
    counter = 0
    nx = 0
    r = []
    for i in range(N):
        counter += 1
        r.append(a[nx])
        if a[nx] - 1 == 1:
            break
        nx = a[nx] - 1
    if counter < N and 2 in r:
        print(counter)
    else:
        print(-1)
    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))  
    a = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, a)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
n=[]
for i in range(N):
  n.append(int(input()))
a=1
c=-1
for i in range(N):
  a=n[a-1]
  if a==2:
    if c<0:
      c=(i+1)
print(c)

Solution (Python):
N = int(input())
n = []
for i in range(N):
  n.append(int(input()))

a = 1
c = -1

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

print(c)

Solution (Python):
N = int(input())
n = []
for i in range(N):
  n.append(int(input()))

a = 1
c = -1

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

print(c)

Solution (Python):
N = int(input())
A = [0]+[int(input()) for _ in range(N)]

v = set()
v.add(1)
x = 1
a = 0
while True:
  x = A[x]
  a += 1
  if x in v:
    print(-1)
    break
  if x == 2:
    print(a)
    break
  v.add(x)

Solution (Python):
def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    a = []
    for _ in range(n):
        a.append(read_int(1)[0])
    return a


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    dummy = -1
    flag_for_not_visited = 0
    flag_for_visited = 1

    buttons_flash_info = list(a)
    buttons_flash_info.insert(0, dummy)

    index = 1
    visited = [flag_for_not_visited for i in range(len(buttons_flash_info))]
    visited[index] = flag_for_visited
    push_count = 1
    while True:
        next_index = buttons_flash_info[index]
        index = next_index
        if index == 2:
            return push_count
        if visited[index] == flag_for_visited:
            return -1
        visited[index] = flag_for_visited
        push_count += 1
    return -1


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
a = [0] * N
for i in range(N):
    a[i] = int(input())

tmp = 1
count = 0
check = 0

while tmp != 2 :
    tmp = a[tmp-1]
    count += 1
    if count >= N:
        check = 1
        break

if check == 0:
    print(count)
else:
    print(-1)

Solution (Python):
N = int(input())
a = [0] + [int(input()) for _ in range(N)]
x = 1
visited = set([1])
answer = 0

while True:
  x = a[x]
  if x in visited:
    answer = -1
    break
  visited.add(x)
  answer += 1
  if x == 2:
    break
    
print(answer)

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
count = 0
button = 1
for i in range(n):
    button = a[button - 1]
    count += 1
    if button == 2:
        break
if button == 2:
    print(count)
else:
    print(-1)

Solution (Python):
N=int(input())
a=[int(input()) for _ in range(N)]
c,s=1,a[0]
while s!=2 and c<N:
    c+=1
    s=a[s-1]
print(c if c<N else -1)

Solution (Python):


n = int(input().split()[0])

index2number_dict = {}
number2index_dict = {}
button_list = []

for _ in range(n):
    number = int(input().split()[0])
    button_list.append(number)
    







start_index = 1



step = 0
lighting_index = start_index 
status_list = ''
can_stop = False

while True:
    status_list += str(lighting_index)

    lighting_number = button_list[lighting_index - 1]
    lighting_index = lighting_number  
    step += 1


    if lighting_index == 2:
        can_stop = True
        break

    if step >= len(button_list):
        break
    
    

    
    

if not can_stop:
    step = -1



print(step)

Solution (Python):
n=int(input())
a=[0]
a+=[int(input()) for i in range(n)]
cnt=1
Light=a[1]
while True:
    if Light==2:
        print(cnt)
        break
    elif cnt<n:
        cnt+=1
        Light=a[Light]
    else:
        print(-1)
        break

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]

now = 1
for i in range(N):
    if A[now - 1] == 2:
        print(i + 1)
        exit()
    now = A[now - 1]

print(-1)

Solution (Python):
N = int(input())
a = [0] * N
for i in range(N):
    a[i] = int(input())


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

print(-1)

Solution (Python):
N = int(input())
button_list =[]
for i in range(1,N+1):
  button_list.append(int(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)

Solution (Python):
N=int(input())
A=[int(input()) for _ in range(N)]
i=1
count=0
while i!=2 and count<=100001:
  i=A[i-1]
  count+=1

if count>100000:
  print(-1)
else:
  print(count)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
INF = 10**6

reached = []
pos = 0
c = 0
while c <= INF:
    reached.append(pos)
    if pos == 1:
        break
    pos = A[pos]-1
    c += 1

if 1 in reached:
    print(reached.index(1))
else:
    print(-1)

Solution (Python):
N = int(input())
button = [0]
for _ in range(N):
  button.append(int(input()))
pos = 1
cnt = 0
for _ in range(N):
  pos = button[pos]
  cnt += 1
  if pos == 2:
    break

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

Solution (Python):
a = int(input())
b = [int(input())for _ in range(a)]
c = 0
d = 1
while True:
    if b[d-1] == 2:
        c += 1
        break
    elif c > a:
        print(-1)
        exit()
    else:
        d = b[d-1]
        c += 1
print(c)

Solution (Python):
from collections import defaultdict

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

pushed = defaultdict(int)

def main(start=0):
     count = 0
     ls = start
     while True:
          if ls == 1:
               return count
          elif pushed[ls] != 0:
               return -1

          pushed[ls] += 1
          count += 1
          ls = A[ls]

print(main())

Solution (Python):


import sys

sys.setrecursionlimit(10**7)


input = sys.stdin.readline


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h(typ) if m > 1 else typ(input()) for _ in range(n)]


def main():
    n, = read_h()
    nxt = [i - 1 for i in read_v(n)]

    idx = 0
    cnt = 0
    for _ in range(n):
        cnt += 1
        idx = nxt[idx]
        if idx == 1:
            print(cnt)
            return

    print(-1)


if __name__ == '__main__':
    main()

Solution (Python):
if __name__ == '__main__':
    n=int(input())
    a=[int(input()) for i in range(n)]
    count = 1
    light_button_position=a[0]
    
    while light_button_position!=2 and count<n:
        count = count+1
        light_button_position = a[light_button_position-1]
        
    print(count if count<n else -1)

Solution (Python):
n = int(input())
a = [int(input()) - 1 for _ in range(n)]

count = 0
index = 0

while count < n:
  count += 1
  if a[index] == 1:
    break
  else:
    index = a[index]

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

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    x, a, b = LI()
    if b <= a:
        print("delicious")
    elif b <= a + x:
        print("safe")
    else:
        print("dangerous")

    return


def B():
    n = II()
    a = IR(n)
    check = [False] * (n + 1)
    check[1] = True
    now = a[0]
    for i in range(1,n+1):
        if check[now]:
            break
        if now == 2:
            print(i)
            return
        check[now] = True
        now = a[now - 1]
    print(-1)
    return


def C():
    return


def D():
    return


if __name__ == '__main__':
    B()

Solution (Python):
N = int(input())
a = []
for i in range(N):
    a.append(int(input())-1)
index = 0

flag = False 
count = 0
for i in range(N):
    count+=1
    index=a[index]
    if(index == 1):
        flag = True
        break
if(flag):
    print(count)
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(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")

Solution (Python):
n = int(input())
a = [int(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")

Solution (Python):
N=int(input())
a=list(int(input()) for _ in range(N))
ans=1
c=0
for i in range(N):
    if ans != 2:
        ans=a[ans-1]
        c+=1
    else:
        print(c)
        exit()
print(-1)

Solution (Python):
a = int(input())
b = [int(input()) for _ in range(a)]
c = 1
d = 0
e = {}
while True:
    d += 1
    if b[c-1] == 2:
        print(d)
        exit()
    else:
        if (c-1) not in e:
            e.setdefault((c-1), "")
        else:
            print(-1)
            exit()
        c = b[c - 1]

Solution (Python):
n=int(input())
l=[0]+[int(input()) for _ in [0]*n]
a=1
c=0
while a!=2 and c<n:
    a=l[a];c+=1
if c==n and a!=2:print(-1)
else:print(c)

Solution (Python):
n = int(input())
li = [int(input()) for _ in range(n)]
ans = 1
index = 0
if li[index] == 2:
  print(ans)
  exit()
else:
  index = li[index] - 1
  ans += 1

for i in range(1, n):
  if li[index] == 2:
    print(ans)
    exit()
  else:
    index = li[index] - 1
    ans += 1
print(-1)