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):
import sys
n = int(input())
a = [int(input()) for i in range(n)]
x = 0
cnt = 0
l = [False] * n
while x != 1:
    l[x] = True
    x = a[x]-1
    cnt += 1
    if l[x]:
        print(-1)
        sys.exit()
print(cnt)

Solution (Python):
N = int(input())
next = []
for i in range(N):
  next.append(int(input()) - 1)
now = 0
count = 0
judge = False
for i in range(N):
  now = next[now]
  count += 1
  if now == 1: 
    judge = True
    print(count)
    break
if not judge:
  print(-1)

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

visited={1,}
old=1
count=0
while 1:
  new = a[old-1]
  count += 1
  if new in visited:
    print(-1)
    exit()
  if new == 2:
    print(count)
    exit()
  old = new
  visited.add(new)

Solution (Python):
n=int(input())
a=[0]*n
for i in range(n):
    a[i]=int(input())
f=1
count=0
while f!=2 and count<n+2:
    f=a[f-1]
    count=count+1
if count>n:
    count=-1
print(count)

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

button = [0] * N
button[0] = 1
next_button = 1
i = 0

while True:
  button[next_button - 1] = 2
  next_button = A[next_button - 1]
  if button[next_button - 1] == 2:
    print("-1")
    break
  elif next_button == 2:
    i += 1
    print(i)
    break
  else:
    button[next_button - 1] = 1
    i += 1

Solution (Python):
n=int(input())
a=[int(input())-1 for _ in range(n)]
i=0
ct=0
while i!=1:
  if a[i]==-1:
    print("-1")
    break
  k=i
  i=a[i]
  a[k]=-1
  ct+=1
else:
  print(ct)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = [0] + [int(input()) for _ in range(N)]
    pushed = [False] * (N + 1)
    pushed[0] = True
    now = 1
    cnt = 0
    while now != 2:
        now = A[now]
        cnt += 1
        if pushed[now]:
            print(-1)
            return
        pushed[now] = True
    print(cnt)



if __name__ == "__main__":
    main()

Solution (Python):









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

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

if (ans == n):
    print(-1)
else:
    print(ans+1)

Solution (Python):
N = int(input())
ls = []
ans = -1
for i in range(N):
    ls.append(int(input()))
on = 0
for i in range(N):
    on = ls[on]-1
    if on == 1:
        ans = i +1
        break
print(ans)

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

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

nextint = A[0]

for i in range(1, N + 1):
    if nextint == 2:
        print(i)
        exit()

    nextint = A[nextint - 1]

print(-1)

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

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

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

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

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

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

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

State = [1]
for _ in range(N-1):
    State.append(0)

i=0
count=1
while 1:
    i = L[i]-1
    if State[i]==1:
        print(-1)
        exit()

    if i+1 == 2:
        print(count)
        exit()

    State[i]=1
    count+=1

Solution (Python):
def resolve():
    N = int(input())
    A = []
    for _ in range(N):
        a = int(input())
        A.append(a)
    count = 0
    num = 1
    for i in range(N):
        num = A[num-1]
        count += 1
        if num == 2:
            print(count)
            exit()
    print("-1")
resolve()

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

Solution (Python):


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

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

print(-1)

Solution (Python):


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

lighted = [0] 
ans = 0 

for i in range(n):
    u = lighted[-1]
    v = a[u]-1
    ans += 1
    if v == 1:
        print(ans)
        exit()
    lighted.append(v)

print(-1)

Solution (Python):
n = int(input())
f = [int(input()) for _ in range(n)]
f = [0] + f
vis = [0] * (n + 1)
s , flag , ans = 1 , 0 , 0
vis[s] = 1
while f[s] != 2:
	if vis[f[s]] == 1:
		flag = 1
		break
	s = f[s]
	vis[s] = 1
	ans+=1

if (flag):
	print(-1)
else:
	print(ans+1)

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

