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:
def solve():
    n = int(input())
    l = []

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

    a = 1
    hist = set([])
    while(True):
        if(a-1 in hist):
            return -1
        if(a-1 == 1):
            return len(hist)
        hist.add(a-1)
        a = l[a-1]

if(__name__ == "__main__"):
    print(solve())

Submission status: Accepted

Solution code:
#Trained?
import numpy as np
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
    
p=1
cnt=0
while 1:
    cnt+=1
    p = a[p-1]
    if(p==2):
        print(cnt)
        break
    if(cnt>N):
        print(-1)
        break

Submission status: Accepted

Solution code:
import sys

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

count = 0
now = 1

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

print(-1)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import sys
N = int(input())
a = [ int(input()) for tmp in range(N) ]
ans = 0
index = 0
while ans < len(a):
    sav = index #last index
    index = a[sav]-1 #new index
    ans += 1
    if index == 1:
        print(ans)
        sys.exit()
    if a[index] == sav+1: #Loop
        print("-1")
        sys.exit()
print("-1")

Submission status: Accepted

Solution code:
n = int(input())
buttons = [int(input()) for i in range(n)]
pushing = 1
counter = 0
while counter <= n:
    pushing = buttons[pushing - 1]
    counter += 1
    if pushing == 2:
        print(counter)
        break
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

count=1
ans=0

while c[1]==0:

    if c[count-1] != 0:
        print(-1)
        exit()
    else:
        c[count-1]=1
        count=a[count-1]
        ans += 1


print(ans-1)

Submission status: Accepted

Solution code:
N = int(raw_input())
a = []
for i in range(N):
	a.append(raw_input())
#print a
for i in range(N):
	a[i] = int(a[i])
cnt = 0
trn = 0
while trn != 1:
	if trn == -2:
		cnt = -1
		break
	#print a

	hako = trn
	trn = a[trn] - 1
	a[hako] = -1
	cnt += 1
	
print cnt

Submission status: Accepted

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

import sys
import subprocess
import json
import time
import math
import re
import sqlite3

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

visited = [False] * N
cur = 0
cnt = 0
while True:
    if visited[cur]:
        print(-1)
        exit(0)    
    if cur == 1:
        print(cnt)
        exit(0)

    visited[cur] = True    
    cur = a[cur]
    cnt += 1

Submission status: Accepted

Solution code:
import sys

n=int(raw_input())
l=[]
cnt=0
for i in xrange(n):
 l.append(int(raw_input()))
if l[0]==2:
 print 1
 sys.exit()
i=0
cnt=1
next=0
while i<n:
 next=l[next]-1
 if l[next]==2:
  print cnt+1
  sys.exit()
 cnt+=1
 i+=1
print -1

Submission status: Accepted

Solution code:
n = int(raw_input())
l = [[0,False]]
for _ in range(n):
    l.append([int(raw_input()), False])
l[1][1] = True
p = 1
while True:
    p = l[p][0]
    if l[p][1] == True:
        break
    l[p][1] = True
    if p == 2:
        break
if l[2][1] == True:
    c = 0
    for [_,f] in l:
        if f == True:
            c += 1
    print c - 1
else:
    print -1

Submission status: Accepted

Solution code:
N=int(input())
a=[int(input()) for i in range(N)]
b=[]
j=1
k=1
c=1
if 2 not in a:
    print(-1)
else:
    while j in range(1,N+1):
         c += 1
         if a[k-1]==1:
                 print(-1)
                 break
         elif a[k-1]==2:
                 print(c-1)
                 break
         else:
            j += 1
            k = a[k-1]
    if c>=(N+1):
        print(-1)

Submission status: Accepted

Solution code:
N=int(input())
A=[ int(input()) - 1 for _ in range(N) ]
B=[ False ] * N

ans = 0
i=0
while True:
  if B[i]:
    ans = -1
    break
  if i == 1:
    break
  B[i] = True
  i = A[i]
  ans += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(input()) for _ in range(N)]
b=[False]*N
count=0
i=0
b[i]=True
flag=True
while b[1]!=True:
    if(b[a[i]-1]==True):
        print(-1)
        flag=False
        break
    b[a[i]-1]=True
    i=a[i]-1
    count+=1
if flag:
    print(count)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()



