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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
ais = [int(raw_input()) for oo in range(int(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: Wrong Answer

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

count = 0
nxt = 1
while count < N:
  nxt = a[nxt]
  if nxt == 2:
    ans = count
    break
  count += 1

print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    N = int(input())
    A = []
    for _ in range(N):
        a = int(input())
        A.append(a)
    count = 0
    num = 1
    for i in range(N):
        num = A[num-1]
        count += 1
        if num == 1:
            print("-1")
            exit()
        elif num == 2:
            print(count)
            exit()
resolve()

Submission status: Wrong Answer

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

for i in range(N):
    id = int(input()) - 1
    a[id].add(i)

step = -1
#already = a[1]
now = a[1]
next = set()
for i in range(N-1):
    #already = already.union(now)
    if 0 in now:
        step = i+1
        break
    for j in now:
        next = next.union(a[j])
    now = next
    next = set()

Submission status: Wrong Answer

Solution code:
n = int(input())
a=[0]
for i in range(n):
    a.append(int(input()))
s=1
cnt=0
while s !=2:
    s=a[s]
    cnt +=1
    print(s)
    print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [(int(input())-1) for _ in range(N)]
flag = [0]*N
flag[0] = 1
flag[1] = 2
cnt = 0
cur_b = 0
for i in range(N):
    cnt += 1
    next_b = l[cur_b]
    if flag[next_b] == 1:
        print(-1)
        break
    elif flag[next_b] == 2:
        print(cnt)
        break
    else:
        flag[next_b] == 1
        cur_b = next_b

Submission status: Wrong Answer

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

a0=a[0]
cnt=0

for i in range(n):
  a0=a[a0-1]
  cnt+=1
  if a0 == 2:
    cnt+=1
    break

if cnt==n:
  cnt=-1
else:
  pass
print(cnt)

Submission status: Wrong Answer

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

a0=a[0]
cnt=0

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

Submission status: Wrong Answer

Solution code:
#1
import sys
n = int(input())
a = []
for i in range(n):
    j = int(input())
    a.append(j)
    
point = a[0]
cnt = 1
for i in range(n):
    point = a[point-1]
    cnt +=1
    if point == 2:
        print(cnt)
        sys.exit()
print("-1")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

    if amap[i] == 1:
        ans = -1
        break

    amap[i] = 1

print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    N = int(input())
    A = [int(input()) - 1 for _ in range(N)]
    flgs = [False for _ in range(N)]
    flgs[0] = True
    i = A[0]
    cnt = 1
    while not flgs[1]:
        i = A[i]
        if flgs[i]:
            print(-1)
            return
        else:
            flgs[i] = True
            cnt += 1
    else:
        print(cnt)


resolve()

Submission status: Wrong Answer

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 = 1
    while x != 2:
        r += 1
        x = a[x - 1]
        if x == 1 or r >= n:
            r = -1
            break
    print(r)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

time = 1
push = 0
while True:
    
    if bL[push] == 2:
        print(time)
        break
    if time == N:
        print("-1")
        break
    
    push = bL[push]-1
    
    time += 1

Submission status: Wrong Answer

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

time = 1
push = 0
while True:
    
    if bL[push] == 2:
        print(time)
        break
    if time == 4:
        print("-1")
        break
    
    push = bL[push]-1
    
    time += 1

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = [0]
a += [int(input()) for i in range(n)]

cnt = 0
ps = 1

for _ in range(1, n + 1):
    ps = a[ps]
    cnt += 1

    if a[ps] == 2:
        print(cnt+1)
        break
else:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0

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

Submission status: Wrong Answer

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import itertools
import sys
input = sys.stdin.readline

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

pushed = set()
nex = A[0]
ans = 1
for i in itertools.count(1):
    nex = A[nex-1]
    if nex == 2:
        ans += 1
        break
    elif nex in pushed:
        ans = -1
        break
    else:
        pushed.add(nex)
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def main():
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    now = 1
    check = [0]*N
    ans = 0
    # while(1):
    for k in range(100):
        if now == 2:
            break
        if check[now-1] == 1:
            print(-1)
            return
        else:
            check[now-1] = 1
        now = A[now-1]
        ans += 1
        # print(now)
    print(ans)
    return
        
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def main():
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    now = 1
    check = [0]*N
    ans = 0
    # while(1):
    for k in range(100):
        if now == 2:
            break
        if check[now-1] == 1:
            print(-1)
            return
        else:
            check[now-1] = 1
        now = A[now-1]
        ans += 1
    print(ans)
        
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(500000)


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: Wrong Answer

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
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 ** 9)
INF = 10**9
mod = 10**9+7

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

Submission status: Wrong Answer

Solution code:
import  sys
N=int(input())
a = [int(input()) for _ in range(N)]
cnt=1
t=1
for i in range(10**5):
    cnt+=1
    t=a[t-1]
    if a[t-1]==2:
        print(cnt)
        sys.exit()

print(-1)

Submission status: Wrong Answer

Solution code:
import  sys
N=int(input())
a = [int(input()) for _ in range(N)]
cnt=1
t=1
for i in range(10000):
    cnt+=1
    t=a[t-1]
    if a[t-1]==2:
        print(cnt)
        sys.exit()

print(-1)

Submission status: Wrong Answer

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

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]


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

tmp = A[0]-1
for i in range(N):
    if A[tmp] == 2:
        print(i+2)
        sys.exit()
    else:
        tmp = A[tmp]-1

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
botton = []
for i in range(N):
   botton.append(int(input()))
now = botton[0]
flag = 0
for i in range(N-1):
  now = botton[now-1]
  if now == 2:
    print(i+2)
    flag += 1
if flag == 0:
  print(-1)

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

count = 1
i = 0

while count < n:
  i = a[i] - 1
  if i == 1:
    print(count)
    break
  count += 1
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = 1
p = a[0]
for _ in range(len(a)):
    if p == 2:
        print(ans)
        break
    elif ans > n:
        print(-1)
        break
    else:
        if p != a[p-1]:
            p = a[p-1]
            ans += 1
        else:
            print(-1)
            break

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = 1
p = a[0]
for _ in range(len(a)):
    if p == 2:
        print(ans)
        break
    elif ans > n:
        print(-1)
        break
    else:
        p = a[p-1]
        ans += 1

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = 1
p = a[0]
ln = []
for _ in range(len(a)):
    if p == 2:
        print(ans)
        break
    elif len(ln) != len(set(ln)):
        print(-1)
        break
    else:
        ln.append(p)
        p = a[p-1]
        ans += 1

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    visited = set()
    a = [0]+a
    i = 1

    while True:
        if i > N:
            print(-1)
            break
        elif a[i] == 2:
            print(i-1)
            break
        elif a[i] in visited:
            print(-1)
            break
        else:
            visited.add(i)
            i = a[i]


    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

b = [True]*N
i = 0
cnt = 1
while b[i]:
    if a[i] == 2:
        break
    b[i] = False
    i = a[i]-1
    cnt += 1
else:
    print(-1)

Submission status: Wrong Answer

Solution code:


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

	if cnt > N:
		print(-1)
		break

Submission status: Wrong Answer

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


cnt = 1
a = A[0]
while True:
	b = a
	a = A[a-1]
	if b == A[a-1]:
		break
	cnt += 1
	if a == 2:
		break

if b == A[a-1]:
	print(-1)
else:
	print(cnt)

Submission status: Wrong Answer

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

if now == 2:
    print(1)
    exit()

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

Submission status: Wrong Answer

Solution code:
n = int(input())
switch = [i - 1 for i in range(n)]
flag = [False] * n
i = 0
cnt = 0
while not flag[i]:
  cnt += 1
  flag[i] = True
  i = switch[i]
  if i == 1:
    print(cnt)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())

