Problem:
 Score : 200 points    Problem Statement Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.  The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N .
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i .
When Button i is not lighten up, nothing will happen by pressing it.  Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.  Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.      Constraints  2 ≤ N ≤ 10^5  1 ≤ a_i ≤ N         Input Input is given from Standard Input in the following format:  N  a_1  a_2 : a_N       Output Print -1 if it is impossible to lighten up Button 2 .
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2 .        Sample Input 1 3
3
1
2      Sample Output 1 2  Press Button 1 , then Button 3 .       Sample Input 2 4
3
4
1
2      Sample Output 2 -1  Pressing Button 1 lightens up Button 3 , and vice versa, so Button 2 will never be lighten up.       Sample Input 3 5
3
3
4
2
4      Sample Output 3 3

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

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

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

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

if a[0]==2:
    print(1)
    exit()
else:
    x=a[0]-1

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

Solution (Python):

import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**6)
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]
INF = float('inf')
MOD = 10 ** 9 + 7

N = ri()
a = [ri()-1 for _ in range(N)]
f = [0] * N
ch = 0
cnt = 0
while 1:
    if f[ch]:
        cnt = -1
        break
    else:
        f[ch] = 1
        cnt += 1
        ch = a[ch]
        if ch == 1:
            break
print(cnt)

Solution (Python):
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  
    a_arr = [None]
    for i in range(N):
        _ = int(_in[i+1])  
        a_arr.append(_)
    
    memo = []
    now = 1
    for i in range(N):
        _ = a_arr[now]
        if _ == 2:
            cnt = i+1
            break
        else:
            now = _
    else:
        cnt = -1
    
    print(cnt)

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

i = 0
count = 0
root = set()
while True:
    light = a[i]
    count += 1

    if i not in root:
        root.add(i)
    else:
        print(-1)
        break

    if light == 2:
        print(count)
        break

    i = light - 1

Solution (Python):
n = int(input())
dic = {}
for i in range(n):
  dic[i+1] = int(input())

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

Solution (Python):
def main():
  N = int(input())
  a = [0] * N
  visited = [False] * N
  l = 0
  ans = 0


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

  visited[0] = True

  while(l != 1):
    l = a[l] - 1

    if(visited[l]):
      ans = -1
      break
    
    visited[l] = True
    ans += 1

  print(ans)

if __name__ == "__main__":
  main();

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

Solution (Python):
A = int(input())
listA=[]
for i in range(A):
    listA.append(int(input()))
listB=set()
B=0
for j in range(A):
    if B == 1:
        print(j)
        exit()
    else:
        B=listA[B]-1
print(-1)

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

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

print(-1)

Solution (Python):
N,*A = map(int,open(0).read().split())


P =[0]+[0 for i in range(N)]

a = 1 

for i in range(1,N+1):
  P[a]+=1
  a = A[a-1]
  if P[a]>1:
    print(-1)
    exit()
  if a == 2:
    print(i)
    exit()

Solution (Python):
import sys

n = [int(x) for x in sys.stdin]

button = 1

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

Solution (Python):
import sys

n = list(map(int, sys.stdin))

button = 1

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

Solution (Python):
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1

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

Solution (Python):
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

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

Solution (Python):
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

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

if count == n:
    print(-1)

Solution (Python):
import copy
N=int(input())
a=[0]*(N+1)
cnta=copy.copy(a)
for i in range(1,N+1):
    a[i]=int(input())

button=1

for i in range(100000):
    cnta[button]=cnta[button]+1
    if cnta[2]==1:
        print(sum(cnta)-1)
        break
    elif cnta[button]>=2:
        print("-1")
        break
    button=a[button]

Solution (Python):
import copy
N=int(input())
a=[0]*(N+1)
cnta=copy.copy(a)
for i in range(1,N+1):
    a[i]=int(input())

button=1

for i in range(100000):
    cnta[button]=cnta[button]+1
    if cnta[2]==1:
        print(sum(cnta)-1)
        break
    elif cnta[button]>=2:
        print("-1")
        break
    button=a[button]

Solution (Python):
import sys