def main():
    n = I()
    a = [I()-1 for _ in range(n)]
    f = [False] * n
    t = 0
    c = 0
    while True:
        if t == 1:
            return c
        if f[t]:
            return -1
        f[t] = True
        t = a[t]
        c += 1


print(main())

Submission status: Accepted

Solution code:
n = int(raw_input())
lst = []
btn = []
for i in range(0, n):
	lst.append(True)
	btn.append(int(raw_input()))
tmp = 0
cnt = 0
while lst[tmp]:
	lst[tmp] = False
	if tmp == 1:
		break
	elif lst[btn[tmp]-1]:
		cnt += 1
	else:
		cnt = -1
	tmp = btn[tmp]-1

print cnt

Submission status: Accepted

Solution code:
#python
import sys

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

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

i=1

while a[i-1]!=2:

	if a[i-1]==0:
		print "-1"
		sys.exit()

	#print i,a[i-1]
	before=i
	i=a[i-1]
	a[before-1]=0
	#print a
	

print a.count(0)+1

Submission status: Accepted

Solution code:
import sys

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

light = 1
for i in range(N):
    if light == 2:
        print(i)
        break
    light = a[light]

if light != 2:
    print(-1)

Submission status: Accepted

Solution code:
def training():
	N = int(input())

	A = [0]

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

	counter = 1
	index = 1
	#judge = [1]
	if A[index] == 2:
		print(counter)
		return
	counter += 1
	index = A[index]
	for i in range(N):
		#if A[index] in judge:
			#print("-1")
			#return
		if A[index] == 2:
			print(counter)
			return
		counter += 1
		#judge.append(A[index])
		index = A[index]
	print("-1")
	

training()

Submission status: Accepted

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

while True:
    i = a[i-1]
    count += 1
    if i == 1 or count == n+1:
        print(-1)
        break
    elif i == 2:
        print(count)
        break

Submission status: Accepted

Solution code:
n = int(input()) 
xs = [int(input())-1 for i in range(n)]
v = [False] * n

i = 0
ans = 0
while True:
  if i == 1:
    break
  elif v[i]:
    ans = -1
    break
  v[i] = True
  ans += 1
  i = xs[i]

print(ans)

Submission status: Accepted

Solution code:
import sys
N=int(input())
a=[int(input()) for i in range(N)]
i=1
s=1
while i<N:
  if a[s-1]==2:
    print(i)
    sys.exit()
  s=a[s-1]
  i+=1
  
print(-1)

Submission status: Accepted

Solution code:
import sys


def main():
    n = int(input())
    buttons = [int(input()) for _ in range(n)]

    def push(x):
        return buttons[x - 1]

    seq = [push(1)]

    for _ in range(n):
        if seq[-1] == 2:
            print(len(seq))
            return 0
        seq += [push(seq[-1])]

    print(-1)

    return 0