prev = []
next = 0
count = 0
while(count < N):
    next = int(A[next])-1
    count += 1
    if(next == 1):
        print(count)
        break
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b = a[0]
c = 1
d = set()
while b != 2:
    b = a[b - 1]
    if b in d:
        print(-1)
        break
    else:
        d.add(b)
        c += 1
else:
    print(c)

Solution (Python):
n=int(input())
li=[int(input()) for i in range(n)]
basyo=1
res=-1
for i in range(2*n):
    basyo=li[basyo-1]
    if basyo==2:
        res=i+1
        break
print(res)

Solution (Python):
N = int(input())
a_list = [int(input())-1 for i in range(N)]
next_i = 0
done_flag = False
for i in range(N):
    next_i = a_list[next_i]
    if  next_i == 1:
        done_flag = True
        break

if done_flag:
    print(i + 1)
else:
    print(-1)

Solution (Python):
N = int(input())
A = [0]
ans = 0
i = 1
q = [False for i in range(N + 1)]
for i in range(N):
    A.append(int(input()))
i = 1
while q[A[i]] == False:
    q[A[i]] = True
    ans += 1
    i = A[i]
    if i == 2:
        break
if i == 2:
    print(ans)
else:
    print(-1)

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


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

  next_ = A[0]
  cnt = 1
  lighten = set([0])
  while True:
    if next_ == 1:
      print(cnt)
      return
    if next_ in lighten:
      break
    lighten.add(next_)
    next_ = A[next_]
    cnt += 1

  print(-1)


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

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

cur = 1
for i in range(N):
    cur = buttons[cur-1]

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

print(-1)

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

for i in range(N):
    id = int(input()) - 1
    a[id].add(i)

step = -1

now = a[1]
next = set()
for i in range(N-1):
    
    if 0 in now:
        step = i+1
        break
    for j in now:
        next = next.union(a[j])
    now = next
    next = set()

print(step)

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

for i in range(N):
    id = int(input()) - 1
    a[id].add(i)

step = -1

now = a[1]
next = set()
for i in range(N-1):
    
    if 0 in now:
        step = i+1
        break
    for j in now:
        next = next.union(a[j])
    now = next
    next = set()

print(step)

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

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
num=1
res=False
time=1
for j in range(n-1):
    if a[num-1]==2:
        res=True
        time+=j
        break
    else:
        num=a[num-1]

if res:
    print(time)
else:
    print(-1)

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

i = 0

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

print(-1)

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

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

Solution (Python):
N = int(input())
l = [(int(input())-1) for _ in range(N)]
flag = [0]*N
flag[0] = 1
flag[1] = 2
cnt = 0
cur_b = 0
while True:
    cnt += 1
    next_b = l[cur_b]
    if flag[next_b] == 1:
        print(-1)
        break
    elif flag[next_b] == 2:
        print(cnt)
        break
    else:
        flag[next_b] = 1
        cur_b = next_b
else:print(-1)

Solution (Python):
N = int(input())
l = [(int(input())-1) for _ in range(N)]
flag = [0]*N
flag[0] = 1
flag[1] = 2
cnt = 0
cur_b = 0
for i in range(N):
    cnt += 1
    next_b = l[cur_b]
    if flag[next_b] == 1:
        print(-1)
        break
    elif flag[next_b] == 2:
        print(cnt)
        break
    else:
        flag[next_b] == 1
        cur_b = next_b
else:print(-1)

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

bn, cnt = 1, 0
for _ in range(n):
    if bn == 2:
        print(cnt)
        exit()
    else:
        bn = a[bn-1]
        cnt += 1

print('-1')

Solution (Python):
N = (int(input()))
A = [0]+[int(input()) for T in range(0,N)]
Now = 1
Count = 0
Flag  = False
for T in range(0,N):
    Next = A[Now]
    Count += 1
    if Next==2:
        Flag = True
        break
    else:
        Now = Next
