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())
al = [[] for _ in range(N+1)]
for i in range(1,N+1):
    al[i] = int(input())



n = 1
cnt = 1
visited = [0 for _ in range(N+1)]
visited[1] = 1
while al[n] != 2:
    cnt += 1
    n = al[n]
    if visited[n] == 1:
        print(-1)
        exit()
    else:
        visited[n] = 1
print(cnt)

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

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

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

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

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

ans = 0

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

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

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

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




def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


def main():
	n=II()
	A=[0]+[II() for _ in range(n)]

	a=1

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

	print(ans)









if __name__ == "__main__":
	main()

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

Solution (Python):
n = int(input())
dic = {}
push = [True] * n
for i in range(1, n+1):
  a = int(input())
  dic[i] = a
i = 1
cnt = 1
push[0] = False
while True:
  if dic[i] == 2:
    break
  else:
    k = dic[i]
    if push[k-1]:
      i = k
      cnt += 1
      push[k-1] = False
    else:
      cnt = -1
      break
print(cnt)

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

Solution (Python):

def main():
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    now = 1
    check = [0]*N
    ans = 0
    while(1):
    
        if now == 2:
            break
        if check[now-1] == 1:
            print(-1)
            return
        else:
            check[now-1] = 1
        now = A[now-1]
        ans += 1
        
    print(ans)
    return
        
        
if __name__ == '__main__':
    main()

Solution (Python):

def main():
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    now = 1
    check = [0]*N
    ans = 0
    
    for k in range(N):
        if now == 2:
            print(ans)
            return
        
        
        
        else:
            check[now-1] = 1
        now = A[now-1]
        ans += 1
        
    print(-1)
    return
        
        
if __name__ == '__main__':
    main()

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

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

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

b = [True]*n
btn = 1
ans = 0
while b[btn - 1]:
    ans += 1
    b[btn - 1] = False
    btn = A[btn - 1]
    if btn == 2:
        b[1] = False
        print(ans)
        break
if b[1]:
    print(-1)

Solution (Python):
def readinput():
    n=int(input())
    a=[0]
    for _ in range(n):
        a.append(int(input()))
    return n,a

def main(n,a):
    button=[0]*(n+1)
    button[1]=1
    i=1
    count=0
    while(True):
        i=a[i]
        count+=1
        if i==2:
            return count
        if button[i]>0:
            return -1
        button[i]+=1

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)

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

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

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

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

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

while count <= N:
    if num != 2:
        num = a[num-1]
        count += 1
    else:
        print(count)
        exit()
print(-1)

Solution (Python):
n=int(input())
lst=[]
for i in range(n):
    lst.append(int(input())-1)
ans=1
if lst[0]==1:
    print(ans)
    exit()
else:
    button=lst[0]
    lst[0]=None

while True:
    p=button
    button=lst[button]
    ans+=1
    if button==1:
        print(ans)
        break
    elif button==None:
        print(-1)
        break
    lst[p]=None

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

button_dict = dict()

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

pushed_button_set = set()
i = 1
while 1:
    if i in pushed_button_set:
        print(-1)
        break
    elif i == 2:
        print(len(pushed_button_set))
        break
    else:
        pushed_button_set.add(i)
        i = button_dict[i]

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

if flag:
    print(-1)

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

for n in range(N):
  if l == 2:
    print(ans)
    exit()
  else:
    l = A[l-1]
    ans+=1
    
print(-1)

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

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

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

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

done = {}
ans = 0
now = A[1]
while True:
    ans += 1
    if now == 2:
        print(ans)
        exit(0)
    if done.get(now) == None:
        done[now] = True
        now = A[now]
    else:
        break
print(-1)

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


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

  set_ = set()
  set_.add(0)
  cur = 0
  next_ = 0
  cnt = 0

  while True:
    
    cnt += 1
    cur = next_
    next_ = A[cur] - 1
    if next_ == 1:
      print(cnt)
      break
    if next_ in set_:
      print(-1)
      break
    set_.add(next_)


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

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

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

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

print(count)

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

next = 0
count = 0
for i in range(len(lst)):
    next = lst[next]
    count+=1
    if next == 1:
        print(count)
        sys.exit()
print(-1)

Solution (Python):
n = int(input())
li = [int(input()) for i in range(n)]
if 2 not in li:
  print(-1)
else:
  ans = 0
  st = 0
  while st != 1:
    if (st == 0 and ans > 0) or ans > n:
      ans = -1
      break
    st = li[st]-1
    ans += 1
  print(ans)

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

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

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

Solution (Python):
import numpy as np
import sys
sys.setrecursionlimit(500000)


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



def button(i,n):
    n_new = a[n-1]
    
    if i == N:
        if n != 2:
            print(-1)
            return
    if n_new == 2:
        print(i)
        return 
    else:
        return button(i+1,n_new)

