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 = [0] + [int(input()) for i in range(n)]
c = a[1]
p = 1
while c > 2:
    d = a[c]
    a[c] = 0
    c = d
    p += 1

if c == 2:
    print(p)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
p = 1
count = 0
se = set([1])
while p!=2:
  count += 1
  p = A[p-1]
  if p == 2:
    print(count)
  elif p in se:
    print('-1')
    break
  se.add(p)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)

n = int(input())

list1 = [int(input()) for i in range(n)]
unch = [0]*n
def fuck(index,times):
    if list1[index] == 2:
        return times + 1
    else:
        if unch[index] == 1:
            return -1
        else:
            unch[index] = 1
            return fuck(list1[index]-1,times+1)

print(fuck(0,0))

Submission status: Accepted

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

answer = 1
a = A[1]

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

Submission status: Accepted

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

i = 0
cnt = 0
while not i == 1:
    i = a[i] - 1
    cnt += 1
    if i >= n or cnt > 100000:
        print(-1)
        exit()
print(cnt)

Submission status: Accepted

Solution code:
import sys

n, *b = map(int, sys.stdin.read().split())

def main():
    on = 1
    lightened = set([1])
    for i in range(1, n):
        on = b[on-1]
        if on == 2:
            return i
        if on in lightened:
            return -1
        lightened.add(on) 

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
d={}
d[1]=1
r=0
cnt=0
while a[r]!=2:
  if a[r] not in d:
    d[a[r]]=1
    r=a[r]-1
    cnt+=1
  else:
    print(-1)
    exit()
print(cnt+1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

i=0
cnt=0
while i!=1:
  i=a[i]-1
  cnt+=1
  if cnt>N:
    print(-1)
    break
else:
  print(cnt)

Submission status: Accepted

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

for _ in range(n):
    a = int(input())
    al.append(a)

ans = 0
idx = 0
for _ in range(n):
    idx = al[idx] - 1
    ans += 1
    if idx == 1:
        print(ans)
        import sys
        sys.exit()

print(-1)

Submission status: Accepted

Solution code:
# 二回訪れたら終了、それまでに2に行ったらOK
# インデックスと値を入れ替えながらやってみる
n = int(input())
dic = []
for i in range(n):
    dic.append(int(input()))

used = [False for _ in range(10 ** 5 + 1)]
tmp = 1
ans = 0
while True:
    target = dic[tmp - 1]
    if target == 2:
        print(ans + 1)  # 次の一手で決まるので
        exit()
    if used[target]:
        print(-1)
        exit()
    tmp = target
    used[tmp] = True
    ans += 1

Submission status: Accepted

Solution code:
import sys

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

ans = 0

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

print(ans)

Submission status: Accepted

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

i = 1
ans = 0
while i!=2 and i>0:
    i_next = A[i-1]
    A[i-1] = -A[i-1]
    i = i_next
    ans += 1
print(ans if i>0 else -1)

Submission status: Accepted

Solution code:
import sys
def resolve():
        input = sys.stdin.readline
        N=int(input().rstrip())
        a=[input().rstrip() for _ in range(N)]

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



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__":
    #unittest.main()
    resolve()

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=[int(input()) for _ in range(n)]
    button = 1
    for i in range(n):
        next = a[button-1]
        if next == 2:
            return i+1
        else:
            button = next
    return -1
print(main())

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
pushed = [False] * n
cnt = 0
index = 0
while not (pushed[index]):
    cnt += 1
    pushed[index] = True
    if index == 1:
        cnt -= 1
        print(cnt)
        exit()
    index = a[index] - 1
print(-1)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
l=[1]
n=0
for i in range(N):
    l.append(A[l[-1]-1])
    n+=1
    if l[-1]==2:
        print(n)
        exit()
print('-1')

Submission status: Accepted

Solution code:
n = int(input())
a = []
count=1
flag = [True]*n
for i in range(n):
  x = int(input())-1
  a.append(x)
i=0

while a[i]!=1:
  if not flag[i]:
    print(-1)
    exit()
  flag[i]=False
  i=a[i]
  count+=1
  
print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    num=int(input())
    a.append([num-1,0]) #番号を１スタートから０スタートに
ans=-1
now=0
for i in range(n+100):
    now=a[now][0]
    if now==1:
        ans=i+1
        break
    if a[now][1]==1:
        break
    a[now][1]=1
print(ans)

Submission status: Accepted

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

bot = 1
ans = -1

for i in range(n):
    if bot == 2:
        ans = i
        break
    bot = A[bot-1]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(input()) for _ in range(n)]
