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=[-1]*n
for i in range(n):
  a[i]=int(input())-1
  
reached=[False]*n

tar=0
cnt=0
suc=False
while reached[tar]==False:
  reached[tar]=True
  cnt+=1
  tar=a[tar]
  if tar==1:
    suc=True
    break
    
print((-1,cnt)[suc])

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
Data = [0]
Push = [0] * (N+1)
for _ in range(N):
    Data.append(int(input()))

nowp = 1
Push[nowp] = 1

ans = 0
for i in range(N):
    nowp = Data[nowp] 
    ans += 1
    if nowp == 2:
        break
    elif Push[nowp] == 1:
        ans = -1
        break
    else:
        Push[nowp] = 1
print(ans)

Submission status: Accepted

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

ans=1
button=1
#flag[button-1]=True
#button=a[button-1]
for i in range(n):
    if a[button-1]==2:
        break
    if flag[button-1]==False:
        ans+=1
        flag[button-1]=True
        button=a[button-1]
    else:
        ans=-1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
idx = 0
cnt = 0
for i in range(N):
    if A[idx] == 2:
        print(cnt+1)
        exit()
    else:
        cnt += 1
        idx = A[idx]-1
    # print("idx", idx)
print(-1)

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

i = a[0]
count = 1
while True:
  if i == 2:
    print(count)
    exit()
  if count >= N:
    print(-1)
    exit()
  count += 1
  i = a[i-1]

Submission status: Accepted

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

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

Submission status: Accepted

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


def main():
    N = int(input())
    A = []
    for _ in range(N):
        A.append(int(input()))
    x = 1
    n = 0
    while 1:
        x = A[x-1]
        n += 1
        if x == 2:
            print(n)
            return
        if n > N:
            print(-1)
            return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

now = 1
flag = 1
cnt = 0
while cnt<N and next!=2:
    cnt+=1
    next = a_lis[now-1]
    now = next
if next==2:print(cnt)
else:print(-1)

Submission status: Accepted

Solution code:
n=int(input())
alis=[int(input()) for i in range(n)]
counter=0
ans = 1
while counter <n and ans !=2:
	counter +=1
	ans=alis[ans-1]
print(counter if ans ==2 else '-1')

Submission status: Accepted

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

Light=1
for i in range(10**5):
    Light=A[Light-1]
    if Light==2:
        ans=i+1
        break
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
x = 1
total = 0
while type(a[x-1])==int and x != 2:
    k = x-1
    x = a[x-1]
    a[k] = "loop"
    total += 1
else:
    if type(a[x-1])==str:
        print(-1)
    elif x == 2:
        print(total)

Submission status: Accepted

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

    histories = [0] * n

    now_button = 0
    histories[now_button] = 1
    count = 0

    while True:
        now_button = buttons[now_button]-1
        count += 1
        if histories[now_button] == 1:
            ans = -1
            break

        histories[now_button] = 1
        if now_button == 1:
            ans = count
            break

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC065 B - Trained?
N = int(input())
A = [[int(input()),False] for i in range(N)]
idx = 0
A[0][1] == True
cnt = 1

while cnt != N+1:
    if A[idx][1]:
        print(-1)
        exit()
    if A[idx][0] == 2:
        print(cnt)
        exit()
    A[idx][1] = True
    idx = A[idx][0]-1
    cnt += 1

Submission status: Accepted

Solution code:
# ABC065 B - Trained?
N = int(input())
# A = [[int(input()),int(i+1),False] for i in range(N)]
A = [[int(input()),False] for i in range(N)]
idx = 0
A[0][1] == True
cnt = 1

while cnt != 2*N:
    if A[idx][1]:
        print(-1)
        exit()
    if A[idx][0] == 2:
        print(cnt)
        exit()
    A[idx][1] = True
    idx = A[idx][0]-1
    cnt += 1

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 1
i = 0
while ans != 2 and i < n:
	ans = a[ans-1]
	i += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a_s = [int(input()) for _ in range(n)]
c = 1
r = a_s[0]
while True:
  if r == 2:
    print(c)
    break
  r = a_s[r-1]
  c += 1
  if c > n:
    print(-1)
    break

Submission status: Accepted

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

count = 0
next_num = 0
flag = False
# print(a)


while count < n + 10:
    next_num = a[next_num]
    # print("next_num, count", next_num, count)
    if next_num != 2:
        next_num = next_num - 1
        count += 1
    else:
        count += 1
        flag = True
        break

