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=[None]+[int(input()) for i in range(N)]
c=1

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

Solution (Python):
n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
    ind = q[ind]-1
    cnt += 1
    if ind == 1:
        print(cnt)
        exit()
    
print(-1)

Solution (Python):
n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
    ind = q[ind]-1
    cnt += 1
    if ind == 1:
        print(cnt)
        exit()
    
print(-1)

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

ans = -1
visited = set()
count = 0
btn = 0
for i in range(n):
  if btn in visited:
    break
  visited.add(btn)
  btn = a[btn]-1
  count += 1
  if btn == 1:
    ans = count
    break

print(ans)

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

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

print(ans)

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

A = [0] + A
i = 1
k = 1
while k <= N:
    if A[i] == 2:
        print(k)
        exit()
    i = A[i]
    k += 1
print(-1)

Solution (Python):

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

    

    next = 0
    cnt = 0
    for i in range(N):
        next = A[next] - 1
        
        cnt += 1
        if next == 1:
            break

    if cnt == N:
        print(-1)
    else:
        print(cnt)


if __name__ == "__main__":
    resolve()

Solution (Python):
N=int(input())
a=[]
for i in range(N):
  a.append(int(input())-1)
flag=True
step=[]
pivot=0
for i in range(N+1):
  step.append(pivot)
  if a[pivot]==1:
    break
  pivot=a[pivot]

if len(step)>N:
  print(-1)
else:
  print(len(step))

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

for i in range(1,n+1):
  light[i] = int(input())
s = 1
ans = 0
while light[s] != 2 and ans <= n:
  s = light[s]
  ans += 1

if light[s] == 2:
  print(ans + 1)
else:
  print(-1)

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

used = [False] * N

cur = 0
ans = 0
while not used[cur]:
    if cur == 1:
        print(ans)
        exit()

    used[cur] = True
    cur = a[cur]
    ans += 1

print(-1)

Solution (Python):
def main():
    N = int(input())
    a = [int(input()) for _ in range(N)]
    cnt = 0
    tmp = 1
    while cnt<=100001:
        tmp = a[tmp-1]
        cnt += 1
        if tmp == 2:
            return cnt
        elif tmp == 1:
            return -1

    return -1

print(main())

Solution (Python):
from collections import defaultdict

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

dic = defaultdict()

for i in range(n):
    dic[i+1] = A[i]

cnt, idx = 1, 0
val = dic[1]

while idx <= n:
    if val == 2:
        print(cnt)
        exit()
    val = dic[val]
    cnt += 1
    idx += 1
print(-1)

Solution (Python):
from typing import List


def answer(n: int, a: List[int]) -> int:
    next_value = 1
    count = 0
    for _ in range(n):
        next_value = a[next_value - 1]
        count += 1
        if next_value == 2:
            return count

    return -1


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    print(answer(n, a))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
al = list(int(input()) for _ in range(n))
num = [0]*n
i = 0
cnt = 0
while True:
    if al[i] == 2:
        print(cnt+1)
        break
    else:
        if num[al[i]-1] == 1:
            print(-1)
            break
        else:
            num[al[i]-1] += 1
            i = al[i]-1
            cnt += 1

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

fin = set()
cnt = 0
i = 0
while True:
    if i in fin:
        cnt = -1
        break
    if i == 1:
        break
    cnt += 1
    fin.add(i)
    i = A[i]
print(cnt)

Solution (Python):
n=int(input())
a=[int(input())-1 for _ in range(n)]
id=[-1]*n
x=0
for c in range(1,n+1):
    if id[x]!=-1:
        print(-1)
        exit()
    id[x]=c
    x=a[x]
    if x==1:
        print(c)
        exit()

Solution (Python):
n = int(input())
done = [False for _ in range(n)]
A = [int(input()) - 1 for _ in range(n)]
tmp = 0
for i in range(n + 1):
    if A[tmp] == 1:
        print(i + 1)

        break
    if done[tmp]:
        print(-1)
        break
    else:
        done[tmp] = True
    tmp = A[tmp]
else:
    print(-1)

