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

i = 0
l = len(A)
n = 1
lst = []

while True:
    lst.append(A[i])
    i = A[i] - 1
    n += 1
    if A[i] == 2:
        print(n)
        break

    if A[i] in lst:
        print(-1)
        break

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

Submission status: Wrong Answer

Solution code:
N = int(input())

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

now=1
c=1

while c<N:
    now=A[now-1]
    c+=1
    if(now==2):
        print(c)
        break
else:
    print("-1")

Submission status: Wrong Answer

Solution code:
N = int(input())

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

now=1-1
c=0

while c<N:
    now=A[now]-1
    c+=1
    if(now==2):
        print(now)
        break
else:
    print("-1")

Submission status: Wrong Answer

Solution code:
#170624B
N = int(input())
flag = 0
X = []
ans = 0
for i in range(N):
    a = int(input())
    X.append(a)
    if a == 2:
        flag = 1
b = X[0]
count = 1

#print(X)
if flag == 0:
    ans = -1

else:
    for i in range(N):
        c = X[b-1]
        #print(c)
        if c == b:
            ans = -1
            break
            
        else:
            b = c
            count += 1
            if c == 2:
                ans = count
                break
if ans == 0:
    ans = -1
    
print(ans)

Submission status: Wrong Answer

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

infer = s[0]
cnt = 1
flag = False
for i in range(n):
  if s[infer-1] == 2:
    flag = True
    break
    
  infer = s[infer-1]
  cnt += 1

print(cnt if flag else -1)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

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

if count == n:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

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

if count == n:
    print(-1)

Submission status: Wrong Answer

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

button=1

for i in range(10000):
    cnta[button]=cnta[button]+1
    if cnta[2]==1:
        print(sum(cnta)-1)
        break
    elif cnta[button]>=2:
        print("-1")
        break
    button=a[button]

Submission status: Wrong Answer

Solution code:
n = int(input())

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

check = [1]*n
p = 1
ans = 1
total = 1
for i in range(n):
    total += 1
    check[p-1] = 0
    p = a[p-1]
    if a[p-1] == 2:
        ans = 0
        break

if ans == 0:
    print(total)
else:
    print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())

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

check = [1]*n
p = 1
ans = 1
total = 1
while check[p-1] == 1:
    total += 1
    check[p-1] = 0
    p = a[p-1]
    if a[p-1] == 2:
        ans = 0
        break

if ans == 0:
    print(total)
else:
    print("-1")

Submission status: Wrong Answer

Solution code:
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
print(a)
cnt = 1
x = a[0]-1
if a[0] == 2:
    print(cnt)
    exit()
for _ in range(10**5+10):

    if a[x] == 2:
        cnt += 1
        print(cnt)
        exit()
    else:
        x = a[x]-1
        cnt += 1
print(-1)

Submission status: Wrong Answer

Solution code:
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
# print(a)
cnt = 1
x = a[0]-1
for _ in range(10**5+10):
    if a[x] == 2:
        cnt += 1
        print(cnt)
        exit()
    else:
        x = a[x]-1

    cnt += 1
print(-1)

Submission status: Wrong Answer

Solution code:
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
# print(a)
cnt = 1
x = a[0]-1
for _ in range(10**5+10):
    cnt += 1
    if a[x] == 2:
        print(cnt)
        exit()
    else:
        x = a[x]-1
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
button = list()
for i in range(N):
    button.append(int(input()))

pre  = 1
print(button)
i = 0
c = 1
rireki = [0]*N
while all: 
    if(button[i]==2):
        print(c)
        break
    rireki[i] +=1
    if(rireki[i]==2):
        print(-1)
        break
    i = button[i]-1
    c+=1

Submission status: Wrong Answer

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

ans = 1
next = A[0]
while ans < n:
    ans += 1
    if A[next-1] == 2:
        print(ans)
        exit()
    next = A[next-1]

print(-1)

Submission status: Wrong Answer

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