readline = sys.stdin.readline

def main():
    N = int(readline())
    A = []
    for _ in range(N):
        A.append(int(readline())-1)
    check = [0]*N
    check[0] = 1
    nxt = 0
    ans = 1
    while True:
        nxt = A[nxt]
        if check[nxt] == 1:
            ans = -1
            break
        if nxt == 1:
            break
        check[nxt] = 1
        ans += 1
    print(ans)

if __name__ == "__main__":
    main()

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

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

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

check = [1]*n
p = 1
ans = 1
total = 0
while check[p-1] == 1:
    total += 1
    check[p-1] = 0
    if a[p-1] == 2:
        ans = 0
        break
    
    p = a[p-1]


if ans == 1:
    total = -1
print(total)

Solution (Python):
n, *a = map(int, open(0))
done = [0] * n
b = 1
cnt = 0
while not done[b - 1]:
    if b == 2:
        print(cnt)
        exit()
    done[b - 1] = 1
    b = a[b - 1]
    cnt += 1
print(-1)

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

Solution (Python):
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))

cnt = 1
x = a[0]-1
if a[0] == 2:
    print(cnt)
    exit()
for _ in range(10**5+10):

    if a[x] == 2:
        cnt += 1
        print(cnt)
        exit()
    else:
        x = a[x]-1
        cnt += 1
print(-1)

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

j=0
ans=0
count=[0]*N
while j!=1:
    ans+=1
    j=A[j]-1
    count[j]+=1
    if count[j]>=2:
        ans=-1
        break

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(200000)
n = int(input())
a = [int(input()) for i in range(n)]

switch = [False] * (n + 1)
def button(x,cnt):
    if switch[x]:
        print(-1)
        return -1
    if x == 2:
        print(cnt)
        return
    switch[x] = True
    button(a[x - 1], cnt + 1)
    return cnt

ans = button(1, 0)

Solution (Python):
N=int(input())
A=[]
for i in range(N):
    a=int(input())
    A.append(a)
count=0
for i in range(N):
    if i==0:
        now=0
        nex=A[0]
        count+=1
    else:
        
        nex=A[nex-1]
        count+=1
    if nex==2:
        print(count)
        exit()

print(-1)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = [inp() for _ in range(n)]
now = 0
s = set()
s.add(0)
cnt = 0
while True:
    nx = a[now] - 1
    cnt += 1
    if nx == 1:
        print(cnt)
        break
    if nx in s:
        print(-1)
        break
    s.add(nx)
    now = nx

Solution (Python):
N = int(input())
button = list()
for i in range(N):
    button.append(int(input()))

pre  = 1

i = 0
c = 1
rireki = [0]*N
while all: 
    if(button[i]==2):
        print(c)
        break
    rireki[i] +=1
    if(rireki[i]==2):
        print(-1)
        break
    i = button[i]-1
    c+=1

Solution (Python):
n = int(input())
buttons = [int(input()) for _ in range(n)]
pushed = [False] * n
b = 0
ans = 0
while True:
    ans += 1
    pushed[b] = True
    b = buttons[b] - 1
    if pushed[b]:
        ans = -1
        break
    if b == 1:
        break
        
print(ans)

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

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

    cur = 0
    cnt = [-1] * N
    cnt[0] = 0
    while cur != 1:
        c = cnt[cur]

        cur = a[cur]
        if ~cnt[cur]: break
        cnt[cur] = c + 1

    print(cnt[1] if ~cnt[1] else -1)


if __name__ == '__main__':
    main()

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

next = 0
ans = 0
while ans <= n:
    ans += 1
    if A[next] == 2:
        print(ans)
        exit()
    next = A[next]-1

print(-1)

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

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

b=1
cnt=0
for i in range(n):
  cnt+=1
  next=l[b-1]
  if next==2:
    print(cnt)
    exit()
  b=next
print(-1)

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

visited = [0]*n
x = 0

ans=1
while True:
    if visited[x]==1:
        ans=-1
        break
    if a_inputs[x]==1:
        break
    visited[x] = 1
    x = a_inputs[x]
    ans+=1
