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:
from collections import defaultdict

def main():
    num = int(input())
    ligh_dic = defaultdict(int)
    for i in range(num):
        a = int(input())
        ligh_dic[i + 1] = a

    visit_flg = [0 for i in range(num + 1)]

    ans = 0
    now_num = 1
    visit_flg[now_num] = 1
    while 1:
        ans += 1
        now_num =ligh_dic[now_num]
        if visit_flg[now_num]:
            ans = -1
            break
        visit_flg[now_num] = 1
        if now_num == 2:
            break

    print(ans)




if __name__ == '__main__':
    main()

Submission status: Accepted

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

tmp = 0
for i in range(N):
  if i ==0:
    tmp = S[0]
  elif tmp == 2:
    status = i
    break
  else:
    tmp = S[tmp - 1]
print(status)

Submission status: Accepted

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

Submission status: Accepted

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

button = 1
count = 0
while(True):
    button = buttons[button - 1]
    count += 1
    if (button == 2):
        print(count)
        break
    elif(count > N):
        print(-1)
        break

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
Ai = list(int(input()) for j in range(int(input())))
chck_nmbrs = []
cnt = 1
lghtd = 1
while cnt <= len(Ai):
    lghtd = Ai[lghtd - 1]
    if lghtd == 2:
        print(cnt)
        exit()
    cnt += 1
print(-1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

visited = [0 for _ in range(N)]

cnt = 0
idx = 0
visited[idx] = 1

while True:
    idx = a[idx] - 1
    cnt += 1
    if visited[idx]:
        cnt = -1
        break
    elif idx == 1:
        break
    
    visited[idx] = 1

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
flag = True
a = [0] * (n+1)
d = [False] * (n+1)
for i in range(n):
    a[i+1] = int(input())
d[1] = True
i = 1
cnt = 0
while(flag):
    i = a[i]
    cnt += 1
    if(d[i] == False):
        d[i] = True
        if(i == 2):
            break
    else:
        flag = False
if(d[2]):
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]
visited = [False]
for i in range(N):
    A.append(int(input()))
    visited.append(False)

idx = 1
ans = 0
while idx != 2 and visited[idx] == False:
    visited[idx] = True
    idx = A[idx]
    ans += 1
if idx == 2:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
b = {}
for i in range(1, N+1):
    b[i] = int(input())

next = 1
cnt = 1
while cnt < N:
    next = b[next]
    if next == 2:
        print(cnt)
        exit()
    cnt += 1
print(-1)

Submission status: Accepted

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

n=int(input())
a={}
for i in range(1,n+1):
    a[i]=a.get(i,int(input()))
on=1
cnt=0
flag=False
for i in range(1,n+1):
    on=a[on]
    cnt+=1
    if on==2:
        flag=True
        break
print(cnt if flag else -1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
nexts = {}
for i in range(N):
    nexts[i+1] = n()

n = 1
seen = set()
seen.add(n)

while n != 2:
    n = nexts[n]
    if n in seen:
        print(-1)
        import sys
        sys.exit()
    seen.add(n)

print(len(seen)-1)

Submission status: Accepted

Solution code:
n=int(input())
al=[int(input()) for _ in range(n)]
ans=1
pd=set([0])
t=1
while True:
    t=al[t-1]
    if t==2:
        print(ans)
        exit()
    if t in pd:
        print(-1)
        exit()
    pd.add(t)
    ans+=1

Submission status: Accepted

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

Submission status: Accepted

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

phai = 1
cnt = 0
loop_hantei = {1}

for _ in range(N):
    phai = A[phai-1]
    cnt += 1
    if phai in loop_hantei:
        print('-1')
        break
    else:
        loop_hantei.add(phai)
    if phai == 2:
        print(cnt)
        break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, a: "List[int]"):
    used = [False] * (N + 1)
    ret = -1
    cnt = 1
    a = [0] + a
    tmp = a[1]
    while True:
        if tmp == 2:
            ret = cnt
            break
        if used[tmp]:
            break
        else:
            used[tmp] = True
            cnt += 1
            tmp = a[tmp]
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: 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+=1
  s=a[s-1]
print(c if c<n else -1)

Submission status: Accepted

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

sta=1
ans=-1
cnt=0
for j in range(n):
    sta=nums[sta-1]
    cnt+=1
    if sta==2:
        ans=cnt
        break
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def main():
    a = [[int(input()), False] for _ in range(N)]
    idx = 0
    ans = 0
    while True:
        idx = a[idx][0] - 1
        ans += 1
        if idx == 1:
            break
        if a[idx][1]:
            ans = -1
            break
        a[idx][1] = True
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

