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

count=1
turn_on=a[0]-1

for _ in range(N):
    count+=1
    if a[turn_on]==2:
        print(count)
        exit()
    turn_on=a[turn_on]-1
print(-1)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[]

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

count=1
turn_on=a[0]-1

for _ in range(N):
    count+=1
    if a[turn_on]==2:
        print(count)
        #exit()
    turn_on=a[turn_on]-1
print(-1)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[]

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

count=1
turn_on=a[0]-1

for _ in range(N+1):
    count+=1
    if a[turn_on]==2:
        print(count)
        exit()
    turn_on=a[turn_on]-1
print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# Trained?

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

trained = []
next = a[0]
step = 1
while True:
    trained.append(next)
    next = a[next-1]
    step += 1
    if next in trained:
        res = -1
        break
    elif next == 2:
        res = step
        break

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
b_list = []
for _ in range(N):
  b_list.append(int(input()))
if 2 not in b_list:
  print(-1)
else:
  i = b_list[0]
  keiyu_list = []
  cnt=0
  while b_list[i-1] not in keiyu_list:
    i = b_list[i-1]
    keiyu_list.append(i)
    cnt+=1
    if i == 2:
      cnt+1
      break
  if i==2:
    print(cnt)
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
b_list = []
for _ in range(N):
  b_list.append(int(input()))
i = b_list[0]
keiyu_list = []
cnt=1
while b_list[i-1] not in keiyu_list:
  i = b_list[i-1]
  keiyu_list.append(i)
  cnt+=1
  if i == 2:
    break
if i==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()))

idx = 1
flag = False
for i in range(1, N+1):
  idx = A[idx - 1]
  if idx == 2:
    flag = True
    break

if flag:
  print(i + 1)
else:
  print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input())-1 for i in range(n)]
 
light = [False for i in range(n)]
cnt = 1
botan = a[0]
if botan == a[1]:
    pass
else:
    while cnt <= n:
        cnt += 1
        botan = a[botan]
        light[botan] = True
        if light[1]:
            break
 
if cnt < n:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

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

light = [False for i in range(n)]
cnt = 1
botan = a[0]
while cnt <= n:
    cnt += 1
    botan = a[botan]
    light[botan] = True
    if light[1]:
        break

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

Submission status: Wrong Answer

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

light = [False for i in range(n)]
cnt = 1
botan = a[0]
while cnt < n:
    cnt += 1
    botan = a[botan]
    light[botan] = True
    if light[1]:
        break

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
li = list(int(input()) for i in range(n))

a = li[0]
num = 1
while a!=2:
    
    a = li[a-1]
    num += 1
    if a == 2:
        print(num)
    
    if num>100000:
        print(-1)
        break

Submission status: Wrong Answer

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

i = a[0]
count = 1

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

print(-1)

Submission status: Wrong Answer

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

i = a[0]
count = 1

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

print(-1)

Submission status: Wrong Answer

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

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

print(-1)

Submission status: Wrong Answer

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

tmp = 0
index = 0
for i in range(N):
    tmp = aLst[tmp] - 1
    aLst[index] = 100
    if tmp == 99:
        break
    elif tmp == 1:
        cnt += 1
        ans = cnt
        break
    else:
        index = tmp
        cnt += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = [0]
for _ in range(N):
  nums.append(int(input()))
check_cnt = 1
_next = nums[1]
while True:
  if check_cnt >= N:
    break
  else:
    if next == 2:
      print(check_cnt)
      exit()
    _next = nums[_next]
    check_cnt += 1
print(-1)

Submission status: Wrong Answer

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

#x=dict(zip(range(1,N+1),a))
print(x)
route=[1,]
while 2 not in route:
  #print(x[route[-1]])
  if x[route[-1]-1] not in route:
    route.append(x[route[-1]-1])
  else:
    print(-1)
    exit()
print(len(route)-1)

Submission status: Wrong Answer

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

x=dict(zip(range(1,N+1),a))
print(x)
route=[1,]
while 2 not in route:
  #print(x[route[-1]])
  if x[route[-1]] not in route:
    route.append(x[route[-1]])
  elif len(route)-1>N:
    print(-1)
    exit()
  else:
    print(-1)
    exit()
print(len(route)-1)

Submission status: Wrong Answer

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

x=dict(zip(range(1,N+1),a))
print(x)
route=[1,]
while 2 not in route:
  #print(x[route[-1]])
  if x[route[-1]] not in route:
    route.append(x[route[-1]])
  elif len(route)>N:
    print(-1)
    exit()
  else:
    print(-1)
    exit()