Solution (Python):
import sys
N = int(input())
A = [int(input()) for _ in range(N)]
x = 0
cnt = 0
for _ in range(N):
    x = A[x] - 1
    cnt += 1
    if x == 1:
        print(cnt)
        sys.exit()
print(-1)

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

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

Solution (Python):
n = int(input())
btn = [0]
for i in range(n):
  btn.append(int(input()))
see = [0 for i in range(n+1)]
  
now = 1
cnt = 0

see[now] = 1
while True:
  now = btn[now]
  cnt += 1
  
  if now == 2:
    print(cnt)
    break
  
  if see[now] == 1:
    print(-1)
    break
  
  see[now] = 1

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

l = []
for i in range(N):
  v = int(input())
  l.append(v)
  
v = l[0]
ans = 1
while v != 2 and v != -1:
  tmp = l[v-1]
  l[v-1] = -1
  v = tmp
  ans += 1
  
if v == 2:
  print(ans)
else: 
  print(-1)

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

if not a.count(2):
    print('-1')
else:
    i = 0
    while True:
        cnt += 1
        if a[i] == 2:
            print(cnt)
            break
        elif a[i] == i+1 or cnt > n:
            print('-1')
            break
        i = a[i]-1

Solution (Python):
import sys
N = int(input())
a = [int(input()) for _ in range(N)]
flag = [0]*N
k = 0
i = 0
flag[0] = 1
while k <N:
  k += 1
  i = a[i] - 1
  if flag[i] == 0:
    flag[i] = 1
  elif flag[i] == 1:
    print(-1)
    sys.exit()
  if i == 1:
    print(k)
    sys.exit()

Solution (Python):
N = int(input())
a = [int(input()) for _ in range(N)]
log = set()
i = 0
count = 1
while a[i] != 2:
  i = a[i] - 1
  if i in log:
    print(-1)
    break
  else:
    log.add(i)
    count += 1
else:
  print(count)

Solution (Python):
N = int(input())
a = tuple(int(input()) for _ in range(N))
log = set()
i = 0
count = 1
while a[i] != 2:
  i = a[i] - 1
  if i in log:
    print(-1)
    break
  else:
    log.add(i)
    count += 1
else:
  print(count)

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

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

flag = False
index = 0
count = 0

for i in range(n):
  index = a[index]
  count += 1
  if index == 1 :
    flag = True
    break

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

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

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

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

Solution (Python):
def input_int():
    return(int(input()))

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

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

def run():
    num_count = input_int()
    nums = []
    for _ in range(num_count):
        nums.append(input_int())

    num_count = 1

    current_num = 1
    while True:
        current_num = nums[current_num - 1]

        if num_count >= len(nums):
            print(-1)
            return

        if current_num == 2:
            print(num_count)
            return
        num_count += 1

run()

Solution (Python):
n = int(input())
a = []
s = [0]*n
x = 1
for i in range(n):
  a.append(int(input()))
if 2 not in a:
  print(-1)
else:
  while True:
    if a[x-1] == 2:
      print(sum(s)+1)
      exit()
    else:
      s[x-1] += 1
      if s[x-1] == 2:
        print(-1)
        exit()
      x = a[x-1]

Solution (Python):
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

n = I()
a = IR(n)










count = 0
cur = 1

while count < n:
    count += 1
    cur = a[cur-1]
    if cur == 2:
        print(count)
        sys.exit()
print(-1)

Solution (Python):


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

ans = pos = 0
flag = [0] * N
while True:
    ans += 1
    if a[pos] == 2:
        break
    
    pos = a[pos] - 1

    if flag[pos] > 0:
        ans = -1
        break
    else:
        flag[pos] = 1

print(ans)

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

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

now = 1
count = 0

for i in range(N):
   now = lst[now - 1]
   count += 1
   if now == 2:
      print(count)
      exit()

print(-1)

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

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

Solution (Python):

def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    cnt = 0
    x = 1

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

            if x == 2:
                print(cnt)
                exit()
        else:
            print(-1)
    else:
        print(-1)
        

if __name__ ==  "__main__":
    main()

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

x = 1
Sum = 0
while x != 2 and Sum < N:
    x = a[x - 1]
    Sum += 1

if Sum >= N:
    print(-1)
