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


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

print(-1)

Submission status: Accepted

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-1]
    b = b+1
  elif a == 2:
    b = b+1
    break
if b == n:
  print(-1)
else:
  print(b)

Submission status: Accepted

Solution code:
# 065 B
N = int(input())
A=[int(input()) for i in range(N)]
cur =A[0]
for i in range(N):
    if cur ==2:
        print(i+1)
        break
    cur = A[cur-1]
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
data = [int(input()) for i in range(N)]
i = 0
a_i = None
res = 1
pre_i = 0
time = -1
while data[i] != 2:
    a_i = data[i]
    if pre_i == a_i or time == N:
        res = -1
        break

    pre_i = i + 1
    i = data[i] -1
    time += 1
    res += 1
print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input())-1)
    
index = 0
count = 0
flag = True
while flag:
    index = A[index]
    if index == 1:
        flag = False
    if count > N:
        count = -1
        flag = False
    else:
        count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=[]
for i in range(n):
  l.append(int(input()))
c=0
s=1
for i in range(n):
  s=l[s-1]
  c+=1
  if s==2:
    print(c)
    break
if s!=2:
  print(-1)

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int, [input() for i in range(N)]))
v = 1
for i in range(100000):
    if l[v - 1] == 2:
        print(i+1)
        break
    else:
        v = l[v - 1]
        if i == 99999:
            print('-1')

Submission status: Accepted

Solution code:
import sys
N = int(input())
inputs = []
for _ in range(0, N):
    inputs.append(int(input()))
index = inputs[0]-1
count = 1
for _ in range(0, N):
    if index == 1:
        print(count)
        sys.exit()
    index = inputs[index]-1
    count += 1
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

B = [1]
i = 0
cnt = 0
flag = False
for _ in range(N):
    i = A[i] - 1
    cnt += 1
    if i == 1:
        flag = True
        break

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

Submission status: Accepted

Solution code:

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

current_light = 0 #index = 1を光らせるのが目標
push_time = 0
while current_light != 1:
    current_light = a[current_light]
    push_time += 1
    if push_time > n:
        print(-1)
        break
else:
    print(push_time)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input())-1)  
    
index = 0
count = 1
flag = True
while flag:
    index = A[index]
    if index == 1:
        flag = False
    else:
        count += 1
    if count > N:
        flag = False
        count = -1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
as_dict = {}
for i in range(1,N+1):
    a_i = int(input())
    as_dict[i] = a_i

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input())-1 for _ in range(N)]
       
ans = 0
n = 0 
s = set()
while True:
    if n == 1:
        print(ans)
        break
    if n in s:
        print(-1)
        break
    ans += 1
    s.add(n)
    n = A[n]

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
cnt=0
now=1
for i in range(N+1):
    if now==2:
        break
    cnt+=1
    now=A[now-1]
if cnt>N:print(-1)
else:print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a = []
for _ in range(N):a.append(int(input()))
index = c = 0
for i in range(N):
    index = a[index] - 1
    c += 1 
    if index == 1:
        print(c)
        break
else:print(-1)

Submission status: Accepted

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


def main():
    """    i = a_i   """

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

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

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a_s = [int(input()) for _ in range(n)]
button = 1
count = 0
for _ in range(len(a_s)):
    button = a_s[button-1]
    count += 1
    if button == 2:
        print(count)
        break
if count == len(a_s):
    print(-1)

Submission status: Accepted

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

def push(i):
    return A[i] - 1

index = 0
cnt = 0
for _ in range(N):
    index = push(index)
    cnt += 1
    if index == 1:
       print(cnt)
       exit()
print(-1)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
history=[1]
ans=-1000
for i in range(N):
  history.append(A[history[-1]-1])
if 2 not in history:
  ans=-1
else:
  ans=history.index(2)
print(ans)

Submission status: Accepted

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

used = [0]*n
current = 0
count = 0
while True:
    if used[current] == 1:
        print(-1)
        exit()
    used[current] = 1
    current = a[current]
    count += 1
    

    if current == 1:
        print(count)
        break

Submission status: Accepted

Solution code:
def train(n):
    x =[0] + [int(input()) for i in range(n)]
    idx = 1
    for i in range(n+1):
        if x[idx] == 2:
            return i + 1
        idx = x[idx]
    return -1