Submission status: Accepted

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

re = [0 for i in range(N)]
re[0] = 1
i = 0
count = 0

while re[a[i]-1] == 0:
    i = a[i] - 1
    count += 1
    re[i] += 1
    if i == 1:
        print(count)
        exit()

print(-1)

Submission status: Accepted

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

liuse = [0]*n
liuse[0] = 1
for i in range(1,n):
  liuse[i] = li[liuse[i-1]-1]
if 2 in liuse:
  print(liuse.index(2))
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

lst = [0] * N

flag = True
i = 0
count = 0
while flag:
    if lst[i] == 1:
        print (-1)
        exit()
    elif a[i] == 2:
        print (count + 1)
        exit()
    else:
        lst[i] = 1
        i = a[i] - 1
        count += 1

Submission status: Accepted

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

count = 1
fp = lista[0]
if fp == 2:
  print(count)
  exit()

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

Submission status: Accepted

Solution code:
import bisect
import os
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import sys
import string

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

sys.setrecursionlimit(2147483647)
INF = float("inf")

# S = sys.stdin.readline().rstrip()
# i = 2
# while len(S) - i > 0:
#     l = len(S) - i
#     if S[:l // 2] == S[l // 2:l]:
#         print(l)
#         break
#     i += 2


N = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for _ in range(N)]

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

Submission status: Accepted

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

tmp = 0

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

Submission status: Accepted

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

n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
check = a[0]
for i in range(1, 10**5):
    if check == 2:
        print(i)
        sys.exit()
    else:
        check = a[check-1]
print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = -1
x = a[0]

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

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())

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

cnt=0
ans=-1
x=1

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

print(ans)

Submission status: Accepted

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

x = 1
visited = set([1])
answer = 0

while True:
  x = A[x]
  if x in visited:
    answer = -1
    break
  visited.add(x)
  answer += 1
  if x == 2:
    break
    
print(answer)

Submission status: Accepted

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

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

Submission status: Accepted

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

light = 1
count = 0

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

can = False
cnt = 0
idx = a[0]-1
while True:
    cnt += 1
    if cnt > N:
        break
    if idx == 1:
        can = True
        break
    idx = a[idx] - 1

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

Submission status: Accepted

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

x = 1
cnt = 0
dic = {x}
while True:
  x = B[x]
  cnt += 1
  if x == 2:
    break
  if {x} <= dic:
    cnt = -1
    break
  dic.add(x)
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

button = 1
for j in range(n):
    k = a[button - 1]
    if k == 2:
       print('{}'.format(j + 1))
       exit(0)
    button = k

print('-1')

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

#一度押したら-1にする
nowon = 0
cnt = 0
while 1:
  nexton = As[nowon]
  As[nowon] = -1
  cnt += 1
  if nexton == 1:
    print(cnt)
    exit()
  if nexton == -1:
    print(-1)
    exit()
  nowon = nexton

Submission status: Accepted

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

i = 1
f = a[0]
while i< 10**6:
    
    if f == 2:
        print(i)
        break
    elif i>n+1:
        print(-1)
        break
    else:
        f = a[f-1]
        i+=1

Submission status: Accepted

Solution code:
n = int(input())
l = [int(input()) for _ in range(n)]
cnt = 0
bu = 1
for _ in range(n):
    cnt +=1
    bu = l[bu-1]
    if bu == 2:
        print(cnt)
        exit()

print(-1)

Submission status: Accepted

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

k = 1
ans = 0
while ans < N+1:
    k = a[k]
    ans += 1
    if k == 2:
        break
if ans > N:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

button_num = 1
push_cnt = 0
while True:
    button_num = a_array[button_num - 1]
    push_cnt += 1
    if button_num == 2:
        print(push_cnt)
        exit()
    if push_cnt == len(a_array):
        print(-1)
        exit()

Submission status: Accepted

Solution code:
import functools


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    a = list(map(lambda x: int(x), inputs))

    count = 0
    light = 1
    N = len(a)
    while 1:
        next_light = a[light - 1]
        count += 1
        if next_light == 2:
            return count
        elif count > N:
            return -1
        light = next_light


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)

Submission status: Accepted

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

