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: Accepted

Solution code:
N = int(input())
A = list(int(input()) for i in range(N))
B = A[0]
cnt = 1
a = False
for i in range(N):
    if B == 2:
        a = True
        print(cnt)
        break
    else:
        B = A[B-1]
        cnt += 1
if a == False:
    print(-1)

Submission status: Accepted

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

visited=set([1])
ans=0
i=0
while True:
    x=a[i]
    if x in visited:
        ans = -1
        break
    visited.add(x)
    ans += 1
    if x == 2:
        break
    i=a[i]-1
print(ans)

Submission status: Accepted

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

ans = 0
c = 0
while c != 1 and c != -1:
    c_old = c
    c = a[c]
    a[c_old] = -1
    ans += 1


if c == -1:
    ans = -1

print(ans)

Submission status: Accepted

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

button = 1
for i in range(n):
  button = A[button - 1]
  if button == 2:
    print(i + 1)
    break
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

i=1
cnt=-1

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

print(cnt)

Submission status: Accepted

Solution code:
#インポート
import sys

#入力用
def ILI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def ISI(): return map(int, sys.stdin.readline().rstrip().split())
def II(): return int(sys.stdin.readline().rstrip())
def ISS(): return sys.stdin.readline().rstrip().split()
def IS(): return sys.stdin.readline().rstrip()

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

Submission status: Accepted

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

L = [0] * (n+1)


def b(s):
    #print(s)
    co = 0
    while True:
        if s == 2:
            return co
        elif L[s] == 1:
            return -1
        else:
            L[s] = 1
            s = A[s]
            co += 1

print(b(1))

Submission status: Accepted

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

pushed = set()
cnt = 0
push= 1
while True:
    if push in pushed:
        cnt = -1
        break
    pushed.add(push)
    push = a[push-1]
    cnt += 1
    if push == 2:
        break

print(cnt)

Submission status: Accepted

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)
        break
    elif num == N:
        print(-1)
    num += 1
    x = a[x] - 1

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A =[0] + [int(input()) for _ in range(n)]
q = [1]
cnt = 0
for i in range(1,n+2):
    x = q.pop(0)
    if x ==2:
        print(cnt)
        exit()
    q.append(A[x])
    cnt += 1
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import copy
import  math
import bisect
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
li = [ni() - 1 for _ in range(n)]
limit = 100005
next = li[0]
ct = 1
while True:
    if ct > limit:
        ct = -1
        break
    if next == 1:
        break
    else:
        next = li[next]
        ct += 1
print(ct)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# ABC065b
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

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

count = 0
used = [False]*n


def push(i):
    # print('push'+str(i))
    global count
    count += 1
    if (i == 2):
        print(count)
        exit()
    if (used[i-1]):
        print('-1')
        exit()
    used[i-1] = True
    push(a[i-1])


push(a[0])

Submission status: Accepted

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 if count<n else -1)

Submission status: Accepted

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

n = int(input())
buttons = [int(input()) for n in range(n)]
count,light = 1,buttons[0]
while light != 2 and count<n:
    count,light = count+1,buttons[light-1]
print(count if count<n else -1)

Submission status: Accepted

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

n = int(input())
buttons = [int(input())-1 for n in range(n)]
light = 0
count = 0
while light != 1:
    if buttons[light] == -1:
        print(-1)
        break
    num = light
    light = buttons[num]
    buttons[num] = -1
    count += 1
else:
    print(count)

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(input()) for _ in range(n)]
btns = set()
count = 0
_next = 1
while True:
    count += 1
    btn = arr[_next-1]
    if btn == 2:
        print(count)
        break
    elif btn in btns:
        print(-1)
        break
    else:
        btns.add(btn)
    _next = btn

Submission status: Accepted

Solution code:
#ABC 065 B Trained?

n = int(input())
a = [0]
for i in range(n):
    l = int(input())
    a.append(l)
k = 1
for i in range(n):
    k = a[k]
    if k == 2:
        print(i + 1)
        break
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
i = 0
count = 1
x = [0]*n
while True:
    if a[i] == 2:
        print(count)
        sys.exit()
    if x[i] == 0:
        x[i] = 1
        i = a[i]-1
        count += 1
    else:
        print(-1)
        sys.exit()

Submission status: Accepted

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=1
  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: Accepted

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+1)
    break
else:
  print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

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