ans = 1
next = A[0]
for _ in range(n-1):
    ans += 1
    if A[next-1] == 2:
        print(ans)
        break
    next = A[next-1]
else:
    print(-1)

Submission status: Wrong Answer

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

ans = 1
next = A[0]
for _ in range(n):
    ans += 1
    if A[next-1] == 2:
        print(ans)
        break
    next = A[next-1]
else:
    print(-1)

Submission status: Wrong Answer

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

cnt, i = 0, 0
while cnt < N:
    print(i)
    cnt += 1
    if A[i] == 1:
        break
    i = A[i]
print(cnt if cnt < N else -1)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

dist=[-1 for i in range(n)]
dist[0]=0

dq=deque()
dq.append(0)
while dq:
  x=dq.pop()
  for i in tree[x]:
    if dist[i]==-1:
      dq.append(i)
      dist[i]=dist[x]+1

print(dist[1]+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
a = [INT() for _ in range(N)]

i = a[0]-1
cnt = 1

A = [1]*N
A[0] = 0

while A[i] != 0:
	A[i] = 0
	x = a[i]
	i = 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 _ in range(N)]
nxt = a[0]-1
cnt = 1

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

print(-1)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
a = [INT() for _ in range(N)]

i = a[0]-1
cnt = 1

A = [1]*N

while A[i] != 0:
	A[i] = 0
	x = a[i]
	i = x-1
	cnt += 1

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

print(-1)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
a = [INT() for _ in range(N)]

i = a[0]-1
cnt = 1

while i != 0:
	x = a[i]
	j = x-1

	if i == j:
		print(-1)
		exit()
	else:
		i = j


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

print(-1)

Submission status: Wrong Answer

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

button = a_lst[0]

cnt = 1 #必ずボタン1は押すため一回はカウント
ans = 0
for i in range(n):
    button = a_lst[button-1]
    if button == 2:
        ans = cnt + 1 #2と書かれたボタンを押す分の一回を追加
        break
    cnt += 1

if cnt == n+1 and ans == 0: #cntの初期値が1だからn+1
    print("-1")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())

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


def fun(k):
    if B[k] == 1:
     	return False                                                    
    else:
        return B[k]
ans = 0
cnt = 0
for i in range(n+1):
    cnt = fun(cnt)
    ans +=1
    if cnt ==False:
        print(ans)
        sys.exit() 
print(-1)
sys.exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())

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


def fun(k):
    if B[k] == 1:
     	return False                                                    
    else:
        return B[k]
ans = 0
cnt = 0
for i in range(n):
    cnt = fun(cnt)
    ans +=1
    if cnt ==False:
        print(ans)
        sys.exit() 
print(-1)
sys.exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
A = [0]*n
B = [int(input())-1 for i in range(n)]
C = [0]
print(B)
def fun(k):
    if B[k] == 1:
     	return False                                                    
    else:
        return B[k]
ans = 0
cnt = 0
for i in range(n):
    cnt = fun(cnt)
    ans +=1
    if cnt ==False:
        print(ans)
        sys.exit() 
print(-1)
sys.exit()

Submission status: Wrong Answer

Solution code:
n, *a = map(int, open(0).read().split())
cnt = 0
a = [0] + a
i = 1
while 1:
    tmp = i
    i = a[i]
    cnt += 1
    if tmp == i:
        print(-1)
        break
    elif a[i] == 2:
        print(cnt+1)
        break
    elif cnt >= n:
        print(-1)
        break

Submission status: Wrong Answer

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

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

j = 1
while j != 2:  
  if a[a[j]] == j:
    ans = -1
    break
  else:
    ans += 1
    j = a[j]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
p = 0
while True:
    if p == 1 or cnt >= n:
        break
    p = A[p-1]
    cnt += 1
if p == 1:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n = int(input())
A = [int(input()) for i in range(n)]
flag = [False for i in range(n)]
flag[0] = True
cnt = 0
for i in range(n):
    if flag[i] == False:
        continue
    flag[A[i]-1] = True
    cnt += 1
    if flag[1] == True:
        break
