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=int(input())
    a.append([i+1,A])
c=0
k=1
for i in range(n+1):
    k=a[k-1][1]
    c+=1
    if k==2:
        print(c)
        break
if c==n+1:print(-1)

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

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

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

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

Solution (Python):
"""
    N個のボタン 初期条件としてボタン1が1個光ってる
    ボタンiが光ってる時に押すとiが消えてaiが光る
    i=ai なこともある
    光ってないボタンは押せない
    ボタン2が光ってる状態でおわる
    回数を求める、不可なら-1

    2が返る前に重複したら不可ということ
"""
n = int(input())

a = [int(input())-1 for _ in range(n)]
button, count = 0, 0
dup_check = {0}

while button != 1:
    len_check = len(dup_check)
    count += 1
    button = a[button]
    dup_check.add(button)
    if len_check == len(dup_check):
        count = -1
        break

print(count)

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

Solution (Python):




def main():
    N = int(input())
    A = [int(input())-1 for i in range(N)]

    cur = 0
    for i in range(2*N):
        cur = A[cur]

        if cur == 1:
            print(i+1)
            exit()

    print(-1)

if __name__ == '__main__':
    main()

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

count = 0
botton = 1

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

print("-1")

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

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

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

button = A[0]

for i in range(N):
    ans += 1

    if button == 2:
        print(ans)
        exit()

    else:
        button = A[button-1]
print(-1)

Solution (Python):
N = int(input())
a = [0 for i in range(N)]
for i in range(N):
    a[i] = int(input())
    
visited = [False for i in range(N)]
cur = 0
laps = 0
while True:
    if cur == 1 or visited[cur]:
        break
        
    visited[cur] = True
    next = a[cur] - 1
    laps += 1
    cur = next
    
if cur == 1:
    print(laps)
else:
    print(-1)

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

Solution (Python):
n = int(input())
button_dict = {i:int(input()) for i in range(1, n+1)}

s = 1
counter = 1
serched_set = set([1])

while button_dict[s]!=2:
  s = button_dict[s]
  counter += 1
  if s in serched_set:
    counter = -1
    break
  else:
    serched_set.add(s)

print(counter)

Solution (Python):
buttonNum = int(input())
button = [0] * buttonNum
for i in range(buttonNum):
  button[i] = int(input())
pushedButton = 1
count = 0
checkAble = False
for i in range(buttonNum):
  count += 1
  if button[pushedButton - 1] == 2:
    checkAble = True
    break
  else:
    pushedButton = button[pushedButton - 1]
if checkAble:
  ans = count
else:
  ans = -1
print(ans)

Solution (Python):
from sys import stdin, exit
N = int(stdin.readline().rstrip())
A = [int(stdin.readline().rstrip()) for _ in range(N)]
button = 1
for i in range(N):
    if button == 2:
        print(i)
        exit()
    button = A[button-1]
print(-1)

Solution (Python):
def main():
    n=int(input())
    a=[]
    for i in range(n):
        a.append(int(input()))
    ans = 1
    tmp = a[0]
    while ans <=n:
        if tmp == 2:
            print(ans)
            exit()
        else:
            tmp = a[tmp-1]
            ans += 1
    print(-1)
if __name__ == '__main__':
    main()

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

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

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

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

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

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




sb = 0
ans = 0
while True:
    if sb == 1:
        flag = True
        break
    elif sb == -1:
        flag = False
        break
    tmp = a[sb]
    a[sb] = -1
    sb = tmp
    ans += 1
if flag:
    print(ans)
else:
    print(-1)

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

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

while i <n:
  if a[s-1]==2:
    print(i)
    exit()
  s = a[s-1]
  i +=1

print(-1)

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

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

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

visited = [0]
steps = 0

while steps<N:
  curr_i = visited[-1]
  curr_val = A[curr_i]
  if curr_i == 1:
    print(steps)
    exit()
  else:
    steps+=1
    visited.append(curr_val -1)
print(-1)

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

Solution (Python):
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())

        dic = {}
        for i in range(1,N+1):
            dic[i] = int(input())

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