print(count if flag else "-1")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#2019/10/16
N, *a = map(int, open(0).read().split())
#print(N, a)
i=0
cnt=0
button=[]
while(True):
    #print("i=",i, "a[i]=",a[i])
    #print("button=", button)
    if i==1:
        print(cnt)
        break
    if cnt >= N:
        print("-1")
        break
    cnt+=1
    i=a[i]-1

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
next_button = 0
while count <= 10 ** 5:
  count += 1
  next_button = list_a[next_button] - 1
  if next_button == 1:
    print(count)
    exit()
print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
T = [int(input())-1 for _ in range(N)]
visited =[0]*N

def main():
  cnt = 0
  nxt = T[0]
  visited[0]=1
  while nxt:
    cnt+=1
    if nxt == 1:
      print(cnt)
      return
    if visited[nxt]:
      break
    visited[nxt]=1
    nxt = T[nxt]
  print(-1)

if __name__=="__main__":
  main()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(-1)

Submission status: Accepted

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

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

print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

a=[int(i)-1 for i in sys.stdin]
N=a[0]+1
a=a[1:]

done=[False]*N

ans=0
i=0

while True:
  if done[i]==False:
    done[i]=True
    ans+=1
    if a[i]==1:
      break
    i=a[i]

  
  else:
    ans=-1
    break
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for n in range(N):
  a=int(input())
  li.append(a)

s=0
count=0

while s!=1:
  if count==N:
    break
  else:
    s=li[s]-1
    count+=1

if s==1:
  print(count)
else:
  print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
x=0
for i in range(1,n):
    y=a[x]
    if y==2:
        print(i)
        exit()
    else:
        x=y-1
print(-1)

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [int(input().rstrip('\n')) for _ in range(n)]
    cnt = 0
    q = 0
    while True:
        if cnt > n:
            print(-1)
            exit()
        elif q == 1:
            print(cnt)
            exit()
        else:
            q = a[q] - 1
            cnt += 1


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

  s = 1
  for i in range(n):
    if a[s-1] == 2:
      print(i+1)
      exit()
    else:
      s = a[s-1]
  print(-1)
  
if __name__=='__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

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

  s = 1
  for i in range(n):
    if a[s-1] == 2:
      print(i+1)
      exit()
    else:
      s = a[s-1]
  print(-1)
  
if __name__=='__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
def search(arr, num, i):
  nxt = arr[num]
  if(nxt == 1):
    return i
  elif(i >= len(arr)):
    return -1
  else:
    return search(arr, nxt, i+1)

n = int(input())
a = [ int(input()) - 1 for _ in range(n)]
print(search(a, 0, 1))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

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

#デバッグポイントを設置
#import pdb;pdb.set_trace()

a_pass = [False] * N
ans = -1
if (not 2 in a):
  ans = -1
else:
  ans = 0
  next_pos = 0
  crnt_pos = 0
  for i in range(N):
    if a_pass[next_pos]:
      ans = -1
      break
    elif next_pos == (2-1):
      break
    else:
      crnt_pos = next_pos
      next_pos = a[crnt_pos] - 1
      a_pass[crnt_pos] = True
      ans += 1
  else:
    ans = -1
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for n in range(N)]
def t(N,A):
    i=1
    for n in range(1,N+1):
        a=A[i-1]
        if a==2:
            return print(n)
        i=a
    return print(-1)
t(N,A)

Submission status: Accepted

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

count=1

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

print(-1)

Submission status: Accepted

Solution code:
n = int(input())

a_list = []

for _ in range(n):
    a_list.append(int(input()))

i = 1
i_set = {i}

while True:
    i = a_list[i - 1]
    if i == 2:
        print(len(i_set))
        break
    elif i in i_set:
        print(-1)
        break
    else:
        i_set.add(i)

Submission status: Accepted

Solution code:
n = int(input())
light = 1
a = [0]
answer = 0
for _ in range(n):
    a.append(int(input()))
while True:
    light = a[light]
    answer += 1
    if light == 2:
        break
    if light == a[light]:
        answer = -1
        break
    if answer == n:
        answer = -1
        break
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
# ボタンの番号を 0 ~ n-1 番に読み替え
a = [int(input())-1 for _ in range(n)]
button, count = 0, 0

for _ in range(n):
    count += 1
    button = a[button]
    if button == 1:
        break

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

Submission status: Accepted