print(len(route)-1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools


def solve(N: int, a: "List[int]"):
    counter = 0
    nx = 0
    r = []
    for i in range(N):
        counter += 1
        if a[nx] - 1 == 1:
            break
        nx = a[nx] - 1
        r.append(a[nx])
    if counter < N and 2 in r:
        print(counter)
    else:
        print(-1)
    return


# Generated by 1.1.4 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:
#!/usr/bin/env python3
import sys, math, fractions, itertools


def solve(N: int, a: "List[int]"):
    counter = 0
    nx = 0
    r = []
    for i in range(N):
        counter += 1
        if a[nx] - 1 == 1:
            break
        nx = a[nx] - 1
        r.append(a[nx])
    if counter < N+1 and 2 in r:
        print(counter)
    else:
        print(-1)
    return


# Generated by 1.1.4 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())
n = []
for i in range(N):
  n.append(int(input()))

a = 1
c = -1

for i in range(N):
  a = n[a-1]
  if a == 2:
    if c != -1:
      c = (i+1)

print(c)

Submission status: Wrong Answer

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

a = 1
c = -1

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

print(c)

Submission status: Wrong Answer

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

def main(N,A):
    tmp = A[0]
    flag = False
    for i in range(N):
        if(tmp==2):
            ans = i + 1
            flag = True
        tmp = A[tmp-1]
    print(ans) if flag else print(-1)

if __name__ == '__main__':
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    main(N,A)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,*b=map(int,open(0))
count_num=0
j=b[0]
for i in range(a):
	count_num += 1
	if b[j-1]==2:
		count_num += 1
		print(count_num)
		break
	else:j=b[j-1]
else:print(-1)

Submission status: Wrong Answer

Solution code:
a,*b=map(int,open(0))
count_num=1
j=b[0]
for i in range(a):
	count_num += 1
	if b[j-1]==2:
		print(count_num)
		break
	else:j=b[j-1]
else:print(-1)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc065/tasks/abc065_b
# B - Trained?
n = int(input().split()[0])

index2number_dict = {}
number2index_dict = {}
button_list = []

for _ in range(n):
    number = int(input().split()[0])
    button_list.append(number)
    

# ボタンが光っているときに押すと、
# 今のボタンの光が消える
# 次のボタンが光る
# ボタン2が光っている状態でやめたい
# print(index2number_dict)

start_index = 1
start_number = button_list[start_index - 1]
# print(start_number)
# print(start_index)

step = 0
lighting_number = start_number
lighting_index = start_index # 1 - n
status_list = str(lighting_index)
can_stop = False

while True:
    step += 1
    if lighting_index == 2:
        can_stop = True
        break

    if step >= len(button_list) + 1:
        break
    # print('i = {}, ai = {}'.format(lighting_index, lighting_number))
    
    lighting_number = button_list[lighting_number - 1]
    lighting_index = lighting_number

    # print('Next -> i = {}, a_i = {} '.format(lighting_index, lighting_number))
    
    status_list += str(lighting_index)

if not can_stop:
    step = -1

# print('===========')
# print(status_list)
print(step)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc065/tasks/abc065_b
# B - Trained?
n = int(input().split()[0])

index2number_dict = {}
number2index_dict = {}
button_list = []

for _ in range(n):
    number = int(input().split()[0])
    button_list.append(number)
    

# ボタンが光っているときに押すと、
# 今のボタンの光が消える
# 次のボタンが光る
# ボタン2が光っている状態でやめたい
# print(index2number_dict)

start_index = 1
start_number = button_list[start_index - 1]
# print(start_number)
# print(start_index)

step = 0
lighting_number = start_number
lighting_index = start_index # 1 - n
status_list = str(lighting_index)
can_stop = False

while True:
    step += 1
    if lighting_index == 2:
        can_stop = True
        break

    if step >= len(button_list):
        break
    # print('i = {}, ai = {}'.format(lighting_index, lighting_number))
    
    lighting_number = button_list[lighting_number - 1]
    lighting_index = lighting_number

    # print('Next -> i = {}, a_i = {} '.format(lighting_index, lighting_number))
    
    status_list += str(lighting_index)


if not can_stop:
    step = -1

# print('===========')
# print(status_list)
print(step)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc065/tasks/abc065_b
# B - Trained?
n = int(input().split()[0])

index2number_dict = {}
number2index_dict = {}
button_list = []

for _ in range(n):
    number = int(input().split()[0])
    button_list.append(number)
    

# ボタンが光っているときに押すと、
# 今のボタンの光が消える
# 次のボタンが光る
# ボタン2が光っている状態でやめたい
# print(index2number_dict)

start_index = 1
start_number = button_list[start_index - 1]
# print(start_number)
# print(start_index)

step = 0
lighting_number = start_number
lighting_index = start_index # 1 - n
status_list = str(lighting_index)
can_stop = False

while True:
    step += 1
    if lighting_index == 2:
        can_stop = True
        break

    if len(status_list) % 2 == 0:
        half = len(status_list) // 2
        # print('status(full): {}'.format(status_list))
        # print('status(half): {}'.format(status_list[:half]))

        if status_list == status_list[:half] * 2:
            break
    # print('i = {}, ai = {}'.format(lighting_index, lighting_number))
    
    lighting_number = button_list[lighting_number - 1]
    lighting_index = lighting_number

    # print('Next -> i = {}, a_i = {} '.format(lighting_index, lighting_number))
    
    status_list += str(lighting_index)


if not can_stop:
    step = -1

# print('===========')
# print(status_list)
print(step)

Submission status: Wrong Answer

Solution code:
n=int(input())

botan=[int(input()) for i in range(n)]
count=0
j=0
ans=-1
if botan[0]==2:
    print(1)
else:
    while count<=100010:
        u=botan[j]-1
        count=count+1
        if botan[u]==2:
            ans=count+1
            break
        j=u

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())

