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())
lis = []
for i in range(n):
    lis.append(int(input()))
button = lis[0]
count = 1
if button == 2:
    ans = 1
while True:
    count += 1
    button = lis[button - 1]
    if button == 2:
        ans = count
        break
    if count > n:
        ans = -1
        break
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = []
for i in range(n):
    lis.append(int(input()))
button = lis[0]
count = 1
if button == 2:
    ans = 1
pushed = [button]
while True:
    count += 1
    button = lis[button - 1]
    if button in pushed:
        ans = -1
        break
    pushed.append(button)
    if button == 2:
        ans = count
        break
print(ans)

Submission status: Wrong Answer

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

ans = 0
tmp = 0

for i in range(n):
    ans += 1
    if a[tmp] == 2:
        break;
    tmp = a[tmp] - 1
if ans == 0:
    ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
buttons = [int(input()) - 1 for _ in range(N)]
# N = 2
# buttons = [2, 1, 3, 4, 1]

is_Yes = True

# はじめに1回押す
move_cnt = 1
next_b = buttons[buttons[0]]
while next_b != 1:
    if move_cnt > N:
        is_Yes = False
        break
    # ボタンを押す
    next_b = buttons[next_b]
    move_cnt += 1

if is_Yes:
    print(move_cnt)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
buttons = [int(input()) - 1 for _ in range(N)]
# N = 3
# buttons = [2, 0, 1]
target = buttons[1]

is_Yes = True

move_cnt = 1
next_b = buttons[buttons[0]]
while next_b != target:
    if move_cnt > N:
        is_Yes = False
        break
    next_b = buttons[next_b]
    move_cnt += 1

if is_Yes:
    print(move_cnt)
else:
    print(-1)

Submission status: Wrong Answer

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

now = 0
ans = -1
count = 0

for n in range(N):
  li.append(int(input()))

for n in range(N + 1):
  if n == 0:
    now = li[0]
    count += 1
  
  now = li[now - 1]
  count += 1

  if now == 2:
    ans = count
    break

print(ans)

Submission status: Wrong Answer

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

now = 0
ans = -1
count = 0

for n in range(N):
  li.append(int(input()))

for n in range(N):
  if n == 0:
    now = li[0]
    count += 1
  
  now = li[now - 1]
  count += 1

  if now == 2:
    ans = count
    break

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
memo = set()
memo.add(0)
count = 1
i = a[0] - 1

while True:
  i = a[i] - 1
  count += 1
  if i == 1:
    break
  if i in memo:
    count = -1
    break
  else:
    memo.add(i)
print(count)

Submission status: Wrong Answer

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

# 1: now, -1: visited, 0: none
b[0] = 1
now = 0
count = 0
while (b[1] != 1 and count <= n):
   next = a[now] - 1

   if (b[next] == -1):
      print(-1)
      exit()

   b[now] = -1
   b[next] = 1

   now = next
   count += 1

print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

nex=a[0]
cnt=0
while nex != 2:
    cnt+=1
    pre=nex
    nex=a[nex-1]
    if nex-1==a.index(pre) :
        print(-1)
        break
    if nex==2:
        print(cnt)
        break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
flag=0
ans=-1
C=[0 for i in range(n+1)]
for j in range(n):#0,n-1
    if j==0:#1回目の操作はj=0, Aの1番目を参照したい
        C[j]=A[0]-1
    else:#j=1からn-1
        C[j]=A[C[j-1]]-1
        if C[j]==1:
            ans=j+1
            flag=1
            break
        else:
            continue
        if flag: break
        else: continue
    if flag:break
    else:continue
print(ans)

Submission status: Wrong Answer

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

ls_next = [ls_a[0]]

for i in range(n):
    Num = ls_a[ls_next[i] - 1]
    ls_next.append(Num)
    if Num == 2:
        print(len(ls_next))
        break
    elif i == n - 1:
        print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for _ in range(N):
    a = int(input())
    A.append(a - 1)
Flags = [False] * N
Flags[0] = True
now = 0
cnt = 0
for _ in range(N):
    cnt += 1
    next_node = A[now]
    if next_node == 1:
        break
    if Flags[next_node]:
        print("-1")
        exit()
    now = next_node
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