if Flag:
    print(Count)
else:
    print(-1)

Solution (Python):
n = int(input())
an = [int(input()) for num in range(n)]

answer = False
count = 1
index = 1
while(True):
  if count > len(an):
    break
  if an[index-1] == 2:
    answer = True
    break
  index = an[index-1]
  count += 1

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

Solution (Python):
N=int(input())
A=[int(input()) for _ in range(N)]
A.insert(0,0)
i=1
count=0
while True:
  if count>N:
    print(-1)
    break
  lamp=A[i]
  count+=1
  if lamp==2:
    print(count)
    break
  i=lamp

Solution (Python):
n = int(input())
arr = []
for i in range(0 , n):
	arr.append(int(input()))
gp =[]
p=0
for i in range(0 ,n):
	arr[i]=arr[i]-1
cnt =0
vis =[0 for i in range(0 , n+1)]
while True:
	if p==1:
		print (cnt)
		break
	if cnt>=n:
		print(-1)
		break
	cnt = cnt+1
	p = arr[p]

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

a0=a[0]
cnt=0
f=True

if a0==2:
  cnt=1
  f=False

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

if cnt==n:
  cnt=-1

print(cnt)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    A = list(int(s) - 1 for s in read().split())

    ans = 0
    x = 0
    lit = [False] * N
    lit[0] = True
    while True:
        x = A[x]
        ans += 1
        if lit[x] or x == 1:
            break
        lit[x] = True

    if x != 1:
        ans = -1

    print(ans)

    return


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
c = 0
l = [True]*(n+1)
a = [0]
idx = 1
for i in range(n):
  a.append(int(input()))

while True:
  if idx == 2:
    print(c)
    break
  else:
    if l[idx]:
      c += 1
      l[idx] = False
      idx = a[idx]
    else:
      print(-1)
      break

Solution (Python):
import sys
n=int(input())
a=list(map(int,sys.stdin.read().split()))
check_a=[False for _ in range(n)]
c=0
i=0
while True:
  if i == 1:
    print(c)
    exit()
  nex=a[i]-1
  if check_a[i]:
    break
  check_a[i]=True
  i=nex
  c+=1
print(-1)

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

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

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

i, count = 0, 0
success = False
for _ in range(N):
    i = a[i]
    count += 1

    if i == 1:
        success = True
        break

if not success:
    count = -1

print(count)

Solution (Python):
N = int(input())
used = [False]*(N+1)
used[1] = True
a = [None]*(N+1)
for i in range(1,N+1):
    x = int(input())
    a[i] = x
x = a[1]
cnt = 1
while True:
    if used[x] == True:
        print(-1)
        break
    if x == 2:
        print(cnt)
        break
    used[x] = True
    x = a[x]
    cnt += 1

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

i=0
for j in range(n):
  nbtn=a[i]
  i=nbtn-1
  if i==1:
    print(j+1)
    sys.exit()
print(-1)

Solution (Python):

import sys
n = int(input())
a = []
for i in range(n):
    j = int(input())
    a.append(j)
    
point = a[0]
cnt = 1
for i in range(n):
    if point == 2:
        print(cnt)
        sys.exit()
    point = a[point-1]
    cnt +=1
    
print("-1")

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

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

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

btn=1
cnt=0

while True:
  btn=a[btn-1]
  cnt+=1
  if btn==2:
    print(cnt)
    break
  if cnt>N:
    print(-1)
    break

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log,gcd
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
'''


 
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
'''
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





n=ii()
arr=[ii() for _ in range(n)]

chk=False
tmp=0
ans=0
for _ in range(n):
    if chk==False:
        ans+=1
    p=arr[tmp]
    if p==2:
        chk=True
    
    tmp=p-1

if chk:
    print(ans)
else:
    print(-1)

Solution (Python):
import sys
from collections import deque, Counter
def I(): return int(sys.stdin.readline().rstrip())

