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:
#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):
    cnt += 1
    b = a[b]
    
    if b == 2:
        ans = 1
        break
if ans == 1:
    print(cnt+1)

else:
    print(-1)

Submission status: Wrong Answer

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

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

b = a[1]
cnt = 0
ans = 0
for j in range(n+1):
    b = a[b]
    cnt += 1
    
    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()))

b = a[1]
cnt = 0

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

Submission status: Wrong Answer

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

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

b=a[0]
step = 1

for j in range(n):
    step += 1
    b = a[b]
    
    if b == 1:
        print(step)
        break
    if step == n:
        print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
nxt = 2
cnt = 0
while nxt != 1 and cnt < N:
    try:
        t = a.index(nxt)
        nxt = t + 1
    except:
        print(-1)
        exit()
    cnt += 1
if cnt >= N:
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
button = [int(input()) for _ in range(N)]
ans = 0
number = button[0]
while ans <= N + 5:
    number = button[number - 1]
    ans += 1
    if number == 2:
        print(ans + 1)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
button = [-1] + [int(input()) for _ in range(N)]
if 2 not in button:
    print(-1)
    exit()
ans = 1
number = button[1]
past = []
while ans <= N + 1:
    past.append(number)
    number = button[number]
    ans += 1
    if number == 2:
        print(ans)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
button = [-1] + [int(input()) for _ in range(N)]
if 2 not in button:
    print(-1)
    exit()
ans = 1
number = button[1]
past = []
while ans < N + 1:
    past.append(number)
    number = button[number]
    ans += 1
    if number == 2:
        print(ans)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
button = [-1] + [int(input()) for _ in range(N)]
if 2 not in button:
    print(-1)
    exit()
ans = 1
number = button[1]
past = []
while True:
    past.append(number)
    number = button[number]
    ans += 1
    if number == 2:
        print(ans)
        exit()
    if number in past:
        print(-1)
        exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[]
for i in range(n):
    a=int(input())
    A.append(a)
s,cnt=0,1
while True:
    cnt+=1
    s=A[s]-1
    if A[s]==2:
        print(cnt)
        quit()
        break
    if cnt==n:
        break
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[]
for i in range(n):
    a=int(input())
    A.append(a)
s,cnt=0,1
while True:
    cnt+=1
    s=A[s]-1
    if A[s]==2:
        print(cnt)
        quit()
        break
    elif cnt>n:
        break
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[]
for i in range(n):
    a=int(input())
    A.append(a)
s,cnt=0,1
while True:
    cnt+=1
    s=A[s]-1
    if A[s]==2:
        print(cnt)
        quit()
        break
    elif cnt==n:
        break
print(-1)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    a = [int(input()) for i in range(1, N + 1)]
    # ループしたら終わる
    l = []
    s = a[0]
    l = [0] * N
    c = 1
    while(True):
        l[c - 1] = True
        if s == 2:
            print(c)
            return True
        s = a[s - 1]
        if l[s] == True:
            print(-1)
            return False
        c += 1
solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(int(input()) for _ in range(n))
num = 1
ans = -1

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

Submission status: Wrong Answer

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

ans, nex = 1, a[0]
while ans < N or nex == 2:
	 nex = a[nex - 1]
	 ans += 1
	 
print(ans if ans != N else -1)

Submission status: Wrong Answer

Solution code:
import sys

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

l = 1
p = a[0] - 1

if N == 2:
    if a[0] == 2:
        print(l)
        sys.exit()

while l < int(1e6):
    l += 1
    if a[p] == 2:
        print(l)
        break
    else:
        p = a[p] - 1
else:
    print(-1)

Submission status: Wrong Answer

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

l = 1
p = a[0] - 1
while l < int(1e5):
    l += 1
    if a[p] == 2:
        print(l)
        break
    else:
        p = a[p] - 1
else:
    print(-1)

Submission status: Wrong Answer

Solution code:

n = int(input())
a = [int(input()) for i in range(n)]
now = a[0]
cnt = 0
for i in range(n):
    #print(now)
    now = a[now - 1]
    cnt += 1
    if now == 1:
        
        print(cnt)
        
        exit()