Solution code:
"""
    N個のボタン 初期条件としてボタン1が1個光ってる
    ボタンiが光ってる時に押すとiが消えてaiが光る
    i=ai なこともある
    光ってないボタンは押せない
    ボタン2が光ってる状態でおわる
    回数を求める、不可なら-1

    2が返る前に重複したら不可ということ
"""
n = int(input())
# ボタンの番号を 0 ~ n-1 番に読み替え
a = [int(input())-1 for _ in range(n)]
button, count = 0, 0
dup_check = {0}

while button != 1:
    len_check = len(dup_check)
    count += 1
    button = a[button]
    dup_check.add(button)
    if len_check == len(dup_check):
        count = -1
        break

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#! usr/bin/env python
# -*- coding: utf-8 -*-


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

    cur = 0
    for i in range(2*N):
        cur = A[cur]

        if cur == 1:
            print(i+1)
            exit()

    print(-1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

count = 0
botton = 1

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

print("-1")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

button = A[0]

for i in range(N):
    ans += 1

    if button == 2:
        print(ans)
        exit()

    else:
        button = A[button-1]
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())
    
visited = [False for i in range(N)]
cur = 0
laps = 0
while True:
    if cur == 1 or visited[cur]:
        break
        
    visited[cur] = True
    next = a[cur] - 1
    laps += 1
    cur = next
    
if cur == 1:
    print(laps)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

s = 1
counter = 1
serched_set = set([1])

while button_dict[s]!=2:
  s = button_dict[s]
  counter += 1
  if s in serched_set:
    counter = -1
    break
  else:
    serched_set.add(s)

print(counter)

Submission status: Accepted

Solution code:
buttonNum = int(input())
button = [0] * buttonNum
for i in range(buttonNum):
  button[i] = int(input())
pushedButton = 1
count = 0
checkAble = False
for i in range(buttonNum):
  count += 1
  if button[pushedButton - 1] == 2:
    checkAble = True
    break
  else:
    pushedButton = button[pushedButton - 1]
if checkAble:
  ans = count
else:
  ans = -1
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, exit
N = int(stdin.readline().rstrip())
A = [int(stdin.readline().rstrip()) for _ in range(N)]
button = 1
for i in range(N):
    if button == 2:
        print(i)
        exit()
    button = A[button-1]
print(-1)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=[]
    for i in range(n):
        a.append(int(input()))
    ans = 1
    tmp = a[0]
    while ans <=n:
        if tmp == 2:
            print(ans)
            exit()
        else:
            tmp = a[tmp-1]
            ans += 1
    print(-1)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N = int(input())
a = [int(input()) for _ in range(N)]
s = 1
for i in range(N):
    if s == 2:
        print(i)
        sys.exit()
    s = a[s-1]
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input())-1 for i in range(n)]
#ボタンを押す=listから値を取得して-1にする
#取得した値=光っているボタンの番号
#光っているボタンの番号が1ならbreak
#取得した値が-1なら2を通らずループしてしまっている
sb = 0
ans = 0
while True:
    if sb == 1:
        flag = True
        break
    elif sb == -1:
        flag = False
        break
    tmp = a[sb]
    a[sb] = -1
    sb = tmp
    ans += 1
if flag:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
li=[int(input()) for i in range(n)]
num=1
for i in range(n):
  if li[num-1]==2:
    print(i+1)
    exit()
  num=li[num-1]
print(-1)

Submission status: Accepted

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

while i <n:
  if a[s-1]==2:
    print(i)
    exit()
  s = a[s-1]
  i +=1

print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

visited = [0]
steps = 0

while steps<N:
  curr_i = visited[-1]
  curr_val = A[curr_i]
  if curr_i == 1:
    print(steps)
    exit()
  else:
    steps+=1
    visited.append(curr_val -1)
print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())

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

        current_b = 1
        cnt = 1
        for _ in range(N):
            if dic[current_b] == 2:
                print(cnt)
                exit()
            else:
                cnt += 1
                current_b = dic[current_b]
        print(-1)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a={x:int(input()) for x in range(1,n+1)}
i=1
cnt=1
while a[i]!=2: 
    i=a[i]
    cnt+=1
    if cnt>n:
        print('-1')
        exit()
print(cnt)

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
 
n = ni()
a = [int(ns()) for i in range(n)]
pushed = {}

next_b = 1
count = 0
for _ in range(0,n):
  if next_b == 2:
    break
  count += 1
  next_b = a[next_b-1]
  if pushed.get(next_b) != None:
    count = -1
    break
  pushed[next_b] = True
  