print(ans)

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

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

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

buttons = []

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

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

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

cnt, i = 0, 0
while cnt < N:
    cnt += 1
    if A[i] == 1:
        break
    i = A[i]
print(cnt if cnt < N else -1)

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

Solution (Python):
N,*a = map(int,open(0).read().split())
a.insert(0,0)
flg = False
on = 1
cnt = 0
for i in range(N):
    on = a[on]
    cnt += 1
    if on == 2:
        print(cnt)
        exit()
print(-1)

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

  ans = 0
  idx = 0
  while True:
    if a[idx] == -1:
      print("-1")
      return
    if idx == 1:
      break
    ans += 1
    tmp = a[idx]
    a[idx] = -1
    idx = tmp
  print(ans)
  return

if __name__ == "__main__":
  resolve()

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

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

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

Solution (Python):
N = int(input())
A = [int(input())-1 for i in range(N)]
used = [False] * N
a = 0
ans = 0
f = False

while True:
    if used[a]: 
        f = True
        break
    if a == 1: break
    used[a] = True
    ans += 1

    a = A[a]

if f: ans = -1
print(ans)

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

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

check = [True] * (N + 1)

count = 0
tmp = 1
while check[tmp]:
    count += 1
    check[tmp] = False
    if A[tmp - 1] == 2:
        print (count)
        exit()
    tmp = A[tmp - 1]

