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:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, a: "List[int]"):
    visited = [False]*N
    curr = 1
    counter = 0
    while curr != 2:
        if visited[curr-1]:
            print(-1)
            return
        visited[curr-1] = True
        curr = a[curr-1]
        counter += 1
    print(counter)

    return


def main():

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n = int(input())
a_tuple = tuple(int(input()) for _ in range(n))
num = 0
count = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

i = 0
ans = 1
while a[i] != -1:
    if a[i] == 1:
        print(ans)
        exit()
    _i = a[i]
    a[i] = -1
    i = _i
    ans += 1
print(-1)

Submission status: Accepted

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

ret=0
now=0
for i in range(N):
    ret+=1
    if AN[now]==2:
        print(ret)
        exit()
    else:
        now=AN[now]-1
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input())-1 for i in range(n)]
 
light = [False for i in range(n)]
cnt = 1
botan = a[0]
if botan == 1:
    pass
else:
    while cnt <= n:
        cnt += 1
        botan = a[botan]
        light[botan] = True
        if light[1]:
            break
 
if cnt < n:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
cnt = 1
now = 0
while True:
    if a[now] == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        now = a[now] - 1
        cnt += 1

Submission status: Accepted

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


count = 0
l = 0
while count <= n:
    l = a[l]-1
    count += 1
    if l == 1:
        break

if l == 1:
    print(count)
else:
    print("-1")

Submission status: Accepted

Solution code:
import sys 
sys.setrecursionlimit(10**6)

n = int(input())

memo= [0]*n

def f(i):
	global count

	if i ==1:
		return (1)
	elif memo[i]:
		return (0)
	else:
		count+=1
		memo[i]=1
		return f(a[i]-1)
	
 

a = [int(input()) for i in range(n)]
count=0
if f(0):
	print(count)
else:
	print(-1)

Submission status: Accepted

Solution code:
import sys

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

count = 1
tmp_num = aL[0]
if tmp_num == 2:
    print(1)
    sys.exit()
for i in range(n-1):
    tmp_num = aL[tmp_num-1]
    count += 1
    if tmp_num == 2:
        print(count)
        sys.exit()
print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

a =[0]

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


f =[0]*(N+1)

i=1
c = 0
while f[i]==0:
    f[i]=1
    i = a[i]
    c +=1
    if i ==2:
        break
if f[i]==1:
    print(-1)
else:
    print(c)

Submission status: Accepted

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

hantei = False
index = 0
cnt = 1
for i in range(N):
    # 今いる場所がボタン2ならば終わり
    if a[index] == 2:
        hantei = True
        break
    index = a[index]-1
    cnt += 1
if hantei:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
N=int(input())
aN=[int(input()) for _ in range(N)]
c=0
i=1
while True:
  c+=1
  if aN[i-1]==2:
    print(c)
    break
  elif aN[i-1]==0:
    print(-1)
    break
  t=aN[i-1]
  aN[i-1]=0
  i=t

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = int(input())
b = [int(input()) for _ in range(a)]
c = 0
d = 1
while True:
    c += 1
    if b[d-1] == 2:
        print(c)
        exit()
    elif c > a:
        print(-1)
        exit()
    else:
        d = b[d-1]

Submission status: Accepted

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

count = 1
now_button = 0
for i in range(len(a_list)):
    if a_list[now_button] == 2:
        print(count)
        exit()
    else:
        now_button = a_list[now_button]-1
        count += 1

print('-1')

Submission status: Accepted

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

count = 1
now_button = 0
for i in range(1, len(a_list)+1):
    if a_list[now_button] == 2:
        print(count)
        exit()
    else:
        now_button = a_list[now_button]-1
        count += 1

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

Submission status: Accepted

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

while count < N :
    if s == 2 :
        print(count + 1)
        hantei = 1
        break
    else :
        s = a[s - 1]
        count += 1

if hantei == 0 :
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
aLst = []
ans = -1
cnt = 0
for i in range(N):
    aLst.append(int(input()) - 1)

tmp = 0
index = 0
for i in range(N):
    tmp = aLst[tmp]
    aLst[index] = 0
    if tmp == 0:
        break
    elif tmp == 1:
        cnt += 1
        ans = cnt
        break
    else:
        index = tmp
        cnt += 1

print(ans)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    buttons = [int(input()) for _ in range(n)]
    c = 1
    now = buttons[0]  # button1
    while now != 2 and c < n:
        c += 1
        now = buttons[now - 1]
    return c if c < n else -1