if count == n:
  count = -1
print(count)

Submission status: Accepted

Solution code:
import os

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

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

print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ls = [0] + [int(input()) for _ in range(n)]
res = [1]
count = 0
while count < n:
    i = ls[res[-1]]
    res.append(i)
    count += 1
    if i == 2:
        print(count)
        exit()
print(-1)

Submission status: Accepted

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

c=1
s=a[0]
while s!=2 and c<n:
    c=c+1
    s=a[s-1]
print(c if c<n else -1)

Submission status: Accepted

Solution code:
import sys
N = int(input())
a = [int(input()) for i in range(N)]
i = 1
s = 1
while i < N:
  if a[s-1] == 2:
    print(i)
    sys.exit()
  s = a[s-1]
  i += 1
  
print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    

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

lt=0
cnt=0

while lt!=1 and cnt<N:
	lt=a[lt]-1
	cnt+=1
	
if lt==1:
	print(cnt)
else:
	print(-1)

Submission status: Accepted

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

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

count = -1 if count == n else count
print(count)

Submission status: Accepted

Solution code:
N = int(input())
a_list = [int(input())-1 for _ in range(N)] # インデックスとボタンの番号を合わせるために-1しておく

a_now = 0
a_pushed = [0]*N

while 1:
    if a_now == 1:
        print(sum(a_pushed)) # ボタンを押した回数を出力
        exit()
    elif a_pushed[a_now] == 1: # 押したことがあるボタンに到達
        print(-1)
        exit()
    else:
        a_pushed[a_now] = 1
        a_now = a_list[a_now]

Submission status: Accepted

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

iindex = 1
count = 0
for n in range(N):
    if iindex == 2:
        print(count)
        break
    elif count >= N - 1:
        print(-1)
    else:
        count += 1
        iindex = a_list[iindex - 1]

Submission status: Accepted

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

ans = 0
current = 0
while g[1] != 1:
    dst = map[current][1]
    if g[dst] == 1:
        ans = -1
        break
    ans += 1
    current = dst
    g[dst] = 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]
for i in range(N):
    a = int(input())
    A.append(a)
#print(A)
ans = 1
now = A[1]
visited = {}
while (not now in visited) and now != 2:
    ans += 1
    visited[now] = 1
    now = A[now]
#print(visited)
if now != 2:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

now = 0
count = 0
while True:
    if now == 1:
        print(count)
        break
    elif count >= n:
        print(-1)
        break
    else:
        count += 1
        now = alist[now]

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# B - Trained?
N = int(input())
a = [int(input()) for k in range(N)]
count = 1
tmp = a[a[0]-1]
while count <= N:
    if a[0] == 2:
        print(count)
        exit()
    elif tmp == 2:
        count += 1
        print(count)
        exit()
    else:
        tmp = a[tmp-1]
        count += 1

print(-1)

Submission status: Accepted

Solution code:
n = int(input())

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

a.insert(0, 0)

i = 1
ans = 0
while True:
    if a[i] == 0:
        print(-1)
        exit()
    elif a[i] == 2:
        print(ans + 1)
        exit()
    a[i], i = 0, a[i]
    ans += 1

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * 100097
for i in range(N):
  A[i + 1] = int(input())
cnt = 0
now = 1
while not(now == 2 or cnt > 100097):
  now = A[now]
  cnt += 1

if cnt > 100001:
  cnt = -1
print(cnt)

Submission status: Accepted

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

M=10**6
b=0
for i in range(M):
  b=a[b]-1
  if b==1:
    break
print(i+1 if b==1 else -1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
a=[-1]*(N+1)
checker = collections.defaultdict(int)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
checker[1] = 1
for _ in range(N):
  if pos == 2:
    print(len(checker.values()) - 1)
    exit(0)
  pos = a[pos]
  if pos not in checker:
    checker[pos] = 1
  else:
    break
  

print(- 1)

Submission status: Accepted

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

print(- 1)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0));c,s=0,1
while s-2and c<n:c+=1;s=a[s-1]
print(c*(c<n)or-1)

Submission status: Accepted

Solution code:
*A,=map(int,open(0));i=1;t=0
while i-2and t<A[0]:t+=1;i=A[i]
print(t*(i==2)or-1)

Submission status: Accepted

Solution code:
*A,=map(int,open(0));i=1;t=[0]*A[0]+[0]
while i-2and t[i]==0:t[i]=1;i=A[i]
print(-1if i-2else sum(t))

Submission status: Accepted

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