if __name__ == '__main__':
    AtCoder().main()

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

Solution (Python):
n=int(input())
a={x:int(input()) for x in range(1,n+1)}
i=1
cnt=1
while a[i]!=2: 
    i=a[i]
    cnt+=1
    if cnt>n:
        print('-1')
        exit()
print(cnt)

Solution (Python):
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
 
n = ni()
a = [int(ns()) for i in range(n)]
pushed = {}

next_b = 1
count = 0
for _ in range(0,n):
  if next_b == 2:
    break
  count += 1
  next_b = a[next_b-1]
  if pushed.get(next_b) != None:
    count = -1
    break
  pushed[next_b] = True
  
if count == n:
  count = -1
print(count)

Solution (Python):
import os

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

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

print(-1)

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

Solution (Python):
n = int(input())
ls = [0] + [int(input()) for _ in range(n)]
res = [1]
count = 0
while count < n:
    i = ls[res[-1]]
    res.append(i)
    count += 1
    if i == 2:
        print(count)
        exit()
print(-1)

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

c=1
s=a[0]
while s!=2 and c<n:
    c=c+1
    s=a[s-1]
print(c if c<n else -1)

Solution (Python):
import sys
N = int(input())
a = [int(input()) for i in range(N)]
i = 1
s = 1
while i < N:
  if a[s-1] == 2:
    print(i)
    sys.exit()
  s = a[s-1]
  i += 1
  
print(-1)

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

Solution (Python):
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    

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

lt=0
cnt=0

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

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

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

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

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

a_now = 0
a_pushed = [0]*N

while 1:
    if a_now == 1:
        print(sum(a_pushed)) 
        exit()
    elif a_pushed[a_now] == 1: 
        print(-1)
        exit()
    else:
        a_pushed[a_now] = 1
        a_now = a_list[a_now]

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

iindex = 1
count = 0
for n in range(N):
    if iindex == 2:
        print(count)
        break
    elif count >= N - 1:
        print(-1)
    else:
        count += 1
        iindex = a_list[iindex - 1]

Solution (Python):
N = int(input())
g = [1] + [0] * (N-1)
map = [(i, int(input())-1) for i in range(N)]

ans = 0
current = 0
while g[1] != 1:
    dst = map[current][1]
    if g[dst] == 1:
        ans = -1
        break
    ans += 1
    current = dst
    g[dst] = 1
print(ans)

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

ans = 1
now = A[1]
visited = {}
while (not now in visited) and now != 2:
    ans += 1
    visited[now] = 1
    now = A[now]

if now != 2:
    print(-1)
else:
    print(ans)

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

now = 0
count = 0
while True:
    if now == 1:
        print(count)
        break
    elif count >= n:
        print(-1)
        break
    else:
        count += 1
        now = alist[now]

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())
a = [int(input()) for k in range(N)]
count = 1
tmp = a[a[0]-1]
while count <= N:
    if a[0] == 2:
        print(count)
        exit()
    elif tmp == 2:
        count += 1
        print(count)
        exit()
    else:
        tmp = a[tmp-1]
        count += 1

print(-1)

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

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

a.insert(0, 0)

i = 1
ans = 0
while True:
    if a[i] == 0:
        print(-1)
        exit()
    elif a[i] == 2:
        print(ans + 1)
        exit()
    a[i], i = 0, a[i]
    ans += 1

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
b=a[0]
c=1
ans=-1
for i in range(10**5):
    if b==2:
        ans=c
        break
    b=a[b-1]
    c+=1
print(ans)

Solution (Python):
N = int(input())
A = [0] * 100097
for i in range(N):
  A[i + 1] = int(input())
cnt = 0
now = 1
while not(now == 2 or cnt > 100097):
  now = A[now]
  cnt += 1

if cnt > 100001:
  cnt = -1
print(cnt)

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


M=10**6
b=0
for i in range(M):
  b=a[b]-1
  if b==1:
    break
print(i+1 if b==1 else -1)

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

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

