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 = [-1]
for i in range(n):
    a.append(int(input()))
cnt = 0
next_ = 1
for i in range(1, n):
    cnt += 1
    if a[next_] == 2:
        print(cnt)
        exit()
    else:
        next_ = a[next_]
print(-1)

Solution (Python):
N = int(input())
A = [0] * N
B = [0] * N
c = 0

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

k = 0
while B[k] == 0 and k != 1:
  B[k] = 1
  k = A[k]
  c += 1

if k == 1:
  print(c)
else:
  print(-1)

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

count = 0
next = 1
for i in range(0, n):
    count += 1
    if As[next] == 2:
        print(count)
        exit(0)
    else:
        next = As[next]

print(-1)

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

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

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

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

    check = [False] * N
    next = 0
    ok = True
    count = 0
    while True:
        check[next] = True
        next = a[next]
        count += 1
        if next == 1:
            break
        if check[next]:
            ok = False
            break

    if ok:
        print(count)
    else:
        print(-1)


if __name__ == "__main__":
    main()

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

Solution (Python):
n = int(input())
D = {}
for i in range(n):
    D[i + 1] = int(input())
ans = 0
button = 1
pushed = set()
while button not in pushed:
    pushed.add(button)
    button = D[button]
    ans += 1
    if button == 2:
        print(ans)
        break
else:
    print(-1)

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

Solution (Python):
import sys
n=int(input())
a=[int(input()) for _ in range(n)]
       
cnt=0
tmp=1

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

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

i = 1
count = 0
Flag = False
while count < N:
    i = a[i-1]
    count += 1
    if i == 2:
        Flag = True
        break

if Flag:
    print(count)
else:
    print(-1)

Solution (Python):
a=int(input())
b=[0]+[int(input()) for i in range(a)]
c=1
d=0
e=[False for i in range(a+1)]
while c!=2:
  e[c]=True
  d+=1
  c=b[c]
  if (e[c]):
    d=-1
    break

print(d)

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

i = 1
count = 0
Flag = False
while count < N:
    i = a[i-1]
    count += 1
    if i == 2:
        Flag = True
        break

if Flag:
    print(count)
else:
    print(-1)

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

Solution (Python):
n = int(input())
a = { (i + 1):int(input()) for i in range(n) }
s = 1
ans = 0
seen = set()
while True:
    if s == 2:
        break
    seen.add(s)
    s = a[s]
    ans += 1
    if s == 1 or s in seen:
        ans = -1
        break
print(ans)

Solution (Python):


n = int(input())

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

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

Solution (Python):
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    i = 1
    c = 0
    v = [0] * (N + 1)
    while v[i] == 0:
        if A[i - 1] == 2:
            print(c + 1)
            return
        v[i] = 1
        i = A[i - 1]
        c += 1
    print(-1)


main()

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

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

Solution (Python):
n = int(input())
a = [int(input())-1 for i in range(n)]
used = [0] * n
br = 0
cnt = 0
while True:
    if used[br]:
        print(-1)
        exit()
    if br == 1:
        print(cnt)
        exit()
    used[br] = 1
    br = a[br]
    cnt += 1

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

p = A[1]
count = 1
while True:
    if count == N+1:
        print('-1')
        break
    if p == 2:
        print(count)
        break
    p = A[p]
    count += 1

Solution (Python):
n = int(input())
l = [0]+[int(input()) for _ in range(n)]
s = set()
ans=0
num=1
while num!=2:
    s.add(num)
    ans+=1
    num=l[num]
    if num in s:
        print(-1)
        exit()
print(ans)

Solution (Python):
from collections import defaultdict

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

def main():
    count = defaultdict(int)

    v = 1
    ans = 0
    while True:
        if count[v] != 0:
            return -1
        count[v] = 1
        v = A[v]
        ans += 1
        if v == 2:
            return ans

print(main())

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

