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 _ in range(N)]

now = 1
L = [0]*N
for i in range(N):
    L[i] = now
    now = a[now-1]

judge = 1
for i in range(N):
    if L[i] == 2:
        print(i)
        judge = 0
        break
    else:
        pass

if judge == 1:
    print(-1)

Solution (Python):
import sys

input = sys.stdin.readline

N = int(input())
a = [0] * N

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

i = a[0]
count = 1

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

if i != 2:
    count = -1

print(count)

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
a = 0
cnt = 1
while 1:
    if A[a] == -1:
        print(-1)
        break
    if A[a]-1 == 1:
        print(cnt)
        break
    b,A[a] = A[a]-1,-1
    a = b
    cnt += 1

Solution (Python):
N=int(input())
L=[0 for i in range(N)]
A=[int(input())-1  for _ in range(N)]
n=0
m=1
while L[n]!=1 and A[n]!=1:
  L[n]=1
  m+=1
  n=A[n]
if L[n]==1:
  print(-1)
else:
  print(m)

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

Solution (Python):
from sys import stdin
from collections import deque

n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip())-1 for _ in range(n)]
now = 0
point = 1
for i in range(n):
    if li[now] == 1:
        print(point)
        exit()
    else:
        now = li[now]
        point += 1

print(-1)

Solution (Python):
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

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

flag = 0
num = 0
bottun = 0
for _ in range(int(1e6)):
    if light[1] == "1":
        flag = 1
        break
    num += 1
    
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    
    
if flag:
    print(num)
else:
    print(-1)