Solution (Python):
import collections
N=int(input())
a=[-1]*(N+1)
checker = collections.defaultdict(int)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
checker[1] = 1
for _ in range(N):
  if pos == 2:
    print(len(checker.values()) - 1)
    exit(0)
  pos = a[pos]
  if pos not in checker:
    checker[pos] = 1
  else:
    break
  

print(- 1)

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

print(- 1)

Solution (Python):
n,*a=map(int,open(0));c,s=0,1
while s-2and c<n:c+=1;s=a[s-1]
print(c*(c<n)or-1)

Solution (Python):
*A,=map(int,open(0));i=1;t=0
while i-2and t<A[0]:t+=1;i=A[i]
print(t*(i==2)or-1)

Solution (Python):
*A,=map(int,open(0));i=1;t=[0]*A[0]+[0]
while i-2and t[i]==0:t[i]=1;i=A[i]
print(-1if i-2else sum(t))

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

Solution (Python):
N = int(input())
A = list(int(input()) for i in range(N))
B = A[0]
cnt = 1
a = False
for i in range(N):
    if B == 2:
        a = True
        print(cnt)
        break
    else:
        B = A[B-1]
        cnt += 1
if a == False:
    print(-1)

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

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

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

ans = 0
c = 0
while c != 1 and c != -1:
    c_old = c
    c = a[c]
    a[c_old] = -1
    ans += 1


if c == -1:
    ans = -1

print(ans)

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

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

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

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

i=1
cnt=-1

for j in range(1,N+1):
    if a[i-1]==2:
        cnt=j
        break
    if a[i-1]==i:
        cnt=-1
        break
    i=a[i-1]

print(cnt)

Solution (Python):

import sys


def ILI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def ISI(): return map(int, sys.stdin.readline().rstrip().split())
def II(): return int(sys.stdin.readline().rstrip())
def ISS(): return sys.stdin.readline().rstrip().split()
def IS(): return sys.stdin.readline().rstrip()

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

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

L = [0] * (n+1)


def b(s):
    
    co = 0
    while True:
        if s == 2:
            return co
        elif L[s] == 1:
            return -1
        else:
            L[s] = 1
            s = A[s]
            co += 1

print(b(1))

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

pushed = set()
cnt = 0
push= 1
while True:
    if push in pushed:
        cnt = -1
        break
    pushed.add(push)
    push = a[push-1]
    cnt += 1
    if push == 2:
        break

print(cnt)

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

x = 0
num = 0
while( num <= N):
    if x == 1:
        print(num)
        break
    elif num == N:
        print(-1)
    num += 1
    x = a[x] - 1

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
a = 0
count = 1
ans = False
for i in range(n):
    if A[a]==2:
        ans = True
        break
    else :
        a = A[a]-1
        count += 1
print(count if ans else '-1')

Solution (Python):
n = int(input())
A =[0] + [int(input()) for _ in range(n)]
q = [1]
cnt = 0
for i in range(1,n+2):
    x = q.pop(0)
    if x ==2:
        print(cnt)
        exit()
    q.append(A[x])
    cnt += 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):
a = [int(input()) for _ in range(int(input()))]
c,s = 1,a[0]
for i in range(len(a)):
    if s ==2:
        print(c)
        break
    c,s = c+1, a[s-1]