N = int(input())
a = [0] * N

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

i = a[0]
count = 1

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

if i != 2:
    count = -1

print(count)

Submission status: Wrong Answer

Solution code:
#Trained?
N =  int(input())
a = [int(input()) for _ in range(N)]
judge2 = 0
judgeloop = 0
now = 1
tugi = a[0]
rireki = []
n = 1
 
if tugi == 2:
    judge2 += 1
 
for _ in range(N):
    n += 1
    rireki.append(now)
    now = tugi
    tugi = a[tugi-1]
    if tugi in rireki:
        judgeloop += 1
        break
    elif tugi == 2:
        judge2 += 1
        break
 
if judgeloop == 1:
    print(-1)
elif judge2 == 1:
    print(n)

Submission status: Wrong Answer

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip()) for _ in range(n)]

point = 1
ima = li[0]
for i in range(n):
    if li[ima-1] == 2:
        point += 1
        print(point)
        exit()
    else:
        ima = li[ima-1]
        point += 1
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
    a = int(input())
    A.append(a)
check = 0
count = 0
botton = 1
print(A[1 -1])
for i in range(N):
    count += 1
    if A[botton - 1] == 2:
        check = 1
        break
    botton = A[botton - 1]

if check == 0:
    print(-1)
else:
    print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
numdict = {}
for i in range(n) :
    numdict[i + 1] = int(input())
bottomnumber = 1
bottomlist = []
count = 0
while True :
    bottomlist += [bottomnumber]
    count += 1
    bottomnumber = numdict[bottomnumber]
    if 2 in bottomlist :
        print(count)
        break
    elif bottomnumber in bottomlist :
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for i in range(n):
    a[i]=int(input())
t=a[0]-1
ans=-1
for j in range(200000):
    if a[t]==2:
        ans=j+2
        break
    else:
        t=a[t]-1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

idx = 0
tryed = 0
for _ in range(10):
    idx = li[idx]-1
    tryed += 1
    if idx == 1:
        print(tryed)
        break
else:
    print('-1')

Submission status: Wrong Answer

Solution code:
A = int(input())
grid = []
flag = False
count = 0
a = 0
for i in range(A):
  array = int(input())
  grid.append(array)
for i in range(A):
  a = grid[i]
  count += 1
  if grid[a-1] == 2:
    flag = True
    break
if flag:
  print(count+1)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
A = int(input())
grid = []
flag = False
count = 0
a = 0
for i in range(A):
  array = int(input())
  grid.append(array)
for i in range(A):
  a = grid[i]
  count += 1
  if grid[a-1] == 2:
    flag = True
    break
if flag:
  print(count+1)
else:
  print('-1')

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
    
now = 1
nex = a[now-1]
ans = 0
flg = 0
while a[now-1] != 0:
    a[now-1], nex = 0, a[now-1]
    now = a[nex-1]
    ans += 1
    if nex == 2:
        flg = 1
        ans += 1
        break

if flg == 1:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
    
now = 1
nex = [now-1]
ans = 0
flg = 0
while a[now-1] != 0:
    a[now-1], nex = 0, a[now-1]
    now = a[nex-1]
    ans += 1
    if nex == 2:
        flg = 1
        ans += 1
        break

if flg == 1:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

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

next_ = list_[1]
list_[1] = -1
count = 1

while True:
    prev = next_
    next_ = list_[prev]
    list_[prev] = -1
    count += 1
    if next_ == 2:
        print(count)
        break
    if next_ == -1:
        print(-1)
        break

Submission status: Wrong Answer

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

next_ = list_[1]
list_[1] = -1
count = 1

while True:
    prev = next_
    next_ = list_[prev]
    list_[prev] = -1
    count += 1
    if next_ == 2:
        print(count)
        break
    if next_ == -1:
        print(-1)
        break

Submission status: Wrong Answer

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

