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



Sample input:
3
3
1
2


Sample output:
2

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
now=0
for i in range(n):
    if a[now]==2:
        print(i+1)
        exit()
    else:
        now=a[now]-1
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(input()) for i in range(n)]
ans = {}
i = 1
while True:
  i = l[i-1]
  if i in ans:
    print(-1)
    exit()
  else:
    ans[i] = 1
    
  if i == 2:
    print(len(ans))
    exit()

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
B = []
cnt = 0
i = 0
while i != 1:
    i = A[i]-1
    B.append(i)
    cnt += 1
    if cnt > 10**5 + 1:
        print(-1)
        exit()
print(cnt)

Submission status: Accepted

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

i = 0
c = 0
while a[i] != 2:
    if a[i] == 0:
        c = -2
        break
    tmp = a[i]
    a[i] = 0
    i = tmp - 1
    c += 1
print(c + 1)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
l=[]
c=1
i=0
f=False
for _ in range(100001):
    i+=1
    c=a[c-1]
    if c==2:
        f=True
        break
if f:print(i)
else:print(-1)

Submission status: Accepted

Solution code:
N = int(input())

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

i = 1
result = 1
while True:
    if t[i] == -1:
        print(-1)
        exit()
    if t[i] == 2:
        print(result)
        exit()
    result += 1
    j = t[i]
    t[i] = -1
    i = j

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
s=set()
tmp=1
cnt=0
while tmp not in s:
    s.add(tmp)
    tmp =a[tmp-1]
    cnt+=1
    if tmp==2:
        break
else:
    cnt=-1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a = [0] * N
for i in range(N):
    k = int(input())
    li = [i+1,k]
    a[i] = li
a.insert(0,[0,0])
import sys
sys.setrecursionlimit(100000000)
seen = [False] * (N+1)
def dfs(i):
    seen[i] = True
    if seen[a[i][1]] == False:
        dfs(a[i][1])
dfs(1)
def search(a):
    import collections
    from collections import deque
    que = deque()
    count = 0
    que.append(1)
    while len(que) != 0:
        i = que.popleft()
        if i != 2:
            count+=1
            que.append(a[i][1])
        if i == 2:
            return count
if seen[2]:
    k = search(a)
    print(k)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
l = [int(input()) for _ in range(n)]
s = set()
tmp = 1
cnt = 0
while tmp not in s:
  s.add(tmp)
  tmp = l[tmp-1]
  cnt += 1
  if tmp == 2:
    break
else:
  cnt = -1
print(cnt)

Submission status: Accepted

Solution code:
import sys

n=int(input())
a=[int(input()) for i in range(n)]
count=0
num=1

while(num!=2):
	num=a[num-1]
	count+=1
	if count>=n:
		count=-1
		break

print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = [0]
tmp = [int(input()) for _ in range(n)]
a = a + tmp

r = -1
cnt = 1
next = a[1]
while cnt < n:
  if next == 2:
    r = cnt
    break
  else:
    next = a[next]
    cnt += 1
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
cnt = idx = 0
for i in range(n):
    cnt += 1
    if a[idx] == 2:
        break
    if a[a[idx] - 1] == idx + 1:
        cnt = -1
        break
    if i == n - 1:
        cnt = -1
        break
    idx = a[idx] - 1
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
a=[]
visitflag=0
count=0
for i in range(N):
    suc=int(input())
    a.append([suc,visitflag])

i=0
#print(a)
#input()
while True:
    if a[i][1]==0:
        a[i][1]=1
        success=(a[i][0])-1
        i=success
        count=count+1
        if i==1:
            print(count)
            break
    else:
        print("-1")
        break
    #print(a)
    #input()

Submission status: Accepted

Solution code:
n = int(input())
buttons = [0 for _ in range(n)]
for i in range(n):
    buttons[i] = int(input())

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
buttons = list(map(int, sys.stdin))

