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

cnt = 0
buttom = 1
for i in range(n):
    buttom = a[buttom-1]
    cnt += 1
    if buttom == 2:
        print(cnt)
        quit()
print(-1)

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

to = 0
cnt = 0
while cnt < n:
    to = a[to] - 1
    cnt += 1
    if to == 1:
        print(cnt)
        exit()

print(-1)

Solution (Python):
"""
author : halo2halo
date : 30, Jan, 2020
"""

import sys
import itertools



read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())
ans = 0

temp = 1
bl = False
for _ in range(len(A)):
    temp = A[temp-1]
    ans += 1
    if temp == 2:
        bl = True
        break
print(ans if bl else -1)

Solution (Python):
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
ans = 0
now = 1
while (1):
    now = l[now - 1]
    ans += 1

    if now == 2:
        print(ans)
        exit()
    if ans > n:
        print(-1)
        exit()

Solution (Python):

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


cnt = 0
visit = [0] 
i = 0 
while cnt <= n:
    cnt += 1
    
    """if a[i] in visit:
        print(-1)
        exit(0)"""
    if a[i] == 1: 
        print(cnt)
        exit(0)
    else:
        
        i = a[i]
print(-1)

Solution (Python):


def main():
    N = int(input())
    a = [int(input()) for _ in range(N)]
    
    b = a[0]
    count = 1
    if b == 2:
        ans = 1
    else:
        while True:
            count += 1
            b = a[b - 1]
            if b == 2:
                ans = count
                break
        
            if count > N:
                ans = -1
                break
    print(ans)

if __name__ == "__main__":
    main()

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 = {i+1: int(input()) for i in range(N)}

on = 1
history = {1}
ans = -1
count = 0
while True:
    on = a[on]
    count += 1
    if on == 2:
        ans = count
        break
    if on in history:
        break
    history.add(on)
print(ans)

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

now=A[0]
can=1

for i in range(2*n):
    if now==2:
        print(i+1)
        can=0
        break

    now=A[now-1]