next = a[0]
cnt = 1
found = False
pushed_list = [0]
while next-1 not in pushed_list:
    if a[next-1] == 2:
        found = True
        cnt += 1
        break
    else:
        pushed_list.append(next-1)
        next = a[next-1]
        cnt += 1
print(cnt if found else -1)

Submission status: Wrong Answer

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

next = a[0]
cnt = 1
found = False
pushed_list = [a[0]]
while a[next-1] not in pushed_list:
    if a[next-1] == 2:
        found = True
        cnt += 1
        break
    else:
        pushed_list.append(a[next-1])
        next = a[next-1]
        cnt += 1
print(cnt if found else -1)

Submission status: Wrong Answer

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

next = a[0]
cnt = 1
found = False
for _ in range(N):
    if a[next-1] == 2:
        found = True
        cnt += 1
        break
    else:
        next = a[next-1]
        cnt += 1
print(cnt if found else -1)

Submission status: Wrong Answer

Solution code:
# ABC065 B - Trained?
N = int(input())
# A = [[int(input()),int(i+1),False] for i in range(N)]
A = [[int(input()),False] for i in range(N)]
idx = 0
A[0][1] == True
cnt = 1

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

Submission status: Wrong Answer

Solution code:
# ABC065 B - Trained?
N = int(input())
# A = [[int(input()),int(i+1),False] for i in range(N)]
A = [[int(input()),False] for i in range(N)]
print(A)
idx = 0
A[0][1] == True
cnt = 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
for i in range(0, N):
  a.append(int(input()))
  a[i] -= 1;

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

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
for i in range(0, N):
  a = int(input())
  if(a == 2):
    break;
  else:
    ans += 1
if ans == N:
  ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
for i in range(0, N):
  a = int(input())
  if(a == 2):
    break;
  else:
    ans += 1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

current_index = 1
for i in range(N - 1):
    current_index = a[current_index - 1]
    if current_index == 2:
        print(i)
        exit()

print(-1)

Submission status: Wrong Answer

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

for n in range(N):
  a=int(input())
  li.append(a)
print(li)

check=[]
s=0
count=0

while s!=1:
  if s in check:
    break
  else:
    check.append(s)
    s=li[s]-1
    count+=1

if s==1:
  print(count)
else:
  print(-1)

Submission status: Wrong Answer

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

for n in range(N):
  a=int(input())
  li.append(a)

check=[0]
s=0
count=1

while s!=1:
  if s in check:
    break
  else:
    s=li[s]-1
    count+=1
    print(s)
    print(count)
  

if s==2:
  print(count)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
I=[]
for n in range(N):
    a=int(input())
    A.append(a)
I.append(1)
def t(I):
    while 2 not in I:
        a=A[I[-1]-1]
        if a in I:
            return print(-1)
        I.append(a)
        print(I)
    return print(len(I)-1)
t(I)

Submission status: Wrong Answer

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

count=1

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

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cur=0
ok=False
for i in range(n+1):
  cur=a[cur]
  if cur==1:
    break

print((-1,i)[ok])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
# B - Trained?
N = int(input())
a = [int(input()) for k in range(N)]
count = 1
tmp = a[a[0]-1]
while count <= N:
    if tmp == 2:
        count += 1
        print(count)
        exit()
    else:
        tmp = a[tmp-1]
        count += 1

print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.insert(0, 0)

i = 1
ans = 0
while True:
    a[i], i = 0, a[i]
    ans += 1
    # print(a)
    if a[i] == 0:
        print(-1)
        exit()
    elif a[i] == 2:
        print(ans + 1)
        exit()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
b=a[0]
c=1
ans=-1
for i in range(10**5):
    b=a[b-1]
    c+=1
    if b==2:
        ans=c
        break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import collections
N=int(input())
a=[-1]*(N+1)
checker = collections.defaultdict(int)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
prev=[1]  
for _ in range(N):
  if pos == 2:
    print(len(checker.values())-1)
    exit(0)
  pos = a[pos]
  if pos not in checker:
    checker[pos] = 1
  

print(- 1)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[-1]*(N+1)
b=[[] for i in range(N+1)]
for i in range(1,N+1):
  a[i]=int(input())
print(-1)
exit(0)