Solution (Python):
N = int(input())
light_rule = {n:int(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
    
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    
    
if flag:
    print(num)
else:
    print(-1)

Solution (Python):
N = int(input())
light_rule = {n:int(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
    
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    
    
if flag:
    print(num)
else:
    print(-1)

Solution (Python):
N = int(input())
A = []
for i in range(N):
    a = int(input())
    A.append(a)
check = 0
count = 0
botton = 1
for i in range(N):
    count += 1
    if A[botton - 1] == 2:
        check = 1
        break
    botton = A[botton - 1]

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

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

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

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

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

b = 0
loop_count = 0
for i in range(n):
  if b == 2:
    print(loop_count)
    exit(0)
    
  if i == 0:
    b = buttons[i]
  else:
    b = buttons[b-1]
  
  loop_count += 1

print(-1)

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



go = 0
ans = -1

for i in range(n):
	
	
	go = l[go]-1
	

	if go == 1:
		if ans < 0:
			ans = i +1


print(ans)

Solution (Python):
N = int(input())
dic = {}
for i in range(1,N+1):
    bi = int(input())
    dic[i] = bi
botan = 1
cnt = 0
while(True):
    botan = dic[botan]
    cnt += 1
    if(botan == 2):
        print(cnt)
        break
    if(cnt > N):
        print(-1)
        break

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
Next = 1
for i in range(N):
  if Next == 2:
    Next = A[Next-1]
    p = i
    break;
  else:
    Next = A[Next-1]
    p = -1
print(p)

Solution (Python):
N = int(input())
import sys
input = sys.stdin.readline
a = []
for i in range(1,N+1):
    a.append(int(input()))
ans = 1
check = a[0]
pre_index = 1
while True:
    if N < ans:
        print(-1)
        exit()
    if check == 2:
        print(ans)
        exit()
    if pre_index == a[check-1]:
        print(-1)
        exit()
    pre_index = check
    check = a[check-1]
    ans += 1

Solution (Python):
N = int(input())
import sys
input = sys.stdin.readline
a = []
for i in range(1,N+1):
    a.append(int(input()))
ans = 1
check = a[0]
pre_index = 1
while True:
    if 10**5 < ans:
        print(-1)
        exit()
    if check == 2:
        print(ans)
        exit()
    if pre_index == a[check-1]:
        print(-1)
        exit()
    pre_index = check
    check = a[check-1]
    ans += 1

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

Solution (Python):
n=int(input())
a=[0]*n
for i in range(n):
    a[i]=int(input())
t=a[0]-1
ans=-1
for j in range(1,200000):
    if t==1:
        ans=j
        break
    else:
        t=a[t]-1
print(ans)

Solution (Python):
import sys, re
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

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()

a = [INT() for _ in range(N)]
tmp = [0]*N
tmp2 = a[0]-1
cnt = 0
while 1:
  if tmp[tmp2] == 1:
    print("-1")
    break
  elif tmp2 == 1:
    print(cnt+1)
    break
  tmp[tmp2] = 1
  tmp2 = a[tmp2]-1
  cnt += 1

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

k = 0
for i in range(N):
    k = A[k]
    if k == 1:
        print(i+1)
        break
else:
    print(-1)

Solution (Python):
N = int(input())
dic = {}
for i in range(N):
  a = int(input())
  dic[i+1] = a
counter = 0
place = 1
while place != 2:
  if dic[place] in dic and dic[place] != place:
    temp = dic[place]
    dic.pop(place)
    place = temp
    counter += 1
  else:
    counter = -1
    break
print(counter)

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

Solution (Python):

n = int(input())
a = [int(input()) for _ in range(n)]
lighting = 1
number = 0

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

Solution (Python):
def bfs(n,e,fordfs):
    
    W = [-1]*n
    
    W[e] = 0
    que = deque()
    que.append(e)
    len = [float("inf")]*n
    len[e] = 0
    while que:
        now = que.popleft()
        nowv = W[now]
        nowlen = len[now]
        for ne in fordfs[now]:
            if nowv == W[ne]:
                
                return -1
            elif W[ne] == -1:
                W[ne] = (nowv+1) % 2
                len[ne] = nowlen+1
                que.append(ne)
    return len


def examB():
    N = I()
    v = [[]for _ in range(N)]
    for i in range(N):
        a = I()
        v[i].append(a-1)
    v[1]=[]
    b = bfs(N,0,v)
    if b==-1:
        ans = -1
    else:
        ans = b[1]
    if ans==float("inf"):
        ans = -1
    print(ans)

import sys
import copy
import bisect
import heapq
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examB()

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

status = 1
count = 0


def press_button(n):
    status = button_list[n-1]
    return status

while status != 2:
    status = press_button(status)
    count += 1
    if count >= 100000:
        print(-1)
        exit()


print(count)

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

Solution (Python):
N = int(input())
A = [int(input())-1 for _ in range(N)]
ans = 0
i = 0
done = set([i])
while 1:    
  if A[i] == 1:
    print(ans+1)
    exit()
  else:
    if A[i] in done:
      print(-1)
      exit()
    else:
      i = A[i]
      done.add(i)
      ans += 1

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

count=0
flag=False
push_num=1

for i in range(n):
  count+=1
  light_num=l[push_num-1]
  if light_num==2:
    flag=True
    break
  push_num=light_num
if flag:
  print(count)
else:
  print(-1)

Solution (Python):
n=int(input())
l=[int(input()) for x in range(n)]
count=1
i=1
ind=1
while i<=n:
    if l[ind-1]==2:
        print(count)
        exit()
    else:
        ind=l[ind-1]
        count+=1
        i+=1    
print(-1)

Solution (Python):
def push():
    n = int(input())
    a = [int(input()) for i in range(n)]
    f = [0]*n
    c = 0
    
    p = a[0]
    f[0] = 1
    
    while 1:
        f[p-1] = 1
        c += 1
        if p == 2:
            return c
        p = a[p-1]
        if f[p-1] == 1:
            return -1
print(push())

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

Solution (Python):
def check():
    N = int(input())
    A = [int(input()) for i in range(N)]
    flag = [0]* N
    count = 0
    
    posi = A[0]
    flag[0] = 1

    while 1:
        flag[posi-1] = 1
        count += 1
        if posi == 2:
            return count
        posi = A[posi-1]
        if flag[posi-1] == 1:
            return -1
print(check())

Solution (Python):
def check():
    N = int(input())
    A = [int(input()) for i in range(N)]
    flag = [0]* N
    count = 0
    
    posi = A[0]
    flag[0] = 1

    while 1:
        flag[posi-1] = 1
        count += 1
        if posi == 2:
            return count
        posi = A[posi-1]
        if flag[posi-1] == 1:
            return -1
print(check())

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

x = 1
c = {x}
count = 1
while True:
    if A[x-1] == 2:
        print(count)
        break
    if A[x-1] in c:
        print(-1)
        break
    x = A[x-1]
    c.add(x)
    count += 1

Solution (Python):
def q(a, N):
    if 2 not in a:
        return -1
    count = 0
    i = 0
    while count < N:
        m = a[i]
        count += 1
        if m == 2:
            return count
        i = m - 1
    return -1

N = int(input())

a = []

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

print(q(a, N))

Solution (Python):
def solve():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    search_table = [False]*N
    itr = 0
    ret = 0
    while not search_table[itr]:
        search_table[itr] = True
        itr = A[itr]-1
        ret += 1
        if itr == 1: break
    
    print(ret if itr == 1 else -1)
    
solve()

Solution (Python):
buttons = int(input())
next_ = []
impossible = []
for i in range(buttons):
    next_.append(int(input()))
now_button = 1
ans = 1
while next_[now_button - 1] != 2:
    if ans <= buttons:
        now_button = next_[now_button - 1]
        ans += 1
        




    else:
        ans = -1
        break

        
print(ans)

Solution (Python):
N=int(input())
A = [int(input()) for i in range(N)]
A_next=A[0]
A_list=[1,A_next]
for _ in A:
    A_list.append(A[A_next-1])
    A_next=A[A_next-1]
if not 2 in A_list:
    print(-1)
else:
    print(A_list.index(2))

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

idx = 0
tryed = 0
for _ in range(10 ** 5):
    idx = li[idx]-1
    tryed += 1
    if idx == 1:
        print(tryed)
        break
else:
    print('-1')

Solution (Python):
n = int(input())
A = [ int(input()) for i in range(n) ]
c = 0
temp = 1
s = set()
while temp not in s:
  s.add(temp)
  temp = A[temp-1]
  c += 1
  if temp == 2:
    print(c)
    exit()
print(-1)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpln(n)
cur = 0
for i in range(10**6):
    cur = a[cur] - 1
    if cur == 1:
        print(i+1)
        break
else:
    print(-1)

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):




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

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

ans = [False for i in range(n)]

trg = 0
cnt = 0
for i in range(n):
    
    memo2 = a[trg]

    if trg == 1:
        print(cnt)
        ans[1] = True
        break

    if not(ans[memo2]):
        ans[memo2] = True
        trg = memo2
        cnt += 1

    
    else:
        break

if not(ans[1]):
    print(-1)

Solution (Python):
N = int(input())
A = [int(input()) - 1 for _ in range(N)]
cur = 0
answer = 0
for i in range(N):
    answer += 1
    if A[cur] == 1:
        print(answer)
        break
    else:
        cur = A[cur]
        
else:
    print(-1)

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

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
index = a[0]-1
count = 1
flg = False
for i in range(len(a)):
    if index == 1:
        flg = True
        break
    index = a[index]-1
    count += 1
print(count if flg else -1)

Solution (Python):
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
    
now = 1
nex = a[now-1]
ans = 0
flg = 0
while a[now-1] != 0:
    a[now-1], nex = 0, a[now-1]
    now = nex
    ans += 1
    if nex == 2:
        flg = 1
        break

if flg == 1:
    print(ans)
else:
    print(-1)

Solution (Python):



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

first = 1
pres = first
count = 0
nex =  a[pres] 
count += 1
for i in range(N-1): 
    if nex == 2: break
    prev = pres
    pres = nex
    nex = a[pres] 
    count += 1
    if nex == prev or nex == pres: break

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

Solution (Python):
N = int(input())
A = []
for i in range(N):
    ai = int(input())
    A.append(ai)
n = A[0]
if n==2:
    print(1)
    exit()
cnt=1
while True:
    N-=1
    n = A[n-1]
    cnt+=1
    if n==2:
        print(cnt)
        exit()
    elif N==0:
        break
    else:
        continue
print(-1)

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

def solve():
    next_ = list_[1]
    list_[1] = -1
    count = 1
    if next_ == 2:
        return count
    if next_ == -1:
        return -1

    while True:
        prev = next_
        next_ = list_[prev]
        list_[prev] = -1
        count += 1
        if next_ == 2:
            return count
        if next_ == -1:
            return -1

print(solve())

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

tar=0
cnt=0
suc=False
while reached[tar]==False:
  reached[tar]=True
  cnt+=1
  tar=a[tar]
  if tar==1:
    suc=True
    break
    
print((-1,cnt)[suc])

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

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

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

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

Solution (Python):
N = int(input())
Data = [0]
Push = [0] * (N+1)
for _ in range(N):
    Data.append(int(input()))

nowp = 1
Push[nowp] = 1

ans = 0
for i in range(N):
    nowp = Data[nowp] 
    ans += 1
    if nowp == 2:
        break
    elif Push[nowp] == 1:
        ans = -1
        break
    else:
        Push[nowp] = 1
print(ans)

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

ans=1
button=1


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

Solution (Python):

N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
idx = 0
cnt = 0
for i in range(N):
    if A[idx] == 2:
        print(cnt+1)
        exit()
    else:
        cnt += 1
        idx = A[idx]-1
    
print(-1)

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

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

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

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

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

i = a[0]
count = 1
while True:
  if i == 2:
    print(count)
    exit()
  if count >= N:
    print(-1)
    exit()
  count += 1
  i = a[i-1]

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

i = a[0]
count = 1
while True:
  if i == 2:
    print(count)
    exit()
  if count >= N:
    print(-1)
    exit()
  count += 1
  i = a[i-1]

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

now = 0
cnt = 0
for _ in range(N):
    if now == 1:
        print(cnt)
        break
    now = a[now]-1
    cnt += 1
else:
    print(-1)

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


def main():
    N = int(input())
    A = []
    for _ in range(N):
        A.append(int(input()))
    x = 1
    n = 0
    while 1:
        x = A[x-1]
        n += 1
        if x == 2:
            print(n)
            return
        if n > N:
            print(-1)
            return


if __name__ == '__main__':
    main()

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

now = 1
flag = 1
cnt = 0
while cnt<N and next!=2:
    cnt+=1
    next = a_lis[now-1]
    now = next
if next==2:print(cnt)
else:print(-1)

Solution (Python):
n=int(input())
alis=[int(input()) for i in range(n)]
counter=0
ans = 1
while counter <n and ans !=2:
	counter +=1
	ans=alis[ans-1]
print(counter if ans ==2 else '-1')

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

Light=1
for i in range(10**5):
    Light=A[Light-1]
    if Light==2:
        ans=i+1
        break
    
print(ans)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
x = 1
total = 0
while type(a[x-1])==int and x != 2:
    k = x-1
    x = a[x-1]
    a[k] = "loop"
    total += 1
else:
    if type(a[x-1])==str:
        print(-1)
    elif x == 2:
        print(total)

Solution (Python):
def main():
    n = int(input())
    buttons = [int(input()) for i in range(n)]

    histories = [0] * n

    now_button = 0
    histories[now_button] = 1
    count = 0

    while True:
        now_button = buttons[now_button]-1
        count += 1
        if histories[now_button] == 1:
            ans = -1
            break

        histories[now_button] = 1
        if now_button == 1:
            ans = count
            break

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
A.insert(0,0)
B = [False]*(N+1)
B[1] = True
ans = 1
i = 1
while i <= N:
    if A[i] == 2:
        break
    if not B[A[i]]:
        B[A[i]] = True
        ans += 1
    else:
        ans = -1
        break
    i = A[i]
print(ans)

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

for i in range(N):
    C = a[C] - 1
    if C == 1:
        print(i+1)
        break
else:
    print(-1)

Solution (Python):
N = int(input())
A = [int(input())-1 for _ in range(N)]
p = 0
for i in range(N):
    if p == 1:
        print(i)
        break
    p = A[p]
else:
    print(-1)

Solution (Python):
N = int(input())
G = {}
for i in range(1,N+1):
    a = int(input())
    G[i] = a
cur = 1
hist = [0 for _ in range(N+1)]
hist[cur] = 1
cnt = 0
while True:
    cur = G[cur]
    cnt += 1
    if cur == 2:break
    if hist[cur]==1:break
    hist[cur]=1
if cur==2:
    print(cnt)
else:
    print(-1)

Solution (Python):

N = int(input())
A = [[int(input()),False] for i in range(N)]
idx = 0
A[0][1] == True
cnt = 1

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

Solution (Python):

N = int(input())

A = [[int(input()),False] for i in range(N)]
idx = 0
A[0][1] == True
cnt = 1

while cnt != 2*N:
    if A[idx][1]:
        print(-1)
        exit()
    if A[idx][0] == 2:
        print(cnt)
        exit()
    A[idx][1] = True
    idx = A[idx][0]-1
    cnt += 1

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

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

ans = 1
i = 0
while ans != 2 and i < n:
	ans = a[ans-1]
	i += 1

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

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

Solution (Python):
n = int(input())
a_s = [int(input()) for _ in range(n)]
c = 1
r = a_s[0]
while True:
  if r == 2:
    print(c)
    break
  r = a_s[r-1]
  c += 1
  if c > n:
    print(-1)
    break

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

count = 0
next_num = 0
flag = False



while count < n + 10:
    next_num = a[next_num]
    
    if next_num != 2:
        next_num = next_num - 1
        count += 1
    else:
        count += 1
        flag = True
        break

print(count if flag else "-1")

Solution (Python):
n=int(input())
a=[int(input())-1 for _ in range(n)]
v=[False]*n
i=0
cnt=0
while i!=1 and not v[i]:
    v[i]=True
    cnt+=1
    i=a[i]
print(cnt if i==1 else -1)

Solution (Python):

N, *a = map(int, open(0).read().split())

i=0
cnt=0
button=[]
while(True):
    
    
    if i==1:
        print(cnt)
        break
    if cnt >= N:
        print("-1")
        break
    cnt+=1
    i=a[i]-1

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

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

count = 0
next_button = 0
while count <= 10 ** 5:
  count += 1
  next_button = list_a[next_button] - 1
  if next_button == 1:
    print(count)
    exit()
print(-1)

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

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

Solution (Python):
N = int(input())
T = [int(input())-1 for _ in range(N)]
visited =[0]*N

def main():
  cnt = 0
  nxt = T[0]
  visited[0]=1
  while nxt:
    cnt+=1
    if nxt == 1:
      print(cnt)
      return
    if visited[nxt]:
      break
    visited[nxt]=1
    nxt = T[nxt]
  print(-1)

if __name__=="__main__":
  main()

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

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

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

print(-1)

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

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

print(-1)

Solution (Python):
n=int(input())
l=[int(input())-1 for i in range(n)]
ans=0
j=0
for i in range(n):
    j=l[j]
    ans+=1
    if j==1:
        print(ans)
        exit()
print(-1)

Solution (Python):

import sys

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

done=[False]*N

ans=0
i=0

while True:
  if done[i]==False:
    done[i]=True
    ans+=1
    if a[i]==1:
      break
    i=a[i]

  
  else:
    ans=-1
    break
    
print(ans)

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

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

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

if b == 2:
    print(cnt)
else:
    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())
l=[int(input()) for i in range(n)]
c=l[0]
for i in range(n):
    if c==2:
        print(i+1)
        break
    else:
        c=l[c-1]
else:
    print(-1)

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

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

s=0
count=0

while s!=1:
  if count==N:
    break
  else:
    s=li[s]-1
    count+=1

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

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

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [int(input().rstrip('\n')) for _ in range(n)]
    cnt = 0
    q = 0
    while True:
        if cnt > n:
            print(-1)
            exit()
        elif q == 1:
            print(cnt)
            exit()
        else:
            q = a[q] - 1
            cnt += 1


if __name__ == '__main__':
    slove()

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

  s = 1
  for i in range(n):
    if a[s-1] == 2:
      print(i+1)
      exit()
    else:
      s = a[s-1]
  print(-1)
  
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 a[s-1] == 2:
    print(i+1)
    exit()
  else:
    s = a[s-1]
print(-1)

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

  s = 1
  for i in range(n):
    if a[s-1] == 2:
      print(i+1)
      exit()
    else:
      s = a[s-1]
  print(-1)
  
if __name__=='__main__':
  main()

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def search(arr, num, i):
  nxt = arr[num]
  if(nxt == 1):
    return i
  elif(i >= len(arr)):
    return -1
  else:
    return search(arr, nxt, i+1)

n = int(input())
a = [ int(input()) - 1 for _ in range(n)]
print(search(a, 0, 1))

Solution (Python):
import sys
sys.setrecursionlimit(int(1e6))

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




a_pass = [False] * N
ans = -1
if (not 2 in a):
  ans = -1
else:
  ans = 0
  next_pos = 0
  crnt_pos = 0
  for i in range(N):
    if a_pass[next_pos]:
      ans = -1
      break
    elif next_pos == (2-1):
      break
    else:
      crnt_pos = next_pos
      next_pos = a[crnt_pos] - 1
      a_pass[crnt_pos] = True
      ans += 1
  else:
    ans = -1
  
print(ans)

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

Solution (Python):
N=int(input())
A=[int(input()) for n in range(N)]
def t(N,A):
    i=1
    for n in range(1,N+1):
        a=A[i-1]
        if a==2:
            return print(n)
        i=a
    return print(-1)
t(N,A)

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

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

a_list = []

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

i = 1
i_set = {i}

while True:
    i = a_list[i - 1]
    if i == 2:
        print(len(i_set))
        break
    elif i in i_set:
        print(-1)
        break
    else:
        i_set.add(i)

Solution (Python):
n = int(input())
light = 1
a = [0]
answer = 0
for _ in range(n):
    a.append(int(input()))
while True:
    light = a[light]
    answer += 1
    if light == 2:
        break
    if light == a[light]:
        answer = -1
        break
    if answer == n:
        answer = -1
        break
print(answer)

Solution (Python):
N=int(input())
A=[(int(input())) for i in range(N)]
count = 0
b=1
for i in range(N+1):
  b = A[b-1]
  count+= 1
  if b==2:
    print(count)
    break
if count == N+1:
  print(-1)

Solution (Python):
N=int(input())
A=[((i+1),int(input())) for i in range(N)]
count = 0
b=1
for i in range(N+1):
  b = A[b-1][1]
  count+= 1
  if b==2:
    print(count)
    break
if count == N+1:
  print(-1)