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 = [int(input()) for i in range(n)]

ans = -1
next_a = a[0]

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

Submission status: Accepted

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

ans = 0
i = 0
while n > 0:
    ans += 1
    if a[i] == 2:
        print(ans)
        quit()
    else:
        i = a[i]-1
    n -= 1
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
a = [e for row in a for e in row]
ans = 0
i = 0
while n > 0:
    ans += 1
    if a[i] == 2:
        print(ans)
        quit()
    else:
        i = a[i]-1
    n -= 1
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

now = 1
cnt = 0
for _ in range(N):
    now = A[now - 1]
    cnt += 1
    if now == 2:
        print(cnt)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N = I()
    a = [0] * N

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

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

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    cnt = 0
    idx = 0
    ans = -1
    while cnt < N:
        idx = A[idx]
        cnt += 1
        if idx == 1:
            ans = cnt
            break
    
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) - 1 for _ in range(N)]
seen = [0] * N
seen[0] = 1
i = 0
ans = 1
while a[i] != 1:
    i = a[i]
    if not seen[i]:
        ans += 1
        seen[i] = 1
    else:
        break
else:
    print(ans)
    exit()
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

now = 0
count = 0
while True:
    if count >= N:
        print(-1)
        break

    if now == 1:
        print(count)
        break

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

pushed = [0] * N
pushed[0] = 1

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

for _ in range(N):
    current_bottom = a[current_bottom-1]
    if pushed[current_bottom-1] == 1:
        print(-1)
        exit()
    else:
        pushed[current_bottom-1] = 1
    ans += 1
    if current_bottom == 2:
        print(ans)
        exit()

Submission status: Accepted

Solution code:
import sys

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


switch = 1

c = 0
while True:
    switch = a[switch-1]
    c += 1
    if switch == 2:
        break
    
    if c > 10**6:
        print(-1)
        sys.exit()
        break

print(c)

Submission status: Accepted

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

cnt = 0
pos = 0

while True:
    if pos == 1:
        print(cnt)
        break
    if cnt >= N:
        print(-1)
        break

    pos = A[pos] - 1
    cnt += 1

Submission status: Accepted

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

ans = 0

for i in range(N):
    if i == 0:
        ans += 1
        next_pos = A[i]
        if next_pos == 2:
            break
        else:
            continue
        
    ans += 1
    next_pos = A[next_pos-1]

    if next_pos == 2:
        break
    else:
        continue

if ans < N:
    print(ans)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        sys.exit()
print("-1")

Submission status: Accepted

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

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

print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = -1
pre = 1
for i in range(n):
    pre = a[pre-1]
    if pre == 2:
        cnt = i + 1
        break

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
count={}
a=[]
for i in range(n):
    tmp=int(input())
    a.append(tmp)
button=a[0]
cnt=1

while(True):
    if button==2:
        print(cnt)
        exit()
    else:
        if button in count:
            print(-1)
            exit()
        else:
            count[button]=1
            button=a[button-1]
            cnt+=1

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list()
for _ in range(n):
    a.append(int(input())-1)
g = 0
cnt = 1
while cnt <= n:
    g = a[g]
    if g == 1:
        print(cnt)
        exit()
    cnt += 1
print(-1)

Submission status: Accepted

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

a = A[0]
ans = True if a == 2 else False
cnt = 0
for i in range(N):
    cnt += 1
    if a == 2:
        ans |= True
        break
    a = A[a - 1]

if ans:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

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

shined_button = A[0]
btn_attempt = 1
while True:
    if shined_button == 2:
        print(btn_attempt)
        break
    shined_button = A[shined_button-1]
    btn_attempt +=1

    if btn_attempt > N+1:
        print(-1)
        break

Submission status: Accepted

Solution code:

import math
#import itertools
#import statistics
#import numpy as np
#import collections
n = int(input())
#a, b, c = list(map(int, input().split()))
#a = list(map(int, input().split()))
a = []
check = [0]*n
for i in range(n):
    a.append(int(input()))

find = False
i = 0
check[0]=1
count = 1
while not find:
    if a[i]!=2:
        if check[a[i]-1]==0:
            check[a[i]-1]=1
            count += 1
        else:
            print(-1)
            exit()

        i = a[i]-1   
    else:
        find=True


print(count)

Submission status: Accepted

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

Submission status: Accepted

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

b_set=set()

ans=1
now=0
while True:
    nxt=a[now]-1
    if nxt==1:
        print(ans)
        exit()
    else:
        if nxt in b_set:
            print(-1)
            exit()
        b_set.add(nxt)
        ans+=1
        now=nxt

Submission status: Accepted

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
    l[0]=True
    while(True):
        l[s - 1] = True
        if s == 2:
            print(c)
            return True
        s = a[s - 1]
        if l[s - 1] == True:
            print(-1)
            return False
        c += 1
solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc065/tasks/abc065_b

N = int(input())
a_list = []

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


current = 0
count = 0
for _ in range(N):
    current = a_list[current] - 1
    count += 1

    if current == 1:
        print(count)
        exit()

print(-1)

Submission status: Accepted

Solution code:
import sys
import os

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    B = iln(N)
    b = 1
    for n in range(N+1):
        b = B[b-1]
        if b == 2:
            print(n+1)
            break
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import os

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    B = iln(N)
    t = 1
    cnt = 1
    while True:
        if cnt > N:
            print(-1)
            exit()
        if B[t-1] == 2:
            print(cnt)
            exit()
        cnt += 1
        t = B[t - 1]


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
direction = []
click = [0]*n
click[0] = 1

for i in range(n):
  direction.append(int(input()))
  
index = 0
cnt = 0
while cnt<=100000:
  cnt += 1
  index = direction[index] - 1
  click[index] += 1
  
  if index == 1:
    break
  if click[index] ==2:
    cnt = -1
    break
   
print(cnt)

Submission status: Accepted

Solution code:
import sys

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

flag = False
button = 0
cnt = 0
for i in range(1, n+1):
    if a[button] == 2:
        cnt = i
        flag = True
        break
    else:
        button = a[button] - 1

print(cnt) if flag else print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

used = [0]*N
ans = 0
i = 0
for _ in range(N):
  used[i] = 1
  i = A[i]-1
  ans += 1
  if used[i] == 1:
    ans = -1
    break
  if i == 1:
    break
print(ans)

Submission status: Accepted

Solution code:
import sys

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

l = 1
p = a[0] - 1

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

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

Submission status: Accepted

Solution code:
import sys

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

l = 1
p = a[0] - 1

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

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
now = 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: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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 = 1
    count = 0
    while count <= 10**6:
        s = a[s-1]
        count += 1
        if s == 2:
            print(count)
            quit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a = [int(input()) for _ in range(n)]
nex=[]
k=1
for i in range(len(a)):
    nex.append(a[k-1])
    k=nex[-1]
    if nex[-1]==2:
        print(len(nex))
        break
    elif len(nex)>=n:print(-1)
    else:pass

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
index = a[0] - 1
cnt = 1
flg = False
for i in range(len(a)):
    if index==1:
        print(cnt)
        flg = True
        break
    index = a[index] - 1
    cnt += 1
if flg:
    pass
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for i in range(n)]
ans = 0
light = 1
list = [0] * n

while True:
    if light == 2:
        break
    elif list[light - 1] == 1:
        ans = -1
        break
    else:
        ans = ans + 1
        list[light - 1] = 1
        light = A[light - 1]

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

is_visited = [ False for _ in range(n) ]
is_visited[0] = True

cnt = 0
now = 0
while True:
    is_visited[now] = True
    if now == 1:
        print(cnt)
        break
    nxt = table[now]
    if is_visited[nxt]:
        print(-1)
        break
    now = nxt
    cnt += 1

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt, i = 0, 0
for j in range(10 ** 6):
    cnt += 1
    if a[i] == 2:
        print(cnt)
        #break
        quit()
    else:
        i = a[i] - 1
print(-1)

Submission status: Accepted

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

c = 0
l = 1

for i in a:
    l = a[l-1]
    c += 1
    if l == 2:
        break
if l != 2:
    c = -1
print(c)

Submission status: Accepted

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


stack=[0]
count = 1
while len(stack):
    v = stack.pop()
    for i in root[v]:
        if i == 1:
            print(count)
            exit()
        count += 1
        stack.append(i)
        if count == 1000000:
            print(-1)
            exit()

print(-1)

Submission status: Accepted

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

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

print(-1)

Submission status: Accepted

Solution code:
n = int(input())

btn_list = {i:int(input()) for i in range(1,n+1)}

count = 1
right = btn_list[1]

while True:
    if right == 2:
        print(count)
        break
    elif count == n:
        print(-1)
        break
    else:
        count += 1
        right = btn_list[right]

Submission status: Accepted

Solution code:
n = int(input())

buttons = [0] * 10**5

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

next = 1
i = 0
while i <= 10**5:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()

print(-1)

Submission status: Accepted

Solution code:
n = int(input())

buttons = []

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

next = 1
i = 0
while i <= 10**5:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()

print(-1)

Submission status: Accepted

Solution code:
n = int(input())

buttons = [0] * 10**5

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

next = 1
i = 0
while next != 2:
    i += 1
    next = buttons[next-1]
    if next == 2:
        print(i)
        exit()
    if i >= 10**5:
        print(-1)
        break

Submission status: Accepted