ans = 0
flg = False
button = set()
x = 1
for _ in range(n):
    ans += 1
    if l[x-1] == 2:
        flg = True
        break
    elif x in button:
        break
    else:
        button.add(x)
        x = l[x-1]
        
    
print(ans if flg else -1)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n=int(input())
    aa=[int(input())-1 for _ in range(n)]
    b=0
    fin=set()
    ans=0
    for _ in range(n):
        if b in fin:
            print(-1)
            exit()
        if b==1:break
        fin.add(b)
        b=aa[b]
        ans+=1
    print(ans)

main()

Submission status: Accepted

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

cur = 1
c = 0
while True:
    c += 1
    cur = a[cur - 1]
    if cur == 2:
        print(c)
        break
    elif c >= 10 ** 5 + 2:
        print(-1)
        break

Submission status: Accepted

Solution code:
import sys
n = int(input())
bottun = [int(input()) for i in range(n)]
i = 0
cnt = 0
while 1:
  if bottun[i] == 0:
    print(-1)
    sys.exit()
  if bottun[i] == 2:
    print(cnt+1)
    sys.exit()
  temp = bottun[i] - 1
  bottun[i] = 0
  i = temp
  cnt += 1

Submission status: Accepted

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
flag=0
ans=-1
C=[0 for i in range(n+1)]

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

Submission status: Accepted

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

index=1#1回目押したら光るボタン
count=0
for i in range(n):
    index=a[index-1]
    count+=1
    if index==2:
        break

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

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
now = 0
for i in range(N):
    now = array[now]
    c += 1
    if now == 1:
        break
if c == N and now != 1:
    print(-1)
else:
    print(c)

Submission status: Accepted

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

current = a[0]
cnt = 0
while current[0] <= n:
    if current[0] == 2:
        cnt += 1
        break
    if not current[1]:
        cnt = -1
        break

    current[1] = False
    current = a[current[0]-1]
    cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,[input() for i in range(n)]))
a=l[0]
m=1
for i in range(n):
    if a==2:
        print(m)
        exit()
    else:
        a=l[a-1]
        m+=1
print(-1)

Submission status: Accepted

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

ls_next = [ls_a[0]]

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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())
a = [int(input()) for i in range(N)]
next_light = 0 
count = 0 
for i in range(N + 1): 
        next_light = a[next_light] - 1 
        count += 1
        if next_light  == 1:
                break
if count > N:
        print(-1)
else:
        print(count)

Submission status: Accepted

Solution code:
import sys

n = int(input())
lst = [int(i) for i in sys.stdin]
i = lst[0]
for c in range(n):
    if i == 2:
        print(c + 1)
        quit()
    i = lst[i - 1]
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

for c in range(n):
    i = lst[i - 1]
    if i == 2:
        print(c + 1)
        quit()
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

now = 1
L = [0]*N
for i in range(N):
    L[i] = now
    now = a[now-1]

judge = 1
for i in range(N):
    if L[i] == 2:
        print(i)
        judge = 0
        break
    else:
        pass

if judge == 1:
    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())

i = a[0]
count = 1

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

if i != 2:
    count = -1

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
L=[0 for i in range(N)]
A=[int(input())-1  for _ in range(N)]
n=0
m=1
while L[n]!=1 and A[n]!=1:
  L[n]=1
  m+=1
  n=A[n]
if L[n]==1:
  print(-1)
else:
  print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip())-1 for _ in range(n)]
now = 0
point = 1
for i in range(n):
    if li[now] == 1:
        print(point)
        exit()
    else:
        now = li[now]
        point += 1

print(-1)

Submission status: Accepted

Solution code:
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)

flag = 0
num = 0
bottun = 0
for _ in range(int(1e6)):
    if light[1] == "1":
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    
if flag:
    print(num)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)

flag = 0
num = 0
bottun = 0
for _ in range(N):
    if light[1] == "1":
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    
if flag:
    print(num)
else:
    print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(-1)

Submission status: Accepted

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

#print(l)

go = 0
ans = -1

for i in range(n):
	#print('----')
	#print(i)
	go = l[go]-1
	#print(go)

	if go == 1:
		if ans < 0:
			ans = i +1

#print('-----')
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
Next = 1
for i in range(N):
  if Next == 2:
    Next = A[Next-1]
    p = i
    break;
  else:
    Next = A[Next-1]
    p = -1