a=[""]*n

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

light = a[0]
counter = 1
result = -1
if light == 2:
  print(1)
  exit()
for i in range(n):
  light = a[light-1]
  counter += 1
  print(light)
  if light == 2:
    result = counter
    break
print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())

a=[""]*n

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
x=1
cnt=0
y=[1]
while True:
    x=a[x-1]
    cnt+=1
    y.append(x)
    if x==2:
        print(cnt)
        break
    elif x==1:
        print('-1')
        break
    elif cnt==10000:
        print(-1)
        break
    else:
        continue

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
x=1
cnt=0
y=[1]
while True:
    x=a[x-1]
    cnt+=1
    y.append(x)
    if x==2:
        print(cnt)
        break
    elif x==1:
        print('-1')
        break
    elif cnt==1000:
        print(-1)
        break
    else:
        continue

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
x=1
cnt=0
y=[]
while True:
    x=a[x-1]
    cnt+=1
    y.append(x)
    if x==2:
        print(cnt)
        break
    elif x==1 or x in y:
        print('-1')
        break
    else:
        continue

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
x = [int(input()) for i in range(N)]
cnt = 1
num = x[0]
if num != 2:
    cnt = 2
    for i in range(N):
        num = x[num-1]

        if(num == 2):
            break
        cnt += 1