print("-1")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
i = a[0]
if i == 1:
  print(-1)
num = [1, i]
for j in range(n):
    if 2 in num:
        print(len(num) - 1)
        break
    i = a[i-1]
    if i in num:
        print(-1)
        break
    num.append(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
if 2 not in a:
    print(-1)
    quit()
else:
    t = a.index(2)
    s = a[0]
    count = 0
    while count <= n:
        s = a[s-1]
        count += 1
        if s == 2:
            print(count+1)
            quit()
print(-1)

Submission status: Wrong Answer

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

ans=0
t=1
while t!=2 and ans<100000:
	ans=ans+1
	t=a[t-1]
if ans>50000:
	print(-1)
else:
	print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())


a = []

for i in range(N):
    a.append(int(input()) - 1)
# a=[0]*N
# for i in range(N):
#   a[i]=int(input())
count=0

#loopの定義→同じインデックスを使用したらloop判定
#in の処理を爆速で実行するためにsetを使用する
#loop=[]
loop=set()

tmp=1
while True:  
  tmp=a[tmp-1]
  count+=1
  if tmp==2:
    print(count)
    exit()
  if tmp-1 in loop:
    print(-1)
    exit()
  #loop.append(tmp-1)
  loop.add(tmp-1)

Submission status: Wrong Answer

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

#loopの定義→同じインデックスを使用したら
loop=[]
is_loop=False

# tmp=a[0]
# if tmp==2:
#   print(1)
#   exit()
tmp=1
while True:
  loop.append(tmp-1)
  tmp=a[tmp-1]
  count+=1
  if tmp==2:
    print(count)
    exit()
  if tmp in loop:
    print(-1)
    exit()

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
isFound = False
a1 = a[1 - 1]
ai = a[a1 - 1]
for i in range(2, N + 1):
    if (ai == 2):
        print(i)
        isFound = True
        break
    ai = a[ai - 1]
if (isFound is not True):
    print(-1)

Submission status: Wrong Answer

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

k = 1
p = False
for i in range(n):
    if a[k-1] == 2:
        print(i+1)
        p = True
    k = a[k-1]
if not p:
    print(-1)

Submission status: Wrong Answer

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

k = 1
p = False
for i in range(n-1):
    if a[k-1] == 2:
        print(i+1)
        p = True
    k = a[k-1]
if not p:
    print(-1)

Submission status: Wrong Answer

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

now=0
cnt=0
for j in range(N):
  #print(j,now,a[now]-1)
  if a[now]==2:
    print(cnt)
    break
  elif a[now]-1==now:
    print(-1)
    break
  else:
    now=a[now]-1
    cnt += 1
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
import sys
import math
# import decimal
# import queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # queue
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    n = int(input())
    a = []
    for i in range(n):
        a.append(int(input()))
    b = [0] * n
    b[0] = 1
    i = 0
    ans = 0
    while not b[1] and not b[a[i]-1]:
        b[a[i]-1] = 1
        ans += 1
        i = a[i]-1
        if not b[1] and b[a[i]-1]:
            ans = -1
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Wrong Answer

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

ans = -1
tapped = [1]
next_button = button[1]
for i in range(1,n):
    k = next_button
    next_button = button[k]
    if next_button == 2:
        ans = i + 1
        break
print(ans)

Submission status: Wrong Answer

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

ans = -1
tapped = [1]
next_button = button[1]
for i in range(1,n):
    k = next_button
    next_button = button[k]
    if next_button == 2:
        ans = i + 1
        break
    elif next_button not in tapped:
        tapped.append(next_button)
    elif next_button in tapped:
        break

print(ans)

Submission status: Wrong Answer

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

tapped = [1]
next_button = button[1]
for i in range(1,n):
    k = next_button
    next_button = button[k]
    if next_button == 2:
        print(i+1)
        break
    elif next_button not in tapped:
        tapped.append(next_button)
    elif next_button in tapped:
        print(-1)
        break

Submission status: Wrong Answer

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