if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('b1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


n = read_int()
a = [read_int() -1 for i in range(n)]
res = -1
s = set()
i = 0
step = 1
while a[i] not in s:
    if a[i] == 1:
        res = step
    s.add(a[i])
    i = a[i]
    step += 1
print(res)

Submission status: Accepted

Solution code:
n = int(input())
btns = [None]*n
for i in range(n):
    btns[i] = int(input())
cnt = 0
nb = 1
while nb != 2:
    cnt += 1
    nb = btns[nb-1]
    if cnt > n:
        break
if cnt > n:
    print(-1)
else:
    print(cnt)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import sys
button = [int(i) for i in sys.stdin]
now = 1
count = 0
for i in range(button[0]):
  if now == 2:
    print(count)
    exit()
  now = button[now]
  count += 1
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
x = [False]*N
n,i = (0, 0)
while x[n] == False:
    x[n] = True
    n = a[n]-1
    i+=1
    if n==1:
        print(i)
        break
else:
    print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = 1
idx = 1
for i in range(n):
    if a[idx] == 2:
        print(cnt)
        break

    else:
        idx = a[idx]
        cnt += 1
else:
    print(-1)

Submission status: Accepted

Solution code:
N=int(raw_input())
a=[-1]
for _ in range(N):
    a.append(int(raw_input()))
 
 
i=1
c=0
while(1):
    if i==2:
        print c
        break
    elif c>=N:
        print -1
        break
    c+=1
    i=a[i]

Submission status: Accepted

Solution code:
n = int(input())
b = []
b_old = []
for i in range(n):
    tmp = int(input())
    b.append(tmp)
n_b = 1
count = 0
flag = 0
while(count < n):
    n_b = b[n_b-1]
    count += 1
    if n_b == 2:
        print(count)
        flag = 1
        break
if flag == 0:
    print("-1")

Submission status: Accepted

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

    b = 1
    pushed = set()
    push_count = 0
    while True:
        push_count += 1
        pushed.add(b)
        b = a[b - 1]
        if b in pushed:
            print(-1)
            break

        if b == 2:
            print(push_count)
            break

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = np.zeros(N+1)
for i in range(N):
    a[i+1] = int(input())
i = 1
n = 0
for j in range(N):
    n = n+1
    i = a[i]
    if i == 2:
        print(n)
        break
if i!=2:
    print(-1)

Submission status: Accepted

Solution code:
#coding=UTF-8

N=int(input())
a=[]

for idx in range(0,N,1):
    a.append(int(input())-1)


lit=0
length=0
ans=None
for idx in range(0,N,1):
    lit=a[lit] #press
    length+=1
    if lit == 2-1:
        ans=True
        break
        



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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
  N = int(input())
  al = []
  for _ in range(N):
    al.append(int(input())-1)
  bl = [False]*N
  i = 0
  nb = 0
  while bl[i] == False:
    bl[i] = True
    i = al[i]
    nb += 1
    if i == 1:
      print(nb)
      break
  else:
    print(-1)

main()

Submission status: Accepted

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

flag = [False] * N

current = 1
count = 0

for i in range(N):
    count += 1
    current = a[current - 1]
    if current == 2:
        break
    elif i == (N - 1):
        count = -1
    elif flag[current - 1]:
        count = -1
        break
    else:
        flag[current - 1] = True

print(count)

Submission status: Accepted

Solution code:
n = int(input())
buttons = []
for _ in range(n):
    buttons.append(int(input()))
def calc():
    count = 0
    now = 1
    flag = True
    for i in range(n):
        if not flag:
            pass
        elif now == 2:
            print(count)
            flag = False
        else:
            now = buttons[now-1]
        count += 1
    if flag:
        print(-1)

calc()

Submission status: Accepted

Solution code:
input_count = int(input().rstrip())
button_list = [0,]

for _ in range(input_count):
    button_list.append(int(input().rstrip()))    

push_count = 0
next_button = 1
while True:
    next_button = button_list[next_button]
    push_count += 1
    
    if next_button == 2:
        print(push_count)
        break
    
    if push_count == len(button_list):
        print(-1)
        break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
ch = [0] * (10**5 + 1)
numB = 1
ch[1] = numB
c = 1
while True:
    numB = a[numB-1]
    if numB == 2:
        print(c)
        break
    if ch[numB] == 1:
        print(-1)
        break
    c += 1
    ch[numB] = 1

Submission status: Accepted

Solution code:
import sys

N = int(input())
a=[]
a.append(0)

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

b=[]
for i in range(N+1):
    b.append(1)

index=1
counter=1
if a[1]==2:
    print(1)
    sys.exit()

while True:
    counter+=1
    b[index]=-1
    index = a[index]
    if b[index]==-1:
        print(-1)
        break
    if a[index]==2:
        print(counter)
        break

Submission status: Accepted

Solution code:
def ini(): return int(input())
def inli(): return list(map(int, input().split()))
def inf(): return float(input())
def inlf(): return list(map(float, input().split()))
def inl(): return list(input())
def pli(): return "".join(list(map(str, ans)))

n =ini()
a=[]
b = [0 for i in range(n)]
for i in range(n):
    a.append(ini())

j = 1
ans = 0
while j !=2:
    if b[j-1] !=0:
        ans = -1
        break
    ans +=1
    b[j-1] +=1
    j = a[j-1]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
switch = True
i = 1
cnt = 1
roop = [1 for i in range(N)]
while switch == True:
    if roop[i-1] == 0:
        print(-1)
        break
    
    nextbottun =  a[i-1]
    if nextbottun == 2:
        print(cnt)
        break
    roop[i-1] = 0
    i = nextbottun
    cnt += 1

Submission status: Accepted

Solution code:
#!/usr/bin/python

import sys

def main(argv):
    N = sys.stdin.readline()
    while N:
        A = [int(raw_input()) - 1 for i in xrange(0, int(N))]

        counter = 0
        visited = set()
        index = 0
        while index != 1 and index not in visited:
            visited.add(index)
            counter += 1
            index = A[index]

        if index != 1:
            counter = -1
        print("{0}".format(counter))

        N = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Submission status: Accepted

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

    index = 0
    count = 0
    number = 1
    flags = [0 for _ in range(N)]
    flags[index] = 1
    while(number != 2):
        number = a[index]
        index = number - 1
        count += 1
        if flags[index] == 1:
            break
        else:
            flags[index] = 1

    if number == 2:
        print(count)
    else:
        print(-1)

Submission status: Accepted

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

flg=[0]*(N+1)
count=0
i=1

while count<N:
    if i==2:
        print(count)
        quit()
    i=a[i]
    count+=1

print('-1')
quit()

Submission status: Accepted

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

count = 0
nextbottun = 1

while(nextbottun != 2 and count <= 100000):
    nextbottun = a[nextbottun - 1]
    count += 1

if count <= 100000:
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
if(__name__=="__main__"):
    N = int(input())
    a =[int(input()) for i in range(N)]
        
    n = -1
    flash = 0
    for i in range(N):
        flash = a[flash] - 1
        if(flash == 1):
            n = i + 1
            break
    
    print(n)

Submission status: Accepted

Solution code:
n = int(input())
ls = []
for i in range(n):
  ls.append([int(input())-1,False])
count = 0
x = 0
while (x != 1):
  if ls[x][1]:
    count = -1
    break
  else:
    count += 1
    ls[x][1] = True
    x = ls[x][0]
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

#ボタン1→いろいろあってボタン2で終わるならボタンを押す回数、無理なら-1を出力

N = int(input())

Bo = []

for i in range(N):
    A = int(input())
    Bo.append(A)


S = Bo[0]

if S == 2:
    print(1)

else:
    
    ans = []
    
    for i in range(N):
        S = Bo[S - 1]
        
        if S == 2:
            C = i + 2
            ans.append(C)
        
    if len(ans) != 0:
        print(ans[0])
    
    else:
        print(-1)

Submission status: Accepted

Solution code:
N=int(input())
a=list(int(input()) for i in range(N))
j=0
i=1
while j < N:
    i=a[i-1]
    j+=1
    if(i==2):
        print(j)
        break
else:
    print("-1")

Submission status: Accepted

Solution code:
# coding: utf-8

n = input()
a = [input() for _ in xrange(n)]
i = 1
c = 0

while True:
    a[i-1], i = 0, a[i-1]
    c += 1

    if i == 2:
        print c
        exit()

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
a = {}
N = int(input())
for i in range(N):
    a[i+1] = (int(input()))

light = 1
count = 0
while count < N:
    
    light = a[light]
    count += 1

    if light == 2:
        print(count)
        break

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

Submission status: Accepted

Solution code:
N = int(input())

a = []

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

c = a[0]

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

Submission status: Accepted

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

count = 0
point = 0

while True:
	number = l[point]-1
	l[point] = 1
	point = number
	count += 1
	if number == 1:
		break
	elif number == 0:
		count = -1
		break

print(count)

Submission status: Accepted

Solution code:
N = int(input())
a = [0]
for i in range(N):
    a.append(int(input()))
haspushed = [0]*(N+1)
reach = 0
itr = 1
i = a[1]
haspushed[1] = 1
while haspushed[i] == 0:
    haspushed[i] = 1
    if i == 2:
        reach=1
        break
    i = a[i]
    itr += 1

if reach == 1:
    print(itr)
else:
    print(-1)

Submission status: Accepted

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

num_button = 0
now_light = 0
pushed = set()
while True:
    if now_light in pushed:
        print(-1)
        break

    pushed.add(now_light)
    now_light = a[now_light]-1
    num_button += 1
    if now_light == 1:
        print(num_button)
        break

Submission status: Accepted

Solution code:
n = int(input())
a_list = []
for i in range(n):
    a_list.append(int(input()))
cnt = 1
i = 0
while a_list[i] != 2:
    i = a_list[i] - 1
    cnt += 1
    if len(a_list) < cnt:
        cnt = -1
        break

print(cnt)

Submission status: Accepted

Solution code:
N = input()
arr = [input()-1 for i in range(N)]

now = 0
count = 0
while (now != 1):
    count += 1
    now = arr[now]
    if count > 100000:
        count = -1
        break
print count

Submission status: Accepted

Solution code:
n = int(input())
a_list = list([0])
for _ in range(n):
    a_list.append(int(input()))
    
count = 0
bangou = 1

while bangou > 0:
    if a_list[bangou] == 0:
        print("-1")
        break
    bangou_next = a_list[bangou]
    count += 1
    if bangou_next == 2:
        print(count)
        break
    
    a_list[bangou] = 0
    bangou = bangou_next

Submission status: Accepted

Solution code:
N = int(input())
a = []
for _ in range(N):
    a.append(int(input()))
i = 1
cnt = 0
while(i != 2):
    i = a[i - 1]
    cnt += 1
    if(cnt > N):
        cnt = -1
        break
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
controller = [int(input()) for _ in range(n)]
seq = []
button = controller[0]
seq.append(1)

for i in range(n):
    if button == 2:
        print(len(seq))
        exit(0)
    button = controller[button - 1]
    seq.append(button)

print(-1)

Submission status: Accepted

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

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

cnt=0
needle=1
while not(needle==2 or cnt >100097):
  needle=a[needle]
  cnt+=1

if cnt>100001:
  cnt=-1
print(cnt)

Submission status: Accepted

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


num = 1
for i in range(N):
    if a[num-1][1]:
        print(-1)
        break
    if a[num-1][0] == 2:
        print(i+1)
        break
    a[num-1][1] = True
    num = a[num-1][0]

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    a = [-1]
    for i in range(N):
        a.append(int(input()))

    count_num = 1
    next_num = a[1]
    done_num = set([1])
    while True:
        if next_num in done_num:
            print(-1)
            break
        elif next_num == 2:
            print(count_num)
            break

        count_num += 1
        done_num.add(next_num)
        next_num = a[next_num]

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
c = 1
tf = False
for i in range(N):
    c = a[c-1]
    if c == 2:
        print(i+1)
        tf = True
        break
if not tf:
    print(-1)

Submission status: Accepted

Solution code:
def b():
    N = int(input())
    A = {i:int(input()) for i in range(1,N+1)}
    tmp = 1; times = 0
    for _ in range(N):
        tmp = A[tmp]; times += 1
        if tmp == 2:
            print(times)
            break
        elif tmp == 1:
            print(-1)
            break
    else:
        print(-1)


b()

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
cnt = 0
light = 1
for i in range(N):
	cnt += 1
	light = A[light - 1]
	if light == 2:
		break

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

Submission status: Accepted

Solution code:

import sys

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

x = 1
n = 0
while x != 2:
    x = d[x]
    n += 1
    if n > N:
        x = -1
        n = -1
        break

print(n)

Submission status: Accepted

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

end = {}

end[1] = True
to = a[1]

cnt = 0
while True:
    cnt += 1
    if to == 2:
        print(cnt)
        __import__("sys").exit()
    if end.get(to) == None:
        end[to] = True
        to = a[to]
    else:
        break

print(-1)

Submission status: Accepted

Solution code:
#coding: utf-8
import sys
N = int(input())
a = [int(input()) for _ in range(N)]
p = 1
for i in range(N):
    if p == 2:
        print(i)
        sys.exit()
    p = a[p-1]
print(-1)

Submission status: Accepted

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

ind = 0
cnt = 0
while 1:
    nex = a[ind]
    cnt += 1
    if nex == 2:
        print(cnt)
        break
    if memo[nex] == 1:
        print(-1)
        break
    memo[nex] = 1
    ind = nex-1

Submission status: Accepted

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

count = 1
flag = False
place = 1
for i in range(N):
    place = a[place-1]
    if place == 2:
        flag = True
        print(count)
        break
    else:
        count +=1
if flag == False:
    print(-1)

Submission status: Accepted

Solution code:
n = input()
l = []

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

count = 0
b = 1
while b != 2 and count <= n:
	count += 1
	b = l[b - 1]

if count > n:
	print -1
else:
	print count

Submission status: Accepted

Solution code:
N = int(input())
a = list()
for i in range(N):
	a.append(int(input()))

temp = 1
count = 0
while temp != 2 and count <= 100001:
	temp = a[temp - 1]
	count += 1

if count > 100000:
	print(-1)
else:
	print(count)

Submission status: Accepted

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

from __future__ import division, print_function
import sys


def main():
    n = int(sys.stdin.readline().strip())
    a = []
    for _ in range(n):
        a.append(int(sys.stdin.readline().strip()))

    visited = set()

    i = 0
    counter = 0
    while True:
        if i in visited:
            print(-1)
            break
        visited.add(i)
        i = a[i] - 1
        counter += 1
        if i == 1:
            print(counter)
            break
    #     pass
    # a[0]
    # if 2 not in a:
    #     print(-1)
    #     return
    # print(n, a)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
k=[]
i=k[0]
cnt=1
for i in range(n):
    l=int(input())
    k.append(l)
if k.count(2)==0:
    print(-1)
else:
    while i != 2 and cnt<=n:     
        cnt += 1
        i=k[i-1]
    if i==2:
        print(cnt+1)
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
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_history = set(1)

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

        if current_num == 1:
            print(-1)
            return

        if current_num == 2:
            print(len(num_history))
            return

        num_history.add(current_num)

run()

Submission status: Runtime Error

Solution code:
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)