if flag[1] == False:
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
dic = {}
for i in range(1,N+1):
    dic[i] = int(input())
b = 1
lis = []
cnt = 0
for i in range(N):
    lis.append(b)
    b = dic[b]
    cnt += 1
    if b == 2:
        print(cnt)
        cnt = -99
        break
    if b in lis:
        print(-1)
        break
if cnt != -99:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
num = 0
count = 0
a_list = []

for i in range(n):
    a = int(input())
    a -= 1
    a_list.append(a)

if a_list.count(1) == 0:
    print(-1)
    exit()

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

print(count)

Submission status: Wrong Answer

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

push = set([])
ans = 0
b = 0

while 1:
  if b in push:
    print(-1)
    exit()
  push.add(b)
  b = A[b]
  ans += 1

  if b== 2:
    print(ans)
    break

Submission status: Wrong Answer

Solution code:
n = int(input())
button = []
for i in range(n):
  button.append(int(input()))
times = 0
next_button = 1
while times < 5:
  times += 1
  next_button = button[next_button-1]
  if next_button == 2:
    print(times)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
flg = False
start = 1
ans = 0
for _ in range(n):
    a.append(int(input()))
for i in range(10**5):
    if a[start - 1] == 2:
        ans +=1
        flg = True
        break
    else:
        ans +=1
        start = a[start] - 1
if flg:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# AtCoder
from collections import defaultdict
N = int(input())
a = [int(input()) for _ in range(N)]

d = defaultdict(int)
for i, e in enumerate(a, 1):
    d[i] = e

count = 1
tmp = d[1]
while count <= N:
    v = d[tmp]
    count += 1
    if v == 2:
        print(count)
        exit()
    tmp = v
print(-1)

Submission status: Wrong Answer

Solution code:
# AtCoder
from collections import defaultdict
N = int(input())
a = [int(input()) for _ in range(N)]

d = defaultdict(int)
for i, e in enumerate(a, 1):
    d[i] = e

count = 1
tmp = d[1]
while count < N:
    v = d[tmp]
    count += 1
    if v == 2:
        print(count)
        exit()
    tmp = v
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
q = 1
cnt = 0
li = []
for i in range(100001):
    q = a[q-1]
    cnt += 1
    if q == 2:
        print(cnt)
        break
print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [int(input()) for i in range(N)]
count=1
i=0
while True:
  if S[i]==2 or count==N:
    print(count)
    exit()
  else:
    count+=1
    i=S[i]-1
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [int(input()) for i in range(N)]
count=1
i=0
while True:
  if S[i]==2 or count==N-1:
    break
  else:
    count+=1
    i=S[i]-1
if count==N-1: print(-1)
else: print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

s = 0
res = 0
while s!=1:
    ns = al[s]
    if s==al[ns]:
        print(-1)
        exit()
    s = ns
    res += 1
print(res)

Submission status: Wrong Answer

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

if not 1 in a:
    print(-1)

# hst = []
btn = 0
cnt = 0

while cnt <= 10**7:
    # hst.append(btn)
    btn = a[btn]
    cnt += 1
    if btn == 1:
        print(cnt)
        exit()

print(-1)

Submission status: Wrong Answer

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

if not 1 in a:
    print(-1)

hst = []
btn = 0
cnt = 0

while cnt <= 10**5:
    hst.append(btn)
    btn = a[btn]
    cnt += 1
    if btn == 1:
        print(cnt)
        exit()

print(-1)

Submission status: Wrong Answer

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

cnt = 0
cur = 1

if a[0] == 2:
    print(1)

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

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
count = 0
i = 1
x = 0
osita = [0]*(N+1)
for b in range(N+1):
    osita[b] = i
    i = a[i-1]
    
if 2 in osita:
    print(a.index(2))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