count = 0
turn_on = 1
visited = [0 for i in range(N)]
while turn_on != 2:
  if visited[turn_on - 1] == 1:
    print(-1)
    sys.exit()
  visited[turn_on - 1] = 1
  turn_on = a[turn_on - 1]
  count += 1
  
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
n = 1
checked = [False for _ in range(N)]
checked[0] = True
for _ in range(N+1):
    n = A[n-1]
    c += 1
    if n == 2:
        ok = True
        break
    if checked[n-1]:
        ok = False
        break
    checked[n-1] = True
if ok:
    print(c)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())

result = -1
count = 0
unreached = True
reached_list = {}
i = 1
while unreached:
    if reached_list.setdefault(i, 0) == 1:
        unreached = False
    else:
        count += 1
        next_button = a[i-1]
        if next_button == 2:
            result = count
            break
        else:
            reached_list[i] = 1
            i = next_button

print(result)

Submission status: Accepted

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

ans=1
step=0
for i in range(n):
    if l[ans] == 2:
        print(step+1)
        break
    ans = l[ans]
    step+=1
else:
    print('-1')

Submission status: Accepted

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

i = 1
cnt = 0
while i != 2 and cnt <= N:
  cnt += 1
  i = A[i-1]
  if i == 2:
    break

if i == 2:
  print(cnt)
else:
  print(-1)

Submission status: Accepted

Solution code:
# ABC 65 B

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

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

    b = 1
    ans = 0
    while (True):
        tmp = a[b]
        a[b] = 0
        b = tmp
        ans += 1

        if b == 2:
            print(ans)
            return
        elif b == 0:
            print(-1)
            return

    print(-1)
    return

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
i = 0
f = True
for j in range(N):
  A.append(int(input()))
for j in range(N):
  i = A[i]-1
  if i == 1:
    print(j+1)
    f = False
    break
if f:
  print(-1)

Submission status: Accepted

Solution code:
N = int(input().strip())
b = [int(input().strip())-1 for _ in range(N)]
now = 0
ans = 0
for i in range(N):
  now = b[now]
  ans += 1
  if now == 1:
    print(ans)
    exit()
print(-1)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = deque([])
for _ in range(N):
    A.append(int(input())-1)
b = 0
cnt = 0
for i in range(N):
    cnt += 1
    b = A[b]
    if b == 1:
        print(cnt)
        break
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
As = [int(input()) for _ in range(n)]
cnt = 0
current_button = 0
for _ in range(n):
    current_button = As[current_button] - 1
    cnt += 1
    if current_button == 1:
        break
if current_button != 1:
    print(-1)
else:
    print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) 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: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = [int(input()) for _ in range(n)]
t = set()
i = 1
count = 0

while(i not in t):
    count += 1
    t.add(i)
    i = s[i-1]
    if i==2:
        print(count)
        break
else:
    print(-1)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [0]
    for _ in range(N):
        A.append(int(input()))
    i = 1
    for j in range(N):
        i = A[i]
        if i == 2:
            return j + 1
    return -1

if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

d = set()

step = 0
now = 0
flag = False
while True:
    step += 1
    nex = arr[now]
    nex -= 1
    # print(nex)
    if nex == 1:
        flag = True
        break

    if nex not in d:
        d.add(nex)
        now = nex
        continue
    else:
        break

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python

import sys
sys.setrecursionlimit(1000000)

N = 1
a = []

def sol(x, cnt) : 
    cnt+=1
    if a[x-1] == 2 :
        return cnt
    if cnt > N :
        return -1

    return sol(a[x-1], cnt)


if __name__ == '__main__' : 
    N = int(input())

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

    print( sol(1,0) )

Submission status: Accepted

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

def solve():
    pushed = [False] * (n + 1)
    cur = 1
    cnt = 0
    while cur != 2:
        pushed[cur] = True
        cur = b[cur]
        cnt += 1
        if pushed[cur]:
            print(-1)
            return
    print(cnt)

solve()

Submission status: Accepted

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

ind = 1
count = 0
while count < N+1:
    ind = button[ind-1]
    count += 1
    if ind == 2:
        break
print(count) if ind == 2 else print(-1)

Submission status: Accepted

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

for i in range(10**5+10):
    NOW=A[NOW]-1
    ANS+=1

    if NOW==1:
        print(ANS)
        break

else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = {i: int(input()) for i in range(1, N + 1)}
i = 1
cnt = 0
while True:
    cnt += 1
    if a[i] == 2:
        print(cnt)
        break
    if cnt == N:
        print(-1)
        break
    i = a[i]

Submission status: Accepted