d = {}
on = 1
cnt = 0
while True:
    next = list[on]
    cnt += 1
    if next in d:
        print("-1")
        exit()
    else:
        d[next] = True
    if next == 2:
        print(cnt)
        exit()
    on = next

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
now = a[0]
count = 1
c = 0
for i in range(10**6):
    if now != 2:
        count += 1
        now = a[now - 1]
    else:
        print(count)
        c = 1
        break
if c == 0:
    print(-1)

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
now = a[0]
count = 1
c = 0
for i in range(10**6):
    if now != 2:
        count += 1
        now = a[now - 1]
    else:
        print(count)
        c = 1
        break
if c == 0:
    print(-1)

Solution (Python):
n = int(input())
lst = [0]
lst += [int(input()) for i in range(n)]
cnt = 1
flashed = lst[1]

while True:
    if flashed == 2:
        print(cnt)
        break
    elif cnt <= 10**6:
        flashed = lst[flashed]
        cnt += 1
    else:
        print(-1)
        break

Solution (Python):

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

Solution (Python):
n = int(input())
aiList = [int(input()) for i in range(n)]
count = 0
now = 1
while True:
    count += 1
    if count >= n:
        count = -1
        break
    nextNum = aiList[now - 1]
    if nextNum == 2:
        break
    now = nextNum
print(count)

Solution (Python):
n = int(input())
a = [0]+[int(input()) for _ in range(n)]
cond = True
cnt = 0
p = 1
while cnt <= len(a):
    p = a[p]
    cnt+=1
    if p == 2:
        print(cnt)
        exit()
print(-1)

Solution (Python):
from collections import defaultdict

def main():
    num = int(input())
    ligh_dic = defaultdict(int)
    for i in range(num):
        a = int(input())
        ligh_dic[i + 1] = a

    visit_flg = [0 for i in range(num + 1)]

    ans = 0
    now_num = 1
    visit_flg[now_num] = 1
    while 1:
        ans += 1
        now_num =ligh_dic[now_num]
        if visit_flg[now_num]:
            ans = -1
            break
        visit_flg[now_num] = 1
        if now_num == 2:
            break

    print(ans)




if __name__ == '__main__':
    main()

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

tmp = 0
for i in range(N):
  if i ==0:
    tmp = S[0]
  elif tmp == 2:
    status = i
    break
  else:
    tmp = S[tmp - 1]
print(status)

Solution (Python):
n=int(input())
button=[int(input()) for i in range(n)]
temp=1
ans=0
while True:
  temp=button[temp-1]
  ans+=1
  if temp==2:
    print(ans)
    break
  elif ans>=n:
    print(-1)
    break

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

button = 1
count = 0
while(True):
    button = buttons[button - 1]
    count += 1
    if (button == 2):
        print(count)
        break
    elif(count > N):
        print(-1)
        break

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
count = 0
index = 1
end = 2
if not end in a:
    print(-1)
else :
    while(True):
        count += 1
        index = a[index-1]
        if (index == end):
            print(count)
            break
        elif count > N:
            print(-1)
            break

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

Solution (Python):
Ai = list(int(input()) for j in range(int(input())))
chck_nmbrs = []
cnt = 1
lghtd = 1
while cnt <= len(Ai):
    lghtd = Ai[lghtd - 1]
    if lghtd == 2:
        print(cnt)
        exit()
    cnt += 1
print(-1)

Solution (Python):



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

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

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
data = []
for _ in range(N):
    data.append((int(input())))

pos = 0
cnt = 0
flag = False 
for _ in range(N):
    cnt += 1
    if data[pos] == 2:
        flag = True
        ans = cnt
        break
    pos = data[pos] - 1
    
if flag:
    print(ans)
else:
    print(-1)

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

visited = [0 for _ in range(N)]

cnt = 0
idx = 0
visited[idx] = 1

while True:
    idx = a[idx] - 1
    cnt += 1
    if visited[idx]:
        cnt = -1
        break
    elif idx == 1:
        break
    
    visited[idx] = 1

print(cnt)

Solution (Python):

import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
if not 2 in a:
    print(-1)
    sys.exit()
s = set()
cur = 1
ans = 0
while not cur in s:
    if a[cur-1] == 2:
        print(ans+1)
        sys.exit()
    else:
        s.add(cur)
        cur = a[cur-1]
        ans += 1