light = buttons[1]
for i in range(buttons[0]):
    if light == 2:
        print(i+1)
        break
    light = buttons[light]
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())

al = [0 for _ in range(n)]
for i in range(n):
    al[i] = int(input())

ans = 1
i = 0
while True:
    if al[i] == 2:
        print(ans)
        break
    elif al[i] == -1:
        print(-1)
        break
    tmp = i
    i = al[i]-1
    al[tmp] = -1
    ans += 1

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
i = a[0]
counter = 1
while counter <= n+1:
  if i == 2:
    print(counter)
    exit()
  else:
    i = a[i-1]
    counter += 1
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
A = [0]*n
for i in range(n):
    A[i] = int(input())-1

i = 0
d = 0
c = 0
for _ in range(n):
    c += 1
    d = A[i]
    if d==1:
        break
    else:
        i = d
print(c if c!=n else -1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    result = 1
    count = -1
    for i in range(N):
        result = A[result - 1]
        if result == 2 and count < 0:
            count = (i + 1)
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input())-1 for _ in range(n)]
c = a[0]
cnt = 1
while cnt <= n:
    if c == 1:
        print(cnt)
        break
    else:
        c = a[c]
        cnt += 1
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a_i = []
for x in range(n):
    a_i.append(int(input()))
a_i2 = []
num = 1
for i in range(n):
    a_i2.append(num)
    num = a_i[num-1]
if a_i2.count(2) == 0:
    print(-1)
else:
    print(a_i2.index(2))

Submission status: Accepted

Solution code:
n = int(input())
a_i = []
for x in range(n):
    a_i.append(int(input()))
a_i_Order = []
btn = 1
for x in range(n):
    a_i_Order.append(a_i[btn - 1])
    btn = a_i[btn - 1]
if a_i_Order.count(2) == 0: print(-1)
else: print(a_i_Order.index(2) + 1)

Submission status: Accepted

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

ans=-1
count=0
tmp=1

for i in range(1,n+1):
    tmp=li[tmp-1]
    if tmp == 2:
        ans=i
        break

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lis = []
for i in range(n):
    lis.append(int(input()))
val = set()
i = 1
cnt =0
while True:
    #print(i)
    cnt += 1
    if lis[i-1] == 2:
        break
    if  lis[i-1] in val:
        cnt = -1
        break
    val.add(lis[i-1])
    i = lis[i-1]

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
B = [int(input()) for _ in range(N)]

count = 0
next_index = 1

for _ in range(N):
    if next_index == 2:
        break

    next_index = B[next_index-1]
    count += 1

if count == N:
    count = -1
print(count)

Submission status: Accepted

Solution code:
N=int(input())
BU=[int(input())-1 for i in range(N)]
ans=-1
onid=0
for i in range(1,N+1):
  onid=BU[onid]
  if onid==1:
    ans=i
    break
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N =int(input())
a =[]
for _ in range(0,N):
    a.append(int(input()))
b =[0]*N
b[0]=1
f=0
for i in range(1,N):
    b[i]=a[b[i-1]-1]
    if b[i]==2:
        f=i
        break
if f>0:
    print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
l={i+1:int(input()) for i in range(n)}
c=1
i=1
while(i<=n):
  c=l.get(c)
  if c==2:
    print(i)
    exit()
  i+=1
print(-1)

Submission status: Accepted

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

cnt = 0
buttom = 1
for i in range(n):
    buttom = a[buttom-1]
    cnt += 1
    if buttom == 2:
        print(cnt)
        quit()
print(-1)

Submission status: Accepted

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

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

print(-1)

Submission status: Accepted

Solution code:
"""
author : halo2halo
date : 30, Jan, 2020
"""

import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())
ans = 0

temp = 1
bl = False
for _ in range(len(A)):
    temp = A[temp-1]
    ans += 1
    if temp == 2:
        bl = True
        break
print(ans if bl else -1)