if can:
    print(-1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
current = 0
visited = {0}
ans = 0

while True:
    current = A[current] - 1
    ans += 1

    if current in visited:
        print(-1)
        break

    if current == 1:
        print(ans)
        break

    visited.add(current)

Solution (Python):
n = int(input())
a = list(int(input()) for _ in range(n))
i= 1
cnt=0

for cnt in range(1, n+1):
    i = a[i-1]
    if(i == 2):
        print(cnt)
        quit()

print(-1)

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

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

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

check = [0] * n
check[0] = 1

i = 0
count = 0
is_success = False
while True:
    count += 1
    i = a[i]
    if i == 2:
        is_success = True
        break
    i -= 1
    if check[i] != 0:
        break
    check[i] = 1
print(str(count) if is_success else '-1')

Solution (Python):
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    A=[]
    check=[0]*n
    temp=0
    ct=1
    for i in range(n):
        A.append((INT()-1))
    check[0]=1
    for i in range(n):
        if A[temp]==1:
            print(ct)
            break
        elif check[A[temp]]==1:
            print(-1)
            break
        else:
            temp=A[temp]
            check[temp]=1
            ct+=1
            
if __name__ == '__main__':
    do()

Solution (Python):
N = int(input())
list=[]
for i in range(N):
    list.append(int(input()))
count=0
X=list[0]
while True:
    count+=1
    if X == 2:
        print(count)
        break
    elif count == N:
        print(-1)
        break
    X=list[X-1]

Solution (Python):
n=int(input())
A={i:int(input()) for i in range(1,n+1)}
now=1
cnt=0
while now!=2:
    if now in A:
        now=A.pop(now)
        cnt+=1
    else:
        print(-1)
        break
else:
    print(cnt)

Solution (Python):

import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    A = [INT() for i in range(N)]
    visited = []
    n = 0
    ans = 0
    while ans < N:
        n = A[n] - 1
        ans += 1
        if n == 1:
            print(ans)
            exit()
    print(-1)





if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = [int(input())for _ in range(N)]
visit = [False] * (N+1)
pre = 1
ans = 0
visit[1] = True
while True:
    ans += 1
    x = A[pre-1]
    if visit[x]:
        break
    visit[x] = True
    if x == 2:
        break
    pre = x
if not visit[2]:
    ans = -1
print(ans)

Solution (Python):

from collections import deque
def main():
    n = int(input())
    a = [int(input())-1 for _ in range(n)]

    Q = deque()
    Q.append(a[0])
    
    cnt = 1
    flag = False
    for _ in range(n):
        x = Q.popleft()
        if x == 1:
            flag = True
            break
        cnt += 1
        Q.append(a[x])

    if not flag:
        print(-1)
    else:
        print(cnt)
main()

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

p = 0
cnt = 1
for _ in range(n):
    if a[p] == 2:
        print(cnt)
        exit()

    p = a[p] - 1 
    cnt += 1

print(-1)

Solution (Python):







def intinput():
    return int(input())

def listintinput():
    return list(map(int,input().split()))

def splitintinput():
    return map(int,input().split())

N = intinput()
a = []
for i in range(N):
    a.append(intinput())
state = 1
flag = 0
count = 0
for i in range(N):
    if a[state-1] == 2:
        flag = 1
        count += 1
        break
    else:
        state = a[state-1]
        count += 1

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

Solution (Python):
N = int(input())
button = [0]*(N+1)
for i in range(1,N+1):
    button[i] = int(input())
current_button = 1
count = 0
while count < 10**5:
    current_button = button[current_button]
    count += 1
    if current_button == 2:
        print(count)
        exit()
print(-1)

Solution (Python):
import sys
n=int(input())
a = [int(input()) for i in range(n)]
s=1
ans=0

for i in range(n+1):
    ans+=1
    if a[s-1]==2:
        print(ans)
        sys.exit()
    s=a[s-1]
print(-1)

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

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

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

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

Solution (Python):
from __future__ import print_function

import sys
input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

























from queue import Queue


























def main():
    n = int(input().strip())
    a = [0 for i in range(n)]
    for i in range(n):
        a[i] = int(input().strip()) - 1
    cnt = 0
    que = Queue()
    que.put(0)
    l_usedlist = [0 for i in range(n)]
    while que.qsize() > 0:
        v_current = que.get()
        l_usedlist[v_current] = 1
        cnt += 1
        if a[v_current] == 1:
            print(cnt)
            return
        else:
            if l_usedlist[a[v_current]] == 0:
                que.put(a[v_current])
    print(-1)
    return


if __name__ == '__main__':
    main()

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

done = [False]*N
i = 0
cnt = 0

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

print(cnt)

Solution (Python):
N = int(input())
a = [0]
for _ in range(N):
    a.append(int(input()))
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())
lis = []
for i in range(n):
    lis.append(int(input()))
button = lis[0]
count = 1
if button == 2:
    ans = 1
else:
    while True:
        count += 1
        button = lis[button - 1]
        if button == 2:
            ans = count
            break
        if count > n:
            ans = -1
            break
print(ans)

Solution (Python):

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

k=1
t=-1

for i in range(N):
    k=l[k-1]
    if k==2:
        t=i+1
        break
print(t)

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

bList = []

bList.append(-1)
for i in range(n):
  bList.append(int(input()))


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

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

ans = 0
tmp = 0

for i in range(n):
    ans += 1
    if a[tmp] == 2:
        break;
    tmp = a[tmp] - 1
if ans == n:
    ans = -1

print(ans)

Solution (Python):
n = int(input())
d = {}
cnt = 0

for i in range(1,n+1):
    d[i] = int(input())

btn = 1
while True:
    cnt += 1
    if btn == 0:
        ans = -1
        break
    elif d[btn] == 2:
        ans = cnt
        break
    else:
        tmp = btn
        btn = d[btn]
        d[tmp] = 0

print(ans)

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