print (-1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
A.insert(0,0)
hist = [0 for _ in range(N+1)]
ind = 1
hist[1] = 1
cnt = 0
flag = 0
while True:
    cnt += 1
    ind = A[ind]
    if ind==2:
        flag = 1
        break
    if hist[ind]==1:
        flag = 0
        break
    hist[ind] = 1
if flag==1:
    print(cnt)
else:
    print(-1)

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

cnt = 0
now = 0
while 1:
    n = button[now]
    cnt += 1
    if n==2:
        break
    if cnt>N+2:
        cnt = -1
        break
    now = n-1
print(cnt)

Solution (Python):
import sys
import re
import math
import collections









INF = sys.maxsize


ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    n = ni()
    d = []
    for _ in range(n):
        d.append(ni())

    idx = 0
    for i in range(10 ** 5 + 1):
        idx = d[idx] - 1
        if idx == 1:
            print(i + 1)
            exit(0)

    print(-1)


if __name__ == '__main__':
    main()

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

d = {}

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

now = 1
ans = -1
for i in range(N+100):
    now = d[now]
    if now == 2:
        ans = i+1
        break
print(ans)

Solution (Python):
def main():
    n = int(input())
    a = [0] + [int(input()) for _ in range(n)]
    checked = [False for _ in range(n + 1)]
    answer = 0
    ind = 1
    while True:
        answer += 1
        checked[ind] = True
        ind = a[ind]
        if checked[ind]:
            answer = -1
            break
        elif ind == 2:
            break
    print(answer if answer > -1 else -1)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(int(input()) for _ in range(n))

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

    print(cnt)


if __name__ == '__main__':
    resolve()

Solution (Python):
n = int(input())
As = [int(input()) for i in range(n)]
As = [a-1 for a in As]
cnt = 1
v = 0
while cnt <= n:
    v = As[v]
    if v == 1:
        print(cnt)
        exit()
    cnt += 1
print("-1")

Solution (Python):
n = int(input())
dic = {}
for i in range(n):
    a = int(input())
    dic[i] = a-1
already = set()
already.add(0)
cur = 0
count = 0
while True:
    cur = dic[cur]
    count += 1
    if cur == 1:
        print(count)
        break
    if cur in already:
        print(-1)
        break
    already.add(cur)

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

s = 0
for i in range(n + 10):
    nxt = a[s] - 1
    if nxt == 1:
        print(i + 1)
        exit()
    s = nxt
print(-1)

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

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

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

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

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

a=[]
for i in range(n):
  a.append(int(input()))
b=0  
s=0
while s !=1:
  s=a[s]-1
  b+=1
  if b==n:
    b=-1
    break
print(b)

Solution (Python):
n = int(input())
arr = []
tmp = 0
res = -1
for i in range(n):
  arr.append(int(input()))
for i in range(n):
  if arr[tmp] == 2 and res == -1:
    res = i + 1
  tmp = arr[tmp] - 1
print(res)

Solution (Python):
N = int(input())
a = [int(input()) for _ in range(N)]
nxt = a[0]-1
cnt = 1
if a[0]-1 == 1:
  print(cnt)
  exit()

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

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

Solution (Python):

n = int(input())
a = [0]
for i in range(n):
    a.append(int(input()))
    
i = 1
ans = 0
while True:
    ans += 1
    if a[i] == 2:
        print(ans)
        break
    if ans > len(a):
        print(-1)
        break
    i = a[i]

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

Solution (Python):
n = int(input())
lst = [0 for _ in range(n + 1)]
num_dict = {}
for i in range(n):
    num_dict[i+1] = int(input())

count = 0
num = 1
lst[1] = -1
while 1:
    count += 1
    num = num_dict[num]
    if num == 2:
        print(count)
        break
    if lst[num] == -1:
        print(-1)
        break
    lst[num] = -1

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

def solve(A):
    ans = 0
    visited = [True] + [False]*(N-1)
    i = 0
    while i!=1:
        i = A[i]-1
        if visited[i]==True:
            break
        visited[i]=True
        ans += 1
    if i==1:
        return ans 
    return -1
print(solve(A))

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

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

button = a_lst[0]

cnt = 1 
ans = 0
for i in range(n):
    button = a_lst[button-1]
    if a_lst[0] == 2:
        ans = 1
        break
    if button == 2:
        ans = cnt + 1 
        break
    cnt += 1

if cnt == n+1 and ans == 0: 
    print("-1")
else:
    print(ans)

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

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


def fun(k):
    if B[k] == 1:
     	return -5                                                    
    else:
        return B[k]
ans = 0
cnt = 0
for i in range(n+1):
    cnt = fun(cnt)
    ans +=1
    
    if cnt ==-5:
        print(ans)
        sys.exit() 
print(-1)
sys.exit()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


n=I()
A=[I()-1 for i in range(n)]
i=0
point=0
while i<n:
    point=A[point]
    i+=1
    if point==1:
        print(i)
        sys.exit()
print(-1)

Solution (Python):
def main():
    button = [-1]
    n = int(input())
    for i in range(n):
        button.append(int(input()))
    count = 0
    push = 1
    while count <= n:
        push = button[push]
        count += 1
        if push == 2:
            break
    print(count) if count < n else print(button[0])

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(500*500)
n = int(input())
a = [int(input()) for _ in range(n)]
def search(i, p):
    if p > n:
        print(-1)
        exit()
    elif i == 2:
        print(p)
        exit()
    else:
        search(a[i-1], p+1)

search(1, 0)

Solution (Python):
n, *a = map(int, open(0).read().split())
cnt = 0
a = [0] + a
i = 1
while 1:
    tmp = i
    i = a[i]
    cnt += 1
    if tmp == i:
        print(-1)
        break
    elif i == 2:
        print(cnt)
        break
    elif cnt > n:
        print(-1)
        break

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

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
p = 1
while True:
    p = A[p-1]
    cnt += 1
    if p == 2 or cnt > n:
        break
if p == 2:
    print(cnt)
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
i = 1
count = 0
btn = {1}
if 2 not in a:
  print(-1)
  exit()
while(1):
  i = a[i-1]
  count += 1
  if i in btn:
    print(-1)
    break
  if i == 2:
    print(count)
    break
  btn.add(i)

Solution (Python):
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))
now = 1
for i in range(150000):
  now = A[now-1]
  if now == 2:
    print(i+1)
    exit()
print(-1)

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

Solution (Python):


def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

N = INT()
a = []
for _ in range(N):
    a.append(INT() - 1)

light_idx = 0

ans = 0
while True:
    light_idx = a[light_idx]
    ans += 1

    if light_idx == 1:
        break

    if ans > N:
        ans = -1
        break

print(ans)

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

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


