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())
bo=[0]
for n in range(N):
    a=int(input())
    bo.append(a)
map=1
count=0
ans=0
for n in range(1,N+1):
    map=bo[map]
    count+=1
    if map==2:
        ans=1
        print(count)
        break
if ans==0:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())

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


def solve(btns, n):

    attempts = 0
    current = 1

    while True:
        if attempts >= n:
            print("-1")
            return
        
        if current == 2:
            print(attempts)
            return

        current = btns[current-1]
        attempts += 1

solve(btns, n)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
hist = [0] * N
bottun = a[0]
cnt = 1
hist[0] = 1

while True:
    # print(hist)
    # print(bottun)
    if hist[bottun-1] == 1:
        print(-1)
        exit()
    
    if bottun == 2:
        print(cnt)
        exit()
    
    hist[bottun-1] = 1
    bottun = a[bottun-1]
    cnt += 1

Submission status: Accepted

Solution code:
N = int(input())

la = list()
for i in range(N):
    la.append(int(input()))

lnum = [0]*N

i = 1
cnt = 0
while lnum[i-1] == 0 and i != 2:
    lnum[i-1] += 1
    i = la[i-1]
    cnt += 1

if i == 2:
    print(cnt)

else:
    print("-1")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
buttons = [int(input()) for _ in range(N)]
counter = 1
now_button = buttons[0]
for _ in range(N):
    if now_button == 2:
        print(counter)
        break
    counter += 1
    now_button = buttons[now_button - 1]
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

# In[28]:


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


# In[30]:


# def func(x, a, history, cnt):
#     print(x, a[x-1], history)
#     if a[x-1] == 2:
#         print(cnt)
#     elif a[x-1] in history:
#         print(-1)
#     else:
#         history.append(a[x-1])
#         cnt += 1
#         func(a[x-1], a, history, cnt)
# func(1, a, [], 1)


# In[ ]:


n = int(input())
a = [int(input()) for _ in range(n)]
L = [0] * n
cnt = 0
i = 0
while True:
    if L[i] == 1:
        print(-1)
        break
    else:
        L[i] = 1
        i = a[i] - 1
        cnt += 1
        if i == 1:
            print(cnt)
            break

Submission status: Accepted

Solution code:
import sys
N = int(input())
a = [int(input()) for i in range(N)]
d = [0] * N
b = 0
c = 0
while True:
    if b == 1:
        print(c)
        sys.exit()
    c += 1
    b = a[b] - 1
    if d[b] == 1:
        print('-1')
        sys.exit()
    d[b] = 1

Submission status: Accepted

Solution code:
n = int(input())
a_list = []
for i in range(n):
    a_list.append(int(input()))
next = a_list[0]
log = [0]*n
log[0] = 1
count = 1
for i in range(n):
    if next == 2:
        print(count)
        break
    if log[next-1] == 1:
        print(-1)
        break
    else:
        log[next-1] = 1
    next = a_list[next-1]
    count += 1

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
pos = 1
ans = 0
while pos != 2:
  pos = A[pos-1]
  ans += 1
  if ans == 100000:
    ans = -1
    break
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


N = int(input().rstrip())
a_dict = {}
for i in range(N):
  a_dict[i+1] = int(input().rstrip())

target = 1
next_target = a_dict[target]
cont = True
ans = 0

if next_target == 2:
  cont = False
  ans += 1

while cont:
  target = next_target
  next_target = a_dict[target]
  ans += 1
  del a_dict[target]
  if next_target not in a_dict:
    ans = -1
    cont = False
  elif next_target == 2:
    ans += 1
    cont = False
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

memo = defaultdict(int)

cnt = 1
current = 1
memo[current] += 1

while True:
    after = a[current-1]
    if after==2:
        print(cnt)
        break
    elif memo[after]==1:
        print(-1)
        break
    else:
        memo[after] += 1
        current = after
        cnt += 1

Submission status: Accepted

Solution code:
# ABC065
# B
# Trained?

n = int(input())
a_list = [None]*n

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

ans = 0
done_list = [0]*n
flag = False

now = 1
done_list[0] = 1

while True:
    now = a_list[now-1]
    ans += 1
    if now == 2:
        flag = True
        break
    elif done_list[now-1] == 1:
        break
    done_list[now-1] = 1
    
if flag:
    print(ans)
else:
    print("-1")

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*n
for i in range(n):
  a[i] = int(input())