Submission status: Accepted

Solution code:
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
ans = 0
now = 1
while (1):
    now = l[now - 1]
    ans += 1

    if now == 2:
        print(ans)
        exit()
    if ans > n:
        print(-1)
        exit()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = [int(input())-1 for _ in range(n)]
# -1をしたので、if a[s] == 1になったら終了
#print(a)
cnt = 0
visit = [0] #出発点は訪れてる
i = 0 # bottom position a[i] = next_bottom
while cnt <= n:#無限ループになる理由がわからん in が最悪のケースでO(nlogn)
    cnt += 1
    #print(visit)
    """if a[i] in visit:#循環するのでbreak
        print(-1)
        exit(0)"""
    if a[i] == 1: #ボタン２を押すので今までの回数を答える
        print(cnt)
        exit(0)
    else:# i を更新　visitに現在のボタンを追加する
        #visit.append(a[i])
        i = a[i]
print(-1)

Submission status: Accepted

Solution code:


def main():
    N = int(input())
    a = [int(input()) for _ in range(N)]
    
    b = a[0]
    count = 1
    if b == 2:
        ans = 1
    else:
        while True:
            count += 1
            b = a[b - 1]
            if b == 2:
                ans = count
                break
        
            if count > N:
                ans = -1
                break
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = {i+1: int(input()) for i in range(N)}

on = 1
history = {1}
ans = -1
count = 0
while True:
    on = a[on]
    count += 1
    if on == 2:
        ans = count
        break
    if on in history:
        break
    history.add(on)
print(ans)

Submission status: Accepted

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

now=A[0]
can=1

for i in range(2*n):
    if now==2:
        print(i+1)
        can=0
        break

    now=A[now-1]

if can:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
current = 0
visited = {0}
ans = 0

while True:
    current = A[current] - 1
    ans += 1

    if current in visited:
        print(-1)
        break

    if current == 1:
        print(ans)
        break

    visited.add(current)

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(input()) for _ in range(n))
i= 1
cnt=0

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

print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

check = [0] * n
check[0] = 1

i = 0
count = 0
is_success = False
while True:
    count += 1
    i = a[i]
    if i == 2:
        is_success = True
        break
    i -= 1
    if check[i] != 0:
        break
    check[i] = 1
print(str(count) if is_success else '-1')

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    A=[]
    check=[0]*n
    temp=0
    ct=1
    for i in range(n):
        A.append((INT()-1))
    check[0]=1
    for i in range(n):
        if A[temp]==1:
            print(ct)
            break
        elif check[A[temp]]==1:
            print(-1)
            break
        else:
            temp=A[temp]
            check[temp]=1
            ct+=1
            
if __name__ == '__main__':
    do()

Submission status: Accepted

Solution code:
N = int(input())
list=[]
for i in range(N):
    list.append(int(input()))
count=0
X=list[0]
while True:
    count+=1
    if X == 2:
        print(count)
        break
    elif count == N:
        print(-1)
        break
    X=list[X-1]

Submission status: Accepted

Solution code:
n=int(input())
A={i:int(input()) for i in range(1,n+1)}
now=1
cnt=0
while now!=2:
    if now in A:
        now=A.pop(now)
        cnt+=1
    else:
        print(-1)
        break
else:
    print(cnt)

Submission status: Accepted

Solution code:
# 要復習
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    A = [INT() for i in range(N)]
    visited = []
    n = 0
    ans = 0
    while ans < N:
        n = A[n] - 1
        ans += 1
        if n == 1:
            print(ans)
            exit()
    print(-1)





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input())for _ in range(N)]
visit = [False] * (N+1)
pre = 1
ans = 0
visit[1] = True
while True:
    ans += 1
    x = A[pre-1]
    if visit[x]:
        break
    visit[x] = True
    if x == 2:
        break
    pre = x
if not visit[2]:
    ans = -1
print(ans)

Submission status: Accepted