botan=[int(input()) for i in range(n)]
count=0
j=0
ans=-1
while count<=100010:
    u=botan[j]-1
    count=count+1
    if botan[u]==2:
        ans=count+1
        break
    j=u

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())

botan=[int(input()) for i in range(n)]
count=0
j=0
ans=-1
while count<=100001:
    u=botan[j]-1
    count=count+1
    if botan[u]==2:
        ans=count+1
        break
    j=u

print(ans)

Submission status: Wrong Answer

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

reached = []
pos = 0#indexで指定
while 1:
    if pos not in reached:
        reached.append(pos)
        if pos == 1:
            break
    else:
        break
    pos = A[pos]-1
#print(reached)

if 1 in reached:
    print(reached.index(1)+1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    x, a, b = LI()
    if b <= a:
        print("delicious")
    elif b <= a + x:
        print("safe")
    else:
        print("dangerous")

    return

#B
def B():
    n = II()
    a = IR(n)
    check = [False] * (n + 1)
    check[1] = True
    now = 1
    tern = 1
    while check[now]:
        check[now] = False
        check[a[now - 1]] = True
        if check[2]:
            print(tern)
            return
        tern += 1
    print(-1)
    return

#C
def C():
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    B()

Submission status: Wrong Answer

Solution code:
a = int(input())
b = [int(input())for i in range(a)]
c = b[0]
d = 1
e = {0: ""}
while True:
    d += 1
    if b[c-1] == 2:
        print(d)
        exit()
    else:
        if (c-1) not in e:
            e.setdefault((c-1), "")
        else:
            print(-1)
            exit()
        c = b[c - 1]

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 1
a = []

for i in range(N):
    n = input()
    a.append(n)
    
next = a[0]    
has_answer = False
for i in range(N):
    if(next == "2"):
        count += 1
        print(count)
        has_answer = True
        break
    else:
        next = a[int(next) - 1]

if(not has_answer):
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 1
a = []

for i in range(N):
    n = input()
    a.append(n)
    
next = a[0]    
has_answer = False
for i in range(N):
    print("next", next, "count", count)
    if(next == "2"):
        count += 1
        print(count)
        has_answer = True
        break
    else:
        next = a[int(next) - 1]

if(not has_answer):
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
cnt = 1
if 2 not in a:
    print(-1)
    exit()
else:
    two_idx = a.index(2)
    a_ = a[0]
    if a_-1 == two_idx:
        cnt += 1
        print(cnt)
        exit()
    while True:
        cnt += 1
        a_ = a[a_-1]
        if a_-1 == two_idx:
            cnt += 1
            break
        if cnt > n:
            print(-1)
            exit()
print(cnt)

Submission status: Wrong Answer

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

next = a[0] - 1
for i in range(1,len(a)):
    if a[next] == 2:
        print(i+1)
        break
    next = a[next]-1
else:
    print("-1")

Submission status: Wrong Answer

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

next = a[0] - 1
for i in range(1,len(a)):
    if a[next] == 2:
        print(i+1)
        break
    next = a[next]-1
else:
    print(-1)

Submission status: Wrong Answer

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

next = a[0]
for i in range(1,len(a)):
    if a[next-1] ==2:
        print(i+1)
        break
    next = a[next-1]
else:
    print(-1)

Submission status: Wrong Answer

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

for i in range(n):
    if a[i]==i+1:
        print(-1)
        
cnt=0
tmp=1
already=[]
while True:
    cnt+=1
    already.append(tmp)
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        break
    elif tmp in already:
        print(-1)
        break

Submission status: Wrong Answer

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

if not(2 in a):
    print(-1)


cnt=0
tmp=1
already=[]
while True:
    cnt+=1
    already.append(tmp)
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        break
    elif tmp in already:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0
light = 1
ans = 0

for i in range(N):
    a = int(input())
    if (i + 1) == light:
        count += 1
        light = a
        if light == 2:
            ans = count

if ans != 0:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
def check_num(N):
    for i in range(N):
        num = int(input())
        if (i + 1 == num) :
            print(-1)
            return False
        a.append(num)
    return True

N = int(input())
a = []
check_result = check_num(N)

def get_ans(a):
    mods = list()
    
    if 2 not in a:
        print(-1)
        return True
    
    count = 0
    mod = 1
    while(True):
        mod = a[mod-1]
        count += 1
        if (mod == 2):
            print(count)
            return True
        elif mod in mods:
            print(-1)
            return True
        else:
            mods.append(mod)

if (check_result):
    ret = get_ans(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

def main():
    count = defaultdict(int)

    count[1] = 1
    v = A[1]
    ans = 1
    while True:
        if count[v] != 0:
            return -1
        count[v] = 1
        v = A[v]
        ans += 1
        if v == 2:
            return ans

print(main())

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

def main():
    count = defaultdict(int)

    count[1] += 1
    v = A[1]
    ans = 1
    while True:
        if count[v] != 0:
            return -1
        count[v] += 1
        v = A[v]
        ans += 1
        if v == 2:
            return ans

print(main())

Submission status: Wrong Answer

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

done = []
cnt = 0
button = 0

while True:
    c = lst[button]
    if c in done:
        print(-1)
        exit()
    else:
        done.append(c)
        cnt += 1
        button = lst[button]-1
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
button=[int(input()) for i in range(n)]
temp=1
ans=1
while True:
  temp=button[temp-1]
  ans+=1
  if button[temp-1]==2:
    print(ans)
    break
  elif ans>=n:
    print(-1)
    break

Submission status: Wrong Answer

Solution code:
n=int(input())
button=[int(input()) for i in range(n)]
temp=1
ans=1
while True:
  temp=button[temp-1]
  ans+=1
  if button[temp-1]==2:
    print(ans)
    break
  elif ans>=n+1:
    print(-1)
    break

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
count = 0
index = 1
end = 2
if end in a:
    print(-1)
else :
    while(True):
        count += 1
        index = a[index]
        if (index == end):
            print(count)
            break
        elif count > N:
            print(-1)
            break

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
a = [int(input()) for i in range(N)]
pos = 0

for i in range(N):
    if a[pos] == 2:
        print(i)
        break
    else:
        pos = a[pos] - 1
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
a = [int(input()) for i in range(N)]
counter, pos, bpos = 0, 0, 0

while True:
    counter += 1
    if a[pos] == 2:
        print(counter)
        break
    else:
        bpos = pos
        pos = a[pos] - 1
        if bpos == a[pos - 1]:
            print(-1)
            break

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
data = []
for _ in range(N):
    data.append((int(input())))

pos = 0
cnt = 0
flag = False 
for _ in range(N):
    cnt += 1
    if data[pos] == 2:
        flag = True
        ans = cnt
    pos = data[pos] - 1
    
if flag:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# 2019/6/25
import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
if not 2 in a:
    print(-1)
    sys.exit()
s = set()
cur = 1
ans = 0
while not cur in s:
    if a[cur-1] == 2:
        print(ans+1)
        sys.exit()
    else:
        s.add(cur)
        cur = a[cur-1]
        s.add(cur)
        ans += 1
print(-1)

Submission status: Wrong Answer

Solution code:
# 2019/6/25
import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
if not 2 in a:
    print(-1)
    sys.exit()
s = set()
s.add(1)
cur = 1
ans = 0
while not cur in s:
    if a[cur-1] == 2:
        print(ans+1)
        sys.exit()
    else:
        cur = a[cur-1]
        s.add(cur)
        ans += 1
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for i in range(n):
    ai=int(input())
    a.append(ai)
finish=False
for i in range(n+10):
    if i==0:
        next=a[a[0]-1]
    else:
        next=a[next-1]
    if next==2:
        print(i+2)
        finish=True
        break
if not finish:
    print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for i in range(n):
    ai=int(input())
    a.append(ai)
finish=False
for i in range(n):
    if i==0:
        next=a[a[0]-1]
    else:
        next=a[next-1]
    if next==2:
        print(i+2)
        finish=True
        break
if not finish:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for _ in range(n):
  a[_]=int(input())
c=1
p=a[0]
if 2 not in a:
  c=-1
else:
  while True:
    p=a[p-1]
    if p==2:
      break
    c+=1
    if p==1:
      c=-1
      break
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
switch_list = [ int(input())-1 for i in range(n)]
move_list = [0] + [None for  i in range(n-1)]
for i in range(n-1):
    move_list[i+1] = switch_list[move_list[i]]
print(move_list)

if move_list.count(1) == 0:
    ans = -1
else:
    ans = move_list.index(1)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
button=[int(input()) for i in range(n)]
check=[]
temp=1
ans=1
while button[temp-1] not in check:
  check.append(temp)
  temp=button[temp-1]
  ans+=1
  if button[temp-1]==2:
    print(ans)
    break
  elif button[temp-1] in check:
    print(-1)
    break

Submission status: Wrong Answer

Solution code:
n=int(input())
button=[int(input()) for i in range(n)]
check=[]
temp=1
ans=0
while button[temp-1] not in check:
  button.append(temp)
  temp=button[temp-1]
  ans+=1
  if button[temp-1]==2:
    print(ans)
    break
  elif button[temp-1] in check:
    print(-1)
    break

Submission status: Wrong Answer

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

liuse = [1]
for i in range(n):
  liuse.append(li[liuse[i]-1])
  if liuse[i+1] == 2:
    print(len(liuse)-1)
    exit()
  if len(liuse) < len(set(liuse)):
    print(-1)
    exit()

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
ans = 1
for _ in range(n) :
	l.append(int(input()))
i = l[0] - 1
while True :
	if l[i] == 2 :
		ans += 1
		break
	i = l[i] - 1
	ans += 1
	if ans == 10**5 :
		print('-1')
		exit()
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
ans = 1
for _ in range(n) :
	l.append(int(input()))
i = l[0] - 1
while True :
	if l[i] == 2 :
		ans += 1
		break
	i = l[i] - 1
	ans += 1
	if ans == 10**9 :
		print('-1')
		exit()
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
ans = 1
for _ in range(n) :
	l.append(int(input()))
i = l[0] - 1
while True :
	if l[i] == 2 :
		ans += 1
		break
	i = l[i] - 1
	ans += 1
	if ans == n :
		print('-1')
		exit()
print(ans)

Submission status: Wrong Answer

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

vi=[0]*n
vi[0]=1
ans=1
st=l[0]
f=0
while vi[1]==0:
    vi[st-1]+=1
    if vi[st-1]>1:
        f=1
        break
    st=l[st-1]
    ans+=1
if f==0:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

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

s = set()
cur = a[0]
ans = 0
num = 0
while not cur in s:
    if num > len(a):
        break
    s.add(cur)
    cur = a[cur-1]
    ans += 1
    if cur == 2:
        print(ans)
        sys.exit()
    num += 1
print(-1)

Submission status: Wrong Answer

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

s = set()
cur = a[0]
ans = 0
num = 0
while not cur in s:
    if num > len(a):
        break
    s.add(cur)
    cur = a[cur-1]
    ans += 1
    if cur == 2:
        print(ans)
    num += 1
print(-1)

Submission status: Wrong Answer

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

ls = []
cur = a[0]
ans = 0
while not cur in ls:
    ls.append(cur)
    cur = a[cur-1]
    ans += 1
    if cur == 2:
        print(ans)
print(-1)

Submission status: Wrong Answer

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

lst = list(map(lambda x: x-1, lst))
lst_p = [0]
tmp = 0
flag =True
for i in range(n+1):
    tmp = lst[tmp]
    if tmp == 1:
        print(i)
        flag = False
        break
if flag:
    print(-1)

Submission status: Wrong Answer

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

light = 1
count = 1
for i in range(n):
  light = li[light - 1]
  count+=1
  if light == 2:
    print(count)
    exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for _ in range(n):
  a[_]=int(input())
c=1
p=a[0]
if 2 not in a:
  c=-1
else:
  while True:
    p=a[p-1]
    c+=1
    if p==2:
      break
    if p==1:
      c=-1
      break
print(c)