index = 1
ret = 1
for i in range(n):
    next = a[index]
    if next == 2:
        break
    if next == -1:
        ret = -1
        break
    ret += 1
    a[index] = -1
    index = next

print(ret)

Submission status: Accepted

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

index = 1
ret = 1
for i in range(n):
    next = a[index]
    if next == 2:
        print(ret)
        exit(0)
    if next == -1:
        break
    ret += 1
    a[index] = -1
    index = next

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())

index = 1
ret = 1
for i in range(n):
    next = a[index]
    if next == 2:
        print(ret)
        break
    if next == -1:
        print(-1)
        break
    ret += 1
    a[index] = -1
    index = next

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(N):
    a=int(input())
    a-=1
    A.append(a)
ans=0
reached=[0]*N
i=0
while(True):
    i=A[i]
    ans+=1
    if i==1:
        print(ans)
        quit()
    if reached[i]:
        print(-1)
        quit()
    reached[i]=1

Submission status: Accepted

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

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

next_num = A[0]
ans = 0


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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

button = []

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

count = 1
tmp = button[0]
while 1:
    if tmp == 2:
        print(count)
        exit()
    if count >= N:
        break
    tmp = button[tmp - 1]
    count += 1

print(-1)

Submission status: Accepted

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

#print(a)
light = 0
counter = 0

while True:
    if light == 1:
        break
    if counter > N:
        counter = -1
        break
    #print(light)
    push = a[light]
    light = push
    counter += 1

print(counter)

Submission status: Accepted

Solution code:
import sys
N = int(input())

a=[]

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

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

print(-1)

Submission status: Accepted

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

tmp = 1

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

print(-1)

Submission status: Accepted

Solution code:
N = int(input())
button = []
for i in range(0, N):
	button.append(int(input())-1)
memo = []
for i in range(0, N):
	memo.append(False)
cnt = 0
now = 0
while not memo[now]:
	memo[now] = True
	if now == 1: break
	now = button[now]
	cnt += 1
if memo[1]: print(cnt)
else: print(-1)

Submission status: Accepted

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

n = int(input())

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

ans = 0
tmp = 0

for i in range(10**5):
    tmp = a[tmp][1] - 1

    if tmp == 1:
        ans += i + 1
        break


print(ans if ans >= 1 else -1)

Submission status: Accepted

Solution code:
a=[int(input()) for _ in range(int(input()))]
ans=1
c=a[0]
if(c==2):
    print(ans)
else:
    while(c!=2):
        ans+=1
        c=a[c-1]
        if(ans>len(a)):
            print(-1)
            exit()
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

idx = 0
cnt = 0
while True:
    cnt += 1
    idx = a[idx] - 1
    if idx+1 == 2:
        break
    
    if cnt > len(a):
        cnt = -1
        break

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

check_li = [1]
s = 1
cnt = 0

while True:
    cnt += 1
    s = li[s-1]
    if cnt >= n:
        print(-1)
        break
    if s == 2:
        print(cnt)
        break

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    count+=1
    next = a[start]
    if(next == 2):
        print(count)
        break
    start = next -1
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:
import sys

def process(a):
    tmp = a[1]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp]
        cnt += 1
    return -1

if __name__ == '__main__':
    # a = [int(input().strip()) for _ in range(int(input().strip()))]
    a = list(map(int, sys.stdin))
    print(process(a))

Submission status: Accepted

Solution code:
#coding: utf-8

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

start = 0
count = 0

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

Submission status: Accepted

Solution code:
def process(a):
    tmp = a[0]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1

if __name__ == '__main__':
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print(process(a))

Submission status: Accepted

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

temp = 1
pushed = set()
clear = False
while temp not in pushed:
    pushed.add(temp)
    temp = a[temp-1]
    if temp == 2:
        clear = True
        break

if clear:   print(len(pushed))
else:   print(-1)

Submission status: Accepted

Solution code:
a = [int(input()) for i in range(int(input()))]
i = a[0]
cnt = 1
while(i != 2 and cnt <= 10**5):
    i = a[i-1]
    cnt += 1
if cnt <= 10**5:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

def search_next(nextbutton : int) -> int:
    nextbutton = a.index(nextbutton) +1
    return nextbutton

n = int(input())
a = [int(input()) for i in range(n)]
nextbutton = 1
count = 0

#print(nextbutton,"first")

for _ in range(n):
    if nextbutton == 2:
        print(count)
        #print(nextbutton,"1")
        
        sys.exit()
    else:
        nextbutton = a[nextbutton -1]
        #print(nextbutton,"el")
        
        count += 1