routed = {1}
steps = 0
for i in range(n):
  if i == 0:
    posn = 1
  elif posn == 2:
    break
  elif a[posn-1] in routed:
    steps = -1
    break
  posn = a[posn-1]
  steps += 1
  routed.add(posn)
print(steps)

Submission status: Accepted

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

#押したボタンリスト
d=[0]*N
d[0]=1
ans=0
light_sw = 0
while 1:
  light_sw = a[light_sw]
  if d[light_sw]==0:
    d[light_sw]=1
  else:
    print(-1)
    exit()
  ans+=1
  if light_sw == 1:
    print(ans)
    exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().strip())
a_lst = [-1]
for _ in range(n):
  a_lst.append(int(stdin.readline().strip()))

if 2 not in a_lst:
  print(-1)
else:
  is_visited = [False] * (n+1)
  cnt = 0
  pos = 1
  while(True):
    if(pos == 2):
      print(cnt)
      break
    elif(is_visited[pos]):
      print(-1)
      break
    else:
      is_visited[pos] = True
      pos = a_lst[pos]
      cnt += 1

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
visited={}
for i in range(1,n+1):
    visited[i]=-1
count=0
i=0
while True:
    count+=1
    if visited[a[i]]==0:
        print("-1")
        exit()
    if a[i]==2:
        print(count)
        exit()
    visited[a[i]]=0
    i=a[i]-1

Submission status: Accepted

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

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

if num != 2:
    count = -1

print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*n
for i in range(0,n):
  a[i]=int(input())
temp=a[0]
count=1
while count<=n:
  if temp==2:
    print(count)
    break
  temp=a[temp-1]
  count=count+1
else:
  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):
    y = x[-1] #今の場所
    z = a[y-1]
    x.append(z)
    if z == 2:
        print(len(x)-1)
        exit()

print(-1)

Submission status: Accepted

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

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

next = 0
visited = [False] * N
cnt = 0
while(True):
    if visited[next]:
        cnt = -1
        break
    if next == 1:
        break
    visited[next] = True
    next = A[next] -1
    cnt += 1
print(cnt)

Submission status: Accepted

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

now = 0
count = 0
while True:
    now = botan[now] - 1
    count += 1
    if count == N:
        print('-1')
        break
    if now == 1:
        print(count)
        break

Submission status: Accepted

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

def main():
    n = int(input())
    a = []
    now = 0

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

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

main()

Submission status: Accepted

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

def main():
    n = int(input())
    a = []
    l = [0]*(n + 1)
    now = 0

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

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

main()

Submission status: Accepted

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

def main():
    n = int(input())
    a = []
    l = [0]*(n + 1)
    cnt = 0
    now = 1

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

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

main()

Submission status: Accepted

Solution code:
n = int(input())
a = []
l = [0]*(n + 1)
cnt = 0
now = 1

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

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

print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

s = 1
cnt = 0
arrive = [0] * (N+1)
while s != 2:
    if arrive[s] == 1:
        print(-1)
        exit()
    arrive[s] = 1
    s = g[s]
    cnt += 1

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
ns = [int(input()) for _ in range(n)]
if not 2 in ns:
    print(-1)
    sys.exit(0)
i = 0
cnt = 0
pushed = [0]*n
flg = False
while(True):
    cnt += 1
    i = ns[i] - 1
    if pushed[i] == 1:
        break
    pushed[i] = 1
    if i == 1:
        flg = True
        break
if flg:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

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

visited = [False] * n

here = 0
for i in range(n):
    visited[here] = True
    moveto = a_list[here] - 1

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

    if visited[moveto]:
        break

    here = moveto

print("-1")
exit()

Submission status: Accepted

Solution code:
from sys import exit

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

i = 0
cnt = 0
while 1:
    if i == 1:
        print(cnt)
        exit()

    if cnt>=n:
        print(-1)
        exit()

    i = a[i] - 1
    cnt += 1

Submission status: Accepted

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

    x = [0]*n
    i = 0
    ans = 0
    while x[i] == 0:
        ans += 1
        x[i] = 1
        i = As[i] - 1
        if i == 1:
            print(ans)
            exit()
    print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

if count > len(a):
    count = -1

print(count)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
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:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
a = [INT() for _ in range(N)]

ans = 0
loc = 1

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

else:
	print(-1)

Submission status: Accepted

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

cnt = 0
n = 1
f = False
while cnt <= N:
  cnt += 1
  n = A[n - 1]
  if n == 2:
    f = True
    break
print(cnt if f else -1)