print(-1)

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

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

idx = 1
ans = 0
while idx != 2 and visited[idx] == False:
    visited[idx] = True
    idx = A[idx]
    ans += 1
if idx == 2:
    print(ans)
else:
    print(-1)

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

next = 1
cnt = 1
while cnt < N:
    next = b[next]
    if next == 2:
        print(cnt)
        exit()
    cnt += 1
print(-1)

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

n=int(input())
a={}
for i in range(1,n+1):
    a[i]=a.get(i,int(input()))
on=1
cnt=0
flag=False
for i in range(1,n+1):
    on=a[on]
    cnt+=1
    if on==2:
        flag=True
        break
print(cnt if flag else -1)

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    ai=int(input())
    a.append(ai)
finish=False
for i in range(n):
    if a[0]==2:
        print(1)
        finish=True
        break
    if i==0:
        next=a[a[0]-1]
    else:
        next=a[next-1]
    if next==2:
        print(i+2)
        finish=True
        break
if not finish:
    print(-1)

Solution (Python):
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
nexts = {}
for i in range(N):
    nexts[i+1] = n()

n = 1
seen = set()
seen.add(n)

while n != 2:
    n = nexts[n]
    if n in seen:
        print(-1)
        import sys
        sys.exit()
    seen.add(n)

print(len(seen)-1)

Solution (Python):
n=int(input())
al=[int(input()) for _ in range(n)]
ans=1
pd=set([0])
t=1
while True:
    t=al[t-1]
    if t==2:
        print(ans)
        exit()
    if t in pd:
        print(-1)
        exit()
    pd.add(t)
    ans+=1

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

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

phai = 1
cnt = 0
loop_hantei = {1}

for _ in range(N):
    phai = A[phai-1]
    cnt += 1
    if phai in loop_hantei:
        print('-1')
        break
    else:
        loop_hantei.add(phai)
    if phai == 2:
        print(cnt)
        break

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

Solution (Python):

import sys
sys.setrecursionlimit(300000)


def solve(N: int, a: "List[int]"):
    used = [False] * (N + 1)
    ret = -1
    cnt = 1
    a = [0] + a
    tmp = a[1]
    while True:
        if tmp == 2:
            ret = cnt
            break
        if used[tmp]:
            break
        else:
            used[tmp] = True
            cnt += 1
            tmp = a[tmp]
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, a)

if __name__ == '__main__':
    main()

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

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

sta=1
ans=-1
cnt=0
for j in range(n):
    sta=nums[sta-1]
    cnt+=1
    if sta==2:
        ans=cnt
        break
print(ans)

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

def main():
    a = [[int(input()), False] for _ in range(N)]
    idx = 0
    ans = 0
    while True:
        idx = a[idx][0] - 1
        ans += 1
        if idx == 1:
            break
        if a[idx][1]:
            ans = -1
            break
        a[idx][1] = True
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
switch_list = [ int(input())-1 for i in range(n)]
move_list = [0] + [None for  i in range(n-1)]
for i in range(n-1):
    move_list[i+1] = switch_list[move_list[i]]


if move_list.count(1) == 0:
    ans = -1
else:
    ans = move_list.index(1)
print(ans)

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

re = [0 for i in range(N)]
re[0] = 1
i = 0
count = 0

while re[a[i]-1] == 0:
    i = a[i] - 1
    count += 1
    re[i] += 1
    if i == 1:
        print(count)
        exit()

print(-1)

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

liuse = [0]*n
liuse[0] = 1
for i in range(1,n):
  liuse[i] = li[liuse[i-1]-1]
if 2 in liuse:
  print(liuse.index(2))
else:
  print(-1)

Solution (Python):
n = int(input())
l = []
ans = 1
for _ in range(n) :
	l.append(int(input()))
if l[0] == 2 :
	print(1)
else :
    i = l[0] - 1
    while True :
        if l[i] == 2 :
            ans += 1
            break
        i = l[i] - 1
        ans += 1
        if ans == n+1 :
            print(-1)
            exit()
    print(ans)

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

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