next_button = button[0]
for i in range(1,n+1):
    k = next_button-1
    next_button = button[k]
    if next_button == 2:
        print(i+1)
        break
    elif next_button not in tapped:
        tapped.append(next_button)
    elif next_button in tapped:
        print(-1)
        break

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    N=int(input())

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

    while True:
        if A[i] not in temp:
            temp[i] = A[i]
            if A[i]==2:
                print(temp.index(A[i]))
                break
            i=A[i]-1

        else:
            print(-1)
            break
resolve()

Submission status: Wrong Answer

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

num = a[0]
cnt = 1

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

print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())

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

print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
used = {1}
pos = 1
count = 0
for _ in range(n):
  pos = A[pos - 1]
  count += 1
  if pos in used:
    print(-1)
    break
  if pos == 2:
    print(count)
    break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
dic = {i+1:int(input()) for i in range(n)}
light = dic[1]
ans = 1

for i in range(n):
    if light == 2:
        break
    elif light == 1:
        ans = -1
        break
    else:
        ans += 1
        light = dic[light]
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
dic = {i+1:int(input()) for i in range(n)}
light = dic[1]
ans = 1

for i in range(n):
    if light == 2:
        break
    
    if light == 1:
        ans = -1
        break
        
    ans += 1
    light = dic[light]
    
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
l,i=[0],0
for _ in range(n):
  i=a[i]-1
  b=a[i]
  if l.count(b)>=1:
    print(-1)
    exit()
  l.append(b)
  if b==2:
    print(len(l))
    exit()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
l,i=[0],0
for _ in range(pow(10,6)):
  i=a[i]-1
  b=a[i]
  if l.count(b)>=1:
    print(-1)
    exit()
  l.append(b)
  if b==2:
    print(len(l))
    exit()

Submission status: Wrong Answer

Solution code:
#ABC065

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

count = 0
i = 1
if 2 in a:
    
    while i != 2:
        i = a[i-1]
        count += 1
        if i == 1:
            print(-1)
            count = 0
            break
        
if count != 0:
    print(count)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
cnt=0
flg=False
light=0

for i in range(N):
    light=A[light]-1
    cnt+=1
    if light==1:
        flg=True
        print(cnt)

if not(flg):
    print(-1)

Submission status: Wrong Answer

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

x = l[0]-1
z = 1

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

if z == 100001:
    print('-1')
else:
    print(z)

Submission status: Wrong Answer

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

x = l[0]-1
z = 1
for i in range(10**5):
    z += 1
    if l[x] == 2:
        break
    else:
        x = l[x]-1

if z == 100001:
    print('-1')
else:
    print(z)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(input()) for _ in range(n)]
m = [int(__) for __ in range(1,n+1)]

x = l[0]
y = 1
for i in range(10**5):
    y += 1
    z = m.index(x)
    if l[z] == 2:
        break
    else:
        x = l[z]

if y == 100001:
    print('-1')
else:
    print(y)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
o = set()
t = 0
c = 0
ans = -1
for i in range(n):
    l.append(int(input())-1)

for i in range(n):
    if l[t] == 1:
        ans = t
        break
    else:
        if t in o:
            break
        else:
            c += 1
            o.add(t)
            t = l[t]

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
o = []
t = 0
c = 0
ans = -1
for i in range(n):
    l.append(int(input())-1)

for i in range(n):
    if l[t] == 1:
        ans = t
        break
    else:
        if t in o:
            break
        else:
            c += 1
            o.append(t)
            t = l[t]

print(ans)

Submission status: Wrong Answer

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

count = 0
now = a[0]

while now != 2 and count < n:
  now = a[now-1]
  count += 1
print(count if count<n else -1)

Submission status: Wrong Answer

Solution code:
n= int(input())

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



cursor = 0
roots = [0] * n
roots[0] = 1
cursor = a[cursor] - 1

for i in range(2,n):
    roots[cursor] = 1
    cursor = a[cursor]-1
    if (cursor == 1):
        print(i)
        break
    elif (roots[cursor] == 1):
        print(-1)
        break

Submission status: Wrong Answer

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

tmp = 0
c = 0

while c != (n + 1):
    tmp = a[tmp-1]

    c += 1
    if tmp == 2:
        print(c+1)
        exit()
        

print(-1)

Submission status: Wrong Answer

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

tmp = a[0]
c = 0

