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: Runtime Error

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

visitedlist = [0]
from = 0
for _ in range(n):
    to = alist[from]
    if to in visitedlist:
        print(-1)
        break
    elif to == 1:
        print(len(visitedlist))
        break
    else:
        visitedlist.append(to)
        from = to

Submission status: Runtime Error

Solution code:
a = [int(input()) for _ in range(int(input()))]
c,s = 1,a[0]
if s != 2 and c < n: c,s = c+1,a[n-1]
print(c if c < n else "-1")

Submission status: Runtime Error

Solution code:
*A,=map(int,open(0));i=1;t=[0]*A[0]
while i-2and t[i]==0:t[i]=1;i=A[i]
print(-1if i-2else sum(t))

Submission status: Runtime Error

Solution code:
*A,=map(int,open(0));i=1;t=[0]*A[0]
while i-2or t[i]:t[i]=1;i=A[i]
print(-1if i-2else sum(t))

Submission status: Runtime Error

Solution code:
*A,=map(int,open(0));i=1;t=[0]*A[0]
while i-2or i in t:t[i]=1;i=A[i]
print(-1if i-2else sum(t))

Submission status: Runtime Error

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

pushed = set()
cnt = 0
push= 1
while True:
    if push in pushed:
        cnt = -1
        break
    pushed.add(push)
    push = a[push]
    cnt += 1
    if push == 2:
        break

print(cnt)

Submission status: Runtime Error

Solution code:
l=[int(input()) for _ in range(int(input()))]
x=l[0]
cnt=1
while l[x]!=2:
  if(l[x]==0 or len(l)==cnt):
    print(-1)
    exit()
  index=l[x]
  l[x]=0
  x=int(index-1)
  cnt+=1
print(cnt)

Submission status: Runtime Error

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

c = 0
n = 1
checked = [False for _ in range(N)]
checked[0] = True
for _ in range(N+1):
    n = A[n-1]
    c += 1
    if n == 2:
        ok = True
        break
    if checked[n-1]:
        ok = False
        break
if ok:
    print(c)
else:
    print(-1)

Submission status: Runtime Error

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

c = 0
n = 1
for _ in range(N+1):
    n = A[n-1]
    c += 1
    if n == 1:
        ok = False
        break
    if n == 2:
        ok = True
        break
if ok:
    print(c)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
n = int(input())
As = [int(input()) for _ in range(n)]
visited = [-1 for i in range(n)]


def rec(visited, i, depth):
    # print(i, visited[i])
    if visited[i] > 0:
        return visited[i]
    elif visited[i] == 0:
        return -1
    if As[i] == 2:
        visited[i] = 1
        return depth
    visited[i] = 0
    visited[i] = rec(visited, As[i] - 1, depth + 1)
    return visited[i]


ret = rec(visited, 1 - 1, 1)
print(ret)

Submission status: Runtime Error

Solution code:
n = int(input())
As = [int(input()) for _ in range(n)]
visited = [-1 for i in range(n)]


def rec(visited, prev_i, i, depth):
    if visited[i] >= 0:
        return visited[i]
    if As[i] == 2:
        visited[i] = 1
        return depth
    if prev_i == As[i] - 1:
        visited[i] = 0
        return visited[i]
    visited[i] = rec(visited, i, As[i] - 1, depth + 1)
    return visited[i]


ret = rec(visited, -1, 1 - 1, 1)
if ret == 0:
    ret = -1
print(ret)

Submission status: Runtime Error

Solution code:
n = int(input())
As = [int(input()) for _ in range(n)]
visited = [-1 for i in range(n)]


def rec(visited, prev_i, i, depth):
    if visited[i] > 0:
        return visited[i]
    if As[i] == 2:
        visited[i] = 1
        return depth
    if prev_i == As[i] - 1:
        return visited[i]
    visited[i] = rec(visited, i, As[i] - 1, depth + 1)
    return visited[i]


ret = rec(visited, -1, 1 - 1, 1)
print(ret)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python

N = 1
a = []

def sol(x, cnt) : 

    cnt+=1
    if a[x-1] == 2 :
        return cnt
    if cnt > N :
        return -1

    return sol(a[x-1], cnt)


if __name__ == '__main__' : 
    N = int(input())

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

    print( sol(1,0) )

