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

    Dic = {i:A[i-1] for i in range(1, n+1)}

    cnt = 0
    now = 1
    rout = []

    for _ in range(n):
        rout.append(now)
        now = Dic[now]
        cnt += 1
        if now == 2:
            break

    if now == 2:
        print(cnt)
    else:
        print("-1")

resolve()

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

ans = 0
now = 1
flag = False
for _ in range(n):
    ans += 1
    now = a[now - 1]
    if now == 2:
        flag = True
        break

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

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

buttons = [int(input()) for _ in range(N)]
m_dict = {}
index = 1
flag = True

while True:
    if index-1 in m_dict:
        flag = False
        break
    elif index == 2:
        break
    m_dict[index-1] = 1
    index = buttons[index-1]

if flag is True:
    print(len(m_dict))
else:
    print(-1)

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

b = 1
s = 0
move = []

while True :
    move.append(b)
    b = a[b - 1]
    s += 1
    if b == 2 :
        print(s)
        exit(0)
    if n < s :
        print(-1)
        exit(0)

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

ans = 1
x = 0
while True:
    if a[x] == 2:
        print (ans)
        exit ()

    if ans > len(a):
        print (-1)
        exit ()

    x = a[x]-1
    ans += 1

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

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

i = 1
ans = 0
while True:
    if a[i-1] == -1:
        print(-1)
        break
    ni = a[i-1]
    a[i-1] = -1
    i = ni
    ans +=1
    if i==2:
        print(ans)
        break

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def Yes(): print("Yes"); return
def No(): print("No"); return


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def Base_10_to_n(X, n):
  if X//n:
    return Base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def Base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N = inputI()
a = [0]+inputILs(N)

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

Solution (Python):
n = int(input())
l = []
i = 1
s = 0
for _ in range(n):
  l.append(int(input()))

while True:
  j = l[i-1]
  
  s += 1
  if j == 2:
    print(s)
    exit()
  if s == n:
    print(-1)
    exit()
  i = j

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

now = a[0]
cnt = 0

while cnt <= n:
    cnt += 1
    if now == 2:
        print(cnt)
        exit()
    now = a[now-1]