Submission status: Wrong Answer

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

x = 0
num = 0
while( num <= N):
    if x == 1:
        print(num)
    elif num == N:
        print(-1)
    num += 1
    x = a[x] - 1

Submission status: Wrong Answer

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

x = 0
num = 0
for i in range(N):
    if x == 1:
        print(num)
    elif num == N:
        print(-1)
    num += 1
    x = a[x] - 1

Submission status: Wrong Answer

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

buttons = list(map(int,sys.stdin))
light = buttons[1]
for i in range(buttons[0]):
    light= buttons[light]
    if light==2:
        print(i+2)
        break
else:
    print(-1)

Submission status: Wrong Answer

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

buttons = list(map(int,sys.stdin))
n = buttons[0]
count,light = 1,buttons[1]
for i in buttons[2:]:
    light,count = buttons[light],count+1
    if light==2:
        print(count)
        break
else:
    print(-1)

Submission status: Wrong Answer

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

buttons = list(map(int,sys.stdin))
n = buttons[0]
count,light = 1,buttons[1]
for i in buttons[2:]:
    light,count = buttons[light],count+1
print(count if count<n else -1)

Submission status: Wrong Answer

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

buttons = list(map(int,sys.stdin))
n = buttons[0]
count,light = 1,buttons[1]
while light!=2 and count<n:
    count,light = count+1,buttons[light]
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list()

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

#print(a[0])

count=1
b=1
cond=0

if a[0]==2:
  cond=0
  count=1
else:
 for j in range(N):
   count=count+1
   b=a[b-1]
   #print(b)
   if a[b-1] ==2:
     cond=1
     break

if cond==0:
  print(-1)
else:
  print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list()

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

#print(a[0])

count=1
b=1
cond=0

if a[0]==2:
  count=1
else:
 for j in range(N):
   count=count+1
   b=a[b-1]
   #print(b)
   if a[b-1] ==2:
     cond=1
     break

if cond==0:
  print(-1)
else:
  print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list()

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

#print(a[0])

count=1
b=1
cond=0
for j in range(N):
  count=count+1
  b=a[b-1]
  #print(b)
  if a[b-1] ==2:
    cond=1
    break

if cond==0:
  print(-1)
else:
  print(count)

Submission status: Wrong Answer

Solution code:
l=[int(input()) for _ in range(int(input()))]
x=int(l[0])-1
cnt=1
while l[x]!=2:
  if(l[x]==0 or len(l)==cnt):
    print(-1)
    exit()
  index=l[x]
  l[x]=0
  x=int(index-1)
  cnt+=1
cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
l=[int(input()) for _ in range(int(input()))]
x=int(l[0])-1
cnt=1
while l[x]!=2:
  if(l[x]==0 or len(l)==cnt):
    print(-1)
    exit()
  index=l[x]
  l[x]=0
  x=int(index-1)
  cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
l=[int(input()) for _ in range(int(input()))]
x=int(l[0])-1
cnt=2
while l[x]!=2:
  if(l[x]==0 or len(l)==cnt):
    print(-1)
    exit()
  index=l[x]
  l[x]=0
  x=int(index-1)
  cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
array = []
for i in range(n):
    array.append(int(input()))
next_bottun = array[0]
cnt = 1
found = False
while cnt <= n:
    next_bottun = array[next_bottun-1]
    cnt +=1
    if next_bottun == 2:
        found=True
        break
if found:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
array = []
for i in range(n):
    array.append(int(input()))
next_bottun = array[0]
cnt = 1
notfound = True
while notfound:
    next_bottun = array[next_bottun-1]
    cnt +=1
    if next_bottun == 2:
        notfound=False
if notfound:
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

n = int(input())
array = []
for i in range(n):
    array.append(int(input()))
next_bottun = array[0]
cnt = 1
while cnt < n:
    next_bottun = array[next_bottun-1]
    cnt +=1
    if next_bottun == 2:
        break
if cnt > n:
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ind = 1
count = 0
for i in range(1, N+1):
    if i == ind:
        ind = button[ind-1]
        count += 1
        if ind == 2:
            break