print(p)

Submission status: Accepted

Solution code:
N = int(input())
import sys
input = sys.stdin.readline
a = []
for i in range(1,N+1):
    a.append(int(input()))
ans = 1
check = a[0]
pre_index = 1
while True:
    if N < ans:
        print(-1)
        exit()
    if check == 2:
        print(ans)
        exit()
    if pre_index == a[check-1]:
        print(-1)
        exit()
    pre_index = check
    check = a[check-1]
    ans += 1

Submission status: Accepted

Solution code:
N = int(input())
import sys
input = sys.stdin.readline
a = []
for i in range(1,N+1):
    a.append(int(input()))
ans = 1
check = a[0]
pre_index = 1
while True:
    if 10**5 < ans:
        print(-1)
        exit()
    if check == 2:
        print(ans)
        exit()
    if pre_index == a[check-1]:
        print(-1)
        exit()
    pre_index = check
    check = a[check-1]
    ans += 1

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()

a = [INT() for _ in range(N)]
tmp = [0]*N
tmp2 = a[0]-1
cnt = 0
while 1:
  if tmp[tmp2] == 1:
    print("-1")
    break
  elif tmp2 == 1:
    print(cnt+1)
    break
  tmp[tmp2] = 1
  tmp2 = a[tmp2]-1
  cnt += 1

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
for i in range(N):
  a = int(input())
  dic[i+1] = a
counter = 0
place = 1
while place != 2:
  if dic[place] in dic and dic[place] != place:
    temp = dic[place]
    dic.pop(place)
    place = temp
    counter += 1
  else:
    counter = -1
    break
print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC 065: B – Trained?
n = int(input())
a = [int(input()) for _ in range(n)]
lighting = 1
number = 0

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

Submission status: Accepted

Solution code:
def bfs(n,e,fordfs):
    #点の数、スタートの点、有向グラフ
    W = [-1]*n
    #各点の状態量、最短距離とか,見たかどうかとか
    W[e] = 0
    que = deque()
    que.append(e)
    len = [float("inf")]*n
    len[e] = 0
    while que:
        now = que.popleft()
        nowv = W[now]
        nowlen = len[now]
        for ne in fordfs[now]:
            if nowv == W[ne]:
                #ループが嫌な時
                return -1
            elif W[ne] == -1:
                W[ne] = (nowv+1) % 2
                len[ne] = nowlen+1
                que.append(ne)
    return len


def examB():
    N = I()
    v = [[]for _ in range(N)]
    for i in range(N):
        a = I()
        v[i].append(a-1)
    v[1]=[]
    b = bfs(N,0,v)
    if b==-1:
        ans = -1
    else:
        ans = b[1]
    if ans==float("inf"):
        ans = -1
    print(ans)

import sys
import copy
import bisect
import heapq
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examB()

Submission status: Accepted

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

status = 1
count = 0


def press_button(n):
    status = button_list[n-1]
    return status

while status != 2:
    status = press_button(status)
    count += 1
    if count >= 100000:
        print(-1)
        exit()


print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input())-1 for _ in range(N)]
ans = 0
i = 0
done = set([i])
while 1:    
  if A[i] == 1:
    print(ans+1)
    exit()
  else:
    if A[i] in done:
      print(-1)
      exit()
    else:
      i = A[i]
      done.add(i)
      ans += 1

Submission status: Accepted

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

count=0
flag=False
push_num=1

for i in range(n):
  count+=1
  light_num=l[push_num-1]
  if light_num==2:
    flag=True
    break
  push_num=light_num
if flag:
  print(count)
else:
  print(-1)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(input()) for x in range(n)]
count=1
i=1
ind=1
while i<=n:
    if l[ind-1]==2:
        print(count)
        exit()
    else:
        ind=l[ind-1]
        count+=1
        i+=1    
print(-1)

Submission status: Accepted

Solution code:
def push():
    n = int(input())
    a = [int(input()) for i in range(n)]
    f = [0]*n
    c = 0
    
    p = a[0]
    f[0] = 1
    
    while 1:
        f[p-1] = 1
        c += 1
        if p == 2:
            return c
        p = a[p-1]
        if f[p-1] == 1:
            return -1
print(push())

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
i = 0
j = 0
count = -1
while i < n:
    j = a[j]-1
    i += 1
    if j == 1:
        count = i
        break
print(count)

Submission status: Accepted