Solution code:
n = int(input())
L = [int(input()) for _ in range(n)]
next = 1
for i in range(n):
  if next == 2:
    print(i)
    quit()
  next = L[next-1]
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
L = [int(input()) for _ in range(n)]
count = 0
next = 0
for i in range(len(L)):
  if L[next] == 2:
    print(count+1)
    quit()
  next = L[next]-1
  count += 1
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
L = [int(input()) for _ in range(n)]
next = 1
count = 0
while True:
  if next == 2:
    print(count)
    exit()
  elif count > n:
    print(-1)
    exit()
  else:
    next = L[next-1]
    count += 1

Submission status: Accepted

Solution code:
n = int(input())
B = {i:int(input()) for i in range(1,n+1)}
count = 0
b = 1
while count <= n:
    b = B[b]
    count +=1
    if b == 2:
        break
else:
    count=-1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
nums = int(input())
bottom = list(range(nums))
check = {}
count = 0
a = 0
for i in range(nums):
    bottom[i] = int(input())
    check[i] = 0

while True:

    if bottom[a] == 2:
        count += 1
        break
    if check[a] == 1:
        print("-1")
        sys.exit()
    check[a] = 1
    a = bottom[a] - 1
    count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
A={x: int(input()) for x in range(1,n+1)}

i=0
b=1
while i < n and b != 2:
    b=A[b]
    i+=1

if b == 2:
    print(i)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0]
S = set()
tmp = 1
count = 0
for i in range(N):
    A.append(int(input()))
while True:
    S.add(tmp)
    tmp = A[tmp]
    count += 1
    if tmp == 2:
        print(str(count))
        break
    if tmp in S:
        print('-1')
        break

Submission status: Accepted

Solution code:
*a,=map(int,open(0).read().split());i=1
for t in range(10**5):
	i=a[i]
	if i==2:
		print(t+1)
		exit()
print(-1)

Submission status: Accepted

Solution code:
nums = int(input())
ary = list(range(nums))
result = -1
for i in range(nums):
    ary[i] = int(input())

check = ary[0]
for i in range(nums):
    if check  == 2:
        result = i+1
        break
    check = ary[check-1]
print(result)

Submission status: Accepted

Solution code:
N = int(input())
As = []
for i in range(N):
  A = int(input())
  A = A-1
  As.append(A)
r = 0
c = 0
M = [-1]*N
M[c] = 0
while True:
  n = As[c]
  if not(M[n] < 0):
    break
  r += 1
  M[n] = r
  c = n
#print(M)
if M[1] < 0:
  print(-1)
else:
  print(M[1])

Submission status: Accepted

Solution code:
import sys


def main():
    n = int(input())
    a = [0] * n
    for i in range(n):
        a[i] = int(sys.stdin.readline()) - 1
    sw = [False for _ in range(n)]
    nextsw = 0
    while True:
        if nextsw == 1:
            print(sw.count(True))
            return
        if sw[nextsw]:
            print(-1)
            return
        sw[nextsw] = True
        nextsw = a[nextsw]


if __name__ == '__main__':
    main()

Submission status: Accepted

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
if count != n:
  print(count)
else:
  print(-1)

Submission status: Accepted

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

i=1
cnt=0
while True:
    if i==2:
        print(cnt)
        break
    
    if l[i]==1:
        print(-1)
        break
        
    l[i]=1
    i=A[i-1]
    cnt+=1

Submission status: Accepted

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

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

Submission status: Accepted

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

def check():
    loop=0
    next=0
    while True:
        loop+=1
        next=a[next]
        if next==1:
            print(loop)
            return
        if loop>n:
            print(-1)
            return 

    
check()

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input())-1)
'''
#TLEチーンコ
i=0
j=0
b=[]
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b:
        print(-1)
        break
    else:
        b.append(a[i])
        i=a[i]
'''
i=0
j=0
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif j==n:
        print(-1)
        break
    else:
        i=a[i]

Submission status: Accepted

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

ans = 0
pos = 0
while True:
    ans += 1
    if visited[pos] == 1:
        print(-1)
        exit()
    visited[pos] = 1
    pos = a[pos] - 1
    if pos == 1:
        print(ans)
        exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())

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

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
a = [int(sys.stdin.readline()) for i in range(n)]
a = [-1] + a

def cond(a):
    if 2 not in a:
        return -1

    visited = set()
    current = 1
    count = 0

    while True:
        if current == 2:
            return count
        if current in visited:
            return -1
        count += 1
        visited.update((current,))
        current = a[current]