else:print(-1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
i = 0
count = 0
while A[i] != 2:
    i = A[i] - 1
    count += 1
    if count >= N:
        print("-1")
        exit()
count += 1
print(count)

Solution (Python):
import sys
import copy
import  math
import bisect
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
li = [ni() - 1 for _ in range(n)]
limit = 100005
next = li[0]
ct = 1
while True:
    if ct > limit:
        ct = -1
        break
    if next == 1:
        break
    else:
        next = li[next]
        ct += 1
print(ct)

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

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

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

buttons = list(map(int,sys.stdin))
light = buttons[1]
for i in range(buttons[0]):
    if light==2:
        print(i+1)
        break
    light= buttons[light]
else:
    print(-1)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

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

count = 0
used = [False]*n


def push(i):
    
    global count
    count += 1
    if (i == 2):
        print(count)
        exit()
    if (used[i-1]):
        print('-1')
        exit()
    used[i-1] = True
    push(a[i-1])


push(a[0])

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

buttons = list(map(int,sys.stdin))
n = buttons[0]
count,light = 1,buttons[1]
while light!=2 and count<n:
    count,light = count+1,buttons[light]
print(count if count<n else -1)

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

n = int(input())
buttons = [int(input()) for n in range(n)]
count,light = 1,buttons[0]
while light != 2 and count<n:
    count,light = count+1,buttons[light-1]
print(count if count<n else -1)

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

n = int(input())
buttons = [int(input())-1 for n in range(n)]
light = 0
count = 0
while light != 1:
    if buttons[light] == -1:
        print(-1)
        break
    num = light
    light = buttons[num]
    buttons[num] = -1
    count += 1
else:
    print(count)

Solution (Python):
n = int(input())
arr = [int(input()) for _ in range(n)]
btns = set()
count = 0
_next = 1
while True:
    count += 1
    btn = arr[_next-1]
    if btn == 2:
        print(count)
        break
    elif btn in btns:
        print(-1)
        break
    else:
        btns.add(btn)
    _next = btn

Solution (Python):


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

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

Solution (Python):
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
i = 0
count = 1
x = [0]*n
while True:
    if a[i] == 2:
        print(count)
        sys.exit()
    if x[i] == 0:
        x[i] = 1
        i = a[i]-1
        count += 1
    else:
        print(-1)
        sys.exit()

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

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



count=1
b=1
cond=0

if a[0]==2:
  cond=1
  count=1
else:
 for j in range(N):
   count=count+1
   b=a[b-1]
   
   if a[b-1] ==2:
     cond=1
     break

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

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)]
f=[0]*N

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

Solution (Python):
import sys

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

count = 0
turn_on = 1
visited = [0 for i in range(N)]
while turn_on != 2:
  if visited[turn_on - 1] == 1:
    print(-1)
    sys.exit()
  visited[turn_on - 1] = 1
  turn_on = a[turn_on - 1]
  count += 1
  
print(count)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
nxt = 1 
cnt = 0
while cnt <= N:
    if A[nxt-1] == 2:
        print(cnt+1)
        exit()
    nxt = A[nxt-1]
    cnt+=1
print(-1)

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

c = 0
n = 1
checked = [False for _ in range(N)]
checked[0] = True
for _ in range(N+1):
    n = A[n-1]
    c += 1
    if n == 2:
        ok = True
        break
    if checked[n-1]:
        ok = False
        break
    checked[n-1] = True
if ok:
    print(c)
else:
    print(-1)

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

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

Solution (Python):


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

result = -1
count = 0
unreached = True
reached_list = {}
i = 1
while unreached:
    if reached_list.setdefault(i, 0) == 1:
        unreached = False
    else:
        count += 1
        next_button = a[i-1]
        if next_button == 2:
            result = count
            break
        else:
            reached_list[i] = 1
            i = next_button

print(result)

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

ans=1
step=0
for i in range(n):
    if l[ans] == 2:
        print(step+1)
        break
    ans = l[ans]
    step+=1
else:
    print('-1')

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

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

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

Solution (Python):


def resolve():
    N = int(input())

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

    b = 1
    ans = 0
    while (True):
        tmp = a[b]
        a[b] = 0
        b = tmp
        ans += 1

        if b == 2:
            print(ans)
            return
        elif b == 0:
            print(-1)
            return

    print(-1)
    return

if __name__ == "__main__":
    resolve()

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

Solution (Python):
N = int(input())
A = []
i = 0
f = True
for j in range(N):
  A.append(int(input()))
for j in range(N):
  i = A[i]-1
  if i == 1:
    print(j+1)
    f = False
    break
if f:
  print(-1)

Solution (Python):
N = int(input().strip())
b = [int(input().strip())-1 for _ in range(N)]
now = 0
ans = 0
for i in range(N):
  now = b[now]
  ans += 1
  if now == 1:
    print(ans)
    exit()
print(-1)

Solution (Python):
from collections import deque
N = int(input())
A = deque([])
for _ in range(N):
    A.append(int(input())-1)