def judge(buttons):
    move_cnt = 1
    
    next_idx = buttons[0]
    if next_idx == 1:
        return move_cnt, True

    while True:
        move_cnt += 1
        if move_cnt > N:
            return None, False
        next_idx = buttons[next_idx]
        if next_idx == 1:
            return move_cnt, True
    return move_cnt, True


cnt, is_Yes = judge(buttons)
if is_Yes:
    print(cnt)
else:
    print(-1)

Solution (Python):

def Trained():
    button = []
    N = int(input())
    for _ in range(N):
        button.append(int(input()) - 1)
    adr = 0
    for i in range(N):
        adr = button[adr]
        if adr == 1:
            print(i + 1)
            return()
    print(-1)
    return()
Trained()

Solution (Python):
N=int(input())
li = []

now = 0
ans = -1
count = 0

for n in range(N):
  li.append(int(input()))

for n in range(N + 1):
  if n == 0:
    now = li[0]
    count += 1

    if now == 2:
      ans = count
      break

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

  if now == 2:
    ans = count
    break

print(ans)

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

lst=set()
i=0
count=0
while not i in lst:
    lst.add(i)
    i=A[i]-1
    count+=1
    if i==1:
        print(count)
        exit()



print(-1)

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
now=0
a=[int(input()) for i in range(n)]
for i in range(n):
    if a[now]==2:
        print(i+1)
        exit()
    else:
        now=a[now]-1
print(-1)

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


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

if flag == True:
    print(count)
else:
    print(-1)

Solution (Python):
N = int(input())
a = {i: int(input()) for i in range(1, N + 1)}
result = 0
i = 1
while True:
    if not i in a:
        result = -1
        break
    result += 1
    nexti = a[i]
    if nexti == 2:
        break
    del a[i]
    i = nexti
print(result)

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

for i in num:
  if a==2:
    b+=1
    print(b)
    exit()
  else:
    b+=1
    a=num[a-1]
    
print("-1")

Solution (Python):


import numpy as np
import fractions as fra

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


k=1
c=0
while(c<=n*2):
    k=a[k]
    c+=1
    if(k==2):break

if(c>=n):c=-1
print(c)

Solution (Python):
N = int(input())
a = [int(input()) for _ in range(N)]
memo = set()
memo.add(0)
count = 1
i = a[0] - 1

while True:
  if i == 1:
    break
  i = a[i] - 1
  count += 1
  if i in memo:
    count = -1
    break
  else:
    memo.add(i)
print(count)

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


b[0] = 1
now = 0
count = 0
while (b[1] != 1 and count <= n):
   next = a[now] - 1

   if (b[next] == -1):
      print(-1)
      exit()

   b[now] = -1
   b[next] = 1

   now = next
   count += 1

if (b[1] == 1):
   print(count)
else:
   print(-1)

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

N = int(input())
g = [0] * (N + 1)
for i in range(N):
    g[i + 1] = int(input())
cnt = 0
now = 1
for i in range(10 ** 5):
    cnt += 1
    now = g[now]
    if now == 2:
        print(cnt)
        sys.exit()
print(-1)

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

amaps = { i + 1: int(input()) for i in range(N) }

if 2 not in amaps.values():
    print(-1)
    exit()

current = amaps[1]
switches = 1
max_switches = len(amaps)

while switches <= max_switches:
    if current == 2:
        print(switches)
        exit()

    if current not in amaps:
        break
    current = amaps[current]
    switches+=1

print(-1)

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

b = 1

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

Solution (Python):
a=[input() for _ in range(int(input()))]
now=1
cnt=0
while cnt<=len(a):
    now=int(a[now-1])
    cnt+=1
    if now==2: break
print(cnt if now==2 else '-1')

Solution (Python):


n = int(input())
a = ["" for _ in range(n)]
for i in range(n):
    a[i] = int(input())
    


count = 0
isOn = False
i = 0
for k in range(n):

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

if isOn:
    pass
else:
    print(-1)

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