Solution code:
#python3
from collections import deque
def main():
    n = int(input())
    a = [int(input())-1 for _ in range(n)]

    Q = deque()
    Q.append(a[0])
    
    cnt = 1
    flag = False
    for _ in range(n):
        x = Q.popleft()
        if x == 1:
            flag = True
            break
        cnt += 1
        Q.append(a[x])

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

Submission status: Accepted

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

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

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

print(-1)

Submission status: Accepted

Solution code:
#from fractions import gcd
#mod = 10 ** 9 + 7
#N = int(input())
#a = list(map(int,input().split()))
#a,b,c = map(int,input().split())
#ans = [0] * N

def intinput():
    return int(input())

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

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

N = intinput()
a = []
for i in range(N):
    a.append(intinput())
state = 1
flag = 0
count = 0
for i in range(N):
    if a[state-1] == 2:
        flag = 1
        count += 1
        break
    else:
        state = a[state-1]
        count += 1

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

Submission status: Accepted

Solution code:
N = int(input())
button = [0]*(N+1)
for i in range(1,N+1):
    button[i] = int(input())
current_button = 1
count = 0
while count < 10**5:
    current_button = button[current_button]
    count += 1
    if current_button == 2:
        print(count)
        exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
n=int(input())
a = [int(input()) for i in range(n)]
s=1
ans=0

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input())-1 for i in range(n)]
c,x=1,0
for i in range(n):
  x=a[x]
  if x==1:
    print(i+1)
    break
else:
  print(-1)

Submission status: Accepted

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

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

print(count if n == 1 else -1)

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys
input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


# import re
# import array
# import copy
# import functools
# import operator

# import math
# import string
# import fractions
# from fractions import Fraction
# from fractions import gcd

# import collections
# import itertools
# import bisect

# import heapq
# from heapq import heappush
# from heapq import heappop
# from heapq import heappushpop
# from heapq import heapify
# from heapq import heapreplace
# from queue import PriorityQueue as pq
from queue import Queue

# from itertools import accumulate

# from collections import deque
# from collections import Counter

# from operator import mul
# from functools import reduce

# def lcm(n,m):
#     return int(n*m/gcd(n,m))

# def reduce(p, q):
#     common = fractions.gcd(p, q)
#     return (p//common , q//common )

# def combinations_count(n, r):
#     r = min(r, n - r)
#     numer = reduce(mul, range(n, n - r, -1), 1)
#     denom = reduce(mul, range(1, r + 1), 1)
#     return numer // denom

# import random
# import time


def main():
    n = int(input().strip())
    a = [0 for i in range(n)]
    for i in range(n):
        a[i] = int(input().strip()) - 1
    cnt = 0
    que = Queue()
    que.put(0)
    l_usedlist = [0 for i in range(n)]
    while que.qsize() > 0:
        v_current = que.get()
        l_usedlist[v_current] = 1
        cnt += 1
        if a[v_current] == 1:
            print(cnt)
            return
        else:
            if l_usedlist[a[v_current]] == 0:
                que.put(a[v_current])
    print(-1)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

done = [False]*N
i = 0
cnt = 0

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

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a = [0]
for _ in range(N):
    a.append(int(input()))
x = 1
visited = set([1])
answer = 0

while True:
    x = a[x]
    if x in visited:
        answer = -1
        break
    visited.add(x)
    answer += 1
    if x == 2:
        break

print(answer)

Submission status: Accepted

Solution code:
n = int(input())
lis = []
for i in range(n):
    lis.append(int(input()))
button = lis[0]
count = 1
if button == 2:
    ans = 1
else:
    while True:
        count += 1
        button = lis[button - 1]
        if button == 2:
            ans = count
            break
        if count > n:
            ans = -1
            break
print(ans)

Submission status: Accepted

Solution code:

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

k=1
t=-1

for i in range(N):
    k=l[k-1]
    if k==2:
        t=i+1
        break