print(cnt)

Submission status: Wrong Answer

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

M = [1]
i = 1
while True:
  M.append(A[i-1])
  if 2 in M:
    print(len(M)+1)
    break
  else:
    for j in range(len(M)):
      if M.count(M[j]) > 1:
        print("-1")
        exit()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Jun  4 22:35:46 2020

@author: naoki
"""

import sys

N = int(input()) # ボタンの個数
a = [int(input()) for i in range(N)] #k番目のボタンに書いてある番号
a = [0] + a #配列の番号合わせ
count = 0 #ボタンを押す回数
light = 1 # 光っているボタンの番号
light2 = [0 for i in range(N+1)] # 光っていたボタンの番号を記憶する
i = 0
if 2 not in a: # 2と書かれたボタンがない場合は不可能なので-1を出力
    print(-1)
    sys.exit()
else: # 2と書かれたボタンがある場合
    while light != 2 : # 光っているボタンが2になるか、ボタンを押した回数がNを超えてしまったら終了
        count += 1 
        light2[i] = light 
        light = a[light]#左から数えたときに光っているボタンの番号の順番にあるボタンに書かれている数字が次に光るボタンの番号である。
        for j in range(i+1):
            if light2[i] == light2[j]: #以前に光っていたボタンと同じものがあるならばループするので不適
                print(-1)
                sys.exit()
            else:
                i+=1
        
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for _ in range(n):
    a=int(input())
    l.append(a)
count=1
result=l[0]
f=-1
while n>0:
    result=l[result-1]
    if result==l[1]:
        f=count
        break
    count+=1
    n-=1
print(f)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for _ in range(n):
    a=int(input())
    l.append(a)
count=1
result=l[0]
f=-1
while n>=0:
    result=l[result-1]
    if result==l[1]:
        f=count
        break
    count+=1
    n-=1
print(f)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for _ in range(n):
    a=int(input())
    l.append(a)
count=1
result=l[0]
f=-1
while n>=0:
    result=l[result-1]
    if result==l[1]:
        f=count
    count+=1
    n-=1
print(f)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for _ in range(n):
    a=int(input())
    l.append(a)
count=1
result=l[0]
f=-1
while n>0:
    result=l[result-1]
    if result==l[1]:
        f=count
    count+=1
    n-=1
print(f)

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline())

button = sys.stdin.readlines()
button = [int(j.replace('\n','')) for j in button]


b = button[0]
counter = 1
for i in range(len(button)):
    counter += 1
    if button[b-1] == 2:
        print(counter)
        exit()
    else:
        b = button[b-1]

print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(input()) - 1 for i in range(n)]
count = 0
i = 0
for _ in range(10000):
	i = A[i]
	count += 1
	if i == 1:
		print(count)
		break
else:
	print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
lst=[int(input()) for i in range(n)]
lst.insert(0,0)

a=lst[1]
count =1
for i in range(n):
    count+=1
    if lst[a]==2:
      print(count)
      exit()
        
    else:
        a=lst[a]

print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
lst=[int(input()) for i in range(n)]
lst.insert(0,0)

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

if count<n:
    print(count)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
lst=[int(input()) for i in range(n)]
lst.insert(0,0)

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

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

Submission status: Wrong Answer

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

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

print(-1)

Submission status: Wrong Answer

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

i = 0
l = 1
while 1:
  l=a[l-1]
  if l==2:
    ans=l
    break
  if i>N:
    ans=-1
    break
  i+=1
print(ans)

Submission status: Wrong Answer

Solution code:

n = int(input())
a = [int(input()) for i in range(n)]
idx = a[0]
cnt = 1
for i in range(100000):
    idx = a[idx-1]
    cnt += 1
    if idx == 2:
        print(cnt)
        exit()
print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cur = 0
cnt = 0
for i in range(10**6):
    if a[cur] == 2:
        print(cnt)
        exit()
    cur = a[cur]-1
    cnt += 1
print(-1)

Submission status: Wrong Answer

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

cur = 0
cnt = 0
for i in range(10**7):
    if a[cur] == 2:
        print(cnt)
        exit()
    cur = a[cur]-1
    cnt += 1
print(-1)

Submission status: Wrong Answer

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

cur = 0
for i in range(10**9):
    if a[cur] == 2:
        print(cur)
        exit()
    cur = a[cur]-1
print(-1)

Submission status: Wrong Answer

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

cur = 0
for i in range(10**5):
    if a[cur] == 2:
        print(cur)
        exit()
    cur = a[cur]-1
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
ans=2
count=0
if 2 in a:
  while count<n:
    for i in range(n):
      if a[i]==ans:
        ans=i+1
        count+=1
        if i==0:
          print(count)
          exit()
        break
      elif i==n-1:
        print(-1)
        exit()

print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
x = [int(input()) for i in range(n)]
ans=1
visited=[False]*n
now=0
while(1):
  visited[now]=True # 今のところをいったことにする
  next=x[now]-1 # 次の行き先
  if visited[next]:
    print(-1)
    break
  if x[next]==2: # 次が2やったら終わる
    print(ans+1)
    break
  else:
    now=next
    ans+=1

Submission status: Wrong Answer

Solution code:
n=int(input())
x = [int(input()) for i in range(n)]
ans=1
visited=[False]*n
now=0
while(1):
  if visited[now]: #もう行ったことがあればループするだけ
    print(-1)
    break
  
  visited[now]=True # 今のところをいったことにする
  next=x[now]-1 # 次の行き先
  if x[next]==2: # 次が2やったら終わる
    print(ans+1)
    break
  else:
    now=next
    ans+=1

Submission status: Wrong Answer

Solution code:
def resolve():
    # 整数 1 つ
    n = int(input())
    # 整数複数個
    # a, b = map(int, input().split())
    # 整数 N 個 (改行区切り)
    A = [int(input()) for i in range(n)]
    # 整数 N 個 (スペース区切り)
    # N = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    Dic = {i:A[i-1] for i in range(1, n+1)}

    cnt = 0
    now = 1
    rout = []
#    while now not in rout:
    for _ in range(n):
        flag = True
        for r in rout:
            if now == r:
                flag = False
                break
        if flag == True:
            rout.append(now)
            now = Dic[now]
            cnt += 1
            if now == 2:
                break
        else:
            break

    if rout[-1] == 2:
        print(cnt)
    else:
        print("-1")

resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    # 整数 1 つ
    n = int(input())
    # 整数複数個
    # a, b = map(int, input().split())
    # 整数 N 個 (改行区切り)
    A = [int(input()) for i in range(n)]
    # 整数 N 個 (スペース区切り)
    # N = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    Dic = {i:A[i-1] for i in range(1, n+1)}

    cnt = 0
    now = 1
    rout = []
    while now not in rout:
        rout.append(now)
        now = Dic[now]
        cnt += 1
        if now == 2:
            break

    if rout[-1] == 2:
        print(cnt)
    else:
        print("-1")

resolve()

Submission status: Wrong Answer

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

while 1 != index:
  A = a[A - 1]
  if index == A -1:
    N = -1
    break
  else:
    N += 1
    index = A - 1
    

print(N)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
i = 1
s = 0
for _ in range(n):
  l.append(int(input()))
#print(l)
while True:
  j = l[i-1]
  #print(j, i, l[i-1])
  s += 1
  if j == 2:
    print(s)
    exit()
  if s == 10000:
    print(-1)
    exit()
  i = j

Submission status: Wrong Answer

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

now = a[0]
cnt = 0

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

print(-1)

Submission status: Wrong Answer

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

#n = 3
#a = [3,1,2]

a.insert(0,0)
i = a[1]
ans = 1
for _ in range(n):
  ans += 1
  if a[i] == 2:
    break
  else:
    i = a[i]
else:
  ans = -1

print(ans)

Submission status: Wrong Answer

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

number=l[0]
tmp=[0]
count = 1
for _ in range(N):
    if l[number] == 1:
        count+=1
        print(count)
        exit()
    elif l[number] in tmp :
        print('-1')
        exit()
    else :
        number = l[number]
        tmp.append(number)
        count+=1
print('-1')

Submission status: Wrong Answer

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

number=l[0]
tmp=[0]
count = 1
while True:
    if l[number] == 1:
        count+=1
        print(count)
        break
    elif l[number] in tmp :
        print('-1')
        break
    else :
        number = l[number]
        tmp.append(number)
        count+=1

Submission status: Wrong Answer

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

number=l[0]
tmp=[0]
count = 1
while True:
    if l[number-1]-1 in tmp:
        print('-1')
        break
    elif l[number-1] == 2:
        count+=1
        print(count)
        break
    else :
        number = l[l[number-1]]
        tmp.append(l[number-1])
        count+=1

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
pos = 1
ans = 0
while pos != 2:
  pos = A[pos-1]
  ans += 1
  if ans == 100000:
    break
print(ans)

Submission status: Wrong Answer

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:
  target = next_target
  next_target = a_dict[target]
  ans += 1
  del a_dict[target]
  if next_target not in a_dict:
    ans = -1
    cont = False
  elif next_target == 2:
    ans += 1
    cont = False

print(ans)

Submission status: Wrong Answer

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

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

if num != 2:
    count = -1

print(count)

Submission status: Wrong Answer

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

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

next = 0
cnt = 0
while(True):
    if next == 1:
        break
    if next == A[A[next] - 1] -1:
        cnt = -1
        break
    next = A[next] -1
    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
ns = [int(input()) for _ in range(n)]
if not 1 in ns:
    print(-1)
    sys.exit(0)
i = 0
cnt = 0
pushed = [0]*n
flg = False
while(True):
    cnt += 1
    i = ns[i] - 1
    if pushed[i] == 1:
        break
    pushed[i] = 1
    if i == 1:
        flg = True
        break
if flg:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

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

i = 0
l = len(A)
n = 1

if not 2 in A:
    print(-1)
else:
    while True:

        i = A[i] - 1

        if A[i] == 2:
            n += 1
            print(n)
            break
        n += 1
        if n == l+5:
            print(-1)
            break

Submission status: Wrong Answer

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

i = 0
l = len(A)
n = 1



while True:

    i = A[i] - 1

    if A[i] == 2:
        n += 1
        print(n)
        break
    n += 1
    if n == l+1:
        print(-1)
        break

Submission status: Wrong Answer

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

i = 0
l = len(A)
n = 1

if not 2 in A:
    print(-1)
else:
    while True:
        i = A[i] - 1
        n += 1
        if A[i] == 2:
            print(n)
            break

Submission status: Wrong Answer

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

i = 0
l = len(A)
n = 1


while True:

    i = A[i] - 1
    n += 1
    if A[i] == 2:
        print(n)
        break

    if n == l+1:
        print(-1)
        break

Submission status: Wrong Answer

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

i = 0
l = len(A)
n = 1


while True:

    i = A[i] - 1
    n += 1
    if A[i] == 2:
        print(n)
        break

    if n == l:
        print(-1)
        break