Solution code:
N = int(input())
As = [int(input())-1 for i in range(N)]
seen = [-1] * N
# print(As)
from collections import deque
q = deque()
q.append(0)
seen[0] = 1
cnt = 1
while len(q) > 0:
    a = q.popleft()
    n = As[a]
    # print("pop",n)
    if n == 1:
       print(seen[a])
       exit()
    if seen[n] > -1: continue
    seen[n] = seen[a] + 1
    q.append(n)
print(-1)

Submission status: Accepted

Solution code:
#abc065_b
N = int(input())
A = [int(input()) for i in range(N)]
A.insert(0,0)
flg = [False]*(N+1)
idx = 1
ans = -1
cnt = 0
while not flg[2]:
#    print(A[idx],idx,flg[A[idx]])
    if flg[A[idx]]:
        break
    else:
        flg[A[idx]]=True
        idx = A[idx]
        cnt +=1
if flg[2]:
    ans = cnt
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
List = [int(input())-1 for i in range(N)]
count = 1
a = List[0]
while(a!=1 and count<=N):
    count+=1
    a=List[a]
if count==N+1:
    print(-1)
else:
    print(count)

Submission status: Accepted

Solution code:
import sys

N = int(input())
a = [0]
for _ in range(N):
    a.append(int(input()))
s = 1
for i in range(N):
    tmp = a[s]
    if tmp == 2:
        print(i+1)
        sys.exit()
    else:
        s = tmp
print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import queue
n=int(input())
a=[int(input()) for _ in range(n)]
q=queue.Queue()
q.put(1)
ans=0
for i in range(n):
    b=q.get()
    if b==2:
        print(ans)
        exit()
    q.put(a[b-1])
    ans+=1
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a=[0]
vis=[0]*(n+1)
for _ in range(n):
    x=int(input())
    a.append(x)
ans=0
cur=1
while vis[cur]==0:
    #print(cur,end=' cur\n')
    vis[cur]=1
    if cur==2:
        break
    ans+=1
    cur=a[cur]
if vis[2]==0:
    print('-1')
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

dic = {}
for n in range(1,N+1):
  dic[n] = int(input())

now = 1
count = 0
flag = False
while True:
  if now==2:
    flag = True
    break
  if now not in dic:
    break
  now = dic.pop(now)
  #del dic[now]
  count += 1

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

Submission status: Accepted

Solution code:
#ABC065.B
N = int(input())
a = [int(input()) for i in range(N)]
x = 1
for i in range(N):
    x = a[x-1]
    if x == 2:
        print(i+1)
        break
else:
    print(-1)

Submission status: Accepted

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

i_next = 1

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

l = [False] * n
p = 0
f = False
cnt = 0
while not l[p]:
    cnt += 1
    l[p] = True
    p = a[p] - 1
    if (p == 1):
        f = True
        break

print(cnt if f else -1)

Submission status: Accepted

Solution code:
N=int(input())
#複数行の入力を高速化
#a=[0]*N
#for i in range(N):
#  a[i]=int(input())
import sys
a=[int(i) for i in sys.stdin]

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

Solution code:
N=int(input())


a = []

for i in range(N):
    a.append(int(input()))
# 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: Accepted

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

Submission status: Accepted

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

#loopの定義→同じインデックスを使用したら
#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.add(tmp-1)

Submission status: Accepted

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

now = 0
count = 0
checked = [-1] * N

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

Submission status: Accepted

Solution code:
N = int(input())

buttons = {}
for i in range(N):
    buttons[i+1] = int(input())
    
pushed = []

b = 1
cnt = 0
flag = False
for i in range(N):
    cnt += 1
    next_button = buttons[b]
    if next_button == 2:
        flag = True
        break
    b = next_button
        
if flag:
    print(cnt)
else :
    print(-1)

Submission status: Accepted

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

visited = {0}

G = 1
t = 0
for i in range(N):
    n = A[t]
    if n in visited:
        print(-1)
        break
    elif n == G:
        print(i+1)
        break
    else:
        visited.add(n)
        t = n
else:
    print(-1)

Submission status: Accepted

Solution code:
# Author: cr4zjh0bp
# Created: Sun Mar 22 23:25:40 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
a = nin(n)

i = 0
ans = 0
flag = True
while i != 1:
    if ans > n:
        flag = False
        break
    i = a[i] - 1
    ans += 1

print(ans if flag else -1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N = list(map(int, sys.stdin))
bot = N[1]

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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
        break
    k = a[k-1]
if not p:
    print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
ch=[0]*n
now=1
ans=0
ch[0]=1

while 1:
    now=a[now-1]
    ans+=1
    
    if now==2:
        print(ans)
        exit()
    else:
        if ch[now-1]==1:
            print("-1")
            exit()
        else:
            ch[now-1]=1

Submission status: Accepted

Solution code:
n=int(input())
count=0
A=[]
for i in range(n):
  A.append(input())
temp=1
ans = -1
for i in A:
  temp=A[int(temp)-1]
  count+=1
  if temp=='2':
    ans=count
    break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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