print(t)

Submission status: Accepted

Solution code:
n = int(input())

bList = []
#offset
bList.append(-1)
for i in range(n):
  bList.append(int(input()))


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

Submission status: Accepted

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

ans = 0
tmp = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = {}
cnt = 0

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

btn = 1
while True:
    cnt += 1
    if btn == 0:
        ans = -1
        break
    elif d[btn] == 2:
        ans = cnt
        break
    else:
        tmp = btn
        btn = d[btn]
        d[tmp] = 0

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
buttons = [int(input()) - 1 for _ in range(N)]
#N = 5
# buttons = [2, 2, 3, 1, 3]
# N = 3
# buttons = [2, 0, 1]


def judge(buttons):
    move_cnt = 1
    # 最初押してそのボタンが1だったら終了
    next_idx = buttons[0]
    if next_idx == 1:
        return move_cnt, True

    while True:
        move_cnt += 1
        if move_cnt > N:
            return None, False
        next_idx = buttons[next_idx]
        if next_idx == 1:
            return move_cnt, True
    return move_cnt, True


cnt, is_Yes = judge(buttons)
if is_Yes:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:

def Trained():
    button = []
    N = int(input())
    for _ in range(N):
        button.append(int(input()) - 1)
    adr = 0
    for i in range(N):
        adr = button[adr]
        if adr == 1:
            print(i + 1)
            return()
    print(-1)
    return()
Trained()

Submission status: Accepted

Solution code:
N=int(input())
li = []

now = 0
ans = -1
count = 0

for n in range(N):
  li.append(int(input()))

for n in range(N + 1):
  if n == 0:
    now = li[0]
    count += 1

    if now == 2:
      ans = count
      break

  now = li[now - 1]
  count += 1

  if now == 2:
    ans = count
    break

print(ans)

Submission status: Accepted

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

lst=set()
i=0
count=0
while not i in lst:
    lst.add(i)
    i=A[i]-1
    count+=1
    if i==1:
        print(count)
        exit()