if __name__ == "__main__":
    print(solve())

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:
# ABC 65, B - trained
n = int(input())
A = [int(input()) for i in range(n)]
now = 1
went = set()
for i in range(n):
    if now == 2:
        print(i)
        break
    if now in went:
        print(-1)
        break
    went.add(now)
    now = A[now-1]

Submission status: Accepted

Solution code:
n = int(input())
l = [0]*n
for i in range(n):
  l[i] = int(input())
ans = 0
b = 1
rireki = set()
while True:
  ans += 1
  b = l[b-1]
  rireki.add(b)
  if b == 2:
    print(ans)
    break
  elif len(rireki) != ans:
    print(-1)
    break

Submission status: Accepted

Solution code:
N = int(input())
nums = [0]
for _ in range(N):
  nums.append(int(input()))
check_cnt = 1
_next = nums[1]
while True:
  if check_cnt >= N:
    break
  else:
    if _next == 2:
      print(check_cnt)
      exit()
    _next = nums[_next]
    check_cnt += 1
print(-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
b = set()
cur = 1
b.add(cur)
for i in range(n):
  cur = a[cur-1]
  if cur == 2:
    print(i+1)
    exit()
  elif cur in b:
    break
  else:
    b.add(cur)
print(-1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
n = int(input())
a_list = [int(input()) for _ in range(n)]
def calc_ans(start_num, count, max_count, all_list):
    ans = all_list[start_num-1]
    if ans == 2:
        print(count)
    elif count == max_count:
        print('-1')
    else:
        calc_ans(ans, count+1, max_count, all_list)
calc_ans(1, 1, n, a_list)

Submission status: Accepted

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

on=1
ans=0
for _ in range(N):
  on=x[on-1]
  ans=ans+1
  if on==2:
    print(ans)
    exit()
  
print(-1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools


def solve(N: int, a: "List[int]"):
    counter = 0
    nx = 0
    r = []
    for i in range(N):
        counter += 1
        r.append(a[nx])
        if a[nx] - 1 == 1:
            break
        nx = a[nx] - 1
    if counter < N and 2 in r:
        print(counter)
    else:
        print(-1)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

a = 1
c = -1

for i in range(N):
  a = n[a-1]
  if a == 2:
    if c < 0:
      c = (i+1)

print(c)

Submission status: Accepted

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

a = 1
c = -1

for i in range(N):
  a = n[a-1]
  if a == 2:
    if c < 0:
      c = (i+1)

print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    a = []
    for _ in range(n):
        a.append(read_int(1)[0])
    return a


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    dummy = -1
    flag_for_not_visited = 0
    flag_for_visited = 1

    buttons_flash_info = list(a)
    buttons_flash_info.insert(0, dummy)

    index = 1
    visited = [flag_for_not_visited for i in range(len(buttons_flash_info))]
    visited[index] = flag_for_visited
    push_count = 1
    while True:
        next_index = buttons_flash_info[index]
        index = next_index
        if index == 2:
            return push_count
        if visited[index] == flag_for_visited:
            return -1
        visited[index] = flag_for_visited
        push_count += 1
    return -1


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

tmp = 1
count = 0
check = 0

while tmp != 2 :
    tmp = a[tmp-1]
    count += 1
    if count >= N:
        check = 1
        break

if check == 0:
    print(count)
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 = []
for i in range(n):
    a.append(int(input()))
count = 0
button = 1
for i in range(n):
    button = a[button - 1]
    count += 1
    if button == 2:
        break
if button == 2:
    print(count)
else:
    print(-1)

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc065/tasks/abc065_b
# B - Trained?
n = int(input().split()[0])

index2number_dict = {}
number2index_dict = {}
button_list = []

for _ in range(n):
    number = int(input().split()[0])
    button_list.append(number)
    

# ボタンが光っているときに押すと、
# 今のボタンの光が消える
# 次のボタンが光る
# ボタン2が光っている状態でやめたい
# print(index2number_dict)

start_index = 1
# print(start_number)
# print(start_index)

step = 0
lighting_index = start_index # 1 - n
status_list = ''
can_stop = False

while True:
    status_list += str(lighting_index)

    lighting_number = button_list[lighting_index - 1]
    lighting_index = lighting_number  # 次のインデックス
    step += 1


    if lighting_index == 2:
        can_stop = True
        break

    if step >= len(button_list):
        break
    # print('i = {}, ai = {}'.format(lighting_index, lighting_number))
    

    # print('Next -> i = {}, a_i = {} '.format(lighting_index, lighting_number))
    

if not can_stop:
    step = -1

# print('===========')
# print(status_list)
print(step)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(-1)

Submission status: Accepted

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


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

print(-1)

Submission status: Accepted

Solution code:
N = int(input())
button_list =[]
for i in range(1,N+1):
  button_list.append(int(input()))

count = 0
dest = 1
for i in range(N):
    dest = button_list[dest - 1]
    count +=1
    if dest == 2:
        break;
if count == N:
    print(-1)
else:
    print(count)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]
i=1
count=0
while i!=2 and count<=100001:
  i=A[i-1]
  count+=1

if count>100000:
  print(-1)
else:
  print(count)

Submission status: Accepted

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

reached = []
pos = 0#indexで指定
c = 0
while c <= INF:
    reached.append(pos)
    if pos == 1:
        break
    pos = A[pos]-1
    c += 1

if 1 in reached:
    print(reached.index(1))
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
button = [0]
for _ in range(N):
  button.append(int(input()))
pos = 1
cnt = 0
for _ in range(N):
  pos = button[pos]
  cnt += 1
  if pos == 2:
    break

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

Submission status: Accepted

Solution code:
a = int(input())
b = [int(input())for _ in range(a)]
c = 0
d = 1
while True:
    if b[d-1] == 2:
        c += 1
        break
    elif c > a:
        print(-1)
        exit()
    else:
        d = b[d-1]
        c += 1
print(c)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

pushed = defaultdict(int)

def main(start=0):
     count = 0
     ls = start
     while True:
          if ls == 1:
               return count
          elif pushed[ls] != 0:
               return -1

          pushed[ls] += 1
          count += 1
          ls = A[ls]

print(main())

Submission status: Accepted

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

import sys

sys.setrecursionlimit(10**7)

# Override `input` function because `stdin.readline()` is 10x faster than built-in `input()`
input = sys.stdin.readline


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h(typ) if m > 1 else typ(input()) for _ in range(n)]


def main():
    n, = read_h()
    nxt = [i - 1 for i in read_v(n)]

    idx = 0
    cnt = 0
    for _ in range(n):
        cnt += 1
        idx = nxt[idx]
        if idx == 1:
            print(cnt)
            return

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
index = 0

while count < n:
  count += 1
  if a[index] == 1:
    break
  else:
    index = a[index]

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    x, a, b = LI()
    if b <= a:
        print("delicious")
    elif b <= a + x:
        print("safe")
    else:
        print("dangerous")

    return

#B
def B():
    n = II()
    a = IR(n)
    check = [False] * (n + 1)
    check[1] = True
    now = a[0]
    for i in range(1,n+1):
        if check[now]:
            break
        if now == 2:
            print(i)
            return
        check[now] = True
        now = a[now - 1]
    print(-1)
    return

#C
def C():
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    B()

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input())-1)
index = 0
#indexが1になったら（２ボタンを押したら）おしまい
flag = False 
count = 0
for i in range(N):
    count+=1
    index=a[index]
    if(index == 1):
        flag = True
        break