Submission status: Accepted

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

    prev_idx = 1
    flag = False
    num = 0
    seen = [-1] * N
    while True:
        if prev_idx == 2:
            flag = True
            break

        # if it visits the same point twice,
        # it means this graph has loop structure.
        if seen[prev_idx-1] != -1:
            break
        seen[prev_idx-1] = num
        prev_idx = A[prev_idx-1]
        num += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

route = [0]*N

next = 1
count = 0

while next != 2:
    if route[next-1]!=0:
        print(-1)
        exit()
    else:
        route[next-1] = 1
        next = a[next-1]
        count += 1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
pos =0
cnt=0
roop = []
visit =[0]*n
while visit[pos]!=2:
    if visit[pos]==0:
        visit[pos]+=1
        if pos == 1:
            print(cnt)
            break
        else:
            cnt+=1
            pos=a[pos]-1
    else:
        print(-1)
        break

Submission status: Accepted

Solution code:
N = int(input())
A = list()
for _ in range(N):
    A.append(int(input()))
ans = 0
now = 1
for _ in range(N):
    if now == 2:
        break
    now = A[now - 1]
    ans += 1
print(ans if ans != N else -1)

Submission status: Accepted

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

x = 0

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

Submission status: Accepted

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

import sys

bottom = [-1]*(N+1)
tmp = 1
point = 0

while bottom[tmp] == -1:
    bottom[tmp] = 0
    tmp = a[tmp]
    point += 1
    if tmp == 2:
        print(point)
        sys.exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
b = [0] * N
i = 0
c = 1
while True:
    if b[i] == 1:
        print(-1)
        sys.exit()
    b[i] = 1
    i = a[i] - 1
    if i == 1:
        print(c)
        sys.exit()
    c += 1

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
pushed = [0 for _ in range(n)]
pushed[0] = 1
counter = 1
start = 1
end = a[start-1]
while True:
    if pushed[end-1] == 1:
        print(-1)
        exit()
    else:
        if end == 2:
            print(counter)
            exit()
        else:
            counter += 1
            pushed[end-1] = 1
            start = end
            end =  a[start-1]

Submission status: Accepted

Solution code:
N = int(input())

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

now=1
c=0

while c<N:
    now=A[now-1]
    c+=1
    if(now==2):
        print(c)
        break
else:
    print("-1")

Submission status: Accepted

Solution code:
N = int(input())

a = []

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

x = 0
for i in range(100000):
    x = a[x]
    if x == 1:
        print(i+1)
        exit()

print(-1)

Submission status: Accepted

Solution code:
#170624B
N = int(input())
flag = 0
X = []
ans = 0
for i in range(N):
    a = int(input())
    X.append(a)
    if a == 2:
        flag = 1
b = X[0]
count = 1

#print(X)
if flag == 0:
    ans = -1

else:
    for i in range(N):
        c = X[b-1]
        b = c
        #print(c)
        count += 1
        if c == 2:
            ans = count
            break
if ans == 0:
    ans = -1
    
if X[0] == 2:
    print(1)
else:
    print(ans)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    a=[int(input()) for i in range(N)]
    tmp=1
    for i in range(N):
        if tmp==2:
            print(i)
            exit()
        tmp = a[tmp-1]
    print(-1)
main()

Submission status: Accepted

Solution code:
n = int(input())
a = [0]+[int(input()) for _ in [0]*n]
used = [False, True]+[False]*(n-1)
ans = 0
now = 1
while now!=2:
    if not used[a[now]]:
        used[a[now]] = True
        now = a[now]
        ans += 1
    else:
        print(-1)
        break
else:
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [0]+[int(input()) for _ in [0]*n]
ans = 0
now = 1
while now!=2:
    now = a[now]
    ans += 1
    if ans > n:
        ans = -1
        break
print(ans)

Submission status: Accepted

Solution code:
# 2020/05/10
# AtCoder Beginner Contest 065 - B

# Input
n = int(input())
a = list()
a.append(0)
for i in range(n):
    a.append(int(input()))

doflg = True
idx = 1
cnt = 1
while(doflg):
    if a[idx] == -1:
        ans = -1
        doflg = False
    elif a[idx] == 2:
        ans = cnt
        doflg = False
    else:
        cnt = cnt + 1
        sav_idx = idx
        idx = a[idx]
        a[sav_idx] = -1

# Output
print(ans)

Submission status: Accepted

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

tmp = a[0]-1
for i in range(N):

    if tmp == 1:
        print(i+1)
        exit()
    tmp = a[tmp]-1

print(-1)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