n = int(input())
print(train(n))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [0] + [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
  tmp = a[i]
  a[i] = i
  i = tmp

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
n = 0
t = 0
while t<N:
    n = A[n]-1
    t +=1
    #print(n)
    if n == 1:
        print(t)
        exit()
    if n == A[n]-1:
        print(-1)
        exit()
print(-1)

Submission status: Accepted

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

cnt = 0
adr = 0

for i in range(N):
    if a[adr] == 2:
        cnt += 1
        break
    adr = a[adr] - 1
    cnt += 1
else:
    cnt = -1

print(cnt)

Submission status: Accepted

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

b = 1
ans = None
for i in range(N):
    b = a_s[b - 1]
    if b == 2:
        ans = i + 1
        break

print(-1 if ans is None else ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input())-1 for _ in range(n)]
s=set()
i=0
n=0
while True:
  n += 1
  i = a[i]
  if i==1:
    print(n)
    break
  elif i in s:
    print(-1)
    break
  else:
    s|={i}

Submission status: Accepted

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

a_pair = list(enumerate(a))

position = 0
position_log = {0}

while True:
    position = a_pair[position][1]
    if position == 1:
        print(len(position_log))
        break
    elif position in position_log:
        print(-1)
        break
    position_log.add(position)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
 
def main():
    N = int(input())
    A = [int(input()) for a in range(N)]
    now = 1
    count = 0
    
    for i in range(N):
        count += 1
        now = A[now-1]
        if(now == 2):
            print(count)
            exit()
    print(-1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
check = [0] * n
count = 0
button = a[0]
while(True):
    count += 1
    if button == 2:
        break
    if check[button-1] == 1:
        print(-1)
        sys.exit()
    check[button-1] = 1
    button = a[button-1]
print(count)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    N = int(input())
    A = [int(input())-1 for _ in range(N)]
    X = [True] * (N)
    p = 0
    ans = 0
    while X[p]:
        if p == 1:
            print(ans)
            return
        ans += 1
        X[p] = False
        p = A[p]
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
t = 0
c = set([1])
n = 1
f = False
while t < len(c):
    t +=1
    n = a[n-1]
    if n == 2:
        f = True
        break
    c.add(n)
print(len(c) if f else -1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).readlines());c,s=1,a[0]
while s!=2and c<n:c+=1;s=a[s-1]
print(c*(c<n)or-1)

Submission status: Accepted

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: Accepted

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

def main():
	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)
	
if __name__ == "__main__":
	main()

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:
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 - 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
cnt = d = 0 
while True:
  cnt += 1
  if a[d] == 2:
    print(cnt)
    break
  elif cnt >= 10**5:
    print(-1)
    break
  else:
    d = a[d] - 1

Submission status: Accepted

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

Submission status: Accepted

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

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

print(-1)

Submission status: Accepted

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

zero = l[0]
count = 1

for i in range(n):
    #print("c:",count)
    if zero != 2:
        count += 1
        zero = l[zero-1]
    else:
        print(count)
        exit()
else:
    print(-1)

Submission status: Accepted

Solution code:
""" AtCoder """

N = int(input())
a = [int(input()) for _ in range(N)]
a.insert(0, 0)

p = 1
ans = 0

for _ in range(N):
    ans += 1
    if a[p] == 2:
        print(ans)
        exit()
    p = a[p]

print(-1)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
btn = [0] + [int(input()) for _ in range(N)]
i, ans = 1, 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: Accepted

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

d = set()
p = 0
ans = 1
while 1:
    if a[p] == 1:
        print(ans)
        exit()
    elif p in d:
        print(-1)
        exit()
    else:
        d.add(p)
        p = a[p]
        ans += 1

Submission status: Accepted

Solution code:
n = int(input())

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

i = 0
count = 0

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

if count == n + 1:
  count = -1
  
print(count)

Submission status: Accepted

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

x = 1
s = {1}
count = 0
while x != 2:
    x = a[x-1]
    count += 1
    if x in s:
        print(-1)
        break
    s.add(x)
else:
    print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []

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

i = 0
judge = 1
count = 0
B.append(1)

while((judge == 1)and(len(B)<=len(A))):
  if(count == 0):
    i = A[0]
    count += 1
    B.append(i)
  else:
    i = A[i-1]
    B.append(i)
    count += 1
  #print(count,i)
  #print(B)
  if(i == 2):
    print(count)
    judge = 0

if(judge == 1):
  print(-1)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = [int(input()) for _ in range(N)]