answer=0
pos=1
while True:
  if answer>N:
    print(-1)
    break
  elif pos==2:
    print(answer)
    break
  else:
    pos=alist[pos]
    answer+=1

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

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


answer=0
pos=1
set_pos=set()
while True:
  if pos==2:
    print(answer)
    break
  else:
    pos=alist[pos]
    answer+=1

  if pos in set_pos:
    print(-1)
    break
  else:
    set_pos.add(pos)

Solution (Python):
N = int(input())
a_li = [int(input()) for i in range(N)]
a_li.insert(0,-1)
osu = 1
for i in range(N):
    ima = a_li[osu]
    if ima == 2:
        print(i+1)
        exit()
    osu = ima
print(-1)

Solution (Python):
N = int(input())
A = []
ex = [0]*N
light = 0
c = 0
for i in range(N) :
    A.append(int(input()))

while light != 1 :
    if ex[light] == 1 :
        print('-1')
        exit()
    ex[light] = 1
    light = A[light]-1
    c += 1
    

print(c)

Solution (Python):

import sys
n = int(input())
a = [int(input()) for _ in range(n)]
lamp = 1
ans=0
for i in range(n):
    if lamp - 1 == 1:
        print(i)
        sys.exit()
    lamp = a[lamp - 1]
print(-1)

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

start = 0
counter = 1
temp_index = a[start]

while counter <= 10**5:

    if temp_index - 1 == 1:
        print(counter)
        exit()

    temp_index = a[temp_index-1]

    counter += 1

print(-1)

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

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

while i != 1 and count <=10 ** 5:
  i = a[i] -1 
  count += 1

if i == 1:
  print(count)

else:
  print('-1')

Solution (Python):
n = int(input())
a = [[int(input())-1, False] for _ in range(n)]
a[0][1] = True
now = 0
cnt = 0
while True:
    now = a[now][0]
    if a[now][1]:
        print(-1)
        break
    a[now][1] = True
    cnt += 1
    if now == 1:
        print(cnt)
        break

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

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

i = 1
already_pressed = set([i])
for count in range(1,N+1):
    i=a[i]
    if i in already_pressed:
        print(-1)
        exit()
    if i==2:
        print(count)
        exit()
    already_pressed.add(i)

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

Solution (Python):
n,*a=map(int,open(0).read().split())
tmp=0
for i in range(n+1):
  tmp=a[tmp]-1
  if tmp==1:
    print(i+1)
    exit()
print(-1)

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]

def to_bin(x: int) -> str: return format(x, 'b') 
def to_oct(x: int) -> str: return format(x, 'o') 
def to_hex(x: int) -> str: return format(x, 'x') 
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]

from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left


def main():
    n = II()
    aa = {i: II()-1 for i in range(n)}
    seen = set()
    s = 0
    cnt = 0
    while True:
        v = aa[s]
        cnt += 1
        if v == 1:
            break
        if not aa[s] in seen:
            seen.add(aa[s])
            s = aa[s]
        else:
            print(-1)
            return None
    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
As = []
for i in range(N):
    As.append(int(input()))
btn = 1
for i in range(N):
    btn = As[btn-1]
    if btn == 2:
        print(i+1)
        exit(0)
print(-1)

Solution (Python):
N = int(input())
As = []
for i in range(N):
    As.append(int(input()))
btn = 1
for i in range(N):
    btn = As[btn-1]
    if btn == 2:
        print(i+1)
        exit(0)
print(-1)

Solution (Python):



























N = int(input())
As = []
for i in range(N):
    As.append(int(input()))
btn = 1
for i in range(N):
    btn = As[btn-1]
    if btn == 2:
        print(i+1)
        exit(0)
print(-1)

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

while s != 2 and c < N:
    c = c + 1
    s = a[s-1]

now = 0
c = 0

while True:
    if now == 1:
        print(c)
        break
    if c >= N:
        print("-1")
        break
    c += 1
    now = a[now]

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

cnt = 1
pos = 0
while cnt < A:
    if a[pos] == 1:
        print(cnt)
        exit()
    else:
        pos = a[pos]
        cnt += 1
print(-1)