else:
    print(Sum)

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

ans = 1
x = 1
for j in range(N):
    if A[x] == 2:
        print(ans)
        sys.exit()
    x = A[x]
    ans += 1
print(-1)

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

if 2 not in a:
  print(-1)
  exit()

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

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

print(-1)

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

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

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

Solution (Python):
def main():
  n=int(input())
  A=[int(input()) for i in range(n)]
  crt=1
  cnt=0
  for i in range(n):
    cnt += 1
    crt = A[crt-1]
    if crt == 2:
      print(cnt)
      break
  else:
    print(-1)
main()

Solution (Python):



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

visit = [-1] * N
while True:
    
    cur = a[cur]
    ans += 1
    if visit[cur] == -1:
        visit[cur] = ans
    else:
        print(-1)
        exit()
    if cur == 1:
        print(ans)
        exit()

Solution (Python):



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

visit = [-1] * N
while True:
    
    cur = a[cur]
    ans += 1
    if visit[cur] == -1:
        visit[cur] = ans
    else:
        print(-1)
        exit()
    if cur == 1:
        print(ans)
        exit()

Solution (Python):
n = int(input())
buttons = [int(input()) for _ in range(n)]
done = [0] * (n + 1)
next_button = buttons[0]
for _ in range(n):
    buttons_status = done[next_button]
    if buttons_status != 0:
        print(-1)
        exit()
    done[next_button] = 1
    if next_button == 2:
        break
    next_button = buttons[next_button - 1]
print(done.count(1))

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

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

Solution (Python):
N = int(input())
L = []
light = [0]*(N)
light[0] = 1
cnt, now = 0, 0

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

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

print(-1)

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

count = [0]*(N)

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

print(-1)

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

N=int(input())
a = []
for _ in range(N):
    a.append(int(input()))
found = [0]*N

steps = 0
i = 0
found[i] = 1
while True:
    j = a[i] - 1
    if found[j] == 1:
        print(-1)
        exit()
    steps += 1
    i = j
    found[i] = 1
    if i == 1:
        print(steps)
        exit()

Solution (Python):
def f():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    x = 1
    c = 0
    for i in range(n):
        if x != 2:
            x = a[x-1]
            c += 1
            if c == n:
                return -1
        else:
            return c
print(f())

Solution (Python):
N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input()) - 1 
index = 0
trials = -1
for i in range(N):
    index = A[index]
    if index == 1:
        trials = i + 1
        break
print(trials)

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


s = ()
nex = 1

for i in range(N):
    
    nex = S[nex-1]
    if nex == 2:
        print(i+1)
        break
    if i == N-1:
        print(-1)

Solution (Python):
import re
import copy

def accept_input():
    N = int(input())
    A = {}
    for i in range(1,N+1):
        A[i] = int(input())
    return N,A

def process(s):
    if s == "
        return 1
    else:
        return 0
N,A = accept_input()
cur = 1
kaisu = 0
for _ in range(N):
    cur = A[cur]
    kaisu += 1
    if cur == 2:
        break
if cur == 2:
    print(kaisu)
else:
    print(-1)

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

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

tmp = 0
count = 0

while owari[tmp] == 0 and tmp != 1:
    count += 1
    owari[tmp]=1
    tmp = a[tmp]

if tmp == 1:
    print(count)
else:
    print(-1)

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

    pressed = 0
    i = 0
    t = 0
    while i != 1:
        i = A[i]
        if pressed > N:
            t = -1
            break
        pressed += 1
        t += 1

    print(t)
    
resolve()

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

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

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

visit = set()
now = 1
for i in range(1, n + 10):
    if now not in visit:
        visit.add(now)
        now = a[now - 1]
    else:
        print(-1)
        break
    if now == 2:
        print(i)
        break

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

counter = 0
now = 1
visit = [False] * n
visit[0] = True
while True:
    counter += 1
    if a[now - 1] == 2:
        print(counter)
        break
    elif visit[a[now - 1]-1] == True:
        print(-1)
        break
    else:
        visit[a[now - 1]-1] = True
    now = a[now - 1]