check = [True] * N
ne = a[0]
check[0] = False
cnt = 1
while True:
    if check[ne-1] == False:
        print(-1)
        break
    cnt += 1
    check[ne-1] = False
    ne = a[ne-1]
    if ne == 2:
        print(cnt)
        break

Submission status: Wrong Answer

Solution code:
N=int(input())
A = []
for i in range(N):
  Ai=int(input())
  A.append(Ai)
on = 1 #現実世界での番号
for i in range(N):
  on = A[on-1] #次のボタンを知るために、配列を参照する
  if on == 2:
    print(i)
    break
if on != 2:
  print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
B = 1
for j in(A):
  if j == 2:
    print(B)
    break
  elif j == [-1]:
    print('-1')
  else:
    B += 1

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[]
for i in range(N):
	L.append(int(input()))
count=0
ans=0
i=0
while(1):
	count+=1
	if count==10*5+10:
		print(-1)
		exit()
	i=L[i]-1
	ans+=1
	if i==1:
		print(ans)
		exit()

Submission status: Wrong Answer

Solution code:
N=int(input())
a_list=[]
exists=False
for _ in range(N):
    a = int(input())
    a_list.append(a)
    if a == 2:
        exists = True
if not exists:
    print(-1)

curr = 0
already = set()
ans=0
while True:
    nexthop = a_list[curr] - 1
    if nexthop in already:
        print(-1)
        exit()
    already.add(nexthop)
    curr = nexthop
    ans+=1
    if nexthop == 1:
        print(ans)
        exit()

Submission status: Wrong Answer

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

b = a[0]
cnt = 1

while b != 2 and cnt <= 1:
    b = a[b-1]
    cnt += 1
if b == 2:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

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

visited = set()
count = 0
next = 1
while True:
  count += 1
  visited.add(next)
  next = a[next-1]
  if next == 1:
    print(count)
    exit(0)
  if next in visited:
    print(-1)
    exit(0)

Submission status: Wrong Answer

Solution code:
N=int(input())
b=list(int(input()) for i in range(N))

n=0
ans=0
for j in range(N):
    n=b[n]-1
    ans += 1
    if n==1:
        break
print(ans)

Submission status: Wrong Answer

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

ans = -1
num = 1
cnt = 0
for _ in range(n):
    num = list_a[num-1]
    cnt += 1
    if num == 2:
        ans = cnt
    if cnt == 2*n:
        break

print(ans)

Submission status: Wrong Answer

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

ans = -1
num = 1
cnt = 0
for _ in range(n):
    num = list_a[num-1]
    cnt += 1
    if num == 2:
        ans = cnt
    if cnt == n+1:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
aaa = [int(input()) for _ in range(n)]
num = 1
ans = -1
for i in range(n):
    num = aaa[num - 1]
    if num == 2:
        ans = i + 1
print(ans)

Submission status: Wrong Answer

Solution code:
*A, = map(int, open(0).read().split())
N = A[0]
visited = [0]*(N+1)

a = A[1]
cnt = 1
while True:
    a = A[a]
    cnt += 1
    if a == 2:
        print(cnt)
        break
    if visited[a]:
        print(-1)
        break
    visited[a] = True

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    switches = [int(input()) for _ in range(N)]

    count = 0
    cur   = 0
    nxt   = 0

    for i in range(2*N):
        count += 1

        if switches[cur] == 2:
            print(count)
            break

        nxt = switches[cur] - 1

        if switches[nxt] - 1 == cur:
            print(-1)
            break

        cur = nxt

main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    switches = [int(input()) for _ in range(N)]

    cur = 0
    nxt = 0
    count = 0
    while True :

        nxt = switches[cur] - 1
        count += 1
        
        if switches[nxt] - 1 == cur:
            print(-1)
            break

        if nxt == 1:
            print(count)
            break

        cur = nxt