if __name__=='__main__':
    n = I()
    l = []
    for _ in range(n):
        l.append(I())
    c = [0]*n
    now = 0
    c[now] = 1
    for i in range(1,n+1):
        now = l[now]-1
        c[now] += 1
        if now==1:
            print(i)
            exit()
        if c[now]==2:
            print(-1)
            exit()
    exit()

Solution (Python):
import sys
from collections import deque, Counter
def I(): return int(sys.stdin.readline().rstrip())

if __name__=='__main__':
    n = I()
    l = []
    for _ in range(n):
        l.append(I())
    c = Counter()
    now = 0
    for i in range(1,n+1):
        now = l[now]-1
        c[now] += 1
        if now==1:
            print(i)
            exit()
        if c[now]==2:
            print(-1)
            exit()
    exit()

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

k = 1
ans = 0
m = 0
while k != 2 and m < n:
    k = lis[k - 1]
    m += 1
if k == 2:
    print(m)
else:
    print("-1")

Solution (Python):




import numpy as np

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return np.array(x)

N = int(input())

a = int_mtx(N)
visit = [0]*(N+1)
now = 1
visit[now] = 1

ans = 0

for i in range(0,N):
    ans += 1
    now = a[now-1][0]
    if now == 2:
        print(ans)
        exit()
    if visit[now] == 1:
        print(-1)
        exit()
    visit[now] = 1

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
Pushed = [False]*N
Pushed[0] = True
i = 0
cnt = 0
while True:
    i = A[i] - 1
    cnt += 1
    if Pushed[i]:
        print(-1)
        break
    Pushed[i] = True
    if i == 1:
        print(cnt)
        break

Solution (Python):


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

i = 1
ans = 0
flg = False
while A[i] != 0 and not flg:
    if i == 2:
        flg = True
    else:
        ans += 1
        tmp = i
        i = A[i]
        A[tmp] = 0

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

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
seen = set()
p = 1
while True:
    if not p in seen:
        seen.add(p)
        if p == 2:
            print(len(seen) - 1)
            break
        p = A[p-1]
    else:
        print(-1)
        break

Solution (Python):
from sys import exit

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

visited = [False] * n
res = 0
button = 0
while True:
    visited[button] = True
    button = a[button] - 1
    res += 1

    if visited[button]:
        print(-1)
        exit()
    
    if button == 1:
        print(res)
        exit()

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

print(ans)

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

i = 0
m = 1
while(1):
    if a[i] == 2:
        ans = m
        break

    if m >= N:
        ans = -1
        break

    i = a[i] - 1 
    m += 1

print(ans)

Solution (Python):
def main():
    N = int(input())
    buttons = {}
    for i in range(N):
        buttons.update({i+1:int(input())})
    
    p = 1
    ans = 0
    while ans <= N:
        p = buttons[p]
        ans += 1
        if p == 2:
            print(ans)
            return
    print(-1)

if __name__ == '__main__':
    main()

Solution (Python):
Row = int(input())
List = []
for i in range (Row):
  List.append(int(input()))
s = 1
res = 0
flag = False
for i in range(Row):
  s = List[s-1]
  res += 1
  if s == 2:
    flag=True
    break
if not flag:
  print(-1)
else:
  print(res)

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

i = 0
c = 0
flag = 0
for _ in range(N):
    i = a[i]
    c += 1
    if i == 1:
        flag = 1 
        break

ans = c if flag else -1
print(ans)

Solution (Python):
def resolve():
    N = int(input())
    A = [int(input()) - 1 for _ in range(N)]
    flgs = [False for _ in range(N)]
    flgs[0] = True
    i = A[0]
    flgs[i] = True
    cnt = 1
    while not flgs[1]:
        i = A[i]
        if flgs[i]:
            print(-1)
            return
        else:
            flgs[i] = True
            cnt += 1
    else:
        print(cnt)