Submission status: Runtime Error

Solution code:
def main():
  n = int(input())
  a =[]
  for n in range(n):
    a.append(int(input())-1)
  sw = [0]
  while True:
    nextsw = sw[-1]
    if nextsw = 1:
      print(len(sw))
      return
    if nextsw in sw:
      print(-1)
      return
    sw.append(nextsw)

main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(int(input()) for i in range(n))
a=0
for x in range(n):
  a=a[i]
  if a==2:
    print(x)
    break

Submission status: Runtime Error

Solution code:
n = int(input())
an = [int(input())-1 for i in range(n)]
cnt = 0
x = 0

for i in range(n):
  if a[x] == 1:
    break
  
  elif a[x] != 1:
    x = a[x]
    cnt = cnt +1
  
if cnt > n:
  print(-1)
else:
  print(cnt)

Submission status: Runtime Error

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

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

turn_on=a[0]

for count in range(2,N):
    if a[turn_on]==2:
        print(count)
        exit()
    turn_on=a[turn_on-1]
    
print(-1)

Submission status: Runtime Error

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

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

turn_on=a[0]

for count in range(1,N):
    if a[turn_on]==2:
        print(count)
        exit()
    turn_on=a[turn_on-1]
    
print(-1)

Submission status: Runtime Error

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

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

turn_on=a[0]

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
list = [int(input()) for i in range(n)]
c = 0
x = 1
while x != 2 and c < N:
    x = list[x - 1]
    c += 1
if x == 2:
    print(c)
else:
    print(-1)

Submission status: Runtime Error

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

i = b_list[0]
keiyu_list = [0 for _ in range(N)]
cnt=0
while keiyu_list[i]==0:
  i = b_list[i-1]
  keiyu_list[i]=1
  cnt+=1
  if i == 2:
    cnt+1
    break
if i==2:
  print(cnt)
else:
  print(-1)

Submission status: Runtime Error

Solution code:
n=int(input())
li=[int(i) for i in input().split()]
x = li[0]
ans = -1
count=0
for i in range(n):
  x = li[x-1]
  if x == 2:
    ans = count+1
    break
  count +=1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
on = 1
count = 0
on_li = [1]
while True:
    on = a[on-1]
    if on in on_li:
        print(-1)
        exit()
    on_li.append(on)
    print(on_li)
    count += 1
    if on == 2:
        print(count)
        exit()

Submission status: Runtime Error

Solution code:
N=int(input())
aN=[int(input()) for _ in range(N)]
c=0
i=1
while True:
  print(aN)
  c+=1
  if aN[i-1]==2:
    print(c)
    break
  elif aN[i-1]==0:
    print(-1)
    break
  t=aN[i-1]
  aN[i-1]=0
  i=t

Submission status: Runtime Error

Solution code:
N=int(input())
aN=[int(input()) for _ in range(N)]
c=0
i=1
while True:
  c+=1
  if a[i]==2:
    print(c)
    break
  elif a[i]==0:
    print(-1)
  i=a[i]
  a[i]=0

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
check = []
cnt = 0
i = 0
while True:
    if i not in check:
        check.append(i)
        i = a[i]-1
    elif cnt >= len(n):
        print(-1)
        break
    if i == 1:
        print(len(check))
        break
    cnt += 1

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
n = int(input())
a_list = [int(input()) for _ in range(n)]
def calc_ans(start_num, count, max_count, all_list):
    ans = all_list[start_num-1]
    if ans == 2:
        print(count)
    elif count == max_count:
        print('-1')
    else:
        calc_ans(ans, count+1, max_count, all_list)
calc_ans(1, 1, n, a_list)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = [int(input()) for _ in range(n)]
def calc(start_num, count, max_count, all_list):
    ans = all_list[start_num-1]
    if ans == 2:
        print(count)
    elif count == max_count:
        print('-1')
    else:
        calc(ans, count+1, max_count, all_list)
calc(1, 1, n, a_list)

Submission status: Runtime Error

Solution code:
a

Submission status: Runtime Error

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

a = 1
c = -1

for i in range(N):
  a = n[a-1]
  if c != -1:
    if a == 2:
      c = (i+1)
  else:

print(c)

Submission status: Runtime Error

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