button(1,1)

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

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

used = set()

i = 0
ans = 0
while True:
    a = A[i]

    if a == 2:
        print(ans)
        break

    if a in used:
        print(-1)
        break

    used.add(i)
    i = a
    ans += 1

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7






N = getN()
A = getArray(N)
for i in range(N):
    A[i] -= 1
ignore = [0] * N
ignore[0] = 1

now = 0
cnt = 0
while True:
    now = A[now]
    cnt += 1
    if ignore[now] == 1:
        print(-1)
        break
    ignore[now] = 1
    if now == 1:
        print(cnt)
        break

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


idx = 0
visited = set()
count = 0
flag = False

for i in range(n):
    next_idx = A[idx]
    next_idx -= 1
    visited.add(idx)

    if idx == 1:
        flag = True
        break
    elif next_idx in visited:
        break
    count += 1
    idx = next_idx

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

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

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

Solution (Python):
N = int(input())
a = [int(input()) for _ in range(N)]
s = [0] * N
c = 0
OK = False
n = 0
while True:
    c += 1
    n = a[n] - 1
    if n == 1:
        OK = True
        break
    if s[n] == 1:
        break
    s[n] = 1
if OK:
    print(c)
else:
    print(-1)

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

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

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

while o != 2:
    if len(t) > n:
        print(-1)
        exit(0)
    t.append(o)
    o = a[o-1]

print(len(t))

Solution (Python):
N = int(input())
a_line = []
a_con = []
flag = True

for _ in range(N):
  a_line.append(int(input()))
  a_con.append(0)
  
ans = 1
place = 0

for i in range(N):
  if a_con[place] == 1:
    flag = False
    break
  if a_line[place] == 2:
    break
  a_con[place] += 1
  place = a_line[place] - 1
  ans += 1

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

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

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

print(-1)

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

    flag = False
    on = 0
    cnt = 0
    for i in range(n):
        if a[on] == 2:
            cnt += 1
            flag = True
            break
        else:
            cnt += 1
            on = a[on] - 1

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



if __name__ == '__main__':
    main()

Solution (Python):
n, *aa = map(int, open(0).read().split())

d = [0]*n

cur = 1

ans = 0

while 1:
    d[cur-1] = 1
    cur = aa[cur-1]
    ans += 1
    if cur == 2:
        print(ans)
        break
    if d[cur-1]:
        print(-1)
        break

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]
count, current = 0, 1
while current != 2:
    current = A[current - 1]
    count += 1
    if count > n:
        count = -1
        break
print(count)

Solution (Python):
N=int(input())
a=[int(input()) for _ in range(N)]
dp=[0]*(10**5)
dp[0]=1
c=0
ans=0
for i in range(N):
  ans+=1
  if a[c]-1==1:
    print(ans)
    exit()
  if dp[a[c]-1]==1:
    print(-1)
    exit()
  dp[a[c]-1]=1
  c=a[c]-1

Solution (Python):
import sys

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())


def main():
    N = ii()
    a = [ii() for i in range(N)]
    now = 0
    for i in range(N+1):
        if now == 1:
            print(i)
            return
        now = a[now]-1
    print(-1)

if __name__ == '__main__':
    main()

Solution (Python):
import sys

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

for i in range(N):
    button[i] = int(input())-1
count, now = 0, 0

while count <= N:
    now = button[now]
    count += 1
    if now == 1:
        print(count)
        sys.exit()
print(-1)

Solution (Python):

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

now = 0
cnt = 0
while now != 1:
    now = A[now] - 1
    cnt += 1
    if cnt > N:
        cnt = -1
        break
print(cnt)

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

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

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

flag = [True]*n
t = a[0]
ans = 1
while t != 2:
  if flag[t-1]:
    flag[t-1] = False
    t = a[t-1]
    ans += 1
  else:
    print(-1)
    exit()
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

def solve(n,a):
  memo = [0]*(n+1)
  def f(a,present=1,cnt=0):
    next = a[present]
    if memo[next]:
      return -1
    elif next == 2:
      return cnt+1
    else:
      memo[present] = 1
      return f(a,next,cnt+1)
  return f(a)
      

N = int(input())
a = [0] + [int(input()) for _ in range(N)]
print(solve(N,a))

Solution (Python):


N = int(input())

a = []

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

ans = 0
b_now = 1
loop = []

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

print(-1)

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

Solution (Python):
from collections import deque

N = int(input())
button = [[i+1, int(input())] for i in range(N)]
button.insert(0, [0, 0])
cnt = 0
times = 0
q = deque([1])

