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

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
while ans:
    find=g.index(find)+1
    count+=1
    if find==1:
        print(count+1)
        break
    elif count>=len(g):
        print(-1)
        break
    else:
        continue

Submission status: Runtime Error

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

a,b,c =I()
ans = min(a+b,b+c)
print(min(ans,a+c))

Submission status: Runtime Error

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

x=a[0]
while True:
  if x==2:
    print(len(t))
    break
  elif x in t:
    print(-1)
    break
  else:
    t.append(x)
    y=x
    x=a[y-1]

Submission status: Runtime Error

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

x=a[0]
while True:
  if x==2:
    print(len(t))
    exit()
  elif x in t:
    print(-1)
    exit()
  else:
    x=a[x-1]

Submission status: Runtime Error

Solution code:
n = int(input())
a = [0,int(input()) for i in range(n)]
cnt = 1
next_a = a[1]
while cnt <= 10**5:
    next_a = a[next_a]
    cnt += 1
    if next_a == 2:
        break

if cnt < 10**5:
    print(cnt)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = []
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif A[push] in count:
      break
      
    else:
      　count.append(push)
        push = A[push]
        
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

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

count = []
n = 0
for k in range(N+1):
  if A[n] == 1:
    break
 
  elif A[n] in count:
    ans = -1
    break
  
  else:
    count.append(A[n])
    ans = k+1
    n = A[n]
 
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
d = []
visited = [False for _ in range(N+1)]
for _ in range(N):
  d.append(int(input())-1)
  
def dfs(i,cnt):
  if i == 1:
    return cnt
  elif cnt >= N:
    return -1
  elif visited[i]:
    return -1
  else:
    visited[i] = True
    return dfs(d[i],cnt+1)
  
print(dfs(0,0))

Submission status: Runtime Error

Solution code:
N = int(input())
d = []
visited = [False for _ in range(N+1)]
for _ in range(N):
  d.append(int(input())-1)
  
def dfs(i,cnt):
  if i == 1:
    return cnt
  elif visited[i]:
    return -1
  else:
    visited[i] = True
    return dfs(d[i],cnt+1)
  
print(dfs(0,0))

Submission status: Runtime Error

Solution code:
N = int(input())
d = []
visited = [False for _ in range(N)]
for _ in range(N):
  d.append(int(input())-1)
  
def dfs(i,cnt):
  if i == 1:
    return cnt
  elif visited[i]:
    return -1
  else:
    visited[i] = True
    return dfs(d[i],cnt+1)

def main():
  
  return dfs(0,0)
  
print(main())

Submission status: Runtime Error

Solution code:
N = int(input())
d = []
visited = [False for _ in range(N)]
for _ in range(N):
  d.append(int(input())-1)
  
def dfs(i,cnt):
  if visited[i]:
    return -1
  elif i == 1:
    return cnt
  else:
    visited[i] = True
    return dfs(d[i],cnt+1)

def main():
  if d[1] == 1:
    return -1
  
  return dfs(0,0)
  
print(main())

Submission status: Runtime Error

Solution code:
import math
n = int(input())
g = [0 for _ in range(int(1e5+3))]
visited = [False] * int(1e5 + 3)
ans = math.inf

def dfs(i, c):
    if visited[i]:
        return
    global ans
    if i == 2:
        ans = min(ans, c-1)
        return

    visited[i] = True
    dfs(g[i], c + 1)

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


dfs(1, 1)
print(ans if ans != math.inf else -1)

Submission status: Runtime Error

Solution code:
import math
n = int(input())
g = [[] for _ in range(int(1e5+2))]
visited = [False] * int(1e5 + 2)
ans = math.inf

def dfs(i, c):
    if visited[i]:
        return
    global ans
    if i == 2:
        ans = min(ans, c-1)
    visited[i] = True
    for to in g[i]:
        dfs(to, c + 1)

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