v = {1}
x = 1
a = 0
while True:
  x = A[x]
  a += 1
  if x in v:
    print(-1)
    break
  if x == 2:
	print(a)
    break
  v.add(x)

Submission status: Runtime Error

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

v = {1}
x = 1
a = 0
while True:
  x = A[x]
  a += 1
  if x in v:
    print(-1)
    break
  if x == 2:
	print(a)
    break

Submission status: Runtime Error

Solution code:
N = int(input())
a = [0] + [int(input()) for _ in range(N)]
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: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc065/tasks/abc065_b
# B - Trained?
n = int(input().split()[0])

index2number_dict = {}
number2index_dict = {}

for i in range(n):
    a_i = int(input().split()[0])
    index2number_dict[i+1] = a_i
    number2index_dict[a_i] = i+1

# ボタンが光っているときに押すと、
# 今のボタンの光が消える
# 次のボタンが光る
# ボタン2が光っている状態でやめたい
# print(index2number_dict)

start_number = 1
start_index = number2index_dict[start_number]
# print(start_number)
# print(start_index)

step = 0
lighting_number = start_number
lighting_index = start_index
status_list = str(lighting_number)
can_stop = False

while True:
    step += 1
    lighting_number = index2number_dict[lighting_number]
    
    if lighting_number == 2:
        can_stop = True
        break

    status_list += str(lighting_number)

    if len(status_list) % 2 == 0:
        half = len(status_list) // 2
        # print('status(full): {}'.format(status_list))
        # print('status(half): {}'.format(status_list[:half]))

        if status_list == status_list[:half] * 2:
            break

if not can_stop:
    step = -1

# print('===========')
# print(status_list)
print(step)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
n=int(input())
a=[int(input()) for _ in range(n)]
       
cnt=0
tmp=1

for i in range(n):
    cnt+=1
    already.append(tmp)
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        sys.exit()
print(-1)

Submission status: Runtime Error

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

for i in range(a):
    if a[i]==i+1:
        print(-1)
        
cnt=0
tmp=1
already=[]
while True:
    cnt+=1
    already.append(tmp)
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        break
    elif tmp in already:
        print(-1)
        break

Submission status: Runtime Error

Solution code:
n=int(input())
button=[int(input()) for i in range(n)]
temp=1
ans=1
while button[temp-1] not in check:
  temp=button[temp-1]
  ans+=1
  if button[temp-1]==2:
    print(ans)
    break
  elif ans>=n:
    print(-1)
    break

Submission status: Runtime Error

Solution code:
n=int(input())
button=[int(input()) for i in range(n)]
temp=1
ans=1
while button[temp-1] not in check:
  temp=button[temp-1]
  ans+=1
  if button[temp-1]==2:
    print(ans)
    break
  elif ans>=n+1:
    print(-1)
    break

Submission status: Runtime Error

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

pushed = list()
button = 1
count = 0
while(True):
    button = buttons[button - 1]
    count +=1
    if (button == 2):
        print(count)
        break
    elif(button in pushed):
        print(-1)
        break
    else:
        pushued.append(button)

Submission status: Runtime Error

Solution code:
=int(input())
a=[]
for i in range(n):
    ai=int(input())
    a.append(ai)
finish=False
for i in range(n):
    if i==0:
        next=a[a[0]-1]
    else:
        next=a[next-1]
    if next==2:
        print(i+2)
        finish=True
        break
if not finish:
    print(-1)

Submission status: Runtime Error

Solution code:
phai = 1
cnt = 0
loop_hantei = {1}

for _ in range(N):
    phai = A[phai-1]
    cnt += 1
    if phai in loop_hantei:
        print('-1')
        break
    else:
        loop_hantei.add(phai)
    if phai == 2:
        print(cnt)
        break

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
l = []
ans = 1
for _ in range(n) :
	l.append(int(input()))
if l[0] == 2 :
	print(1)
else :
	i = l[0] - 1
while True :
	if l[i] == 2 :
		ans += 1
		break
	i = l[i] - 1
	ans += 1
	if ans == n+1 :
		print('-1')
		exit()
print(ans)

Submission status: Runtime Error

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

count = 1
fp = lista[0]
for i in range(n):
  fp = lista[fp]
  count += 1
  if fp == 2:
    print(count)
    exit()
print(-1)

Submission status: Runtime Error

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