main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    switches = [int(input()) for _ in range(N)]

    count = 0
    cur   = 0
    nxt   = 0

    for i in range(N):
        count += 1

        if switches[cur] == 2:
            print(count)
            break

        nxt = switches[cur] - 1

        if switches[nxt] - 1 == cur:
            print(-1)
            break
        
        cur = nxt

main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    switches = list()

    for _ in range(N):
        s = int(input())
        switches.append(s)

    cur = 0
    nxt = 0
    count = 0
    while True :

        nxt = switches[cur] - 1
        count += 1
        
        if switches[nxt] - 1 == cur:
            print(-1)
            break

        if nxt == 1:
            print(count)
            break

        cur = nxt
main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

count = 0
ans = -1

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

print(ans)

Submission status: Wrong Answer

Solution code:
# ABC 065: B – Trained?
n = int(input())
a = [int(input()) - 1 for _ in range(n)]

lightned = ''
lightning = 0

while lightned.count(str(a[lightning])) == 0:

    if a[lightning] == 1:
        print(len(lightned) + 1)
        break

    lightned += str(lightning)
    lightning = a[lightning]

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# ABC 065: B – Trained?
n = int(input())
a = [int(input()) - 1 for _ in range(n)]

lightned = ' '
lightning = 0

while lightned.count(str(a[lightning])) == 0:

    if a[lightning] == 1:
        print(len(lightned) + 1)
        break

    lightning = a[lightning]
    lightned += str(lightning)

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# ABC 065: B – Trained?
n = int(input())
a = [int(input()) - 1 for _ in range(n)]

lightned = []
lightning = 0

while a[lightning] not in lightned:
    lightning = a[lightning]

    if lightning== 1:
        break

    lightned.append(lightning)
    
print(len(lightned) + 1 if a[lightning] == 1 else -1)

Submission status: Wrong Answer

Solution code:
n, *A = map(int, open(0).read().split())
x = 0
for i in range(n):
  x, A[x] = A[x]-1, 0
  if x == -1:
    print(-1)
    exit()
  if x == 1:
    print(i+1)
    exit()

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=[int(input())-1 for _ in range(n)]
if arr[0]==1:
    print(-1)
else:
    step = 0
    curr=arr[0]
    while True:
        if step>n:
            print(-1)
            break
        else:
            if curr == 1:
                print(step+1)
                break
            else:
                curr = arr[curr]
                step += 1

Submission status: Wrong Answer

Solution code:
arr=[int(input())-1 for _ in range(int(input()))]
if arr[0]==1:
    print(-1)
else:
    step = 1
    visited={0} # index
    curr=arr[0]
    while True:
        if curr in visited:
            print(-1)
            exit()
        else:
            if curr == 1:
                print(step)
                exit()
            curr = arr[curr]
            step+=1

Submission status: Wrong Answer

Solution code:
N = int(input())
button = [int(input()) for _ in range(N)]
current_button = button[0]
ans = 1
while True:
    current_button = button[current_button-1]
    ans += 1
    if current_button == 2:
        print(ans)
        exit()
    if ans >= 10**5 + 10:
        print(-1)
        exit()

Submission status: Wrong Answer

Solution code:
#1
n = int(input())
a = [0]

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

cnt = 0
b = 0
ans = 0

for j in range(n+1):
    cnt += 1
    b = a[b]
    
    if b == 2:
        ans = 1
        break
if ans == 1:
    print(cnt)

else:
    print("-1")

Submission status: Wrong Answer

Solution code:
#1
n = int(input())
a = [0]

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

cnt = 0
b = a[1]
ans = 0

for j in range(n+1):
    cnt += 1
    b = a[b]
    
    if b == 2:
        ans = 1
        break
if ans == 1:
    print(cnt)

else:
    print("-1")

Submission status: Wrong Answer

Solution code:
#1
n = int(input())
a = [0]

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

cnt = 0
b = a[1]
ans = 0

for j in range(n+1):
    cnt += 1
    b = a[b]
    
    if b == 2:
        ans = 1
        break
if ans == 1:
    print(cnt)

else:
    print(-1)