print(-1)

Submission status: Accepted

Solution code:
n = int(input())

buttons = []

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

index = 1
cnt = 0

for i in range(n):
    index = buttons[index-1]
    cnt += 1

    if index == 2:
        print(cnt)
        exit()

print(-1)

Submission status: Accepted

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

while(i!=2 and cnt<=10**5):
    i=a[i-1]
    cnt+=1

if cnt<=10**5:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
dat_a = [-1] * (n + 1)
for i in range(n):
    a = int(input())
    dat_a[i + 1] = a
cur = 1
f = False
for i in range(n):
    if cur == 2:
        f = True
        break
    cur = dat_a[cur]
if f:
    print(i)
else:
    print("-1")

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input())-1 for _ in range(N)]
B=[]
i=0
cnt=0
while(cnt<=10**5):
    cnt+=1
    i=A[i]
    if i==1:
        print(cnt)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
button_list = [int(input()) for i in range(N)]
button =  1
count = 0

while True:
    button = button_list[button-1]
    count += 1
    if button == 2:
        break
    if count == N:
        print(-1)
        exit()
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

import itertools
from collections import Counter
from collections import defaultdict
import bisect
import math
import heapq

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

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

    print(-1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n = int(input())
a_list = [int(input()) for i in range(n)]
ans = 0
idx = 0

for i in range(n):
    ans += 1
    if a_list[idx] == 2:
        break
    idx = a_list[idx]-1

if ans == n:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
cnt = 0
botton = 1
ans = 'false'
while cnt <= n:
    if botton == 2:
        ans = 'true'
        break
    botton = a[botton-1]
    cnt += 1
if ans == 'false':
    print(-1)
else:
    print(cnt)

Submission status: Accepted

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

def solve():
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input())-1)
    
    is_pushed = [False for i in range(N)] # 既に押されたかどうか

    i = A[0]
    ans = 1
    is_pushed[0] = True
    while True:
        if i == 1:
            print(ans)
            return
        # ボタンを押す
        if is_pushed[i]:
            print(-1)
            return
        is_pushed[i] = True
        i = A[i]
        ans += 1


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

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

Submission status: Accepted

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

data.insert(0, 0)

i = 1
cnt = 0

push_button = {}

while True:
  
  trans_value = data[i]
  i = trans_value
  
  cnt += 1
  
  if trans_value == 2:
    break
  
  elif trans_value in push_button:
    cnt = -1
    break
    
  else:
    pass
  
  push_button[trans_value] = "PUSH"

print(cnt)

Submission status: Accepted

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

data.insert(0, 0)

cnt = 0
i = 1
v = 0

while v != 2:
  v = data[i]
  i = v
  if cnt >= N: 
    cnt = -1
    break
  cnt += 1
  
print(cnt)

Submission status: Accepted

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

match = []
counter = 0
now = 1
for i in range(n):
    if now == 2:
        print(counter)
        exit()

    else:
        now = a[now]
        counter += 1

print("-1")

Submission status: Accepted

Solution code:
import itertools
import math
N = int(input())
di = {}
for i in range(N):
    di[i+1] = int(input())

flag = False
next = 1
for i in range(N):
    if next == 2:
        flag = True
        break
    next = di[next]

    # print(next)
if flag:
    print(i)
else:
    print(-1)

Submission status: Accepted

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

on = 1
cnt = 0
while True:
    if on == 2:
        print(cnt)
        break
    elif on == A[on]:
        print('-1')
        break
    elif cnt >= N-1:
        print('-1')
        break

    on = A[on]
    cnt += 1

Submission status: Accepted

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

on = 1
cnt = 0
for _ in range(N):
    if on == 2:
        print(cnt)
        break

    on = A[on]
    cnt += 1
else:
    print('-1')

Submission status: Accepted

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

l=[]
for i in range(N):
  l.append(int(input()))

# 押してない：0
# 押した：1
l2=[0]*N

# 1番目は「押した：1」にしておく
l2[0]=1

# 回数カウント
c=0

i=1
while True:
  # 回数をインクリメント
  c+=1

  # 次のボタン番号を取得
  i=l[i-1]

  # 2番目のライトが光る場合は回数を出力して終了
  if i==2:
    print(c)
    exit()

  if l2[i-1]==1:
    # 無限ループ確定で終了
    print(-1)
    exit()
  else:
    # 押した／押してない を更新
    l2[i-1]=1