resolve()

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *a = map(int, read().split())
    r = 0
    x = 1
    while x != 2:
        r += 1
        x = a[x - 1]
        if x == 1 or r >= n:
            r = -1
            break
    print(r)

if __name__ == '__main__':
    main()

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

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

now = 0
cnt = 0
for i in range(n):
    if now == 1:
        print(cnt)
        exit()
    cnt += 1
    now = A[now] - 1

print(-1)

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

time = 0
push = 0
while True:
    time += 1
    if bL[push] == 2:
        print(time)
        break
    if time == N:
        print("-1")
        break
    push = bL[push]-1

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

time = 1
push = 0
while True:
    
    if bL[push] == 2:
        print(time)
        break
    if time == N:
        print("-1")
        break
    
    push = bL[push]-1
    
    time += 1

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

j = 0
pointer = 0
b = False
for i in range(n):
    j += 1
    if a[pointer] == 1:
        b = True
        break
    else:
        pointer = a[pointer]

print(j) if b == True else print(-1)

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

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

c = 0
i = 0
while True:
    i = a[i]  - 1
    c += 1
    if i == 1:
        print(c)
        break
    if c > n:
        print(-1)
        break

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

while i != 2:
    if i in s:
        ans = -1
        break
    else:
        s.add(i)
        i, ans = a[i-1], ans+1
print(ans)

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

visited = set([])
cur = 0
ok = False

while True:
    if not (cur in visited):
        visited.add(cur)
    else:
        break
    cur = A[cur] - 1
    if cur == 1:
        ok = True
        break
if ok:
    print(len(visited))
else:
    print(-1)

Solution (Python):

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

cnt = 0
ps = 1

for _ in range(1, n + 1):
    ps = a[ps]
    cnt += 1

    if ps == 2:
        print(cnt)
        break
else:
    print(-1)

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

num = -1
crr_btn = 1
for i in range(1, n + 1):
    if nxt_btn[crr_btn] == 2:
        num = i
        break
    crr_btn = nxt_btn[crr_btn]
print(num)

Solution (Python):

n, *a = map(int, open(0).read().split())
b = [1] * n
i = 0
c = 0
while True:
    if b[i] < 1:
        break
    if i == 1:
        print(c)
        exit()
    b[i] = 0
    i = a[i] - 1
    c += 1
print(-1)

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

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

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    s = []
    ans = 0
    now = 1
    for i in range(n):
        s.append(int(input()))
    for i in s:
        now = s[now-1]
        ans += 1
        if now == 2:
            print(ans)
            break
    else:
        print(-1)

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

count =0
x = 0
while x != 1 and count < 100000:
  count +=1
  x = list1[x]
  
if count < 100000:
  print(count)
else:
  print(-1)

Solution (Python):
n = int(input())
a_list = [int(input()) - 1 for _ in range(n)]
s = set()
i = 0
ans = -1
while i not in s:
    if i == 1:
        ans = len(s)
        break
    s.add(i)
    i = a_list[i]
print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = [None]*n
for i in range(n):
    a[i] = int(input())-1
s = set()
c = 0
while True:

    if c in s:
        ans = -1
        break
    s.add(c)
    c = a[c]
    if c==1:
        ans = len(s)
        break
print(ans)

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

visited = set()

cur = 0
visited.add(0)
ans = 0
while cur != 1:
    if a[cur] not in visited:
        visited.add(a[cur])
        cur = a[cur]
        ans += 1
    else:
        ans = -1
        break

print(ans)

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

nodes = []

for i in range(n):
    p = input()
    q = int(p)
    nodes.append(q)

answer = -1
m = []

x = nodes[0]
m.append(x)
for i in range(n-1):
    w = nodes[x-1]
    m.append(w)
    x = w

for i in range(len(m)):
    if m[i] == 2:
        answer = i + 1
        break

print(answer)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
cnt = 0
btn = 1
while(True):
    btn = A[btn-1]
    cnt += 1
    if btn == 2:
        print(cnt)
        break
    if cnt > 10**6:
        print(-1)
        break

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