lst = [a[0]]
for i in range(1,n):
    lst.append(a[lst[i-1]-1])

    if lst[i]==2:
        break

if 2 in lst:
    print(lst.index(2)+1)
else:
    print(-1)

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

idx = 1
cnt = 0
for _ in range(N):
  idx = A[idx]
  cnt += 1
  if idx == 2:
    print(cnt)
    exit()
else:
  print(-1)

Solution (Python):
n=int(input())
a=[]
cnt=0
now=0
for i in range(n):
  a.append(int(input())-1)
flg=False
for i in range(n):
  if now==1:
    flg=True
    break
  now=a[now]
  cnt+=1
print(cnt if flg else -1)

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

Solution (Python):
n=int(input())
A=list(int(input()) for _ in range(n))
s=A[0]
ans=1
for i in range(n):
    if s==2:
        print(ans)
        exit()
    s=A[s-1]
    ans += 1
print('-1')

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
seen = [False] * n
seen[0] = True
ans = 0
now = 1
while not now == 2:
    ans += 1
    now = a[now-1]
    if seen[now-1] == True:
        print(-1)
        exit(0)
    seen[now-1] = True
print(ans)

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

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

nex=a[0]
cnt=1
while nex != 2 and cnt<N:
    cnt+=1
    nex=a[nex-1]
    
if cnt<N:
    print(cnt)
else:
    print(-1)

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

Solution (Python):
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
    
ans = set()
i = 0
count = 0
while True:
    if a[i] == 2:
        print(count+1)
        exit()
    elif a[i]-1 not in ans:
        ans.add(a[i]-1)
        count += 1
        i = a[i] - 1
    else:
        print(-1)
        exit()

Solution (Python):

 
import math
import itertools
import sys
import copy
 



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


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

Solution (Python):
n = int(input())
li = [0]
for i in range(n):
    li.append(int(input()))
x = 1
visited = set([1])
ans = 0

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

Solution (Python):

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

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

Solution (Python):

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

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

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

Solution (Python):
n = int(input())
a = [0] + [int(input()) for i in range(n)]
c = a[1]
p = 1
while c > 2:
    d = a[c]
    a[c] = 0
    c = d
    p += 1

if c == 2:
    print(p)
else:
    print(-1)

Solution (Python):
n = int(input())
a = list(int(input()) for i in range(n))
t = 0
i = 1
while t < n:
    i = a[i-1]
    t += 1
    if i == 2:
        print(t)
        break
else:
    print("-1")

Solution (Python):
N = int(input())
a = [int(input())-1 for i in range(N)]
i = 0
count = -1
for n in range(N):
    if i == 1:
        count = n
        break
    i = a[i]
print(count)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
p = 1
count = 0
se = set([1])
while p!=2:
  count += 1
  p = A[p-1]
  if p == 2:
    print(count)
  elif p in se:
    print('-1')
    break
  se.add(p)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)

n = int(input())

list1 = [int(input()) for i in range(n)]
unch = [0]*n
def fuck(index,times):
    if list1[index] == 2:
        return times + 1
    else:
        if unch[index] == 1:
            return -1
        else:
            unch[index] = 1
            return fuck(list1[index]-1,times+1)

print(fuck(0,0))

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

answer = 1
a = A[1]

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

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

i = 0
cnt = 0
while not i == 1:
    i = a[i] - 1
    cnt += 1
    if i >= n or cnt > 100000:
        print(-1)
        exit()
print(cnt)

Solution (Python):
import sys

n, *b = 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)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
d={}
d[1]=1
r=0
cnt=0
while a[r]!=2:
  if a[r] not in d:
    d[a[r]]=1
    r=a[r]-1
    cnt+=1
  else:
    print(-1)
    exit()
print(cnt+1)

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

i = 1
cnt = 0
while i != 2 and cnt <= N:
    cnt += 1
    i = A[i-1]

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

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

i=0
cnt=0
while i!=1:
  i=a[i]-1
  cnt+=1
  if cnt>N:
    print(-1)
    break