while c != (n + 1):
    tmp = a[tmp-1]
    c += 1
    if tmp == 2:
        print(c+1)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
b=[1]
buf=1
if 2 not in a:
    print(-1)
else:
    for i in range(n):
        a_buf=a[buf-1]
        #print("buf,a[buf-1],b=",buf,a[buf-1],b)
        if a_buf==2:
            #print("check_2")
            print(len(b))
            break
        #elif a_buf in b:
            #print("check_loop")
            #print(-1)
            #break
        b.append(a_buf)
        buf=a_buf

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]
for _ in range(n):
    a.append(int(input()))
v=[1]
cnt=1
while len(v)==len(set(v)):
    v.append(a[v[-1]])
    cnt+=1
    if a[v[-1]]==2:
        print(cnt)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
lis = ["0"]*n
bot = a[0]
lis[0] == "1"
cnt = 1
while True:
    if bot == 2:
        print(cnt)
        break
    elif lis[bot-1] == "1":
        print(-1)
        break
    else:
        cnt += 1
        bot = a[bot-1]
        lis[bot-1] = "1"

Submission status: Wrong Answer

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

r = -1
cnt = 1
next = a[1]
while cnt < n+2:
  if a[next] == 2:
    r = cnt + 1
    break
  else:
    next = a[next]
    cnt += 1
print(r)

Submission status: Wrong Answer

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

r = -1
cnt = 1
next = a[1]
while cnt < n:
  if a[next] == 2:
    r = cnt + 1
    break
  else:
    next = a[next]
    cnt += 1
print(r)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input())-1 for _ in range(N)]
tmp = a[0]
cnt = 0
for _ in range(100001):
    tmp = a[tmp]
    cnt += 1
    if tmp == a[1]:
        print(cnt)
        exit()
print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

light = buttons[0]
for i in range(n):
    print(buttons[light-1])
    if light == 2:
        break
    light = buttons[light-1]
else:
    print(-1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    result = A[0]
    count = 1

    for _ in A:
        result = A[result - 1]
        count += 1
        if result == 2:
            print(count)
            exit(0)
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans=-1
tmp=li[0]
count=1

while(count <= n):
    tmp=li[tmp-1]
    if tmp == 2:
        ans=count+1
        break
    count+=1

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(input()) for _ in range(n))
i= 1
cnt=0

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

print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(input()) for _ in range(n))
i= 1
cnt=0

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(input()) for _ in range(n))
i= 1
cnt=0

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

Submission status: Wrong Answer

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    A=[]
    check=[0]*n
    temp=0
    ct=1
    for i in range(n):
        A.append((INT()-1))
    check[0]=1
    for i in range(n):
        if A[temp]==1:
            print(ct)
            break
        elif check[A[temp]]==1:
            print(-1)
            break
        else:
            temp=A[temp]
            check[A[temp]]=1
            ct+=1
            
if __name__ == '__main__':
    do()

Submission status: Wrong Answer

Solution code:
N = int(input())
list=[]
for i in range(N):
    list.append(int(input()))
count=1
X=list[0]
while X != 2 and count < N:
    X=list[X-1]
    count+=1
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
list=[]
for i in range(N):
    list.append(int(input()))
count=1
X=list[0]
while True:
    X=list[X-1]
    count+=1
    if X == 2:
        print(count)
        break
    if count == N:
        print("-1")
        break

Submission status: Wrong Answer

Solution code:
N = int(input())
list=[]
for i in range(N):
    list.append(int(input()))
count=1
X=list[0]
while True:
    X=list[X-1]
    count+=1
    if X == 2:
        print(count)
        break
    if count == N:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
N = int(input())
list=[]
for i in range(N):
    list.append(int(input()))
count=0
X=list[0]
while True:
    X=list[X-1]
    count+=1
    if X == 2:
        print(count+1)
        break
    if count == 100000:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
#python3
from collections import deque
def main():
    n = int(input())
    a = [int(input())-1 for _ in range(n)]

    Q = deque()
    Q.append(a[0])

    cnt = 1
    flag = False
    for _ in range(n):
        x = Q.popleft()
        cnt += 1
        if a[x] == 1:
            flag = True
            break
        Q.append(a[x])

    if not flag:
        print(-1)
    else:
        print(cnt)