while q:
    p = q.popleft()
    l = button[p][1]
    cnt += 1
    if l == 1:
        print(-1)
    elif l == 2:
        print(cnt)
    else:
        q.append(l)

    times += 1
    if times >= N:
        print(-1)
        exit()

Solution (Python):
import numpy as np
def resolve():
    N = int(input())
    A = np.zeros(N + 1, dtype = int)
    for i in range(1, N + 1):
        A[i] = int(input())
    current_num = 1
    ans = 0
    Found = False
    for _ in range(N):
        next_num = A[current_num]
        ans += 1 
        if next_num == 2:
            Found = True
            break
        current_num = next_num
    if (Found):
        print(ans)
    else:
        print(-1)
resolve()

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

Solution (Python):
n = int(input())
a = []
for i in range(n):
  a_ = int(input())
  a.append(a_)
res = []
i = 1
for _ in range(n):
  res.append(a[i-1])
  i = a[i-1]
  if i == 2:
    break

if 2 in res:
  print(len(res))
else:
  print(-1)

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

a = 1
cnt = 0
while a != 2 and not selected[a - 1]:
    selected[a - 1] = True
    a = A[a - 1]
    cnt += 1
if a == 2:
    print(cnt)
else:
    print('-1')

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

Solution (Python):
import  sys
N=int(input())
a = [int(input()) for _ in range(N)]
cnt=1
t=1
if a[t - 1] == 2:
    print(cnt)
    sys.exit()
for i in range(10**5):
    cnt+=1
    t=a[t-1]
    if a[t-1]==2:
        print(cnt)
        sys.exit()

print(-1)

Solution (Python):




import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy












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

if A[0] == 2:
    print(1)
    sys.exit()

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

print(-1)

Solution (Python):
from collections import defaultdict

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n = readInt()
a = [readInt() for i in range(n)]

visited= [0]*(10**5+1)
b = 1
ans = 1
while 1:
  if a[b-1]==2:
    break
  elif visited[a[b-1]]==0:
    visited[a[b-1]]=1
    ans += 1
    b = a[b-1]
  else:
    print(-1)
    exit()

print(ans)

Solution (Python):
N = int(input())
botton = []
for i in range(N):
  a = int(input())
  botton.append(a)
now = botton[0]
if now == 2:
  print(1)
else:
  flag = 0
  for i in range(N-1):
    now = botton[now-1]
    if now == 2:
      print(i+2)
      flag += 1
      break
  if flag == 0:
    print(-1)

Solution (Python):
import sys
from io import StringIO
import unittest

def resolve():
    lines = sys.stdin.readlines()
    lines = list(map(lambda x: int(x.rstrip()), lines))
    n = lines[0]
    i = 1
    for c in range(1, n+1):
        i = lines[i]
        if i == 2:
            print(c)
            return
    print(-1)
    
resolve()

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

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

dp = [0]*N
light = 1
while(light != 2):
    if dp[light-1] == 1:
        print(-1)
        exit()
    else:
        dp[light-1] = 1
    
    light = a[light-1]

print(sum(dp))

Solution (Python):
N=int(input())
A=[]
for i in range(N):
  A.append(int(input())-1)
L=[]
i=0
cnt=0
while True:
  if cnt>10**6:
    print(-1)
    exit()  
  i=A[i]
  L.append(i)
  cnt+=1
  if i==1:
    print(cnt)
    exit()

Solution (Python):
import sys
N = int(input())
if N < 1 or N > 10 ** 5:
    sys.exit()

int_array = []
for I in range(N):
    int_array.append(int(input()))
if len(int_array) < 0 or len(int_array) > N:
    sys.exit()

next = int_array[0]
count = 1
for L in range(N):
    if next == 2:
        print(count)
        sys.exit()
    next = int_array[next-1]
    count += 1
print("-1")

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

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

print(-1)

Solution (Python):









import os
import sys




def main():
    n = int(input())
    a = [0]
    for i in range(n):
        a.append(int(input()))
    cnt = 0
    flag = [0]*(n+1)
    current = 1
    tmp=0
    while flag[current] == 0:
        flag[current] = flag[tmp] + 1
        tmp = current
        current = a[current]
        if current == 2:
            print(flag[tmp])
            return
    print("-1")


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
T = {}
k = 1
cnt = 0
while True:
    if k == 2:
        print(cnt)
        exit()
    if k not in T: T[k]=1
    else:
        print(-1)
        exit()
    k = A[k-1]
    cnt+=1

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect_left
from heapq import heappush, heappop
import networkx as nx
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
a = [0]+[INT() for _ in range(N)]

visited = [0]*(N+1)
visited[1] = 1
now = 1
cnt = 0
while not visited[a[now]]:
    now = a[now]
    visited[now] = 1
    cnt += 1
    if now == 2:
        print(cnt)
        break
else:
    print(-1)