lst = [0] * N

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

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

count = 1
fp = lista[0]
if fp == 2:
  print(count)
  exit()

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

Solution (Python):
import bisect
import os
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import sys
import string

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")











N = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for _ in range(N)]

ans = 0
i = 0
for _ in range(N+1):
    i = A[i]-1
    ans += 1
    if i == 1:
        print(ans)
        exit()
print(-1)

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

tmp = 0

for i in range(N):
    if a[tmp] == 2:
        print(i + 1)
        break
    else:
        tmp = a[tmp] - 1
else:
    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())
check = a[0]
for i in range(1, 10**5):
    if check == 2:
        print(i)
        sys.exit()
    else:
        check = a[check-1]
print(-1)

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

vi=[0]*n
vi[0]=1
ans=0
st=l[0]
f=0
while vi[1]==0:
    vi[st-1]+=1
    if vi[st-1]>1:
        f=1
        break
    st=l[st-1]
    ans+=1
if f==0:
    print(ans)
else:
    print(-1)

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

cnt = -1
x = a[0]

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

print(cnt)

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

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

cnt=0
ans=-1
x=1

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

print(ans)

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

lst = list(map(lambda x: x-1, lst))
lst_p = [0]
tmp = 0
flag =True
for i in range(n+1):
    tmp = lst[tmp]
    if tmp == 1:
        print(i+1)
        flag = False
        break
if flag:
    print(-1)

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

light = 1
count = 0

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

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

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

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

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

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

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

can = False
cnt = 0
idx = a[0]-1
while True:
    cnt += 1
    if cnt > N:
        break
    if idx == 1:
        can = True
        break
    idx = a[idx] - 1

if can:
    print(cnt)
else:
    print(-1)

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

x = 1
cnt = 0
dic = {x}
while True:
  x = B[x]
  cnt += 1
  if x == 2:
    break
  if {x} <= dic:
    cnt = -1
    break
  dic.add(x)
print(cnt)

Solution (Python):

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

button = 1
for j in range(n):
    k = a[button - 1]
    if k == 2:
       print('{}'.format(j + 1))
       exit(0)
    button = k

print('-1')

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

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

print(ans)

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


nowon = 0
cnt = 0
while 1:
  nexton = As[nowon]
  As[nowon] = -1
  cnt += 1
  if nexton == 1:
    print(cnt)
    exit()
  if nexton == -1:
    print(-1)
    exit()
  nowon = nexton

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

i = 1
f = a[0]
while i< 10**6:
    
    if f == 2:
        print(i)
        break
    elif i>n+1:
        print(-1)
        break
    else:
        f = a[f-1]
        i+=1

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

print(-1)

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

k = 1
ans = 0
while ans < N+1:
    k = a[k]
    ans += 1
    if k == 2:
        break
if ans > N:
    print(-1)
else:
    print(ans)

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

button_num = 1
push_cnt = 0
while True:
    button_num = a_array[button_num - 1]
    push_cnt += 1
    if button_num == 2:
        print(push_cnt)
        exit()
    if push_cnt == len(a_array):
        print(-1)
        exit()

Solution (Python):
import functools


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    a = list(map(lambda x: int(x), inputs))

    count = 0
    light = 1
    N = len(a)
    while 1:
        next_light = a[light - 1]
        count += 1
        if next_light == 2:
            return count
        elif count > N:
            return -1
        light = next_light


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)

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

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

print(ret)

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

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

print(-1)

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

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

Solution (Python):
N=int(input())
A=[]
for i in range(N):
    a=int(input())
    a-=1
    A.append(a)
ans=0
reached=[0]*N
i=0
while(True):
    i=A[i]
    ans+=1
    if i==1:
        print(ans)
        quit()
    if reached[i]:
        print(-1)
        quit()
    reached[i]=1

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

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

next_num = A[0]
ans = 0


for i in range(N):
  ans += 1
  if next_num == 2:
    break
  else:
    if i != N-1:
      next_num = A[next_num-1]
    else:
      ans = -1
      break