main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

n = 0
count = 0
for i in range(N):
  count += 1
  n = a[n] - 1
  if n == 1:
    print(count)
    break  

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

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

import sys
input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


# import re
# import array
# import copy
# import functools
# import operator

# import math
# import string
# import fractions
# from fractions import Fraction
# from fractions import gcd

# import collections
# import itertools
# import bisect

# import heapq
# from heapq import heappush
# from heapq import heappop
# from heapq import heappushpop
# from heapq import heapify
# from heapq import heapreplace
# from queue import PriorityQueue as pq
from queue import Queue
# from itertools import accumulate

# from collections import deque
# from collections import Counter

# from operator import mul
# from functools import reduce

# def lcm(n,m):
#     return int(n*m/gcd(n,m))

# def reduce(p, q):
#     common = fractions.gcd(p, q)
#     return (p//common , q//common )

# def combinations_count(n, r):
#     r = min(r, n - r)
#     numer = reduce(mul, range(n, n - r, -1), 1)
#     denom = reduce(mul, range(1, r + 1), 1)
#     return numer // denom

# import random
# import time


def main():
    n = int(input().strip())
    a = []
    for i in range(n):
        a.append(int(input().strip()))
    # a = map(int, open(0).read().split())
    a = list(map(lambda x: x - 1, a))
    eprint("a ", end=": ")
    eprint(a)
    adj_l = [[] for i in range(n)]
    for i in range(n):
        adj_l[i].append(a[i])
        # adj_l[a[i]].append(i)
    eprint("adj_l ", end=": ")
    eprint(adj_l)
    start = 0
    cnt = 0
    l_usedlist = [0 for i in range(n)]
    que = Queue()
    que.put(start)
    l_usedlist[0] = 1

    while (que.qsize() > 0):
        v_current = que.get()
        cnt += 1
        for v_next in adj_l[v_current]:
            if v_next == 1:
                print(cnt + 1)
                return
            else:
                if l_usedlist[v_next] == 0:
                    que.put(v_next)
                    l_usedlist[v_next] = 1
    print(-1)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

import sys
input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


# import re
# import array
# import copy
# import functools
# import operator

# import math
# import string
# import fractions
# from fractions import Fraction
# from fractions import gcd

# import collections
# import itertools
# import bisect

# import heapq
# from heapq import heappush
# from heapq import heappop
# from heapq import heappushpop
# from heapq import heapify
# from heapq import heapreplace
# from queue import PriorityQueue as pq
from queue import Queue
# from itertools import accumulate

# from collections import deque
# from collections import Counter

# from operator import mul
# from functools import reduce

# def lcm(n,m):
#     return int(n*m/gcd(n,m))

# def reduce(p, q):
#     common = fractions.gcd(p, q)
#     return (p//common , q//common )

# def combinations_count(n, r):
#     r = min(r, n - r)
#     numer = reduce(mul, range(n, n - r, -1), 1)
#     denom = reduce(mul, range(1, r + 1), 1)
#     return numer // denom

# import random
# import time


def main():
    n = int(input().strip())
    a = []
    for i in range(n):
        a.append(int(input().strip()))
    # a = map(int, open(0).read().split())
    a = list(map(lambda x: x - 1, a))
    eprint("a ", end=": ")
    eprint(a)
    adj_l = [[] for i in range(n)]
    for i in range(n):
        adj_l[i].append(a[i])
        adj_l[a[i]].append(i)
    eprint("adj_l ", end=": ")
    eprint(adj_l)
    start = 0
    cnt = 0
    l_usedlist = [0 for i in range(n)]
    que = Queue()
    que.put(start)
    l_usedlist[0] = 1

    while (que.qsize() > 0):
        v_current = que.get()
        cnt += 1
        for v_next in adj_l[v_current]:
            if v_next == 1:
                print(cnt + 1)
                return
            else:
                if l_usedlist[v_next] == 0:
                    que.put(v_next)
                    l_usedlist[v_next] = 1
    print(-1)
    return


if __name__ == '__main__':
    main()