vi=[0]*n
vi[0]=1
ans=1
st=l[0]
f=0
while vi[1]==0:
    vi[st]+=1
    if vi[st]>1:
        f=1
        break
    st=l[st-1]
    ans+=1
if f==0:
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[0]*n
for _ in range(n):
  a[_]=int(input())
c=0
p=a[0]
while True:
  p=a[p-1]
  c+=1
  if p=2:
    break
  if p=1:
    c=-1
    break
print(c)

Submission status: Runtime Error

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

stack = [2]

can = False
while True:
    if stack[len(stack)-1] == 1:
        can = True
        break
    c = stack[len(stack)-1]
    i = a.index(c)+1
    if i in stack:
        break
    stack.append(i)

if can:
    print(len(stack)-1)
else:
    print(-1)

Submission status: Runtime Error

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


def solve(a,index,l):
    if l == len(a)+1:
        return print(-1)

    value = a[index-1]
    if value == 2:
        return print(l)
    else:
        solve(a,value,l+1)
    
solve(a,1,1)

Submission status: Runtime Error

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

def solve(a,index,l):
    if l == len(a)+1:
        return print(-1)

    value = a[index-1]
    if value == 2:
        return print(l)
    else:
        solve(a,value,l+1)
    
solve(a,index,0)

Submission status: Runtime Error

Solution code:
import functools


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    a = list(map(lambda x: int(x), inputs))
    a[1] = -1
    return wh(a, 2, 0, len(a))


def wh(a, search, count, N):
    counts = []

    if search == 1:
        return count
    elif count > N:
        return -1

    for i, value in enumerate(a):
        if value == search:
            counts.append(wh(a, i + 1, count + 1, N))

    counts_ignore_minus = list(filter(lambda x: x != -1, counts))

    if len(counts_ignore_minus) == 0:
        return -1

    min = counts_ignore_minus[0]
    for c in counts_ignore_minus:
        if min > c:
            min = c
    return min


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)

Submission status: Runtime Error

Solution code:
import functools


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    a = list(map(lambda x: int(x), inputs))
    a[1] = -1
    return wh(a, 2, 0)


def wh(a, search, count):
    counts = []

    if search == 1:
        return count

    for i, value in enumerate(a):
        if value == search:
            counts.append(wh(a, i + 1, count + 1))

    counts_ignore_minus = list(filter(lambda x: x != -1, counts))

    if len(counts_ignore_minus) == 0:
        return -1

    min = counts_ignore_minus[0]
    for c in counts_ignore_minus:
        if min > c:
            min = c
    return min


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)

Submission status: Runtime Error

Solution code:
a=[input() for _ in range(int(input()))]
ans=1
c=a[0]
if(c==2):
    print(ans)
else:
    while(c!=2):
        ans+=1
        c=a[c-1]
        if(ans>len(a)):
            print(-1)
            exit()
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#coding: utf-8
import sys

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

start = 0
count = 0

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

Submission status: Runtime Error

Solution code:
#coding: utf-8
import sys

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

start = 0
count = 0

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

Submission status: Runtime Error

Solution code:
#coding: utf-8
import sys

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

start = 0
count = 0

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

Submission status: Runtime Error

Solution code:
#coding: utf-8
import sys

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

start = 0
count = 0

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

Submission status: Runtime Error

Solution code:
import sys

def search_next(nextbutton : int) -> int:
    nextbutton = a.index(nextbutton) +1
    return nextbutton

n = int(input())
a = [int(input()) for i in range(n)]
nextbutton = 2
count = 1

try :
    nextbutton = search_next(nextbutton)
except:
    print(-1)
    sys.exit()
#print(nextbutton,"first")

for _ in range(n):
    if nextbutton == 2:
        print(-1)
        #print(nextbutton,"2")
        
        sys.exit()
    elif nextbutton == 1:
        print(count)
        #print(nextbutton,"1")
        
        sys.exit()
    else:
        nextbutton = search_next(nextbutton)
        #print(nextbutton,"el")
        
        count += 1
print(-1)

Submission status: Runtime Error

Solution code:
import sys

def search_next(nextbutton : int) -> int:
    nextbutton = a.index(nextbutton) +1
    return nextbutton

n = int(input())
a = [int(input()) for i in range(n)]
nextbutton = 2
count = 1