print(count) if ind == 2 else print(-1)

Submission status: Wrong Answer

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

ind = 1
count = 0
for i in range(1, N+1):
    if i == ind:
        ind = button[ind-1]
        count += 1
        if ind == 2:
            break
        elif ind <= i:
            count = -1
            break

print(count)

Submission status: Wrong Answer

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

ind = 1
count = 0
for i in range(1, N+1):
    if i == ind:
        ind = button[ind-1]
        count += 1
        if ind == 2:
            break
        elif ind < i:
            count = -1
            break

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
index = 1
count = 0
while index != 2 and count < n:
  index = a[index-1]
  count += 1
print(count)

Submission status: Wrong Answer

Solution code:
def main():
  n = int(input())
  a = []
  for n in range(n):
    a.append(int(input())-1)
  sw = [0]
  while True:
    nextsw = a[sw[-1]]
    if nextsw == 1:
      print(len(sw)+1)
      return
    if nextsw in sw:
      print(-1)
      return
    sw.append(nextsw)

Submission status: Wrong Answer

Solution code:
def main():
  n = int(input())
  a =[]
  for n in range(n):
    a.append(int(input())-1)
  sw = [0]
  while True:
    nextsw = sw[-1]
    if nextsw == 1:
      print(len(sw))
      return
    if nextsw in sw:
      print(-1)
      return
    sw.append(nextsw)

Submission status: Wrong Answer

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

#TLEチーンコ
i=0
j=0
for i in range(n):
    j+=1
    if a[i]==1:
        print(j)
        break
    elif i==n-1:
        print(-1)
        break
    else:
        i=a[i]

Submission status: Wrong Answer

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

#TLEチーンコ
i=0
j=0
b=[]
for i in range(n):
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b or i==n-1:
        print(-1)
        break
    else:
        b.append(i)
        i=a[i]

Submission status: Wrong Answer

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

num = a[0]
count = 0

if 2 not in a:
    print(-1)
else:
    while num != 2 and count < n:
        b = num
        num = a[num - 1]
        count += 1
        if a[num - 1] == b:
            print(-1)
            sys.exit()
    print(count + 1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
s = a[0]
count = 1
while a[s-1]!=2:
  s = a[s-1]
  if count==10000:
    print(-1)
    break
  else:
    count += 1
else:
  print(count+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
s = a[0]
count = 0
while a[s-1]!=2:
  s = a[s-1]
  if count==1000:
    print(-1)
    break
  else:
    count += 1
else:
  print(count+2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

light = a[0]
count = 1
while count < n:
  #print(light)
  light = a[light]
  count +=1  
  if light == 1:
   # print('b')
    ans = count
    break
else:
  ans = -1

print(ans)

Submission status: Wrong Answer

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

light = a[0]
count = 1
while count <= n+1:
  #print(light)
  light = a[light]
  count +=1  
  if light == 1:
   # print('b')
    ans = count
    break
else:
  ans = -1

print(ans)

Submission status: Wrong Answer

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

light = a[0]
count = 1
while count <= n:
  #print(light)
  light = a[light]
  count +=1  
  if light == 1:
   # print('b')
    ans = count
    break
else:
  ans = -1

print(ans)

Submission status: Wrong Answer

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

light = a[0]
count = 0
while count <= n:
  #print(light)
  light = a[light]
  count +=1  
  if light == 1:
   # print('b')
    ans = count+1
    break
else:
  ans = -1

print(ans)

Submission status: Wrong Answer

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

light = a[0]
count = 0
while count <= n:
  #print(light)
  light = a[light]
  count +=1  
  if light == 1:
   # print('b')
    ans = count+1
    break
else:
  ans = -1

print(ans)

Submission status: Wrong Answer

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

light = a[0]
count = 0
while count < n:
  #print(light)
  light = a[light]
  count +=1  
  if light == 1:
   # print('b')
    ans = count+1
    break
else:
  ans = -1

print(ans)

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(1,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*N):
    count+=1
    if a[turn_on]==2:
        print(count)
        exit()
    turn_on=a[turn_on]-1
print(-1)