if(flag):
    print(count)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
ans = False
used = set()
i = 1
count = 0
while ans == False:
    count += 1
    if a[i-1] == 2:
        ans = True
    elif i-1 in used:
        break
    else:
        used.add(i-1)
        i = a[i-1]
if ans == True:
    print(count)
else:
    print("-1")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = int(input())
b = [int(input()) for _ in range(a)]
c = 1
d = 0
e = {}
while True:
    d += 1
    if b[c-1] == 2:
        print(d)
        exit()
    else:
        if (c-1) not in e:
            e.setdefault((c-1), "")
        else:
            print(-1)
            exit()
        c = b[c - 1]

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, n):
  if li[index] == 2:
    print(ans)
    exit()
  else:
    index = li[index] - 1
    ans += 1
print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N
B = [0] * N
c = 0

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

k = 0
while B[k] == 0 and k != 1:
  B[k] = 1
  k = A[k]
  c += 1

if k == 1:
  print(c)
else:
  print(-1)

Submission status: Accepted

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

count = 0
next = 1
for i in range(0, n):
    count += 1
    if As[next] == 2:
        print(count)
        exit(0)
    else:
        next = As[next]

print(-1)

Submission status: Accepted

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

if a[0] == 2:
    print(1)
else:
    next = a[0] - 1
    for i in range(1,len(a)):
        if a[next] == 2:
            print(i+1)
            break
        next = a[next]-1
    else:
        print(-1)