try :
    nextbutton = search_next(nextbutton)
except:
    print(-1)
    sys.exit()
#print(nextbutton,"first")

while 1:
    if nextbutton == 2:
        print(-1)
        #print(nextbutton,"2")
        
        sys.exit()
    elif nextbutton == 1:
        print(count)
        #print(nextbutton,"1")
        
        sys.exit()
    else:
        nextbutton = search_next(nextbutton)
        #print(nextbutton,"el")
        
        count += 1

Submission status: Runtime Error

Solution code:
length = int(input())  
 
data = ([int(input()) for i in range(length)])

cnt = 0
i = 1
v = 0

while v != 2:
  v = data[i]
  i = v
  if cnt >= data[0]: 
    cnt = -1
    break
  cnt += 1
  
print(cnt)

Submission status: Runtime Error

Solution code:
length = int(input())  
 
data = ([int(input()) for i in range(length)])

cnt = 0
i = 1
v = 0

while v != 2:
  v = data[i]
  i = v
  cnt += 1
  if cnt >= data[0]: 
    cnt = -1
    break
print(cnt)

Submission status: Runtime Error

Solution code:
import sys
n=int(input())
a=[]
f=[]
has2=False
for i in range(n):
  ai=int(input())
  if ai==2:
    has2=True
  a.append(ai)
  f.append(False)

if has2==False:
  print("-1")
  sys.exit()

f[0]=True
now=0
cnt=0
while True:
  if f[now]==True:
    print(-1)
    break
  if now==2:
    print(cnt)
    break
  f[now]=True
  now=a[now]
  cnt++

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))

next = a[0]
count = 1

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

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(int(input()) for _ in range(n))
i=1
c=1
i=a[a.index(i)-1]
while  c!=n or i == 2:
    i=a[a.index(i)-1]
    print(i)
    c+=1
print(-1 if c==n+1 else c)
print(c)

Submission status: Runtime Error

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

def f(i, n):
  if a[n] == 2: return i
  if c[n] == 1: return -1
  c[n] = 1
  return f(i + 1, a[n] - 1)

print(f(1, 0))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
a = []
for i in range(N):
  a.append(int(input()))
j = 0
count = 0
flag = False
for i in range(N):
  if a[j] == 2:
    flag = True
    break
  else:
    j = a[j]
    count += 1

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

Submission status: Runtime Error

Solution code:
n = int(input())
arr = []
passed = []
for i in range(n):
  a = int(input())
  arr.append(a)
  
next_position = 1
ans = 0

while(1):
  next_position = arr[next_position-1]
  #print(position)
  if ans == n
    print(-1)
    break
  #passed.append(next_position)
  ans += 1
  if next_position == 2:
    print(ans)
    break

Submission status: Runtime Error

Solution code:
import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())

n = ni()

a = [ni() for _ in range(n)]
count = 0
nexti = 0
for i in range(n):
    if nexti == 1:
        print(count)
        exit()
    else:
        nexti = a[a[nexti] - 1]
        count += 1
print(-1)

Submission status: Runtime Error

Solution code:
count = 0
def judge(a, i):
    global count
    count += 1
    if count > len(a):
        return -1
    else:
        if 0 <= i < len(a): 
            if a[i] == 2:
                return count
            else:
                return judge(a, a[i]-1)
        else:
            return -1

def B_Trained():
    n = int(input())
    a = [int(input()) for i in range(n)]
    count = judge(a, 0)
    print(count)

def main():
    B_Trained()

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
count = 0
def judge(a, i):
    global count
    count += 1
    if count > len(a):
        return -1
    else:
        if a[i] == 2:
            return count
        else:
            return judge(a, a[i]-1)

def B_Trained():
    n = int(input())
    a = []
    for i in range(n):
        a.append(int(input()))
    
    count = judge(a, 0)
    print(count)

def main():
    B_Trained()

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(5000)
N=int(input())
A=[int(input())-1 for _ in range(N)]
result=[]
app=result.append
def next(k):
	newlight=A[k]
	if k==1:
		return(1)
	elif k in result:
		return(0)
	else:
		app(k)
		return(next(newlight))
if not next(0):
	print(-1)
else:print(len(result))

Submission status: Runtime Error

