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())
k=[]
cnt=1
for i in range(n):
    l=int(input())
    k.append(l)
i=k[0]
if k.count(2)==0:
    print(-1)
else:
    while i != 2 and cnt<=n:     
        cnt += 1
        i=k[i-1]
    if i==2:
        print(cnt+1)
    else:
        print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
  m = q[ind]-1
  if ind+1 == q[m]:
    if ind+1 == 2 or q[m]==2:
        print(cnt)
        break
    print(-1)
    exit()
  ind = q[ind]-1
  cnt += 1
  if ind == 1:
    print(cnt)
    break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
  m = q[ind]-1
  if ind+1 == q[m]:
    print(-1)
    exit()
  ind = q[ind]-1
  cnt += 1
  if ind == 1:
    print(cnt)
    break

Submission status: Wrong Answer

Solution code:
n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n-1):
  m = q[ind]-1
  if ind+1 == q[m]:
    print(-1)
    exit()
  ind = q[ind]-1
  cnt += 1
  if ind == 1:
    print(cnt)
    break

Submission status: Wrong Answer

Solution code:
n = int(input())
light = [0] * (n+1)

for i in range(1,n+1):
  light[int(input())] = i
s = 2
ans = 0
while light[s] != 0 and light[s] != 1 and ans <= n:
  s = light[s]
  ans += 1