Submission status: Accepted

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

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

    check = [False] * N
    next = 0
    ok = True
    count = 0
    while True:
        check[next] = True
        next = a[next]
        count += 1
        if next == 1:
            break
        if check[next]:
            ok = False
            break

    if ok:
        print(count)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
D = {}
for i in range(n):
    D[i + 1] = int(input())
ans = 0
button = 1
pushed = set()
while button not in pushed:
    pushed.add(button)
    button = D[button]
    ans += 1
    if button == 2:
        print(ans)
        break
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=[int(input()) for _ in range(n)]
       
cnt=0
tmp=1

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

Submission status: Accepted

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

i = 1
count = 0
Flag = False
while count < N:
    i = a[i-1]
    count += 1
    if i == 2:
        Flag = True
        break

if Flag:
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
a=int(input())
b=[0]+[int(input()) for i in range(a)]
c=1
d=0
e=[False for i in range(a+1)]
while c!=2:
  e[c]=True
  d+=1
  c=b[c]
  if (e[c]):
    d=-1
    break

print(d)

Submission status: Accepted

Solution code:
a = int(input())
b = [[int(input()), False]for i in range(a)]
c = 1
d = 0
while True:
    d += 1
    if b[c-1][0] == 2:
        print(d)
        exit()
    else:
        b[c-1][1] = not b[c-1][1]
        if b[c-1][1]:
            c = b[c-1][0]
        else:
            print(-1)
            exit()

Submission status: Accepted

Solution code:
n = int(input())
a = { (i + 1):int(input()) for i in range(n) }
s = 1
ans = 0
seen = set()
while True:
    if s == 2:
        break
    seen.add(s)
    s = a[s]
    ans += 1
    if s == 1 or s in seen:
        ans = -1
        break
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC65 B
n = int(input())

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    i = 1
    c = 0
    v = [0] * (N + 1)
    while v[i] == 0:
        if A[i - 1] == 2:
            print(c + 1)
            return
        v[i] = 1
        i = A[i - 1]
        c += 1
    print(-1)


main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

p = A[1]
count = 1
while True:
    if count == N+1:
        print('-1')
        break
    if p == 2:
        print(count)
        break
    p = A[p]
    count += 1

Submission status: Accepted

Solution code:
n = int(input())
l = [0]+[int(input()) for _ in range(n)]
s = set()
ans=0
num=1
while num!=2:
    s.add(num)
    ans+=1
    num=l[num]
    if num in s:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

def main():
    count = defaultdict(int)

    v = 1
    ans = 0
    while True:
        if count[v] != 0:
            return -1
        count[v] = 1
        v = A[v]
        ans += 1
        if v == 2:
            return ans

print(main())

Submission status: Accepted

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

d = {}
on = 1
cnt = 0
while True:
    next = list[on]
    cnt += 1
    if next in d:
        print("-1")
        exit()
    else:
        d[next] = True
    if next == 2:
        print(cnt)
        exit()
    on = next

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
now = a[0]
count = 1
c = 0
for i in range(10**6):
    if now != 2:
        count += 1
        now = a[now - 1]
    else:
        print(count)
        c = 1
        break
if c == 0:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
now = a[0]
count = 1
c = 0
for i in range(10**6):
    if now != 2:
        count += 1
        now = a[now - 1]
    else:
        print(count)
        c = 1
        break
if c == 0:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
lst = [0]
lst += [int(input()) for i in range(n)]
cnt = 1
flashed = lst[1]

while True:
    if flashed == 2:
        print(cnt)
        break
    elif cnt <= 10**6:
        flashed = lst[flashed]
        cnt += 1
    else:
        print(-1)
        break

Submission status: Accepted

Solution code:
# B問題
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
flag = False
count = 0
tmp = 1
for i in range(N):
    tmp = a[tmp-1]
    count += 1
    if tmp == 2:
        flag = True
        break
if flag:
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
aiList = [int(input()) for i in range(n)]
count = 0
now = 1
while True:
    count += 1
    if count >= n:
        count = -1
        break
    nextNum = aiList[now - 1]
    if nextNum == 2:
        break
    now = nextNum
print(count)

Submission status: Accepted

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