print(cond(a))

Submission status: Accepted

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

num = a[0]
count = 1

while num != 2 and count < n:
    count += 1
    num = a[num - 1]

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li = []
for i in range(n):
    li.append(int(input())-1)
cnt = 0
# p ... 現在光っているライトの座標
# l ... 次光るライトの座標
p = 0
l = li[0]
i = 0
while i < n:
# l = l[0] にしているので cnt の位置をここにしないと答えより 1 少なくなる
    cnt += 1
    p = l
    if p == 1:
        print(cnt)
        break
    l = li[l]
    i += 1
if p != 1:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
checked = set()
now = 1
while(True):
    if a[now-1] == 2:
        print(len(checked)+1)
        break
    elif a[now-1] in checked:
        print(-1)
        break
    else:
        checked.add(a[now-1])
        now = a[now-1]

Submission status: Accepted

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

Submission status: Accepted

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+1)
    break
else:
  print(-1)

Submission status: Accepted

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

light = 0
count = 0
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: Accepted

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

ans = 0
for i in range(n+1):
    if now == 2:
        break
    else:
        now = a[now]
        ans +=1
print(ans if ans != n+1 else -1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
start = 0
end = 1

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

pushed = {}
l = 0
cnt = 0
while True:
    pushed[l] = True
    l = a[l]
    cnt += 1
    if l in pushed:
        print(-1)
        exit()
    if l == end:
        break
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
list = [int(input()) for i in range(N)]
c = 0
x = 1
while x != 2 and c < N:
    x = list[x - 1]
    c += 1
if x == 2:
    print(c)
else:
    print(-1)

Submission status: Accepted

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
        break
if flag==1:
    print(count)
else:
    print(-1)

Submission status: Accepted

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

print(c if c < n else -1)

Submission status: Accepted

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

i = 0
cnt=0
keiyu_list = [0 for _ in range(N)]

while keiyu_list[i]==0:
  keiyu_list[i]=1
  i = b_list[i]
  cnt+=1
  if i == 1:
    cnt+1
    break
if i==1:
  print(cnt)
else:
  print(-1)

Submission status: Accepted

Solution code:
# Trained?

# input
N = int(input())
a = [int(input()) for _ in range(N)]
shining = a[0]
flag = False
for _ in range(1, N):
    if shining == 2:
        flag = True
        break
    shining = a[shining-1]

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

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=[int(input())-1 for i in range(n)]
used=[0]*n
cur=0
cnt=0
while not used[cur]:
    if cur==1:
        print(cnt)
        sys.exit()
    used[cur]=1
    cnt+=1
    cur=a[cur]

print(-1)

Submission status: Accepted

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)
else:
  print(-1)

Submission status: Accepted

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

b = 0
c = 0
s = {0}
while b != 1:
  c += 1
  b = l[b] - 1
  if b == 1:
    print(c)
    exit()
  if b in s:
    break
  s.add(b)

print(-1)

Submission status: Accepted

Solution code:

n = int(input())
A = [int(input()) for _ in range(n)]
count = 1
next = 0
for i in range(n):
    if A[next] == 2:
        print(count)
        exit()

    next = A[next]-1
    count += 1

print(-1)

Submission status: Accepted

Solution code:
n = int(input())
nlist = [int(input()) for i in range(n)]
c = 0
x = 1
while x != 2 and c < n:
    x = nlist[x - 1]
    c += 1
if x == 2:
    print(c)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())

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

count=0
b=0
while True:
    b=ali[b]-1
    count+=1
    if(b==1):
        print(count)
        break
    elif(count>n):
        print(-1)
        break

Submission status: Accepted

Solution code:
N = int(input())

a = [int(input()) for _ in range(N)]
cnt = 0
b = 0
while True:
  b = a[b] - 1
  cnt += 1
  if b == 1:
    print (cnt)
    break
  if cnt > N: 
    print (-1)
    break

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
count =1 #ボタンを押した回数用 
s=0 #スタートするボタン　これを操作ボタンにしていく
while True:
    if a[s]==2:
        print(count)
        sys.exit()
    elif count>n:
        print(-1)
        sys.exit()
    else:
        count+=1
        s=a[s]-1

Submission status: Accepted

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

Submission status: Accepted

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

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

pushed = [False] * N
now = 0
ret = 0
while True :
    if pushed[now] :
        print(-1)
        break
    if now == 1 :
        print(ret)
        break
    pushed[now] = True
    now = a[now]
    ret += 1