print(ans)

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

button = []

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

count = 1
tmp = button[0]
while 1:
    if tmp == 2:
        print(count)
        exit()
    if count >= N:
        break
    tmp = button[tmp - 1]
    count += 1

print(-1)

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


light = 0
counter = 0

while True:
    if light == 1:
        break
    if counter > N:
        counter = -1
        break
    
    push = a[light]
    light = push
    counter += 1

print(counter)

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

a=[]

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

x = 0
 
for i in range(N):
    if a[x] == 2:
        print(i+1)
        sys.exit()
    else:
        x = a[x] - 1

print(-1)

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

tmp = 1

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

print(-1)

Solution (Python):
N = int(input())
button = []
for i in range(0, N):
	button.append(int(input())-1)
memo = []
for i in range(0, N):
	memo.append(False)
cnt = 0
now = 0
while not memo[now]:
	memo[now] = True
	if now == 1: break
	now = button[now]
	cnt += 1
if memo[1]: print(cnt)
else: print(-1)

Solution (Python):


n = int(input())

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

ans = 0
tmp = 0

for i in range(10**5):
    tmp = a[tmp][1] - 1

    if tmp == 1:
        ans += i + 1
        break


print(ans if ans >= 1 else -1)

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

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

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

idx = 0
cnt = 0
while True:
    cnt += 1
    idx = a[idx] - 1
    if idx+1 == 2:
        break
    
    if cnt > len(a):
        cnt = -1
        break

print(cnt)

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

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

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

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

check_li = [1]
s = 1
cnt = 0

while True:
    cnt += 1
    s = li[s-1]
    if cnt >= n:
        print(-1)
        break
    if s == 2:
        print(cnt)
        break

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

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

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

Solution (Python):
import sys

N = list(map(int, sys.stdin))
bot = N[1]

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

Solution (Python):
import sys

def process(a):
    tmp = a[1]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp]
        cnt += 1
    return -1

if __name__ == '__main__':
    
    a = list(map(int, sys.stdin))
    print(process(a))

Solution (Python):


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

start = 0
count = 0

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

Solution (Python):
def process(a):
    tmp = a[0]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1

if __name__ == '__main__':
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print(process(a))

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

temp = 1
pushed = set()
clear = False
while temp not in pushed:
    pushed.add(temp)
    temp = a[temp-1]
    if temp == 2:
        clear = True
        break

if clear:   print(len(pushed))
else:   print(-1)

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

Solution (Python):
import sys

def search_next(nextbutton : int) -> int:
    nextbutton = a.index(nextbutton) +1
    return nextbutton

n = int(input())
a = [int(input()) for i in range(n)]
nextbutton = 1
count = 0



for _ in range(n):
    if nextbutton == 2:
        print(count)
        
        
        sys.exit()
    else:
        nextbutton = a[nextbutton -1]
        
        
        count += 1
print(-1)

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

buttons = []

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

index = 1
cnt = 0

for i in range(n):
    index = buttons[index-1]
    cnt += 1

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

print(-1)

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

while(i!=2 and cnt<=10**5):
    i=a[i-1]
    cnt+=1

if cnt<=10**5:
    print(cnt)
else:
    print(-1)

Solution (Python):
n = int(input())
dat_a = [-1] * (n + 1)
for i in range(n):
    a = int(input())
    dat_a[i + 1] = a
cur = 1
f = False
for i in range(n):
    if cur == 2:
        f = True
        break
    cur = dat_a[cur]
if f:
    print(i)
else:
    print("-1")

Solution (Python):
N=int(input())
A=[int(input())-1 for _ in range(N)]
B=[]
i=0
cnt=0
while(cnt<=10**5):
    cnt+=1
    i=A[i]
    if i==1:
        print(cnt)
        break
else:
    print(-1)

Solution (Python):
N = int(input())
button_list = [int(input()) for i in range(N)]
button =  1
count = 0

while True:
    button = button_list[button-1]
    count += 1
    if button == 2:
        break
    if count == N:
        print(-1)
        exit()
print(count)