Solution code:
N=int(input())
A=[int(input())-1 for _ in range(N)]
result=[]
app=result.append
def next(k):
	print(result,k)
	newlight=A[k]
	if k==1:
		return(1)
	elif k in result:
		return(0)
	else:
		app(k)
		return(next(newlight))
if not next(0):
	print(-1)
else:print(len(result))

Submission status: Runtime Error

Solution code:
n=int(input())
d=[int(input())-1for i in range(n)]
s=set(0)
ans=0
b=0
while 1>0:
  if d[b] in s:
    ans=-1
    break
  b=d[b]
  s.add(b)
  ans+=1
  if b==1:
    break
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
d=[int(input())-1for i in range(n)]
s=set(0)
ans=0
b=0
while 1>0:
  if d[b] in s:
    ans=-1
    break
  b=d[b]
  s+=d[b]
  ans+=1
  if b==1:
    break
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
listA = [int(input()) for i in range(n)]
a = listA[0]
b = 0
for j in range(n):
  if a != 2:
    a = listA[a]
    b = b+1
  elif a == 2:
    b = b+1
    break
if b == n:
  print(-1)
else:
  print(b)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,a,b,m=int(input()),[int(input())for i in range(n)],[],1
while m!=2 and m in b:
  b.append(m)
  m=b[m-1]
if m==2:print(len(b))
else:print(-1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
i = 1
c = 0
while True:
  if i == 2:
    print(c)
    quit(0)
  if a[i] == i:
    print(-1)
    quit(0)
  c += 1
  i, a[i] = a[i], i

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
i = 1
c = 0
while a[i] != i:
  if i == 2:
    print(c)
    quit(0)
  c += 1
  i, a[i] = a[i], i
print(-1)

Submission status: Runtime Error

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	i = 1
	if tugi-1 == 1:
	print(i)
	sys.exit()
	while i < N+1:
		i += 1
		tugi = btnList[tugi-1]
		if tugi-1 == 1:
			print(i)
			sys.exit()
	print(-1)
	
if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
N = int(input())
    ai = [int(input()) for i in range(N)]
    
    now = 1
    count = 0
    for i in range(N):
        count = count + 1
        now = ai[now-1]
        if now == 2:
            print(count)
            exit()
    print(-1)

Submission status: Runtime Error

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	lists = [0]
	while True:
		lists.append(tugi-1)
		tugi = btnList[tugi-1]
		print(lists)
		if tugi-1 == 1:
			print(len(lists))
			sys.exit()
		if (tugi-1) in lists:
			print(-1)
			sys.exit()
	
if __name__ == "__main__":
	main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

zero = l[0]
count = 1
th = [zero]

def r(index,throw):
    global l
    now = l[index-1]
    # print(now,throw)
    if (now in throw) or (2 not in l):
        print(-1)
        return

    if now != 2:
        throw.append(now)
        r(now,throw)
    else:
        print(len(throw)+2)
        return

r(zero,th)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N = int(input())
btn = [int(input()) for _ in range(N)]
i, ans = 0, 0
checked = defaultdict(int)
while 1:
  next = btn[i]
  ans += 1
  if next == 2:
    print(ans)
  	break
  if checked[next] == 1:
    print(-1)
    break
  i = next
  checked[i] = 1

Submission status: Runtime Error

Solution code:
n = int(input())

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

i = 0
count = 0

while(1):
  i = a[i]
  count += 1
  if i == 1:
    break
  elif i == 0:
    print(-1)
    return 0
  
print(count)

Submission status: Runtime Error

Solution code:
import sys

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

N = l[0]

a = l[1:]

done = [False] * N

i = 0

ans = 0

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

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
lamp=[]
status=1
count=0
possible=1
for i in range(n):
  p=int(input())
  lamp.append(p)
while status!=2 or count==n:
  status=lamp[status]
  count+=1
  if count==n and status!=2:
    possible=0
if possible==1:
  print(count)
else:
  print(-1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
c = 0
p = 0
while True:
    c = a[c]
    p += 1
    if c == 1:
        print(p)
        exit()
    elif c == 0 and p > 1:
        print(-1)
        exit()

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
c = 0
p = 0
while True:
    c = s[c]
    p += 1
    if c == 1:
        print(p)
        exit()
    elif c == 0 and p > 1:
        print(-1)
        exit()