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:
# import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # queue
# from operator import itemgetter
# from fractions import Fraction

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

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

if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

a.insert(0,0)
ans = 0
i = 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
# 列ベクトルとかの受け取り
lists = [int(input()) for _ in range(n)]

ans = 0
btn = 1
if not 2 in lists:
    ans = -1
else:
    while ans <=n:
        if lists[btn-1]==2:
            ans +=1
            break
        else:
            btn = lists[btn-1]
            ans +=1
            continue
    else:
        ans = -1

print(ans)
# print("{:.10f}".format(ans))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(10000000)

#const
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    N,=pin()
    l=[int(input())-1 for i in range(N)]
    i=0#index
    ans=0#answer
    while(i!=1):
        i=l[i]
        ans+=1
        if ans>N*10:
            print(-1)
            return
    print(ans)
    return
"""         
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """3
3
1
2"""
        output = """2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """4
3
4
1
2"""
        output = """-1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """5
3
3
4
2
4"""
        output = """3"""
        self.assertIO(input, output)

if __name__=="__main__":resolve()

Submission status: Accepted

Solution code:
N=int(input())
a={i+1:int(input()) for i in range(N)}

cnt=1
flag=0
b=a[1]

for i in range(N):
    if b==2:
        flag=1
        break
    else:
        b=a[b]
        cnt+=1
    

print(cnt if flag else -1)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

visited = [0]*n
ans = 0
i = 0
while True:
  i = A[i]
  ans += 1  
  if visited[i]==1:
    ans = -1
    break    
  visited[i] = 1
  if i == 1:
    break
  
print(ans)

Submission status: Accepted

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

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


print(-1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    N = INT()
    Button = [INT() for _ in range(N)]

    count = 0
    isVisited = [False]*N
    GOAL = 1

    cur = 0
    while True:
        next = Button[cur]-1
        count += 1

        if next == GOAL:
            print(count)
            return
        
        if isVisited[next]:
            print(-1)
            return
        isVisited[next] = True
        cur = next
        
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

    A=[int(input()) for i in range(N)]
    num=0
    cnt=0

    for i in range(N):
        if A[num]==2:
            print(cnt+1)
            break
        num=A[num]-1
        cnt+=1

        if i==N-1:
            print(-1)

resolve()

Submission status: Accepted

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

num = 1
cnt = 1

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

print("-1")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, a: "List[int]"):
    k = 0
    for i in range(N + 1):
        if k == 1:
            print(i)
            return
        k = a[k] - 1
    print(-1)


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 = []
for _ in range(n):
    a.append(int(input()))
light_position =1
light_position = a[light_position-1]
count = 1
while True:
    if light_position == 2:
        break
    elif light_position == 1 or count > n:
        count = -1
        break        
    light_position = a[light_position-1]
    count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

    count = 0
    j = 1
    for i in range(n):
        j = l[j-1]
        count += 1
        if j == 2:
            return print(count)

    return print(-1)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

N = int(input())
a = [int(input()) for _ in range(N)]
ans = -1
step = 0
idx = 0
while True:
    if step == N:
        break
    next = a[idx]
    idx = next-1
    step += 1
    if next == 2:
        ans = step
        break
print(ans)

Submission status: Accepted

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

cur = 1
cnt = 0
flag = False
while data[cur] > 0:
    if cur == 2:
        flag = True
        break
    a = data[cur]
    data[cur] = 0
    cnt += 1
    cur = a

print(cnt if flag else -1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
list=[]
for b in range(N):
    b=int(input())
    list.append(b)
number=1
c=list[0]
numbers=[]
numbers.append(c)
if c==2:
    print(number)
else:
    while True:
        number+=1
        d=list[numbers[number-2]-1]
        numbers.append(d)
        if d==2:
            print(number)
            break
        elif number==N+1:
            print(-1)
            break
        else:
            continue

Submission status: Accepted

Solution code:
n=int(input())

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

count=1
right=a[0]
for i in range(100000+1):
    #print(right,"right")
    if right==2:
        print(count)
        break
    else:
        right=a[right-1]
        count+=1

else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n=int(input())
an=[0]*(n+1)
for i in range(n):
  an[i+1]=int(input())

t=1
cnt=0
for _ in range(n*2):
  t=an[t]
  cnt+=1
  if t==2:
    print(cnt)
    sys.exit()
print(-1)

Submission status: Accepted

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

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 = [0]*N
for i in range(N):
  a[i]=int(input())
cur = 1-1
ret = 0
v = set()
while cur != 2-1:
  #print(cur,v)
  if cur in v:
    print(-1)
    exit()
  v.add(cur)
  cur = a[cur]-1
  ret += 1
print(ret)

Submission status: Accepted

Solution code:
n=int(input())
l=[]
cnt=0

for i in range(0,n):
            a=int(input())
            l.append(a)
j=0
for i in range(0,n+1):
            if l[j]!=2:
                        cnt+=1
                        j=int(l[j])-1
                        #print('j',j)
            else:
                         print(cnt+1)
                         exit()
print(-1)
exit()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
#s = input()
#n, k= map(int,input().split())
#da = list(map(int,input().split()))
a = [int(input()) for i in range(n)]

roop = 0
now = 0
while roop < n+1:
    if now == 1:
        print(roop)
        break
    else:
        now = a[now]-1
        roop += 1
else:
    print(-1)

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    N=I()
    a=[0]*N
    for i in range(N):
        a[i]=I()
        
    used=[0]*N
    used[0]=1
    now=0
    cnt=0
    f=0
    for i in range(N):
        cnt+=1
        now=a[now]-1
        if now==1:
            f=1
            break
        if used[now]==1:
            break
        used[now]=1
        
    if f==1:
        print(cnt)
    else:
        print(-1)

main()

Submission status: Accepted

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

ans = -1
nxt = 1
cnt = 0
visited = [0] * 100000
while True:
    if nxt == 2:
        ans = cnt
        break
    nxt = nums[nxt - 1]
    if visited[nxt - 1] == 1:
        break
    visited[nxt - 1] = 1
    cnt += 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=[]
for i in range(N):
    a.append(int(input()))
seen=[False]*N
cnt=0
now=0
while(True):
    if(seen[now]):
        print(-1)
        exit()
    elif(now==1):
        print(cnt)
        exit()
    else:
        seen[now]=True
        cnt+=1
        now=a[now]-1

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0
var = 0

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

Submission status: Accepted

Solution code:
N = int(input())
D = {i+1:int(input()) for i in range(N)}
d = D[1]
ans = 1


for i in range(N):
  if d==2:
    print(ans)
    exit()
    
  d = D[d]
  ans +=1
  
  
print(-1)

Submission status: Accepted

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

hist = set()
next_i = 0
ans = 0
for i in range(N):
    ans += 1
    hist.add(next_i)
    next_i = a[next_i]
    if next_i == 1:
        break
    elif next_i in hist:
        ans = -1
        break

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
D={i+1:int(input()) for i in range(N)}
d=D[1]
ans=1
for i in range(N):
    if d==2:
        print(ans)
        exit()
    d=D[d]
    ans+=1
print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ABC065

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

i = 1
for _ in range(n):
    
    b.append(i)
    i = a[i-1]
    
    
        
if 2 in b:
    print(b.index(2))
else:
    print(-1)

Submission status: Accepted

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

ans = 0
i = 1
while i != 2:
    i = a[i-1]
    #print(i)
    ans += 1
    if ans >= n:
        print(-1)
        sys.exit()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(input()) for i in range(n)]
x = 0
check = [True] * n
ans = 0
flg = False
while (check[x]):
    if x == 1:
        flg = True
        break
    else:
        ans += 1
        check[x] = False
        x = l[x] - 1
if flg:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
#065_B
n=int(input())
a=[None]+[int(input()) for _ in range(n)]
x=1
ans=0
while x!=2 and ans<n:
    x=a[x]
    ans+=1
print(ans if ans<n else -1)

Submission status: Accepted

Solution code:
n = int(input())

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

checker = {}
nxt = 0
count = 0
while(True):
    checker.setdefault(nxt, False)
    if nxt == 2 - 1:
        break
    elif not checker[nxt]:
        checker[nxt] = True
        count += 1
        nxt = al[nxt]
    else:
        count = -1
        break
    
print(count)

Submission status: Accepted

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

x = 1
ans = 1
while A[x-1] != 2:
    x = A[x-1]
    ans += 1
    if ans == 10**5:
        ans = -1
        break
    
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

button_count = int(input().rstrip())

buttons = [int(input().rstrip()) - 1 for i in range(button_count)]

current = 0
flag = [False for i in range(button_count)]
ans = 0
for i in range(button_count):
    if flag[current]:
        ans = -1
        break
    if current == 1:
        break
    flag[current] = True
    current = buttons[current]
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N = int(input())
A = [0] * (N+1)
Flg = True
for i in range(N):
    a = int(input())
    if a == 2:
        Flg = False
    A[i+1] = a

if Flg:
    print(-1)

else:
    ans = -1
    i = 1
    cnt = 0
    while i != 2:
        i = A[i]
        cnt += 1
        if cnt > N:
            print(-1)
            Flg = True
            break
    if Flg == False:
        print(cnt)

Submission status: Accepted

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

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

if not(flg):
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
al = list(int(input()) for _ in range(n))

a = 1
cnt = 0
while True:
    cnt += 1
    a = al[a-1]
    if a == 2:
        print(cnt)
        break
    if cnt == n:
        print(-1)
        break

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

if l[0] == 2:
    print(1)
    exit()

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

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

count = 1
now = a[0]

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

Submission status: Accepted

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

na = a[1]
cnt = 1
pushed[na] = 1
while True:
    if na == 2:
        break
    na = a[na]
    cnt += 1
    if pushed[na] == 1:
        cnt = -1
        break
    pushed[na] = 1
print(cnt)

Submission status: Accepted

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

check = [0 for _ in range(n)]
check[0] = 1
i = a[0]
c = 1
while i != 1 and check[i]==0:
    check[i] = 1
    i = a[i]
    c += 1
    
if i==1:
    print(c)
else:
    print(-1)

Submission status: Accepted

Solution code:
n= int(input())

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



cursor = 0
roots = [0] * n

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
f = [False]*n
for _ in range(n):
    a.append(int(input()))
cnt = 0
i = 0
while(True):
    if f[i]:
        cnt = -1
        break
    if i == 1:
        break
    f[i]=True
    cnt += 1
    i = a[i]-1
print(cnt)

Submission status: Accepted

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

def main():
    N = int(input())
    a_list = list([int(input())-1 for _ in range(N)])
    pushed_a = set([0])
    i = 0
    ans = 0
    while True:
        ans += 1
        next_a = a_list[i]
        if next_a == 1:
            break
        elif next_a in pushed_a:
            ans = -1
            break
        else:
            pushed_a.add(a_list[i])
            i = next_a
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

    btn = 1
    hist = set()
    while True:
        if btn == 2:
            print(len(hist))
            break

        if btn in hist:
            print(-1)
            break

        hist.add(btn)
        btn = a[btn - 1]


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
b = 1
ans = 0
two = False

for i in range(len(a)):
    b = a[b-1]
    ans += 1
    if b == 2:
        two = True
        break

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

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	a = int(input())
	al=[]
	for i in range(a):
		b = int(input())
		al.append(b)
		
	opened_list=set()
	st=0
	opened_list.add(st)
	count=0
	while st != 1:
		st = al[st]-1
		count+=1
		if st in opened_list:
			return -1
		opened_list.add(st)
			
	return count
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
def main():
    buttons = int(input())
    next_lighten_up = [int(input()) for _ in range(buttons)]
    searched = [False] * (buttons + 1)
    answer = 1
    index = 1
    while True:
        if next_lighten_up[index - 1] == 2:
            break
        elif searched[next_lighten_up[index - 1]]:
            answer = -1
            break
        else:
            searched[next_lighten_up[index - 1]] = True
            answer += 1
            index = next_lighten_up[index - 1]
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

cnt = 0
current = 0
s = set()
while True:
    current = a[current]
    if current in s:
        print(-1)
        exit(0)
    s.add(current)
    cnt += 1
    if current == 1:
        print(cnt)
        exit(0)

Submission status: Accepted

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

next = list_a[0]
ans = 1

for i in range(1,N+1) :
    if next == 2 :
        print(ans)
        exit()
    next = list_a[next-1]
    ans += 1

print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
now = 1
ans = -1
for k in range(N):
    now = a[now-1]
    if now ==2:
        ans = k +1
        break
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LMI(): return list(map(int, input().split()))

N = I()
A = [I() for _ in range(N)]

seen = set()
i = 0
light = 1
while light != 2:
    seen.add(light)
    light = A[light - 1]
    if light in seen:
        print(-1)
        exit(0)
    i += 1
print(i)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
x = {0}
now = 0
cnt = 0
while True:
    now = a[now] - 1
    cnt += 1
    if now == 1:
        print(cnt)
        break
    if now in x:
        print(-1)
        break
    x.add(now)

Submission status: Accepted

Solution code:
N = int(input())
next = []
for i in range(N):
    next.append(int(input()))
count = 0
light = 1
for i in range(N):
    light = next[light - 1]
    count += 1
    if light == 2:
        print(count)
        exit()
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

#----------
N = int(input().strip())
A_list = [int(input().strip()) for _ in range(N)]
#----------
# ボタン番号と、リストのIndex番号を合わせるために、要素[0]にダミー値を挿入
A_list.insert(0,0)

pressed_button={}
end_flag=False
index_button=1
count=0
while end_flag == False:
    count += 1
    pressed_button[str(index_button)] = True
    
    # 次のボタンを指定・チェック
    index_button = A_list[index_button]
    if ( str(index_button) in pressed_button ) or (index_button == 2):
        end_flag = True

if index_button == 2:
    print(count)
else:
    print("-1")

Submission status: Accepted

Solution code:
N = int(input())
a_li = [int(input()) for i in range(N)]
ans = 0
now = a_li[0]

while ans <= N :
    ans += 1
    if now == 2 :
        print(ans)
        exit()
    now = a_li[now - 1]
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

tmp = a[0]
c = 0

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

print(-1)

Submission status: Accepted

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

i = 0
button = 0
while i <= n:
  i += 1
  button = li[button]-1
  
  if button == 1:
    break
  
  


if button == 1:
  print(i)
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[0]
for _ in range(n):
    a.append(int(input()))
visited=set([1])
answer=0
x=1
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())
a = [int(input()) for _ in range(n)]
lis = ["0"]*n
bot = a[0]
lis[0] = "1"
cnt = 1

while True:
    if bot == 2:
        print(cnt)
        break
    elif bot != 2 and lis[bot-1] == "1":
        print(-1)
        break
    else:
        cnt += 1
        lis[bot-1] = "1"
        bot = a[bot-1]

Submission status: Accepted

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

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