ans = -1
nxt = 0
for i in range(N):
    nxt = A[nxt] - 1
    if nxt == 1:
        ans = i + 1
        break
print(ans)

Submission status: Accepted

Solution code:
lst = [int(input())]
lst += [int(input()) for x in range(lst[0])]
el = 1
for i in range(lst[0]):
    el = lst[el]
    if el == 2:
        print(i+1); break
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a_i_list = [0] + [int(input()) for i in range(N)]  # 第0要素に0を入れて添え字の整合性をとる
button_lighted = 1
counter = 0

for i in range(N):
    button_lighted = a_i_list[button_lighted]
    counter += 1

    if button_lighted == 2:
        print(counter)
        break
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy

def j(q):
    if q==1: print("Heisei")
    else:print("TBD")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())


#n = ip()                              #入力整数1つ
#n,k= (int(i) for i in input().split())      #入力整数横2つ
#n,x,y = (int(i) for i in input().split())    #入力整数横3つ
#n,a,b,c= (int(i) for i in input().split())  #入力整数横4つ
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
n = ip()                      #入力セット(整数改行あり)(1/2)
a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
b = [[-1,-1]]
for i in range(n):
    b.append([i+1,a[i]])
c = 1
count = 0
while b[c][0] != -1:
    b[c][0] = -1
    count +=1
    c = b[c][1]
    if c == 2:
        print(count)
        exit(0)
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
num=0
cnt=0
for i in range(n):
    if a[num]==2:
        cnt+=1
        print(cnt)
        break
    elif cnt==n-1:
        print("-1")
        break
    else:
        num=a[num]-1
        cnt+=1

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import sys

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

N = l[0]

a = [int(i) - 1 for i in 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: Accepted

Solution code:
import sys

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

done = [False] * (l[0] + 1)

i = 1

ans = 0

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

print(ans)

Submission status: Accepted

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

cur = 1
for i in range(n):
    cur = aa[cur-1]
    # print(cur)
    if cur == 2:
        print(i+1)
        break
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):
    a.append(int(input()))
c=0
v=0
while v!=1:
    b[v]+=1
    if b[v]==2:
        print(-1)
        exit()
    c+=1
    v=a[v]-1
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
A = []
for i in range(n):
    x = int(input())
    A.append(x-1)
k = 0
for i in range(n-1):
    k = A[k]
    if(k == 1):
        print(i+1)

        break
    if(i == n-2):
        print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
p = a[0]-1
result = False
ans = 1

if a[0] == 2:
    result = True
else:
    for i in range(10**5):
        if a[p] == 2:
            ans += 1
            result = True
            break
        else:
            p = a[p]-1
            ans += 1

if result == True:
    print(ans)
else:
    print("-1")

Submission status: Accepted

Solution code:
n = int(input())
num_list = list()
param = 1
counter = 0

for i in range(n):
    num_list.append(int(input()))
    
for i in range(n):
    param = num_list[param-1]
    counter += 1
    if param == 2:
        print(counter)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
N = int(input())
a = [0]*(N+1)
for i in range(1, N+1):
    a[i] = int(input())

def solve(i, n):
    if n == 2:
        print(i)
    elif i == N:
        print(-1)
    else:
        return solve(i+1, a[n])

solve(0, 1)

Submission status: Accepted

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

p = 1
ok = 0

for i in range(n):
  p = a[p]
  if p == 2:
    ok = 1
    break

if ok == 1:
  print(i+1)
else:
  print(-1)

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
a=[int(input()) for i in range(n)]
b=defaultdict(list)
s=1
c=0
while True:
  c+=1
  b[s].append(a[s-1])
  if b[s]==[2]:print(c);exit()
  if len(b[s])>1:print(-1);exit()
  s=a[s-1]

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())

a = []

for i in range(N):
    a.append(int(input()) - 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: Accepted

Solution code:
a = int(input())
l = []
for i in range(a):
    t = int(input())
    l.append((i,t-1))
dic={0}
k=l[0][1]
ans=1
for i in range(a):
    if k == 1:
        print(ans)
        break
    elif k not in dic:
        dic.add(k)
        k=l[k][1]
        ans+=1
    else:
        print(-1)
        break

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
A=[]
for i in range(n):
  A.append(int(input()))
A=[0]+A
a=1
ans=1
for i in range(100100):
  if A[a]==2:
    print(ans)
    exit()
  ans+=1
  
  a=A[a]
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
b=1
cnt=0
ok=False
for i in range(N+1):
    cnt+=1
    if a[b-1]==2:
        ok=True
        break
    b=a[b-1]
print(cnt if ok else -1)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]  # parser
A += [int(input()) for i in range(N)]
count = 1
flashed = A[1]
if flashed==2:
    print(1)
    exit(0)