dfs(1, 1)
print(ans if ans != math.inf else -1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

pos = 0
for i in range(N):
    pos = A[pos] - 1
    if (pos == 1):
        print(i+1)
        exit()
print(-1)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int main() {
    /* 入力処理 */
    int N;
    cin >> N;
    vector<int> a(N);
    for(int i =0;i<N;i++) {
        cin >> a.at(i);
    }

    /* 変数宣言 */
    int count = 0; //ボタンを押した回数のカウンター
    int j = 0; //最初のボタン1(すなわち、j=0)

    /* ボタンを押していく */
    for(int i=0;i<N;i++) {
        count++; // ボタンを押した回数のカウンター

        if(a.at(j) == 2) { //もしボタンjを押してボタン2に行ったら
            cout << count << endl; //ボタンを押した回数を出力して、プログラムの終了
            exit(0); //プログラムの強制終了
        }

        j = a.at(j) - 1; //次のボタンの番号（配列は0スタートのため、-1する）
    }
    
    cout << -1 << endl; //for文でN個のボタンに対して計算しても、ボタン2に至らなかった場合、-1を出力
}

Submission status: Runtime Error

Solution code:
n = int(input())
a = []
s = [1]
t = 1
ans = 0
for i in range(n):
  a.append(int(input())
while t != 2:
  t = a[t-1]
  s.append(t)
  if s.count(t) == 2:
    ans = -1
    break
  ans += 1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
cnt = 1
l = a[0]
for _ in range(N):
    if l in a:
        l = a[l-1]
        cnt += 1
        if l == 2:
            print(cnt)
            break
else:
    print(-1)

Submission status: Runtime Error

Solution code:
ais = [int(raw_input()) for oo in range(raw_input())]#n = raw_input()
seen = set([])
u = 0
while(u!= 1 or u not in seen):
	seen.add(u)
	u = ais[u] - 1

print len(seen) if u == 1 else -1

Submission status: Runtime Error

Solution code:
ais = [int(raw_input()) for oo in range(raw_input())]#n = raw_input()
#for
ais = map(int , raw_input().split())
seen = set([])
u = 0
while(u!= 1 or u not in seen):
	seen.add(u)
    u = ais[u] - 1

print len(seen) if u == 1 else -1

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
Row = int(input())
List = []
for i in range (Row):
  List.append(int(input()))
s = 1
res = 0
flag = False
for i in range(Row):
  s = List[s]
  res += 1
  if s == 2:
    flag=True
    break
if not flag:
  print(-1)
else:
  print(res)

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *a = map(int, read().split())
    if n == 2:
        print(1)
        sys.exit()
    r = 0
    x = 2
    while x != 1:
        r += 1
        x = a.index(x) + 1
        if x == 2 or r >= n:
            r = -1
            break
    print(r)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *a = map(int, read().split())
    if n == 2:
        print(1)
        sys.exit()
    r = 0
    x = 2
    while x != 1:
        r += 1
        x = a.index(x) + 1
        if x == 2:
            r = -1
            break
    print(r)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def check(now, memo, cnt):
  now = a[now]
  if now in memo:
    return -1
  if now == 2:
    return cnt
  check(now, memo.add(now), cnt+1)
    
print(check(1,{1},0))

Submission status: Runtime Error

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

def check(now, memo, cnt):
  now = a[now]
  if now in memo:
    return False
  if now == 2:
    return cnt
  check(now, memo.add(now), cnt+1)
    
if flg:
  print(cnt)
else:
  print(-1)

Submission status: Runtime Error

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

memo = {1}
now = 1
cnt = 0
flg = False
while True:
  now = a[now]
  cnt += 1
  if now in memo:
    break
  if now == 2:
    flg = True
    break
    
if flg:
  print(cnt)
else:
  print(-1)

Submission status: Runtime Error

Solution code:
import numpy as np

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


#回数i,インデックスn
def button(i,n):
    if i == N:
        return

    n_new = a[n-1]
    if n_new == 2:
        print(i)
        return 
    else:
        return button(i+1,n_new)

button(1,1)

Submission status: Runtime Error

Solution code:
# ABC065_B
N = int(input())
A = [int(input()) for _ in range(N)]
next = A[0]
ans = -1
cnt = 0
for i in range(N):
    next = A[next]
    cnt += 1
    if next == 2:
        ans = cnt
        break
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[input() for i in range(n)]
bu=0
end=-1
for i in range(n):
   if bu==1:
    end=i-1
    break
  bu=a[bu]-1
print(end)

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest

    lines = sys.stdin.readlines()
    lines = list(map(lambda x: int(x.rstrip()), lines))
    n = lines[0]
    i = 1
    for c in range(1, n+1):
        i = lines[i]
        if i == 2:
            print(c)
            return
    print(-1)

Submission status: Runtime Error

Solution code:
    lines = sys.stdin.readlines()
    lines = list(map(lambda x: int(x.rstrip()), lines))
    n = lines[0]
    i = 1
    for c in range(1, n+1):
        i = lines[i]
        if i == 2:
            print(c)
            return
    print(-1)

Submission status: Runtime Error

Solution code:
S={n:int(input()) for n in list(range(1,N+1))}
a=1
cnt=0
while True:
    a=S[a]
    cnt+=1
    if a==2:
        print(cnt)
        break
    if a==1:
        print(-1)
        break

Submission status: Runtime Error

Solution code:
S={n:int(input()) for n in range(1,N+1)}
a=1
cnt=0
while True:
    a=S[a]
    cnt+=1
    if a==2:
        print(cnt)
        break
    if a==1:
        print(-1)
        break

Submission status: Runtime Error

Solution code:
N=int(input())
S={n:int(input()) for n in range(1,N+1)}
a=1
while True:
    a=S[a]
    cnt+=1
    if a==2:
        print(cnt)
        break
    if a==1:
        print(-1)
        break

Submission status: Runtime Error

Solution code:
S={n:int(input()) for n in range(1,N+1)}
a=1
while True:
    a=S[a]
    cnt+=1
    if a==2:
        print(cnt)
        break
    if a==1:
        print(-1)
        break

Submission status: Runtime Error

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

#最初はボタン１が光っている
index = 1
#押した回数0からスタート
count = 0

while count < N:
  
  if a[index-1]==2:
    count += 1
    print(count)
    exit()
    
  else:
    count += 1
    #次に光るボタン
    index=a[index-1]

print(-1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
button = [0]*n
button1 = a[0] - 1
try:
    a.remove(2)
    print('01')
    exit()
except:
    pass
count = 1
while True:
    button[button1] += 1
    button1 = a[button1] - 1
    if button[1] == 1:
        print(count)
        break
    try:
        button.remove(2)
        print('-1')
        break
    except:
        count += 1
        pass

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.appent(int(input()))
i=0
count=1
while(1):
  if a[i]==1:
    frag=-1
    break
  elif a[i]==2:
    frag=count
    break
  else:
    i=a[i]-1
    count+=1
print(frag)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.appent(int(input))
i=0
count=1
while(1):
  if a[i]==1:
    frag=-1
    break
  elif a[i]==2:
    frag=count
    break
  else:
    i=a[i]-1
    count+=1
print(frag)

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0

if lamps.count(2)==0:
    print("-1")
    exit()

while PressedButtons[a]==False :
    PressedButtons[a]=True
    count+=1
    a=lamps[a]
    if a==2:
        print(count)
        exit()
print('-1')

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0

if lamps.count(2)==0:
    print("-1")

while PressedButtons[a]==False :
    PressedButtons[a]=True
    count+=1
    a=lamps[a]
    if a==2:
        print(count)
        exit()
print('-1')

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0

while PressedButtons[a]==False :
    PressedButtons[a]=True
    count+=1
    a=lamps[a]
    if a==2:
        print(count)
        exit()
print('-1')

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0

while True:
    if PressedButtons[a]==False:
        PressedButtons[a]=True
        count+=1
        a=lamps[a]
        if a==2:
            print(count)
            exit()
    # elif PressedButtons[a]==True:
    #     print("-1")
    #     exit()
    # else:
    #     print("-1")
    #     exit()
print("-1")

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0


while True:
    if PressedButtons[a]==False:
        PressedButtons[a]=True
        count+=1
        a=lamps[a]
        if a==2:
            print(count)
            exit()
    elif PressedButtons[a]==True:
        print("-1")
        exit()
    else:
        print("-1")
        exit()
print("-1")

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0


while True:
    if PressedButtons[a]==False:
        PressedButtons[a]=True
        count+=1
        a=lamps[a]
        if a==2:
            print(count)
            exit()
    elif PressedButtons[a]==True:
        print("-1")
        exit()
    else:
        print("-1")
        exit()

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0


while True:
    if PressedButtons[a]==False:
        PressedButtons[a]=True
        count+=1
        a=lamps[a]
        if a==2:
            print(count)
            exit()
    elif PressedButtons[a]==True:
        print("-1")
        exit()
    else:
        print("-1")

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0

while True:
    if PressedButtons[a]==False:
        PressedButtons[a]=True
        count+=1
        a=lamps[a]
        if a==2:
            print(count)
    elif PressedButtons[a]==True:
        print("-1")
        exit()
    else:
        print("-1")

Submission status: Runtime Error

Solution code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,-1)

if lamps.count(2)==0:
    print("-1")
    exit()

# no2=[i for i , x in enumerate(lamps) if x==2]
PressedButton=[]

num=2
while lamps[1]!=num:
    a=lamps.index(num)
    if PressedButton.count(a)==0:
        PressedButton.append(a)
        num=a
    else:
        print("-1")
        exit()
print(PressedButton)

Submission status: Runtime Error

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

if lamps.count(2)==0:
    print("-1")
    exit()

# no2=[i for i , x in enumerate(lamps) if x==2]
PressedButton=[]

num=2
while lamps[0]!=num:
    a=lamps.index(num)
    if PressedButton.count(a)==0:
        PressedButton.append(a)
        num=a
    else:
        print("-1")
        exit()
print(PressedButton)

Submission status: Runtime Error

Solution code:
#import numpy as np
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with
#from itertools import permutations as perm


N = int(input())
#H1,M1,H2,M2,K= map(int,input().split())
#A = list(map(int,input().split()))
#B = list(map(int,input().split()))
#S= str(input())
l=[]
for i in range(N):
    B = int(input())
    l.append(B)

now=0
next=1
c=0
while c<10**6

Submission status: Runtime Error

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

button = 0
cnt = 0
done = [False] * n

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

print(cnt)

Submission status: Runtime Error

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

count = 0
next_index = 2
while count < n:
    if next_index < 1 or len(buttons) - 1 <= next_index:
        count = -1
        break

    prev_index = buttons.index(next_index)
    count += 1
    if prev_index == 1:
        break
    else:
        next_index = prev_index

print(count if count < n else -1)

Submission status: Runtime Error

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

count = 0
next_index = 2
while count < n:
    if next_index < 1 or len(buttons) - 1 <= next_index:
        count = -1
        break

    prev_index = buttons.index(next_index)
    count += 1
    if prev_index == 1:
        break
    else:
        next_index = prev_index

print(count if count < n else -1)

Submission status: Runtime Error

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

count = 0
next_index = 2
while count < n:
    if len(buttons) - 1 <= next_index <= 0:
        count = -1
        break

    prev_index = buttons.index(next_index)
    count += 1
    if prev_index == 1:
        break
    else:
        next_index = prev_index

print(count if count < n else -1)

Submission status: Runtime Error

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

count = 0
next_index = 2
while count < n:
    if len(buttons) - 1 <= next_index:
        count = -1
        break

    prev_index = buttons.index(next_index)
    count += 1
    if prev_index == 1:
        break
    else:
        next_index = prev_index

print(count if count < n else -1)

Submission status: Runtime Error

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

count = 0
next_index = 2
while count < n:
    if len(buttons) - 1 < next_index:
        count = -1
        break

    prev_index = buttons.index(next_index)
    count += 1
    if prev_index == 1:
        break
    else:
        next_index = prev_index

print(count if count < n else -1)

Submission status: Runtime Error

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

count = 0
next_index = 2
while count < n:
    prev_index = buttons.index(next_index)
    count += 1
    if prev_index == 1:
        break
    else:
        next_index = prev_index

print(count if count < n else -1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
tmp = a.index(2)
count = 1
while tmp != 0:
  tmp = a.index(tmp+1)
  count += 1
  if count >= n:
    print(-1)
    exit()
print(count)

Submission status: Runtime Error

Solution code:
import sys
N = int(input())
A = [int(input()) for _ in range(N)]
ans = 1
a = A.index(1)+1
#print(a)
for i in range(N*2):
    a = A[a-1]
    #print(a)
    if a == 2:
        print(ans)
        sys.exit()
    ans += 1
print(-1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

routedDic = []

def NextRoute(i):
    x = dic[i]
    if x in routedDic:
        return -1
    
    routedDic.append(x)

    if x == 2:
        return len(routedDic)
    else:
        return NextRoute(x)

print(NextRoute(1))

Submission status: Runtime Error

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

now = a[0]
cnt = 0

while now != 2:
    cnt += 1
    now = a[now-1]
    if cnt >= n:
        print(-1)
        exit()
    fp.append(now)

if now == 2:
    cnt += 1

print(cnt)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

# In[28]:


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


# In[29]:


def func(x, a, history, cnt):
#     print(x, a[x-1], history)
    if a[x-1] == 2:
        print(cnt)
    elif a[x-1] in history:
        print(-1)
    else:
        history.append(a[x-1])
        cnt += 1
        func(a[x-1], a, history, cnt)
func(1, a, [], 1)


# In[ ]:

Submission status: Runtime Error

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

Submission status: Runtime Error

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


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

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

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

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
n = int(input())
a = [int(input()) for i in range(n)]

switch = [False] * (n + 1)
def button(x,cnt):
    if switch[x]:
        print(-1)
        return -1
    if x == 2:
        print(cnt)
        return
    switch[x] = True
    button(a[x - 1], cnt + 1)
    return cnt

ans = button(1, 0)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(50000)
n = int(input())
a = [int(input()) for i in range(n)]

switch = [False] * (n + 1)
def button(x,cnt):
    if switch[x]:
        print(-1)
        return -1
    if x == 2:
        print(cnt)
        return
    switch[x] = True
    button(a[x - 1], cnt + 1)
    return cnt

ans = button(1, 0)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)
n = int(input())
a = [int(input()) for i in range(n)]

switch = [False] * (n + 1)
def button(x,cnt):
    if switch[x]:
        print(-1)
        return -1
    if x == 2:
        print(cnt)
        return
    switch[x] = True
    button(a[x - 1], cnt + 1)
    return cnt

ans = button(1, 0)

Submission status: Runtime Error

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

switch = [False] * (n + 1)
def button(x,cnt):
    if switch[x]:
        print(-1)
        return -1
    if x == 2:
        print(cnt)
        return
    switch[x] = True
    button(a[x - 1], cnt + 1)
    return cnt

ans = button(1, 0)

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
a = []
ans = 0
lamp = 1
for _ in range(n):
  A = int(input())
  a.append(a)
  
for i in range(n):
  if lamp -1 == 1:
    print(i)
    sys.exit()
  lamp = a[lamp - 1]
print(-1)

Submission status: Runtime Error

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

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

print(-1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
def search(i, p):
    if p > n:
        print(-1)
        exit()
    elif i == 2:
        print(p)
        exit()
    else:
        search(a[i-1], p+1)

search(1, 0)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
def search(i, p):
    if p > n:
        print(-1)
        exit()
    if i == 2:
        print(p)
        exit()
    else:
        search(a[i-1], p+1)

search(1, 0)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
def search(i, p):
    if i == 2:
        print(p)
        exit()
    elif p > n:
        print(-1)
        exit()
    else:
        search(a[i-1], p+1)

search(1, 0)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
keep=1
count=0
ababa=0
for i in range(n):
  count+=1
  keep=a[keep-1]
  if keep==2:
    ababa=1
    print(count)
    break
if ababa=0:
  print(-1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [[int(input())-1, False] for _ in range(n)]
a[0][1] = True
now = 0
cnt = 0
while True:
    now = a[now][0]
    if a[now][1]:
        print(f"now={now}") 
        print(-1)
        break
    a[now][1] = True
    cnt += 1
    if now == 1:
        print(cnt)
        break

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

while cnt <= N:
  lgt = a[i]
  cnt += 1
  if lgt == 2:
    print(cnt)
    break
  i = lgt - 1
else:
  print(-1)

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**5)

def rec(n, a, seen):
	if seen[n-1]:
		return -1
	if a[n-1] == 2:
		return seen.count(True) + 1
	seen[n-1] = True
	return rec(a[n-1], a, seen)

def resolve():
	n = int(input())
	a = []
	seen = [False]*n
	for _ in range(n):
		a.append(int(input()))
	print(rec(1, a, seen))
resolve()

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(100**5)

def rec(n, a, seen):
	if seen[n-1]:
		return -1
	if a[n-1] == 2:
		return seen.count(True) + 1
	seen[n-1] = True
	return rec(a[n-1], a, seen)

def resolve():
	n = int(input())
	a = []
	seen = [False]*n
	for _ in range(n):
		a.append(int(input()))
	print(rec(1, a, seen))
resolve()

Submission status: Runtime Error

Solution code:
def rec(n, a, seen):
	if seen[n-1]:
		return -1
	if a[n-1] == 2:
		return seen.count(True) + 1
	seen[n-1] = True
	return rec(a[n-1], a, seen)

def resolve():
	n = int(input())
	a = []
	seen = [False]*n
	for i in range(n):
		a.append(int(input()))
	print(rec(1, a, seen))
resolve()

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
ans=-1
b=a[0]
if b==2:
    print(1)
    break
else:
    for i in range(n):
        b=a[b-1]
        # print(f"b={b}")
        if b==2:
            ans=i+2
            break
    print(ans)