Solution (Python):
n = int(input())
A = [int(input())-1 for _ in range(n)]
a = A[0]
i = 1
aset = set()
while a != 1:
    a = A[a]
    if not a in aset:
        aset.add(a)
        i += 1
    else:
        print(-1)
        break
else:
    print(i)

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

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

has_pushed_button = set()
pushed_button_num = 1
count = 0
can_push_2 = False
while not pushed_button_num in has_pushed_button:
    has_pushed_button.add(pushed_button_num)
    pushed_button_num = a_list[pushed_button_num - 1]
    count += 1
    if pushed_button_num == 2:
        can_push_2 = True
        break

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

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

pushed = set()

now = 0
ans = 0
while True:
    if now in pushed:
        ans = -1
        break
    pushed.add(now)
    ans += 1
    now = la[now]
    if now == 1:
        break
print(ans)

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

cnt = 0
num = 1

for i in range(n):
    num = A[num - 1]
    cnt += 1
    if num == 2:
        print(cnt)
        exit()
print(-1)

Solution (Python):
n,*a=map(int,open(0).read().split())
i=ans=1
while a[i-1]!=2 and ans<=n:
    ans+=1
    i=a[i-1]
print(ans if ans<=n else -1)

Solution (Python):


N = int(input())

pushed = []
button = [0] * (N + 1)

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

push_button = 1
for _ in range(1, N+1):
    if push_button == 2:
        print(len(pushed))
        exit(0)
    else:
        pushed.append(push_button)
        push_button = button[push_button]

print("-1")

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

A=[0]*(N+1)
for k in range(1,N+1):
    A[k]=int(input())

T=[False]*(N+1)
x=1
I=0
while not T[x] and x!=2:
    T[x]=True
    I+=1
    x=A[x]
    
if x==2:
    print(I)
else:
    print(-1)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
button=[False]*(n+1)
button[1]=True
a={}
for i in range(1,n+1):
    ai=int(input())
    a[i]=ai
tmp=1
pushed={}
ans=0
while not button[2]:
    button[tmp]=False
    if tmp in pushed:
        print(-1)
        exit()
    pushed[tmp]=1
    tmp=a[tmp]
    button[tmp]=True
    ans+=1
print(ans)

Solution (Python):
na = 0
a = []
b = []
n = int(input())

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

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

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

v = [False]*n
v[0] = True

ans = 0
tmp = 0

while tmp != 1:
    ans += 1
    tmp = a[tmp]
    if v[tmp]:
        print(-1)
        exit()
    else:
        v[tmp] = not v[tmp]

print(ans)

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

A = []

for i in range(N):
  a = int(input())
  A.append(a-1)
  
seen = set()

pos = 0
seen.add(0)
count = 0
while pos != 1:
  pos = A[pos]
  if pos in seen:
    count = -1
    break
  else:
    seen.add(pos)
  count += 1
  
print(count)

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

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

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

light = 0
visited = [False] * N
count = 0
while True:
    if light == 1:
        break

    if visited[light]:
        print(-1)
        exit(0)

    visited[light] = True
    light = a[light]
    count += 1

print(count)

Solution (Python):
import sys

input = sys.stdin.readline

def main():
    ans = 1
    n = int(input())
    a = [int(input()) for _ in range(n)]
    c = [False]*n
    now = a[0]
    c[0] = True
    while now != 2:
        now = a[now-1]
        if c[now-1] == True:
            ans = -1
            break
        ans += 1
        c[now-1] = True
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [0] * (n + 1)

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

cnt = 0
light = 1
push = [0] * (n+1)

for i in range(1, len(a) + 1):
    if push[light] == 0:
        push[light] = 1
        light = a[light]
        cnt += 1
        if light == 2:
            print(cnt)
            exit()
    else:
        print(-1)
        exit()