while True:
    flashed = A[flashed]
    count += 1
    if flashed == 2:
        print(count)
        exit(0)
    elif count > 10 ** 6:
        print(-1)
        exit(0)

Submission status: Accepted

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

pushed=[False]*n
i = 0
count = 0
while True:
  if i ==1:
    print(count)
    exit()
  if not pushed[a[i]]:
    pushed[a[i]]=True
    i=a[i]
    count+=1
  else:
    print(-1)
    exit()

Submission status: Accepted

Solution code:
N = int(input())
L = [0]

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

tmp = 1
cnt = 0

while True:
    tmp = L[tmp]
    cnt += 1
    if tmp == 2:
        print(cnt)
        quit()
    if cnt > 10**5 +1:
        print(-1)
        quit()

Submission status: Accepted

Solution code:
n=int(input())
data=[int(input()) for i in range(n)]
x=1
was=set()
now=1
while x==1:
    if now==2:
        print(len(was))
        break
    if now in was:
        print('-1')
        break
    was|={now,}
    now=data[now-1]

Submission status: Accepted

Solution code:
import sys

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

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        sys.exit()
print("-1")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [None]+[int(input()) for _ in range(n)]
state = 1
for i in range(1, n+1):
    state = a[state]
    if state == 2:
        print(i)
        break
else:
    print(-1)

Submission status: Accepted

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

ans = -1
p = 0
for i in range(N):
    if A[p]==2:
        ans = i+1
        break
    else:
        p = A[p]-1

print(ans)

Submission status: Accepted

Solution code:
def func():
    pass


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

    m = a[0]
    for i in range(n + 1):
        if m == 2:
            print(i + 1)
            return
        m = a[m - 1]
    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def func():
    pass


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

    m = a[0]
    for i in range(n + 100):
        if m == 2:
            print(i + 1)
            return
        m = a[m - 1]
    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

light = 1

for ans in range(n + 1):
    if light == 2:
        break
    light = li[light]

if light != 2:
    ans = -1

print(ans)

Submission status: Accepted

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

now = 1
for i in range(n):
    if now == 2:
        print(i)
        exit()

    now = a[now-1]

print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
arr=[]
cnt=1
for i in range(n):
  arr.append(int(input()))
i=0
while cnt < n:
  j = arr[i]
  if j == 2:
    print(cnt);
    break
  else:
    i=j-1
    cnt+=1
    if cnt>=n:
      print(-1)
      break

Submission status: Accepted

Solution code:
import time

n = int(input())
listA=[]
index=0
while index < n:
    a = int(input())
    listA.append(a)
    index+=1

startT = time.time()
buttonLighted=0
pushCounter=0
index=0
while 1:
    pushCounter+=1
    if listA[buttonLighted] == 2:
        print(pushCounter)
        break
    else:
        buttonLighted = listA[buttonLighted]-1
    if time.time()-startT > 0.9:
        print(-1)
        break

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    A = [0]+imi(N)
    current = 1
    for i in range(1, 100001):
        current = A[current]
        if current == 2:
            return i
    return -1


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

Submission status: Accepted

Solution code:
import sys
n = int(input())
b = [0]
pushed = [0] * (n + 1)
for i in range(n):
  b.append(int(input()))
push = 1
cnt = 0
while(push != 2):
  if (pushed[push] != 1):
    pushed[push] = 1
    cnt += 1
    push = b[push]
  else:
    print(-1)
    sys.exit()
print(cnt)

Submission status: Accepted

Solution code:
# ABC 065
def getInt(): return int(input())
def getIntFromRows(n): return [int(input()) for i in range(n)]
debug = True
def db(x): 
    if debug: print(x)

debug = False

n = getInt()
a = getIntFromRows(n)
db(a)
btn = 1
c = 0
while btn!=2 and c<n:
    btn = a[btn-1]
    c += 1
if c>=n: c = -1
print(c)

Submission status: Accepted

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

    history = set()
    current = 0
    count = 0
    while current != 1:
        if current in history:
            return -1
        history.add(current)
        count += 1
        current = nxts[current]

    return count

if __name__ == '__main__':
    print(main())