Solution code:
def check():
    N = int(input())
    A = [int(input()) for i in range(N)]
    flag = [0]* N
    count = 0
    
    posi = A[0]
    flag[0] = 1

    while 1:
        flag[posi-1] = 1
        count += 1
        if posi == 2:
            return count
        posi = A[posi-1]
        if flag[posi-1] == 1:
            return -1
print(check())

Submission status: Accepted

Solution code:
def check():
    N = int(input())
    A = [int(input()) for i in range(N)]
    flag = [0]* N
    count = 0
    
    posi = A[0]
    flag[0] = 1

    while 1:
        flag[posi-1] = 1
        count += 1
        if posi == 2:
            return count
        posi = A[posi-1]
        if flag[posi-1] == 1:
            return -1
print(check())

Submission status: Accepted

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

x = 1
c = {x}
count = 1
while True:
    if A[x-1] == 2:
        print(count)
        break
    if A[x-1] in c:
        print(-1)
        break
    x = A[x-1]
    c.add(x)
    count += 1

Submission status: Accepted

Solution code:
def q(a, N):
    if 2 not in a:
        return -1
    count = 0
    i = 0
    while count < N:
        m = a[i]
        count += 1
        if m == 2:
            return count
        i = m - 1
    return -1

N = int(input())

a = []

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

print(q(a, N))

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    search_table = [False]*N
    itr = 0
    ret = 0
    while not search_table[itr]:
        search_table[itr] = True
        itr = A[itr]-1
        ret += 1
        if itr == 1: break
    
    print(ret if itr == 1 else -1)
    
solve()

Submission status: Accepted

Solution code:
buttons = int(input())
next_ = []
impossible = []
for i in range(buttons):
    next_.append(int(input()))
now_button = 1
ans = 1
while next_[now_button - 1] != 2:
    if ans <= buttons:
        now_button = next_[now_button - 1]
        ans += 1
        
#    if next_[now_button - 1] not in impossible:
#        impossible.append(next_[now_button - 1])
#        now_button = next_[now_button - 1]
#        ans += 1
    else:
        ans = -1
        break

        
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A = [int(input()) for i in range(N)]
A_next=A[0]
A_list=[1,A_next]
for _ in A:
    A_list.append(A[A_next-1])
    A_next=A[A_next-1]
if not 2 in A_list:
    print(-1)
else:
    print(A_list.index(2))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
A = [ int(input()) for i in range(n) ]
c = 0
temp = 1
s = set()
while temp not in s:
  s.add(temp)
  temp = A[temp-1]
  c += 1
  if temp == 2:
    print(c)
    exit()
print(-1)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpln(n)
cur = 0
for i in range(10**6):
    cur = a[cur] - 1
    if cur == 1:
        print(i+1)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
p = 1
count = 0
se = set([1])
while p!=2:
  count += 1
  p = A[p-1]
  if p == 2:
    print(count)
  elif p in se:
    print('-1')
    break
  se.add(p)

Submission status: Accepted

Solution code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

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

for i in range(n):
    memo = int(input())-1
    if a[i] == -1:
        a[i] = memo

ans = [False for i in range(n)]

trg = 0
cnt = 0
for i in range(n):
    
    memo2 = a[trg]

    if trg == 1:
        print(cnt)
        ans[1] = True
        break

    if not(ans[memo2]):
        ans[memo2] = True
        trg = memo2
        cnt += 1

    
    else:
        break

if not(ans[1]):
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

N, *a = [int(x) for x in open(0)]
a.insert(0, None)

first = 1
pres = first
count = 0
nex =  a[pres] # press the button
count += 1
for i in range(N-1): # operations are at most N-1 times.
    if nex == 2: break
    prev = pres
    pres = nex
    nex = a[pres] # press the button
    count += 1
    if nex == prev or nex == pres: break

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    ai = int(input())
    A.append(ai)
n = A[0]
if n==2:
    print(1)
    exit()
cnt=1
while True:
    N-=1
    n = A[n-1]
    cnt+=1
    if n==2:
        print(cnt)
        exit()
    elif N==0:
        break
    else:
        continue
print(-1)

Submission status: Accepted

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

def solve():
    next_ = list_[1]
    list_[1] = -1
    count = 1
    if next_ == 2:
        return count
    if next_ == -1:
        return -1

    while True:
        prev = next_
        next_ = list_[prev]
        list_[prev] = -1
        count += 1
        if next_ == 2:
            return count
        if next_ == -1:
            return -1

print(solve())