b = 0
cnt = 0
for i in range(N):
    cnt += 1
    b = A[b]
    if b == 1:
        print(cnt)
        break
else:
    print(-1)

Solution (Python):
n = int(input())
array = []
for i in range(n):
    array.append(int(input()))
next_bottun = array[0]
cnt = 0
found = False
while cnt <= n:
    cnt += 1
    if next_bottun == 2:
        found=True
        break
    next_bottun = array[next_bottun-1]
if found:
    print(cnt)
else:
    print(-1)

Solution (Python):
n = int(input())
As = [int(input()) for _ in range(n)]
cnt = 0
current_button = 0
for _ in range(n):
    current_button = As[current_button] - 1
    cnt += 1
    if current_button == 1:
        break
if current_button != 1:
    print(-1)
else:
    print(cnt)

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

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

Solution (Python):
n = int(input())
s = [int(input()) for _ in range(n)]
t = set()
i = 1
count = 0

while(i not in t):
    count += 1
    t.add(i)
    i = s[i-1]
    if i==2:
        print(count)
        break
else:
    print(-1)

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

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

b=a[0]-1
for i in range(n):
  if b==1:
    print(i+1)
    exit()
  else:
    b=a[b]-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):
def main():
    N = int(input())
    A = [0]
    for _ in range(N):
        A.append(int(input()))
    i = 1
    for j in range(N):
        i = A[i]
        if i == 2:
            return j + 1
    return -1

if __name__ == '__main__':
    print(main())

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

d = set()

step = 0
now = 0
flag = False
while True:
    step += 1
    nex = arr[now]
    nex -= 1
    
    if nex == 1:
        flag = True
        break

    if nex not in d:
        d.add(nex)
        now = nex
        continue
    else:
        break

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

Solution (Python):


import sys
sys.setrecursionlimit(1000000)

N = 1
a = []

def sol(x, cnt) : 
    cnt+=1
    if a[x-1] == 2 :
        return cnt
    if cnt > N :
        return -1

    return sol(a[x-1], cnt)


if __name__ == '__main__' : 
    N = int(input())

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

    print( sol(1,0) )

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

def solve():
    pushed = [False] * (n + 1)
    cur = 1
    cnt = 0
    while cur != 2:
        pushed[cur] = True
        cur = b[cur]
        cnt += 1
        if pushed[cur]:
            print(-1)
            return
    print(cnt)

solve()

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

ind = 1
count = 0
while count < N+1:
    ind = button[ind-1]
    count += 1
    if ind == 2:
        break
print(count) if ind == 2 else print(-1)

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

for i in range(10**5+10):
    NOW=A[NOW]-1
    ANS+=1

    if NOW==1:
        print(ANS)
        break

else:
    print(-1)

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

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

Solution (Python):
n = int(input())
L = [int(input()) for _ in range(n)]
next = 1
for i in range(n):
  if next == 2:
    print(i)
    quit()
  next = L[next-1]
print(-1)

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

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

Solution (Python):
n = int(input())
B = {i:int(input()) for i in range(1,n+1)}
count = 0
b = 1
while count <= n:
    b = B[b]
    count +=1
    if b == 2:
        break
else:
    count=-1
print(count)

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):
import sys
nums = int(input())
bottom = list(range(nums))
check = {}
count = 0
a = 0
for i in range(nums):
    bottom[i] = int(input())
    check[i] = 0

while True:

    if bottom[a] == 2:
        count += 1
        break
    if check[a] == 1:
        print("-1")
        sys.exit()
    check[a] = 1
    a = bottom[a] - 1
    count += 1
print(count)

Solution (Python):
N = int(input())
A = [int(input()) - 1 for _ in range(N)]
j = 0
for i in range(N):
    j = A[j]
    if j == 1:
        print(i + 1)
        exit()
print(-1)

Solution (Python):


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

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

print(-1)

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

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

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

Solution (Python):
n=int(input())
A={x: int(input()) for x in range(1,n+1)}

i=0
b=1
while i < n and b != 2:
    b=A[b]
    i+=1

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