else:
  print(cnt)

Solution (Python):
n = int(input())
al = []

for _ in range(n):
    a = int(input())
    al.append(a)

ans = 0
idx = 0
for _ in range(n):
    idx = al[idx] - 1
    ans += 1
    if idx == 1:
        print(ans)
        import sys
        sys.exit()

print(-1)

Solution (Python):


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

used = [False for _ in range(10 ** 5 + 1)]
tmp = 1
ans = 0
while True:
    target = dic[tmp - 1]
    if target == 2:
        print(ans + 1)  
        exit()
    if used[target]:
        print(-1)
        exit()
    tmp = target
    used[tmp] = True
    ans += 1

Solution (Python):
import sys

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

ans = 0

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

print(ans)

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

i = 1
ans = 0
while i!=2 and i>0:
    i_next = A[i-1]
    A[i-1] = -A[i-1]
    i = i_next
    ans += 1
print(ans if i>0 else -1)

Solution (Python):
import sys
def resolve():
        input = sys.stdin.readline
        N=int(input().rstrip())
        a=[input().rstrip() for _ in range(N)]

        index=0
        ans=0
        for i in range(N):
            index=int(a[index])-1
            ans+=1
            if(index==1):
                print(ans)
                return
        print(-1)



from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """3
3
1
2"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """4
3
4
1
2"""
        output = """-1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """5
3
3
4
2
4"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    
    resolve()

Solution (Python):
def main():
    n=int(input())
    a=[int(input()) for _ in range(n)]
    button = 1
    for i in range(n):
        next = a[button-1]
        if next == 2:
            return i+1
        else:
            button = next
    return -1
print(main())

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
pushed = [False] * n
cnt = 0
index = 0
while not (pushed[index]):
    cnt += 1
    pushed[index] = True
    if index == 1:
        cnt -= 1
        print(cnt)
        exit()
    index = a[index] - 1
print(-1)

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
l=[1]
n=0
for i in range(N):
    l.append(A[l[-1]-1])
    n+=1
    if l[-1]==2:
        print(n)
        exit()
print('-1')

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
l=[1]
n=0
for i in range(N):
    l.append(A[l[-1]-1])
    n+=1
    if l[-1]==2:
        print(n)
        exit()
print('-1')

Solution (Python):
n = int(input())
a = []
count=1
flag = [True]*n
for i in range(n):
  x = int(input())-1
  a.append(x)
i=0

while a[i]!=1:
  if not flag[i]:
    print(-1)
    exit()
  flag[i]=False
  i=a[i]
  count+=1
  
print(count)

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    num=int(input())
    a.append([num-1,0]) 
ans=-1
now=0
for i in range(n+100):
    now=a[now][0]
    if now==1:
        ans=i+1
        break
    if a[now][1]==1:
        break
    a[now][1]=1
print(ans)

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

bot = 1
ans = -1

for i in range(n):
    if bot == 2:
        ans = i
        break
    bot = A[bot-1]

print(ans)

Solution (Python):
n = int(input())
l = [int(input()) for _ in range(n)]
ans = 0
flg = False
button = set()
x = 1
for _ in range(n):
    ans += 1
    if l[x-1] == 2:
        flg = True
        break
    elif x in button:
        break
    else:
        button.add(x)
        x = l[x-1]
        
    
print(ans if flg else -1)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n=int(input())
    aa=[int(input())-1 for _ in range(n)]
    b=0
    fin=set()
    ans=0
    for _ in range(n):
        if b in fin:
            print(-1)
            exit()
        if b==1:break
        fin.add(b)
        b=aa[b]
        ans+=1
    print(ans)

main()

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

cur = 1
c = 0
while True:
    c += 1
    cur = a[cur - 1]
    if cur == 2:
        print(c)
        break
    elif c >= 10 ** 5 + 2:
        print(-1)
        break

Solution (Python):
import sys
n = int(input())
bottun = [int(input()) for i in range(n)]
i = 0
cnt = 0
while 1:
  if bottun[i] == 0:
    print(-1)
    sys.exit()
  if bottun[i] == 2:
    print(cnt+1)
    sys.exit()
  temp = bottun[i] - 1
  bottun[i] = 0
  i = temp
  cnt += 1

Solution (Python):
import sys
n = int(input())
bottun = [int(input()) for i in range(n)]
i = 0
cnt = 0
while 1:
  if bottun[i] == 0:
    print(-1)
    sys.exit()
  if bottun[i] == 2:
    print(cnt+1)
    sys.exit()
  temp = bottun[i] - 1
  bottun[i] = 0
  i = temp
  cnt += 1

Solution (Python):
n=int(input())
A=[int(input()) for i in range(n)]
flag=0
ans=-1
C=[0 for i in range(n+1)]

for j in range(n):
    if j==0:
        C[j]=A[0]-1
    else:
        C[j]=A[C[j-1]]-1
        if C[j]==1:
            ans=j+1
            flag=1
            break
        else:
            continue
        if flag: break
        else: continue
    if flag:break
    else:continue
if A[0]==2:
    ans=1
print(ans)

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

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

print(count if index==2 else -1)

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

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

c = 0
now = 0
for i in range(N):
    now = array[now]
    c += 1
    if now == 1:
        break
if c == N and now != 1:
    print(-1)
else:
    print(c)

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

current = a[0]
cnt = 0
while current[0] <= n:
    if current[0] == 2:
        cnt += 1
        break
    if not current[1]:
        cnt = -1
        break

    current[1] = False
    current = a[current[0]-1]
    cnt += 1

print(cnt)

Solution (Python):
n=int(input())
l=list(map(int,[input() for i in range(n)]))
a=l[0]
m=1
for i in range(n):
    if a==2:
        print(m)
        exit()
    else:
        a=l[a-1]
        m+=1
print(-1)

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

ls_next = [ls_a[0]]

for i in range(n):
    Num = ls_a[ls_next[i] - 1]
    ls_next.append(Num)
    if ls_a[0] == 2:
        print(1)
        break
    elif Num == 2:
        print(len(ls_next))
        break
    elif i == n - 1:
        print(-1)

Solution (Python):
N = int(input())
A = []
for _ in range(N):
    a = int(input())
    A.append(a - 1)
Flags = [False] * N
Flags[0] = True
now = 0
cnt = 0
for _ in range(N):
    cnt += 1
    next_node = A[now]
    if next_node == 1:
        break
    if Flags[next_node]:
        print("-1")
        exit()
    Flags[next_node] = True
    now = next_node
print(cnt)

Solution (Python):
N=int(input())
a = [int(input()) for i in range(N)]
cnt=0
light=1
while True:
    light=a[light-1]
    cnt=cnt+1
    if light==2:
        break
    if cnt>N:
        break

if light==2:
    print(cnt)
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 = [int(input()) for i in range(N)]
next_light = 0 
count = 0 
for i in range(N + 1): 
        next_light = a[next_light] - 1 
        count += 1
        if next_light  == 1:
                break
if count > N:
        print(-1)
else:
        print(count)

Solution (Python):
import sys

n = int(input())
lst = [int(i) for i in sys.stdin]
i = lst[0]
for c in range(n):
    if i == 2:
        print(c + 1)
        quit()
    i = lst[i - 1]
print(-1)

Solution (Python):
n = int(input())
lst = [int(input()) for _ in range(n)]
i = lst[0]
for c in range(n):
    if i == 2:
        print(c + 1)
        quit()
    i = lst[i - 1]
print(-1)

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

for c in range(n):
    i = lst[i - 1]
    if i == 2:
        print(c + 1)
        quit()
print(-1)

Solution (Python):
n = int(input())
lst = [int(input()) for _ in range(n)]
i = 1
c = 0
for _ in range(n):
    i = lst[i - 1]
    c += 1
    if i == 2:
        print(c)
        quit()
print(-1)

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