# N個のボタンがあり、今1個のボタンが光っている。
# ボタンは1~Nまで番号があり、iが光っているときに押すと、iの光が消え、aiが光る。
# 光っていないボタンを押しても反応しない。
# ボタン１が光っている状態で開始、ボタン2が光っている状態で終了したい。
# そのようなことが不可能であれば－1、可能であれば最小回数を出力

#　普通にシミュレートしたらよい。
#　n回ボタン押したうえで、2にたどり着かなかったらどこかで同じボタンを2回押していることになる。

count = 0
cur = 1

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

Submission status: Runtime Error

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

def check(al,bl,i):
    bl.append(i)
    i = al[i]-1
    if len(bl) == len(al):
        print(-1)
    elif i in bl:
        print(-1)
    elif i == 1:
        print(len(bl))
    else:
        check(al,bl,i)

bl = []
check(al,bl,0)

Submission status: Runtime Error

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

def check(al,bl,i):
    bl.append(i)
    i = al[i]-1
    if i in bl:
        print(-1)
    elif i == 1:
        print(len(bl))
    else:
        check(al,bl,i)

bl = []
check(al,bl,0)

Submission status: Runtime Error

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

def check(al,bl,i):
    bl.append(i)
    i = al[i]-1
    if i in bl:
        print("-1")
    elif i == 1:
        print(len(bl))
    else:
        check(al,bl,i)