Solution (Python):
N = int(input())
lis = [int(input()) for i in range(N)]
a=1
for i in range(1,N+1):
    a=lis[a-1]
    if a == 2:
        print(i)
        break
if a !=2:
    print(-1)

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

count = 1
i = 0

while count < n:
  i = a[i] - 1
  if i == 1:
    print(count)
    break
  count += 1
if count >= n:
  print(-1)

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

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

print(-1)

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

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

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

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

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

idx = 1
stopped = set([idx])
while True:
    idx = l[idx - 1]
    if idx == 2:
        print(len(stopped))
        break
    if idx in stopped:
        print(-1)
        break
    else:
        stopped.add(idx)

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

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

Solution (Python):
N = int(input())
A = [0] + [int(input()) for i in range(N)]
 
now = 1
count = 0
pushed = {1}
while now != 2:
  now = A[now]
  count += 1
  if now in pushed:
    print(-1)
    quit()
  pushed.add(now)
 
print(count)

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

now = 1
count = 0
pushed = {1}
while now != 2:
  now = A[now]
  count += 1
  if now in pushed:
    print(-1)
    quit()
  pushed.add(now)

print(count)

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

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

cur = 0
ok = False
hist = set()

while True:
    if cur == 1:
        ok = True
        break

    if cur in hist:
        break
    else:
        hist.add(cur)
    
    cur = A[cur]

if ok:
    print(len(hist))
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
ans = 1
p = a[0]
while True:
    if p == 2:
        print(ans)
        break
    elif (p == a[p-1]) | (ans > n):
        print(-1)
        break
    else:
        p = a[p-1]
        ans += 1

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

count = 0
li = 1
while True:
    li = a_list[li-1]
    count += 1

    if li == 2:
        answer = count
        break

    if count == N:
        answer = -1
        break 

print(answer)

Solution (Python):
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n = I()
    a = [I() for i in range(n)]
    b = [1] + [0]*(n-1)
    cnt = 0
    j = 0
    for i in range(n):
        b[j] = 0
        b[a[j]-1] = 1
        j = a[j]-1
        cnt += 1
        
        if b[1] == 1:
            print(cnt)
            exit()
    print(-1)

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


index = 1


count = 0

while count < N:
  
  if a[index-1]==2:
    count += 1
    
    print(count)
    exit()
    
  else:
    
    count += 1
    
    index=a[index-1]
    

print(-1)

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


def main():
    input = stdin.buffer.readline
    n = int(input())
    g = [int(input()) - 1 for _ in range(n)]
    q = deque([0])
    seen = [-1] * n
    seen[0] = 0

    while q:
        u = q.popleft()
        v = g[u]
        if seen[v] == -1 or seen[v] > seen[u] + 1:
            seen[v] = seen[u] + 1
            q.append(v)

    print(seen[1])


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

index = 0
count = 1
tf = False

while True:
  if a[index]==2:
    tf = True
    break
  else:
    index=a[index]-1
    if count == n:
      break
    else:
      count += 1
if tf:
    print(count)
else:
  print(-1)

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

index = 0
count = 1
tf = False

while True:
  if a[index]==2:
    tf = True
    break
  else:
    index=a[index]-1
    if count == n:
      break
    else:
      count += 1
if tf:
    print(count)
else:
  print(-1)

Solution (Python):

import sys


def solve(N: int, a: "List[int]"):

    i = 0
    for _ in range(N):

        if a[i] == 2:
            print(_+1)
            return
        i = a[i] - 1

    print(-1)
    

    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 _ in range(n)]
now = 1
ans = 0
while True:
    now = a[now-1]
    ans += 1
    if (now==2):
        print(ans)
        break
    elif (now==1):
        print(-1)
        break
    elif (ans>len(a)):
        print(-1)
        break

Solution (Python):

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

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

Solution (Python):
n=int(input())
Alist=[0]+[int(input()) for i in range(n)]
 
now=1
count=0
Hist={1}
 
while now!=2:
    now=Alist[now]
    count+=1
    if now in Hist:
        count=-1
        break
    Hist.add(now)
print(count)

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

now=1
Hist={1}

for i in range(n):
    now=Alist[now]
    if now==2:
        print(i+1)
        break
    if now in Hist:
        print(-1)
        break
    Hist.add(now)

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

now=1
count=0

for _ in range(n):
    now=Alist[now]
    count+=1
    if now==2:
        print(count)
        exit()
print(-1)

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

L = []

for i in range (0, N):
	L.append(int(input()))

counter = 0
initial = 0

for i in range (0, 2*N+1):
	initial = L[initial]-1
	counter+=1
	if initial == 1:
		print(counter)
		exit()

print(-1)

Solution (Python):



import os
import sys







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

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

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


if __name__ == '__main__':
    main()