if light[s] == 1:
  print(ans + 1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from typing import List


def answer(n: int, a: List[int]) -> int:
    next_value = a[0]
    count = 1
    for _ in range(n):
        next_value = a[next_value - 1]
        count += 1
        if next_value == 2:
            return count

    return -1


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    print(answer(n, a))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

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

v = l[0]
print(l)
print(v)
ans = 0
while v != 2 and v != -1:
  tmp = l[v-1]
  l[v-1] = -1
  v = tmp
  ans += 1

if v == 2:
  print(ans)
else:
  print(-1)

Submission status: Wrong Answer

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

if not a.count(2):
    print('-1')
else:
    i = 0
    cnt = 0
    while True:
        cnt += 1
        if a[i] == 2:
            print(cnt)
            break
        elif a[i] == i or dup.count(i):
            print('-1')
            break
        dup.append(i)
        i = a[i]-1

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
M = []
cn = 1
for i in range(N):
    if i == 0:
        Z = a[0]
    else:
        Z = a[Z-1]
        cn = cn + 1
        M.append(Z)
        if Z == 2:
            print(cn)
            exit()
       
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
print(a)
M = []
cn = 1
for i in range(N):
    if i == 0:
        Z = a[0]
    else:
        Z = a[Z-1]
        cn = cn + 1
        M.append(Z)
        if Z == 2:
            print(cn)
            exit()
       
print(-1)

Submission status: Wrong Answer

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

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

ans = pos = 0
flag = [0] * N
while True:
    #print(pos,a[pos]-1)
    pos = a[pos] - 1
    ans += 1
    if a[pos] == 2:
        ans += 1
        break
    if flag[pos] > 0:
        ans = -1
        break
    else:
        flag[pos] = 1

print(ans)

Submission status: Wrong Answer

Solution code:
# B - Trained?
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    cnt = 0
    x = 1

    if 2 in a:
        for _ in range(n-1):
            x = a[x-1]
            cnt += 1

            if x == 2:
                print(cnt)
                exit()

    else:
        print(-1)
        exit()
        

if __name__ ==  "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# B - Trained?
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    flg = True
    cnt = 1
    c = []
    

    while flg:
        if 2 in a:
            p = a.index(2)
            c = [p]
            for _ in range(n):
                if p+1 in a:
                    p = a.index(p+1)
                    cnt += 1
                    c.append(p)
                    if p == 0:
                        print(cnt)
                        exit()

                    if cnt != len(c):
                        print(-1)
                        exit()
                
        else:
            print(-1)
            exit()


if __name__ ==  "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# B - Trained?
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    flg = True
    cnt = 1
    

    while flg:
        if 2 in a:
            p = a.index(2)
            for _ in range(n):
                if p+1 in a:
                    p = a.index(p+1)
                    cnt += 1
                    if p == 0:
                        print(cnt)
                        exit()
            else:
                print(-1)
                exit()
        else:
            print(-1)
            exit()


if __name__ ==  "__main__":
    main()

Submission status: Wrong Answer

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

if 2 not in a:
  print(-1)

x = 1
for i in range(n-1):
 
  x = a[x-1]
  cnt += 1

            
  if x == 2:
    print(cnt)
    exit()

print(-1)

Submission status: Wrong Answer

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

if 2 not in a:
  print(-1)

x = 1
for i in range(2,n+1):
 
  x = a[x-1]
  cnt += 1

            
  if x == 2:
    print(cnt)
    exit()

print(-1)

Submission status: Wrong Answer

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

if 2 not in a:
  print(-1)

x = 1
for i in range(2,n):
 
  x = a[x-1]
  cnt += 1

            
  if x == 2:
    print(cnt)
    exit()

print(-1)

Submission status: Wrong Answer

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

if 2 not in a:
  print(-1)

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

print(-1)

Submission status: Wrong Answer

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

next = a[0] - 1
for i in range(n):
    next = a[next] - 1
    b.append(next + 1)
print(b.index(2) + 2) if 2 in b else print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
data = [int(input()) for i in range(n)]
count = 1
temp = data[0]
for i in range(n):
    temp = data[temp-1]
    count += 1
    if temp == 2:
        print(count)
        exit()
print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*(N+1)
light[0] = 1
cnt = 0

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

for i in range(N+1):
    if light[i] == 1 and light[L[i]] == 0:
        light[i], light[L[i]] = 0, 1
        cnt += 1
        if light[1] == 1:
            print(cnt)
            exit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*(N+1)
light[0] = 1
cnt = 0

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

for i in range(N+1):
    if light[1] == 1:
        print(cnt)
        exit()
    
    if light[i] == 1 and light[L[i]] == 0:
        light[i], light[L[i]] = 0, 1
        cnt += 1

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*(N+1)
light[0] = 1
cnt = 0

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

for i in range(N):
    if light[i] == 1 and light[L[i]-1] == 0:
        light[i], light[L[i]-1] = 0, 1
        cnt += 1
    if light[1] == 1:
        print(i)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*N
light[0] = 1
cnt = 0

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

for i in range(N):
    if light[i] == 1 and light[L[i]-1] == 0:
        light[i], light[L[i]-1] = 0, 1
        cnt += 1
    else:
        cnt += 1
    if light[1] == 1:
        print(cnt-1)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*N
light[0] = 1
cnt = 0

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

'''
print(L)
print(light)
'''

for i in range(N):
    if light[i] == 1 and light[L[i]-1] == 0:
        light[i], light[L[i]-1] = 0, 1
        cnt += 1
    if light[1] == 1:
        print(cnt)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*N
light[0] = 1
cnt = []

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

for i in range(N):
    if light[i] == 1:
        if light[i] != light[L[i]-1]:
            light[i], light[L[i]-1] = 0, 1
            cnt.append(L[i]-1)
    if light[1] == 1:
        print(len(set(cnt)))
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*N
light[0] = 1
cnt = []

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

for i in range(N):
    if light[i] == 1:
        if light[i] != light[L[i]-1]:
            light[i], light[L[i]-1] = 0, 1
            cnt.append(i)
    if light[1] == 1:
        print(len(set(cnt)))
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*N
light[0] = 1
cnt = 0

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

for i in range(N):
    if light[i] == 1:
        if light[i] != light[L[i]-1]:
            light[i], light[L[i]-1] = 0, 1
            cnt += 1
    if light[1] == 1:
        print(cnt)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
light = [0]*N
light[0] = 1
cnt = 0

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

for i in range(N):
    if light[i] == 1:
        light[i], light[L[i]-1] = 0, 1
        cnt += 1
    if light[1] == 1:
        print(cnt)
        exit()


print(-1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
# a = list(map(lambda x: int(x) - 1, a))	#すべての要素から-1

p = a[0]

if p == 2:
    print(1)    

cnt = 1

for _ in range(n):
    p = a[p-1]
    cnt += 1
    if p == 2:
        print(cnt)
        exit()

print(-1)

Submission status: Wrong Answer

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

while True:
  if now == A[1]:
    print(cnt+1)
    break
  if (cnt > N):
    print(-1)
    break
  now = A[now-1]
  cnt += 1

Submission status: Wrong Answer

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

while True:
  if now == A[1]:
    print(cnt+1)
    break
  if (cnt > N):
    print(-1)
    break
  now = A[now-1]
  print(now)
  cnt += 1

Submission status: Wrong Answer

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

if 2 not in a:
  print(-1)

x = 1
for i in range(n):
  x = a[x-1]
  cnt += 1
  if x == 2:
    print(cnt)
    exit()

print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(a)

cnt = 0
a_before = 1
while cnt < n:
    if cnt == 0 and a[1] == 2:
        print(0)
        break
    else:
        cnt += 1
        if a[a_before] == 2:
            print(cnt)
            break
        a_before = a[a_before]
        print('**', cnt, a_before)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())

for i in range(n):
    a = int(input())
    if i ==0 and a==2:
        print(0)
        break
    else:
        if a == 2:
            print(i)
            break
        if a == 1:
            print(-1)
            break        
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
cnt = 0

for i in range(n):
    a = int(input())
    if i ==0 and a==2:
        print(0)
        break
    else:
        if a == 2:
            print(i)
            break
        if a == 1:
            print(-1)
            break

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
a = [ri() for _ in range(n)]
idx = 1
cnt = 1
while idx != -1:
    a[idx-1], idx = -1, a[idx-1]
    cnt += 1
    if a[idx-1] == 2:
        print(cnt)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [(i+1, int(input())) for i in range(N)]
cnt = 1
for i in range(N):
  if 2 not in A[i]:
    print(-1)
    exit()
  if A[i][0] == A[i][1]:
    if i != 2:
      print(-1)
      exit()
    
    
x = A[0]
while x[1] != 2:  
  if x[1] == 1:
    print(-1)
    exit()
  x = A[x[1]-1]
  cnt += 1
  
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [(i+1, int(input())) for i in range(N)]
cnt = 0
for i in range(N):
  if 2 not in A[i]:
    print(-1)
    exit()
  if A[i][0] == A[i][1]:
    if i != 2:
      print(-1)
      exit()
    
    
x = A[0]
while x[1] > 2:  
  
  x = A[x[1]-1]
  cnt += 1
  if x[1] == 1:
    print(-1)
    exit()
print(cnt+1)

Submission status: Wrong Answer

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

if 2 not in A:
  print(-1)

a = A[0]
while a != 2: 
  a = A[a-1]
  cnt += 1
  if a == 1:
    print(-1)
    exit()
  elif a == A[a-1]:
    print(-1)
    exit()
print(cnt)

Submission status: Wrong Answer

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

if 2 not in A:
  print(-1)

a = A[0]
while a != 2: 
  if a == 1:
    print(-1)
    exit()
  elif a == A[a-1]:
    print(-1)
    exit()
  else:
    a = A[a-1]
    cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [(i+1, int(input())) for i in range(N)]
cnt = 1
for i in range(N):
  if 2 not in A[i]:
    print(-1)
    exit()
    
x = A[0]
while x[1] != 2:  
  
  x = A[x[1]-1]
  cnt += 1
  if x[1] == 1:
    print(-1)
    exit()
print(cnt+1)

Submission status: Wrong Answer

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

if 2 not in A:
  print(-1)

a = A[0]
while a != 2: 
  a = A[a-1]
  cnt += 1
  if a == 1:
    print(-1)
    exit()
print(cnt)

Submission status: Wrong Answer

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

if 2 not in A:
  print(-1)

a = A[0]
while a > 2: 
  a = A[a-1]
  cnt += 1
  if a == 1:
    print(-1)
    exit()
print(cnt+1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [(i+1, int(input())) for i in range(N)]
cnt = 0
for i in range(N):
  if 2 not in A[i]:
    print(-1)
    exit()
    
x = A[0]
while x[1] > 2:  
  
  x = A[x[1]-1]
  cnt += 1
  if x[1] == 1:
    print(-1)
    exit()
print(cnt+1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [(i+1, int(input())) for i in range(N)]
cnt = 0

x = A[0]
while x[1] > 2:  
  
  x = A[x[1]-1]
  cnt += 1
  if x[1] == 1:
    print(-1)
    exit()
print(cnt+1)

Submission status: Wrong Answer

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

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

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

num=l[0]
cnt=1
for i in range(n+1):
  num=l[num-1]
  cnt+=1
  if num==2:
    break
    
if cnt==n+1:
  cnt=-1
  
print(cnt)

Submission status: Wrong Answer

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

num=l[0]
cnt=1
for i in range(10**5):
  num=l[num-1]
  cnt+=1
  if num==2:
    break
    
if cnt==10**5:
  cnt=-1
  
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0

for i in range(N+1):

    if A[push] == 1:
        ans = i
        break
        
    else:
        push = A[push]
      
print(ans)

Submission status: Wrong Answer

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

ans = -1
push = 1
for i in range(N+1):
    light = A[push]
    push = light
    if light == 2:
        ans = i+1
        break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [int(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)
    exit()
if turn ==2:
  print(count)
else:
  print(-1)

Submission status: Wrong Answer

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

# N 1
ans = 0
next_idx = 0
for _ in range(n):
    ans += 1
    next_idx = a_l[next_idx]-1
    if next_idx == 1:
        break
print(ans)

Submission status: Wrong Answer

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

flag = True
position = 1

for i in range(n):
    position = a[position-1]
    if position == 2:
        print(i+1)
        flag = False
        
if flag:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys 
N = list(map(int, sys.stdin))
bot = N[1]
for i in range(N[0]):
  if bot == 2:
    print(i+1)
    break
  bot == N[bot]
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
import sys 
N = list(map(int, sys.stdin))
bot = N[1]
for i in range(N[0]):
  if bot == 2:
    print(i)
    break
  bot == N[bot]
else:
  print(-1)

Submission status: Wrong Answer

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

t = 1
while a.count(-1) <= n:
    t = a[t-1]
    a[t-1] = -1
    
#    print(t)
#    print(a)
    if t == -1:
        break
    elif t == 2:
        break

if a.count(-1) >= n:
    print(-1)
else:
    print(a.count(-1))

Submission status: Wrong Answer

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

k = 1
al = []
count = 1
while count <= n:
    k = a[k-1]
    if k in al:
        print('-1')
        break
    elif k == 2:
        print(count)
        break
    else:
        count += 1
        
    al.append(k)

if count >= n:
    print(-1)

Submission status: Wrong Answer

Solution code:
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")


N = I()
a = [I() for _ in range(N)]
a = list(map(lambda x:x-1,a))

ans = -1

# 訪れる順序、indexは移動回数
route = [0]
# 訪れた番号、高速検索用にhash tableを使う
visited = set([0])
next = a[0]

while True:
    if next in visited:
        loopStart = route.index(next)
        break
    else:
        route.append(next)
        visited.add(next)
        next = a[next]
        
print(route)
# 番号2のボタンが含まれる   
if 1 in visited:
    ans = route.index(1)

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[]
I=[]
A=[]
for i in range(N):
    I.append(i+1)
    a.append(int(input()))
for i in range(N):
    A.append([I[i],a[i]])
B=A[0]
C=0
D=[]
for i in range(N):
    for j in range(N):
        if B[1]==A[j][0]:
            C+=1
            B=A[j]
            D.append(B[1])
        if B[1]==2:
            break
if 2 not in D:
    print('-1')
else:
    print(C+1)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[]
I=[]
A=[]
for i in range(N):
    I.append(i+1)
    a.append(int(input()))
for i in range(N):
    A.append([I[i],a[i]])
B=A[0]
C=0
D=[]
for i in range(N):
    for j in range(N):
        if B[1]==A[j][0]:
            C+=1
            B=A[j]
            D.append(B[1])
        if B[1]==2:
            break
print(D)
if 2 not in D:
    print('-1')
else:
    print(C+1)

Submission status: Wrong Answer

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
 
  if ans > n-1:
    print(-1)
    flag = False

Submission status: Wrong Answer

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
 
  if ans >= n-1:
    print(-1)
    flag = False

Submission status: Wrong Answer

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
 
  if ans >= n:
    print(-1)
    flag = False

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
a = [s-1 for s in a]
index = a[0]
count = 1
flag = False
while(count <= N):
  count += 1
  if a[index] == 1:
    flag = True
    break
  else:
    index = a[index]

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
a = [s-1 for s in a]
index = a[0]
count = 1
flag = False
while(count <= N):
  if a[index] == 1:
    flag = True
    break
  else:
    index = a[index]
    count += 1
if (flag):
  print(count)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
a = [s-1 for s in a]
index = 0
count = 1
flag = False
while(count <= N):
  index = a[index]
  count += 1
  if a[index] == 1:
    flag = True
    break
if (flag):
  print(count)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
a = [s-1 for s in a]
index = 0
count = 1
flag = False
while(count < N):
  index = a[index]
  count += 1
  if a[index] == 1:
    flag = True
    break
if (flag):
  print(count)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
d={}
for i in range(1, n+1):
  x=int(input())
  d[i] = x
tmp = d[1]
ans=1
for _ in range(n):
  ans+=1
  tmp = d[tmp]
  #print(tmp)
  if tmp == 2:
    print(ans)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
d={}
for i in range(1, n+1):
  x=int(input())
  d[i] = x
tmp = d[1]
ans=1
for _ in range(n-1):
  ans+=1
  tmp = d[tmp]
  #print(tmp)
  if tmp == 2:
    print(ans)
    exit()
print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(int(input()) for _ in range(N))
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: Wrong Answer

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

light = lst[1]
count = 1

while light != 2 and count < n:
    light = lst[light]
    count += 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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