Solution (Python):
n = int(input())
a = list(map(int, [input() for i in range(n)]))
ans = 1
hikari = 1
for i in range(n):
        hikari = a[hikari -1]
        if hikari == 2:
                print(ans)
                exit()
        else:
                ans  += 1
print(-1)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
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 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

N = k()
A = []
for i in range(N):
    a = k()
    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())
l = [int(input())-1 for _ in range(0,N)]

i = 0;
flg = False

for n in range(0,N):
  if l[i] == 1 :
    print(n+1)
    flg = True
    break
  else:
    i = l[i]


if not flg:
  print("-1")

Solution (Python):

import sys


def solve(N: int, a: "List[int]"):
    s = {1}
    i = 1
    c = 0
    while True:
        c += 1
        i = a[i-1]
        if i == 2:
            print(c)
            break
        if i in s:
            print(-1)
            break
        s.add(i)

    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 = []
for i in range(N):
    a = int(input())
    A.append(a)
b = 1
x = A[0]
while x != 2:
    x = A[x - 1]
    if b > N:
        print(-1)
        exit()
    b += 1

print(b)

Solution (Python):
n = int(input())
A = []
bool = [True] * n
for i in range(n):
    a = int(input())
    a -= 1
    A.append(a)
j = A[0]
cnt = 1
for i in range(n):
    if j == 1:
        print(cnt)
        exit()
    if bool[A[j]] == True:
        bool[A[j]] = False        
        j = A[j]
        cnt += 1
    else :
        print(-1)
        exit()

Solution (Python):
N=int(input())
a=[int(input())-1 for _ in range(N)]
x=0
ans=0
while True:
    ans+=1
    x=a[x]
    if x==1:
        print(ans)
        break
    if ans>N:
        print(-1)
        break;

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


p = a[0]

if p == 2:
    print(1)
    exit()
    
cnt = 1

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

print(-1)

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

j = 1
cnt = 0
check = False

for i in range(N):
  if j == 2:
    check= True
    break
  cnt += 1
  j = A[j]
  
print(cnt if check else -1)

Solution (Python):
N = int(input())
A = {}
for i in range(N):
  A[i+1] = int(input())
B = [-1 for i in range(N+1)]
B[1] = 0
i = 1
while 1:
  j = A[i]
  if j == 2:
    print(B[i] + 1)
    break
  elif B[j] != -1:
    print(-1)
    break
  else:
    B[j] = B[i] + 1
    i = j

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

    answer = -1
    counter = 0

    i = 0
    while True:
        counter += 1
        if i == a[i]:
            answer = -1
            break
        elif a[i] == 1:
            answer = counter
            break
        elif a[i] == 0:
            answer = -1
            break
        else:
            temp = a[i]
            a[i] = 0
            i = temp

    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):


import sys
nyuryoku = [int(i) - 1 for i in sys.stdin]

N = nyuryoku[0] + 1
a_list = nyuryoku[1:]


cnt_push = 0
button_list = [False] * N
i = 0


while True:
    if button_list[i] == False:
        button_list[i] = True
        cnt_push += 1
        if a_list[i] == 1:
            break
        i = a_list[i]
    else:
        cnt_push = -1
        break

print(cnt_push)

"""
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)
"""

Solution (Python):
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)

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

Solution (Python):
with open(0) as f:
    N, *a = map(int, f.read().split())

from itertools import count
p = 0
seen = {0}
for i in count(1,1):
    p = a[p]-1
    if p == 1:
        ans = i
        break
    if p in seen:
        ans = -1
        break
    seen.add(p)
print(ans)

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

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

count = 0
now = 1
while now != 2:
    if li[now] == 0:
        count = -1
        break
    else:
        a = li[now]
        li[now] = 0
        now = a
        count += 1

print(count)

Solution (Python):
import sys
N = int(input())
d = [int(input()) for i in range(N)]

cnt = 0
button = d[0]
while button != 2:
    button = d[button-1]
    cnt += 1

    if cnt > 100000:
        print(-1)
        sys.exit(0)

print(cnt+1)

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

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush

sys.setrecursionlimit(10**6)
INF = 10**20

def mint():
    return map(int,input().split())
def lint():
    return map(int,input().split())

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

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

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