print(-1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
pushed = [False] * N
pushed[0] = True
button = 0
count = 0
while True:
    count += 1
    button = A[button] - 1
    if button == 1:
        print(count)
        exit()
    if pushed[button]:
        print(-1)
        exit()
    pushed[button] = True

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

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

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=int(input())
    a=[int1(input()) for i in range(N)]
    used=[False]*N
    v=0
    c=0
    while True:
        used[v]=True
        c+=1
        v=a[v]
        if used[v]:
            print(-1)
            exit()
        if v==1:
            print(c)
            exit()

if __name__ == '__main__':
    main()

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

count = 0
pos = 1
for n in range(N):
    count += 1
    if a_list[pos-1]==2:
        print(count)
        break
    pos = a_list[pos-1]
    
if a_list[pos-1]!=2:
    print(-1)

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




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

print(ans)

Solution (Python):
n = int(input())
btn = list(int(input()) for i in range(n))
push = set()
ans = 0
x = 1
while x != 2:
    if x in push:
        ans = -1
        break
    push.add(x)
    x = btn[x-1]
    ans += 1
print(ans)

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

def resolve():
    n=int(input())
    l=[int(input()) for i in range(n)]
    light=0
    cnt=0
    while True:
        if l[light]==2:
            cnt+=1
            print(cnt)
            break
        else:
            if cnt>n:
                print(-1)
                break
            else:
                light=l[light]-1
                cnt+=1
            
resolve()

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

def resolve():
    n=int(input())
    edge = [[] * n for i in range(n)] 
    for i in range(n): 
        x, y = i,int(input())
        y = y - 1
        edge[x].append(y)
    sagasu=0
    cnt=0
    while True:
        a=edge[sagasu]
        if a==[1]:
            print(cnt+1)
            break
        else:
            if cnt>n:
                print(-1)
                break
            else:
                cnt+=1
                sagasu=a[0]
resolve()

Solution (Python):
N=int(input())
a=[int(input()) for i in range(N)]
cnt=0
tgt=0
while tgt!=1:
    tgt=a[tgt]-1
    cnt+=1
    if cnt>N+1:
        print(-1)
        exit()
print(cnt)

Solution (Python):
N=int(input())
bo=[0]
for n in range(N):
    a=int(input())
    bo.append(a)
map=1
count=0
ans=0
for n in range(1,N+1):
    map=bo[map]
    count+=1
    if map==2:
        ans=1
        print(count)
        break
if ans==0:
    print(-1)

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

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


def solve(btns, n):

    attempts = 0
    current = 1

    while True:
        if attempts >= n:
            print("-1")
            return
        
        if current == 2:
            print(attempts)
            return

        current = btns[current-1]
        attempts += 1

solve(btns, n)

Solution (Python):
N = int(input())
a = [int(input()) for _ in range(N)]
hist = [0] * N
bottun = a[0]
cnt = 1
hist[0] = 1

while True:
    
    
    if hist[bottun-1] == 1:
        print(-1)
        exit()
    
    if bottun == 2:
        print(cnt)
        exit()
    
    hist[bottun-1] = 1
    bottun = a[bottun-1]
    cnt += 1

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

la = list()
for i in range(N):
    la.append(int(input()))

lnum = [0]*N

i = 1
cnt = 0
while lnum[i-1] == 0 and i != 2:
    lnum[i-1] += 1
    i = la[i-1]
    cnt += 1

if i == 2:
    print(cnt)

else:
    print("-1")

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

Solution (Python):
N = int(input())
buttons = [int(input()) for _ in range(N)]
counter = 1
now_button = buttons[0]
for _ in range(N):
    if now_button == 2:
        print(counter)
        break
    counter += 1
    now_button = buttons[now_button - 1]
else:
    print(-1)

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

Solution (Python):































n = int(input())
a = [int(input()) for _ in range(n)]
L = [0] * n
cnt = 0
i = 0
while True:
    if L[i] == 1:
        print(-1)
        break
    else:
        L[i] = 1
        i = a[i] - 1
        cnt += 1
        if i == 1:
            print(cnt)
            break

Solution (Python):
import sys
N = int(input())
a = [int(input()) for i in range(N)]
d = [0] * N
b = 0
c = 0
while True:
    if b == 1:
        print(c)
        sys.exit()
    c += 1
    b = a[b] - 1
    if d[b] == 1:
        print('-1')
        sys.exit()
    d[b] = 1

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

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
pos = 1
ans = 0
while pos != 2:
  pos = A[pos-1]
  ans += 1
  if ans == 100000:
    ans = -1
    break
print(ans)

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

ans = 0
visited = set([0])
next_visit_idx = 0
while True:
  next_visit_idx = a[next_visit_idx] -1
  ans += 1
  if next_visit_idx == 1:
    print(ans)
    break
  if next_visit_idx in visited:
    print(-1)
    break
    
  visited.add(next_visit_idx)

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

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


N = int(input().rstrip())
a_dict = {}
for i in range(N):
  a_dict[i+1] = int(input().rstrip())

target = 1
next_target = a_dict[target]
cont = True
ans = 0

if next_target == 2:
  cont = False
  ans += 1

while cont:
  target = next_target
  next_target = a_dict[target]
  ans += 1
  del a_dict[target]
  if next_target not in a_dict:
    ans = -1
    cont = False
  elif next_target == 2:
    ans += 1
    cont = False
print(ans)

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

Solution (Python):
from collections import defaultdict

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

memo = defaultdict(int)

cnt = 1
current = 1
memo[current] += 1

while True:
    after = a[current-1]
    if after==2:
        print(cnt)
        break
    elif memo[after]==1:
        print(-1)
        break
    else:
        memo[after] += 1
        current = after
        cnt += 1

Solution (Python):




n = int(input())
a_list = [None]*n

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

ans = 0
done_list = [0]*n
flag = False

now = 1
done_list[0] = 1

while True:
    now = a_list[now-1]
    ans += 1
    if now == 2:
        flag = True
        break
    elif done_list[now-1] == 1:
        break
    done_list[now-1] = 1
    
if flag:
    print(ans)
else:
    print("-1")

Solution (Python):
n = int(input())
a = [0]*n
for i in range(n):
  a[i] = int(input())
routed = {1}
steps = 0
for i in range(n):
  if i == 0:
    posn = 1
  elif posn == 2:
    break
  elif a[posn-1] in routed:
    steps = -1
    break
  posn = a[posn-1]
  steps += 1
  routed.add(posn)
print(steps)

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


d=[0]*N
d[0]=1
ans=0
light_sw = 0
while 1:
  light_sw = a[light_sw]
  if d[light_sw]==0:
    d[light_sw]=1
  else:
    print(-1)
    exit()
  ans+=1
  if light_sw == 1:
    print(ans)
    exit()

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

Solution (Python):
from sys import stdin

n = int(stdin.readline().strip())
a_lst = [-1]
for _ in range(n):
  a_lst.append(int(stdin.readline().strip()))

if 2 not in a_lst:
  print(-1)
else:
  is_visited = [False] * (n+1)
  cnt = 0
  pos = 1
  while(True):
    if(pos == 2):
      print(cnt)
      break
    elif(is_visited[pos]):
      print(-1)
      break
    else:
      is_visited[pos] = True
      pos = a_lst[pos]
      cnt += 1

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
visited={}
for i in range(1,n+1):
    visited[i]=-1
count=0
i=0
while True:
    count+=1
    if visited[a[i]]==0:
        print("-1")
        exit()
    if a[i]==2:
        print(count)
        exit()
    visited[a[i]]=0
    i=a[i]-1

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

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

if num != 2:
    count = -1

print(count)

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

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

print(-1)

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

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

next = 0
visited = [False] * N
cnt = 0
while(True):
    if visited[next]:
        cnt = -1
        break
    if next == 1:
        break
    visited[next] = True
    next = A[next] -1
    cnt += 1
print(cnt)

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

now = 0
count = 0
while True:
    now = botan[now] - 1
    count += 1
    if count == N:
        print('-1')
        break
    if now == 1:
        print(count)
        break

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

def main():
    n = int(input())
    a = []
    now = 0

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

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

main()

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

def main():
    n = int(input())
    a = []
    l = [0]*(n + 1)
    now = 0

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

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

main()

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

def main():
    n = int(input())
    a = []
    l = [0]*(n + 1)
    cnt = 0
    now = 1

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

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

main()

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

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

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

print(-1)

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

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

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

s = 1
cnt = 0
arrive = [0] * (N+1)
while s != 2:
    if arrive[s] == 1:
        print(-1)
        exit()
    arrive[s] = 1
    s = g[s]
    cnt += 1

print(cnt)

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

Solution (Python):
import sys
n = int(input())
ns = [int(input()) for _ in range(n)]
if not 2 in ns:
    print(-1)
    sys.exit(0)
i = 0
cnt = 0
pushed = [0]*n
flg = False
while(True):
    cnt += 1
    i = ns[i] - 1
    if pushed[i] == 1:
        break
    pushed[i] = 1
    if i == 1:
        flg = True
        break
if flg:
    print(cnt)
else:
    print(-1)

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

visited = [False] * n

here = 0
for i in range(n):
    visited[here] = True
    moveto = a_list[here] - 1

    if moveto == 1:
        print(i+1)
        exit()

    if visited[moveto]:
        break

    here = moveto

print("-1")
exit()

Solution (Python):
from sys import exit

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

i = 0
cnt = 0
while 1:
    if i == 1:
        print(cnt)
        exit()

    if cnt>=n:
        print(-1)
        exit()

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

Solution (Python):
def main():
    n = int(input())
    As = [int(input()) for _ in range(n)]

    x = [0]*n
    i = 0
    ans = 0
    while x[i] == 0:
        ans += 1
        x[i] = 1
        i = As[i] - 1
        if i == 1:
            print(ans)
            exit()
    print(-1)


if __name__ == "__main__":
    main()

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

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

for i in range(len(a)):
    if a[value-1] == 2:
        break
    value = a[value-1]
    count += 1

if count > len(a):
    count = -1

print(count)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()

a = [INT() for _ in range(N)]
tmp = [0]*(N+1)
tmp2 = a[0]
cnt = 0
while 1:
	if tmp[tmp2] == 1:
		print(-1)
		break
	elif tmp2 == 2:
		print(cnt+1)
		break
	tmp[tmp2] = 1
	tmp2 = a[tmp2-1]
	cnt += 1

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()

a = [INT() for _ in range(N)]
tmp = [0]*N
tmp2 = a[0]-1
cnt = 0
while 1:
	if tmp[tmp2] == 1:
		print("-1")
		break
	elif tmp2 == 1:
		print(cnt+1)
		break
	tmp[tmp2] = 1
	tmp2 = a[tmp2] -1
	cnt += 1

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
a = [INT() for _ in range(N)]

ans = 0
loc = 1

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

else:
	print(-1)

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

cnt = 0
n = 1
f = False
while cnt <= N:
  cnt += 1
  n = A[n - 1]
  if n == 2:
    f = True
    break
print(cnt if f else -1)

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

    prev_idx = 1
    flag = False
    num = 0
    seen = [-1] * N
    while True:
        if prev_idx == 2:
            flag = True
            break

        
        
        if seen[prev_idx-1] != -1:
            break
        seen[prev_idx-1] = num
        prev_idx = A[prev_idx-1]
        num += 1

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


if __name__ == '__main__':
    main()

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

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

route = [0]*N

next = 1
count = 0

while next != 2:
    if route[next-1]!=0:
        print(-1)
        exit()
    else:
        route[next-1] = 1
        next = a[next-1]
        count += 1

print(count)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
pos =0
cnt=0
roop = []
visit =[0]*n
while visit[pos]!=2:
    if visit[pos]==0:
        visit[pos]+=1
        if pos == 1:
            print(cnt)
            break
        else:
            cnt+=1
            pos=a[pos]-1
    else:
        print(-1)
        break

Solution (Python):
N = int(input())
A = list()
for _ in range(N):
    A.append(int(input()))
ans = 0
now = 1
for _ in range(N):
    if now == 2:
        break
    now = A[now - 1]
    ans += 1
print(ans if ans != N else -1)

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

x = 0

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

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

import sys

bottom = [-1]*(N+1)
tmp = 1
point = 0

while bottom[tmp] == -1:
    bottom[tmp] = 0
    tmp = a[tmp]
    point += 1
    if tmp == 2:
        print(point)
        sys.exit()
print(-1)

Solution (Python):
import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
b = [0] * N
i = 0
c = 1
while True:
    if b[i] == 1:
        print(-1)
        sys.exit()
    b[i] = 1
    i = a[i] - 1
    if i == 1:
        print(c)
        sys.exit()
    c += 1

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
pushed = [0 for _ in range(n)]
pushed[0] = 1
counter = 1
start = 1
end = a[start-1]
while True:
    if pushed[end-1] == 1:
        print(-1)
        exit()
    else:
        if end == 2:
            print(counter)
            exit()
        else:
            counter += 1
            pushed[end-1] = 1
            start = end
            end =  a[start-1]

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

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

now=1
c=0

while c<N:
    now=A[now-1]
    c+=1
    if(now==2):
        print(c)
        break
else:
    print("-1")

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

a = []

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

x = 0
for i in range(100000):
    x = a[x]
    if x == 1:
        print(i+1)
        exit()

print(-1)

Solution (Python):

N = int(input())
flag = 0
X = []
ans = 0
for i in range(N):
    a = int(input())
    X.append(a)
    if a == 2:
        flag = 1
b = X[0]
count = 1


if flag == 0:
    ans = -1

else:
    for i in range(N):
        c = X[b-1]
        b = c
        
        count += 1
        if c == 2:
            ans = count
            break
if ans == 0:
    ans = -1
    
if X[0] == 2:
    print(1)
else:
    print(ans)

Solution (Python):
def main():
    N=int(input())
    a=[int(input()) for i in range(N)]
    tmp=1
    for i in range(N):
        if tmp==2:
            print(i)
            exit()
        tmp = a[tmp-1]
    print(-1)
main()

Solution (Python):
n = int(input())
a = [0]+[int(input()) for _ in [0]*n]
used = [False, True]+[False]*(n-1)
ans = 0
now = 1
while now!=2:
    if not used[a[now]]:
        used[a[now]] = True
        now = a[now]
        ans += 1
    else:
        print(-1)
        break
else:
    print(ans)

Solution (Python):
n = int(input())
a = [0]+[int(input()) for _ in [0]*n]
ans = 0
now = 1
while now!=2:
    now = a[now]
    ans += 1
    if ans > n:
        ans = -1
        break
print(ans)

Solution (Python):




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

doflg = True
idx = 1
cnt = 1
while(doflg):
    if a[idx] == -1:
        ans = -1
        doflg = False
    elif a[idx] == 2:
        ans = cnt
        doflg = False
    else:
        cnt = cnt + 1
        sav_idx = idx
        idx = a[idx]
        a[sav_idx] = -1


print(ans)

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

tmp = a[0]-1
for i in range(N):

    if tmp == 1:
        print(i+1)
        exit()
    tmp = a[tmp]-1

print(-1)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

pos = 1
ans = 0
reached_index = set([1])
while True:
    pos = A[pos]
    ans += 1
    if pos in reached_index:
        ans = -1
        break
    if pos == 2:
        break
    reached_index.add(pos)

print(ans)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

pos = 1
ans = 0
reached_index = set()
while True:
    pos = A[pos]
    ans += 1
    if pos in reached_index:
        ans = -1
        break
    if pos == 2:
        break
    reached_index.add(pos)

print(ans)

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

b = [0]*(N+1)

button2 = [] 

for i in range(1,N+1):
    a = int(input())
    b[i] = a
    if a == 2:
        button2.append(i)
        
now = 1
count = -1

for i in range(0, N+1):

    if now == 2:
        count = i
        break
    
    now = b[now]

print(count)

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

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

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

pre = 1
flg = False
cnt = 0
for i in range(N+1):
  if pre == 2:
    flg = True
    break
  if pre == a_list[pre-1]:
    break
    
  pre = a_list[pre-1]
  cnt += 1
    
if flg:
  print(cnt)
else:
  print(-1)

Solution (Python):
n = int(input())
a_list = [int(input()) for nesya in range(n)]
s = 1
a = 1
while a <= n:
  s = a_list[s-1]
  if s == 2:
    print(a)
    break
  elif a == n:
    print(-1)
  a += 1

Solution (Python):

n = int(input())
DONE = 1

btns = []
for i in range(n):
    btns.append([int(input()), 0])

current = 1
count = 0
while(current != 2):
    if btns[current - 1][1] == DONE:
        print(-1)
        exit()
    btns[current - 1][1] = DONE
    current = btns[current - 1][0]
    count += 1
else:
    print(count)