bl = []
check(al,bl,0)

Submission status: Runtime Error

Solution code:
n = int(input())
data = [input().split() for i in range(n)]
light = [0]*n
light[0] = 1
count = 0
bool = False
for i in range(n):
    temp = light.index(1)
    light[int(data[temp][0])  - 1] = 1
    light[temp] = 0
    if temp == 1:
        bool = True
        print(count)
        exit()
    count += 1
if bool == False:
    print(-1)

Submission status: Runtime Error

Solution code:
def main():
  n=int(input())
  A=[int(input()) for i in range(n)]
  crt=1
  for i in range(n):
    crt = A[crt-1]
    if crt == 2:
      print(len(L))
      break
  else:
    print(-1)
main()

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
ans = 1
seen = [0] * N
seen[0] = 1
now = a[0]

while True:
    if now == 1 or seen[now] == 1:
        print(-1)
        break
    if now == 2:
        print(ans)
        break
    now = a[now-1]
    seen[now] = 1
    ans += 1

Submission status: Runtime Error

Solution code:
# import math
# import statistics
a=int(input())
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(int(i))
# e1,e2,e3 = map(int,input().split())
#f = list(map(int,input().split()))
g = [int(input()) for _ in range(a)]
# zoro = int(a[0]*len(a))
find = g.index(2)+1
ans=True
count=0
if g[0]==2:
    print(1)
else:
    while ans:
        if find in g:
            find=g.index(find)+1
            count+=1
            if find==1:
                print(count+1)
                ans=False
            elif count>=len(g):
                print(-1)
                ans=False
            else:
                continue
        else:
            print(-1)
            ans=False