Solution (Python):
import sys
import cProfile
input = sys.stdin.readline
def main():
    n=int(input())
    a=[int(input()) for _ in range(n)]
    l=[1]
    i=0
    aa=1
    while True:
        aa=a[aa-1]
        if aa == 2:
            print(i+1)
            sys.exit()
        if i>n:
            print(-1)
            sys.exit()
        i+=1
main()

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
a = [ri() for _ in range(n)]
idx = 1
cnt = 1
if a[0] == 2:
    print(1)
    exit()
while idx != -1:
    a[idx-1], idx = -1, a[idx-1]
    cnt += 1
    if a[idx-1] == 2:
        print(cnt)
        exit()
print(-1)

Solution (Python):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

cur = 1
ans = 0
pushed = [False] * (N + 1)
pushed[1] = True
ok = True
while True:
    pushed[cur] = True
    ans += 1
    cur = A[cur]
    if cur == 2:
        break
    if pushed[cur]:
        ok = False
        break

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

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

print(-1)

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

if a[1] == 2:
    print(cnt)

else:
    while cnt <= 10**5:
        next_a = a[next_a]
        cnt += 1
        if next_a == 2:
            break

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

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

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

num=l[0]
cnt=0
for i in range(10**5):
  if num==2:
    cnt+=1
    break
  else:
    num=l[num-1]
    cnt+=1
    
if cnt==10**5:
  cnt=-1
  
print(cnt)

Solution (Python):
n = int(input())
l = [0 for _ in range(n)]
already = set()
next = 1
c = 0
for i in range(n):
  a = int(input())
  l[i] = a
while c <= n:
  next = l[next-1]
  c += 1
  if next == 2:
    print(c)
    break
  elif next in already:
    print(-1)
    break
  already.add(next)

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

index = 0
count = 0
for i in range(N):
    b = al[index]
    count += 1
    if b == 2:
        break
    index = b - 1

if b != 2:
    count = -1

print(count)

Solution (Python):
N = int(input())
As = [int(input()) for i in range(N)]
visited=[False]*N
c = 0
now = 0
while True:
    if visited[now]:
        c = -1
        break
    visited[now] = True
    c+=1
    now = As[now]-1
    if now == 1:
        break
print(c)

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N = int(readline())
    seq = []

    for _ in range(N):
        a = int(readline())
        a -= 1
        seq.append(a)

    cur = 0

    for i in range(1, 10 ** 6):
        cur = seq[cur]
        if cur == 1:
            return print(i)

    print(-1)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = set()
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif A[push] in count:
      break
      
    else:
      count.add(push)
      push = A[push]
        
print(ans)

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

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

ans = -1
push = 0

for i in range(N+1):

    if A[push] == 1:
        ans = i+1
        break
        
    else:
        push = A[push]
      
print(ans)

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

for i in range(1, n+2):
    
    if a[push] == 2:
        ans = i
        break
        
    else:
        push = a[push]
      
print(ans)

Solution (Python):
n = int(input())
al = []
for i in range(n):
    a = int(input())
    al.append(a)
 
al.insert(0, 0)
ans = -1
push = 1
for i in range(1, n+2):
    light = al[push]
    push = light
    if light == 2:
        ans = i
        break
print(ans)

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



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

al.insert(0, 0)
ans = -1
push = 1
for i in range(1, n+2):
    light = al[push]
    push = light
    if light == 2:
        ans = i
        break
print(ans)

Solution (Python):
N = int(input())
d = []
visited = [False for _ in range(N+1)]
for _ in range(N):
  d.append(int(input())-1)

i = 0
cnt = 0
while True:
  if i == 1:
    break
  elif cnt >= N:
    cnt = -1
    break
  elif visited[i]:
    cnt = -1
    break
  else:
    visited[i] = True
    cnt += 1
    i = d[i]
  
print(cnt)

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


ans = -1
memo = 0
light = 1

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


print(ans)

Solution (Python):
import sys

n = int(input())
A = list(map(int, sys.stdin.readlines()))

index = 0
past_index = list()
found = False

for i in range(len(A)):
    if index == 1:
        print(i)
        found = True
        break
    index = A[index]-1

if not found:
    print(-1)

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