print(-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
now=0
a=[int(input()) for i in range(n)]
for i in range(n):
    if a[now]==2:
        print(i+1)
        exit()
    else:
        now=a[now]-1
print(-1)

Submission status: Accepted

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


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

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

Submission status: Accepted

Solution code:
N = int(input())
a = {i: int(input()) for i in range(1, N + 1)}
result = 0
i = 1
while True:
    if not i in a:
        result = -1
        break
    result += 1
    nexti = a[i]
    if nexti == 2:
        break
    del a[i]
    i = nexti
print(result)

Submission status: Accepted

Solution code:
n=int(input())
num=[int(input()) for i in range(n)]
a=num[0]
b=0

for i in num:
  if a==2:
    b+=1
    print(b)
    exit()
  else:
    b+=1
    a=num[a-1]
    
print("-1")

Submission status: Accepted

Solution code:


import numpy as np
import fractions as fra

n=int(input())
a = [0] + [int(input()) for _ in range(n)]
#print(a)

k=1
c=0
while(c<=n*2):
    k=a[k]
    c+=1
    if(k==2):break

if(c>=n):c=-1
print(c)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
memo = set()
memo.add(0)
count = 1
i = a[0] - 1

while True:
  if i == 1:
    break
  i = a[i] - 1
  count += 1
  if i in memo:
    count = -1
    break
  else:
    memo.add(i)
print(count)

Submission status: Accepted

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

# 1: now, -1: visited, 0: none
b[0] = 1
now = 0
count = 0
while (b[1] != 1 and count <= n):
   next = a[now] - 1

   if (b[next] == -1):
      print(-1)
      exit()

   b[now] = -1
   b[next] = 1

   now = next
   count += 1

if (b[1] == 1):
   print(count)
else:
   print(-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
g = [0] * (N + 1)
for i in range(N):
    g[i + 1] = int(input())
cnt = 0
now = 1
for i in range(10 ** 5):
    cnt += 1
    now = g[now]
    if now == 2:
        print(cnt)
        sys.exit()
print(-1)

Submission status: Accepted

Solution code:
N = int(input())

amaps = { i + 1: int(input()) for i in range(N) }

if 2 not in amaps.values():
    print(-1)
    exit()

current = amaps[1]
switches = 1
max_switches = len(amaps)

while switches <= max_switches:
    if current == 2:
        print(switches)
        exit()

    if current not in amaps:
        break
    current = amaps[current]
    switches+=1

print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(input()) for _ in range(n))

b = 1

for i in range(1, n):
    b = a[b-1]
    if b == 2:
        print(i)
        exit()
print(-1)

Submission status: Accepted

Solution code:
a=[input() for _ in range(int(input()))]
now=1
cnt=0
while cnt<=len(a):
    now=int(a[now-1])
    cnt+=1
    if now==2: break
print(cnt if now==2 else '-1')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

n = int(input())
a = ["" for _ in range(n)]
for i in range(n):
    a[i] = int(input())
    
#print(a)

count = 0
isOn = False
i = 0
for k in range(n):
#    print("pushed")
    count += 1
    if (i + 1) == 2:
        print(count-1)
        isOn = True
        break
    i = a[i] - 1
#    print("i",i)
if isOn:
    pass
else:
    print(-1)

Submission status: Accepted

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

lst = [a[0]]
for i in range(1,n):
    lst.append(a[lst[i-1]-1])

    if lst[i]==2:
        break

if 2 in lst:
    print(lst.index(2)+1)
else:
    print(-1)

Submission status: Accepted

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

idx = 1
cnt = 0
for _ in range(N):
  idx = A[idx]
  cnt += 1
  if idx == 2:
    print(cnt)
    exit()
else:
  print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
cnt=0
now=0
for i in range(n):
  a.append(int(input())-1)
flg=False
for i in range(n):
  if now==1:
    flg=True
    break
  now=a[now]
  cnt+=1
print(cnt if flg else -1)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input())-1 for i in range(n)]
now=0
x=[0]*n
i=0
while x[now]==0:
    i+=1
    x[now]=1
    now=a[now]
    if now==1:
        print(i)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
A=list(int(input()) for _ in range(n))
s=A[0]
ans=1
for i in range(n):
    if s==2:
        print(ans)
        exit()
    s=A[s-1]
    ans += 1
print('-1')

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
seen = [False] * n
seen[0] = True
ans = 0
now = 1
while not now == 2:
    ans += 1
    now = a[now-1]
    if seen[now-1] == True:
        print(-1)
        exit(0)
    seen[now-1] = True
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=[0]*n
i=0
s=0
while i!=1 and b[i]==0:
    b[i]=1
    i=a[i]-1
    s+=1
if i==1:
    print(s)
else:
    print(-1)

Submission status: Accepted

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

nex=a[0]
cnt=1
while nex != 2 and cnt<N:
    cnt+=1
    nex=a[nex-1]
    
if cnt<N:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
    
ans = set()
i = 0
count = 0
while True:
    if a[i] == 2:
        print(count+1)
        exit()
    elif a[i]-1 not in ans:
        ans.add(a[i]-1)
        count += 1
        i = a[i] - 1
    else:
        print(-1)
        exit()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#H, W = map(int, input().split())
#S = list(map(int, input().split()))
N = int(input())
a = []
a.append(0)
for _ in range(N) :
  a.append(int(input()))
#print (a)

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

Submission status: Accepted

Solution code:
n = int(input())
li = [0]
for i in range(n):
    li.append(int(input()))
x = 1
visited = set([1])
ans = 0

while True:
    x = li[x]
    if x in visited:
        ans = -1
        break
    visited.add(x)
    ans += 1
    if x == 2:
        break
print(ans)

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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