pos = 1
ans = 0
reached_index = set([1])
while True:
    pos = A[pos]
    ans += 1
    if pos in reached_index:
        ans = -1
        break
    if pos == 2:
        break
    reached_index.add(pos)

print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

pos = 1
ans = 0
reached_index = set()
while True:
    pos = A[pos]
    ans += 1
    if pos in reached_index:
        ans = -1
        break
    if pos == 2:
        break
    reached_index.add(pos)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

b = [0]*(N+1)

button2 = [] # 2があるボタン

for i in range(1,N+1):
    a = int(input())
    b[i] = a
    if a == 2:
        button2.append(i)
        
now = 1
count = -1

for i in range(0, N+1):

    if now == 2:
        count = i
        break
    
    now = b[now]

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

pre = 1
flg = False
cnt = 0
for i in range(N+1):
  if pre == 2:
    flg = True
    break
  if pre == a_list[pre-1]:
    break
    
  pre = a_list[pre-1]
  cnt += 1
    
if flg:
  print(cnt)
else:
  print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(input()) for nesya in range(n)]
s = 1
a = 1
while a <= n:
  s = a_list[s-1]
  if s == 2:
    print(a)
    break
  elif a == n:
    print(-1)
  a += 1

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
DONE = 1

btns = []
for i in range(n):
    btns.append([int(input()), 0])

current = 1
count = 0
while(current != 2):
    if btns[current - 1][1] == DONE:
        print(-1)
        exit()
    btns[current - 1][1] = DONE
    current = btns[current - 1][0]
    count += 1
else:
    print(count)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(input()) for _ in range(N)]
#print(a)
x=1#最初に付いているボタン
count=0
for i in range(N):
    x=a[x-1]
    count+=1
    if x==2:
        break
if x!=2:
    print(-1)
else:
    print(count)

Submission status: Accepted

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

Submission status: Accepted

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

if a[0]==2:
    print(1)
    exit()
else:
    x=a[0]-1

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

Submission status: Accepted

Solution code:
# region header
import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**6)
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]
INF = float('inf')
MOD = 10 ** 9 + 7
# endregion
N = ri()
a = [ri()-1 for _ in range(N)]
f = [0] * N
ch = 0
cnt = 0
while 1:
    if f[ch]:
        cnt = -1
        break
    else:
        f[ch] = 1
        cnt += 1
        ch = a[ch]
        if ch == 1:
            break
print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    a_arr = [None]
    for i in range(N):
        _ = int(_in[i+1])  # type:int
        a_arr.append(_)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    memo = []
    now = 1
    for i in range(N):
        _ = a_arr[now]
        if _ == 2:
            cnt = i+1
            break
        else:
            now = _
    else:
        cnt = -1
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

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

i = 0
count = 0
root = set()
while True:
    light = a[i]
    count += 1

    if i not in root:
        root.add(i)
    else:
        print(-1)
        break

    if light == 2:
        print(count)
        break

    i = light - 1

Submission status: Accepted

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

visited = set([1])
i = 1
ans = 1
while True:
  if dic[i] == 2:
    print(ans)
    break
  elif dic[i] in visited:
    print(-1)
    break
  visited.add(dic[i])
  i = dic[i]
  ans += 1

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  a = [0] * N
  visited = [False] * N
  l = 0
  ans = 0


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

  visited[0] = True

  while(l != 1):
    l = a[l] - 1

    if(visited[l]):
      ans = -1
      break
    
    visited[l] = True
    ans += 1

  print(ans)

if __name__ == "__main__":
  main();

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A = int(input())
listA=[]
for i in range(A):
    listA.append(int(input()))
listB=set()
B=0
for j in range(A):
    if B == 1:
        print(j)
        exit()
    else:
        B=listA[B]-1
print(-1)

Submission status: Accepted

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

next = 0
for i in range(1, N):
    if a[next] == 2:
        print(i)
        exit()
    next = a[next]-1

print(-1)

Submission status: Accepted

Solution code:
N,*A = map(int,open(0).read().split())
#A：次に光るボタン

P =[0]+[0 for i in range(N)]#各ボタンを押した回数

a = 1 #現在光っているボタン

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

Submission status: Accepted

Solution code:
import sys

n = [int(x) for x in sys.stdin]

button = 1

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

Submission status: Accepted

Solution code:
import sys

n = list(map(int, sys.stdin))

button = 1

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

Submission status: Accepted

Solution code:
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1

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

Submission status: Accepted

Solution code:
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

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

Submission status: Accepted

Solution code:
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

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

if count == n:
    print(-1)