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: Time Limit Exceeded

Solution code:
n = int(input())
ls = []
for i in range(n):
  ls.append(int(input()) - 1)
count = 0
if (1 not in ls):
  count = -1
else:
  used = []
  x = 0
  count = 0
  while (x != 1):
    used.append(x)
    x = ls[x]
    count += 1
    if (x in used):
      count = -1
      break
print(count)

Submission status: Time Limit Exceeded

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


if not 2 in a:
    print -1
else:
    alr=[]
    i=1
    c=1
    while(1):
        if a[i]==2:
            print c
            break
        elif i in alr:
            print -1
            break
        else:
            alr.append(i)
            i=a[i]
            c+=1

Submission status: Time Limit Exceeded

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

end = {}

end[1] = True
to = a[1]

cnt = 0
while True:
    cnt += 1
    if to == 2:
        print(cnt)
        __import__("sys").exit()
    if end.get(to) == None:
        to = a[to]
    else:
        break

print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ls = []
for i in range(n):
  ls.append(int(input()) - 1)
used = []
x = 0
count = 0
while (x != 1):
  used.append(x)
  x = ls[x]
  count += 1
  if (x in used):
    count = -1
    break
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
history = []
a = [0  for _ in xrange(N)]
for i in xrange(N):
    a[i] = int(raw_input())
i = 1
cnt = 0
while True:
    cnt += 1
    history.append(i)
    i = a[i-1]
    if i in history:
        print -1
        break
    
    if i ==2:
        print cnt
        break

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
    ind = q[ind]-1
    cnt += 1
    if ind == 1:
        print(cnt)
        exit()
    
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
q = [int(input())  for i in range(n)]
cnt = 0
ind = 0
for i in range(n):
    ind = q[ind]-1
    cnt += 1
    if ind == 1:
        print(cnt)
        exit()
    
print(-1)

Submission status: Accepted

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

ans = -1
visited = set()
count = 0
btn = 0
for i in range(n):
  if btn in visited:
    break
  visited.add(btn)
  btn = a[btn]-1
  count += 1
  if btn == 1:
    ans = count
    break

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

A = [0] + A
i = 1
k = 1
while k <= N:
    if A[i] == 2:
        print(k)
        exit()
    i = A[i]
    k += 1
print(-1)

Submission status: Accepted

Solution code:

def resolve():
    N = int(input())
    A = [ int(input()) for _ in range(N)]

    #print(N, A)

    next = 0
    cnt = 0
    for i in range(N):
        next = A[next] - 1
        #print(next)
        cnt += 1
        if next == 1:
            break

    if cnt == N:
        print(-1)
    else:
        print(cnt)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N=int(input())
a=[]
for i in range(N):
  a.append(int(input())-1)
flag=True
step=[]
pivot=0
for i in range(N+1):
  step.append(pivot)
  if a[pivot]==1:
    break
  pivot=a[pivot]

if len(step)>N:
  print(-1)
else:
  print(len(step))

Submission status: Accepted

Solution code:
n = int(input())
light = [0] * (n+1)

for i in range(1,n+1):
  light[i] = int(input())
s = 1
ans = 0
while light[s] != 2 and ans <= n:
  s = light[s]
  ans += 1

if light[s] == 2:
  print(ans + 1)
else:
  print(-1)

Submission status: Accepted

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

used = [False] * N

cur = 0
ans = 0
while not used[cur]:
    if cur == 1:
        print(ans)
        exit()

    used[cur] = True
    cur = a[cur]
    ans += 1

print(-1)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a = [int(input()) for _ in range(N)]
    cnt = 0
    tmp = 1
    while cnt<=100001:
        tmp = a[tmp-1]
        cnt += 1
        if tmp == 2:
            return cnt
        elif tmp == 1:
            return -1

    return -1

print(main())

Submission status: Accepted

Solution code:
from collections import defaultdict

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

dic = defaultdict()

for i in range(n):
    dic[i+1] = A[i]

cnt, idx = 1, 0
val = dic[1]

while idx <= n:
    if val == 2:
        print(cnt)
        exit()
    val = dic[val]
    cnt += 1
    idx += 1
print(-1)

Submission status: Accepted

Solution code:
from typing import List


def answer(n: int, a: List[int]) -> int:
    next_value = 1
    count = 0
    for _ in range(n):
        next_value = a[next_value - 1]
        count += 1
        if next_value == 2:
            return count

    return -1


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    print(answer(n, a))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

fin = set()
cnt = 0
i = 0
while True:
    if i in fin:
        cnt = -1
        break
    if i == 1:
        break
    cnt += 1
    fin.add(i)
    i = A[i]
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input())-1 for _ in range(n)]
id=[-1]*n
x=0
for c in range(1,n+1):
    if id[x]!=-1:
        print(-1)
        exit()
    id[x]=c
    x=a[x]
    if x==1:
        print(c)
        exit()

Submission status: Accepted

Solution code:
n = int(input())
done = [False for _ in range(n)]
A = [int(input()) - 1 for _ in range(n)]
tmp = 0
for i in range(n + 1):
    if A[tmp] == 1:
        print(i + 1)

        break
    if done[tmp]:
        print(-1)
        break
    else:
        done[tmp] = True
    tmp = A[tmp]
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
N = int(input())
A = [int(input()) for _ in range(N)]
x = 0
cnt = 0
for _ in range(N):
    x = A[x] - 1
    cnt += 1
    if x == 1:
        print(cnt)
        sys.exit()
print(-1)

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

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

Submission status: Accepted

Solution code:
n = int(input())
btn = [0]
for i in range(n):
  btn.append(int(input()))
see = [0 for i in range(n+1)]
  
now = 1
cnt = 0

see[now] = 1
while True:
  now = btn[now]
  cnt += 1
  
  if now == 2:
    print(cnt)
    break
  
  if see[now] == 1:
    print(-1)
    break
  
  see[now] = 1

Submission status: Accepted

Solution code:
N = int(input())

l = []
for i in range(N):
  v = int(input())
  l.append(v)
  
v = l[0]
ans = 1
while v != 2 and v != -1:
  tmp = l[v-1]
  l[v-1] = -1
  v = tmp
  ans += 1
  
if v == 2:
  print(ans)
else: 
  print(-1)

Submission status: Accepted

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

if not a.count(2):
    print('-1')
else:
    i = 0
    while True:
        cnt += 1
        if a[i] == 2:
            print(cnt)
            break
        elif a[i] == i+1 or cnt > n:
            print('-1')
            break
        i = a[i]-1

Submission status: Accepted

Solution code:
import sys
N = int(input())
a = [int(input()) for _ in range(N)]
flag = [0]*N
k = 0
i = 0
flag[0] = 1
while k <N:
  k += 1
  i = a[i] - 1
  if flag[i] == 0:
    flag[i] = 1
  elif flag[i] == 1:
    print(-1)
    sys.exit()
  if i == 1:
    print(k)
    sys.exit()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
log = set()
i = 0
count = 1
while a[i] != 2:
  i = a[i] - 1
  if i in log:
    print(-1)
    break
  else:
    log.add(i)
    count += 1
else:
  print(count)

Submission status: Accepted

Solution code:
N = int(input())
a = tuple(int(input()) for _ in range(N))
log = set()
i = 0
count = 1
while a[i] != 2:
  i = a[i] - 1
  if i in log:
    print(-1)
    break
  else:
    log.add(i)
    count += 1
else:
  print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

flag = False
index = 0
count = 0

for i in range(n):
  index = a[index]
  count += 1
  if index == 1 :
    flag = True
    break

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def input_int():
    return(int(input()))

def input_int_list():
    return(list(map(int,input().split())))

def input_int_map():
    return(map(int,input().split()))

def run():
    num_count = input_int()
    nums = []
    for _ in range(num_count):
        nums.append(input_int())

    num_count = 1

    current_num = 1
    while True:
        current_num = nums[current_num - 1]

        if num_count >= len(nums):
            print(-1)
            return

        if current_num == 2:
            print(num_count)
            return
        num_count += 1

run()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

n = I()
a = IR(n)

# N個のボタンがあり、今1個のボタンが光っている。
# ボタンは1~Nまで番号があり、iが光っているときに押すと、iの光が消え、aiが光る。
# 光っていないボタンを押しても反応しない。
# ボタン１が光っている状態で開始、ボタン2が光っている状態で終了したい。
# そのようなことが不可能であれば－1、可能であれば最小回数を出力

#　普通にシミュレートしたらよい。
#　n回ボタン押したうえで、2にたどり着かなかったらどこかで同じボタンを2回押していることになる。

count = 0
cur = 1

while count < n:
    count += 1
    cur = a[cur-1]
    if cur == 2:
        print(count)
        sys.exit()
print(-1)

Submission status: Accepted

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

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

ans = pos = 0
flag = [0] * N
while True:
    ans += 1
    if a[pos] == 2:
        break
    #print(pos,a[pos]-1)
    pos = a[pos] - 1

    if flag[pos] > 0:
        ans = -1
        break
    else:
        flag[pos] = 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

now = 1
count = 0

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

print(-1)

Submission status: Accepted

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

i = 0
for j in range(n):
    i = al[i]-1
    if i == 1:
        print(j+1)
        exit()
print(-1)

Submission status: Accepted

Solution code:
# B - Trained?
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    cnt = 0
    x = 1

    if 2 in a:
        for _ in range(n-1):
            x = a[x-1]
            cnt += 1

            if x == 2:
                print(cnt)
                exit()
        else:
            print(-1)
    else:
        print(-1)
        

if __name__ ==  "__main__":
    main()

Submission status: Accepted

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

x = 1
Sum = 0
while x != 2 and Sum < N:
    x = a[x - 1]
    Sum += 1

if Sum >= N:
    print(-1)
else:
    print(Sum)

Submission status: Accepted

Solution code:
import sys
N = int(input())
A ={}
for i in range(1,N+1):
    A[i] = int(input())
#print(A)
ans = 1
x = 1
for j in range(N):
    if A[x] == 2:
        print(ans)
        sys.exit()
    x = A[x]
    ans += 1
print(-1)

Submission status: Accepted

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

if 2 not in a:
  print(-1)
  exit()

x = 1
for i in range(n-1):
 
  x = a[x-1]
  cnt += 1

            
  if x == 2:
    print(cnt)
    exit()

print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  n=int(input())
  A=[int(input()) for i in range(n)]
  crt=1
  cnt=0
  for i in range(n):
    cnt += 1
    crt = A[crt-1]
    if crt == 2:
      print(cnt)
      break
  else:
    print(-1)
main()

Submission status: Accepted

Solution code:
#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc065/tasks/abc065_b

N = int(input())
a = [int(input())-1 for _ in range(N)]
ans = 0
cur = 0

visit = [-1] * N
while True:
    # print(cur, visit)
    cur = a[cur]
    ans += 1
    if visit[cur] == -1:
        visit[cur] = ans
    else:
        print(-1)
        exit()
    if cur == 1:
        print(ans)
        exit()

Submission status: Accepted

Solution code:
#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc065/tasks/abc065_b

N = int(input())
a = [int(input())-1 for _ in range(N)]
ans = 0
cur = 0

visit = [-1] * N
while True:
    # print(cur, visit)
    cur = a[cur]
    ans += 1
    if visit[cur] == -1:
        visit[cur] = ans
    else:
        print(-1)
        exit()
    if cur == 1:
        print(ans)
        exit()

Submission status: Accepted

Solution code:
n = int(input())
buttons = [int(input()) for _ in range(n)]
done = [0] * (n + 1)
next_button = buttons[0]
for _ in range(n):
    buttons_status = done[next_button]
    if buttons_status != 0:
        print(-1)
        exit()
    done[next_button] = 1
    if next_button == 2:
        break
    next_button = buttons[next_button - 1]
print(done.count(1))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
L = []
light = [0]*(N)
light[0] = 1
cnt, now = 0, 0

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

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

print(-1)

Submission status: Accepted

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

i, count = 0, 0
success = False
for _ in range(N):
    i = a[i]
    count += 1

    if i == 1:
        success = True
        break
 
if not success:
    count = -1
 
print(count)

Submission status: Accepted

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

count = [0]*(N)

i = 1
min_num = 0
count[i-1]+=1
while True:
    i = a[i-1]
    min_num += 1
    if i == 2:
        print(min_num)
        exit()
    if count[i - 1] == 0:
        count[i-1]+=1
    else:
        break

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()))
found = [0]*N

steps = 0
i = 0
found[i] = 1
while True:
    j = a[i] - 1
    if found[j] == 1:
        print(-1)
        exit()
    steps += 1
    i = j
    found[i] = 1
    if i == 1:
        print(steps)
        exit()

Submission status: Accepted

Solution code:
def f():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    x = 1
    c = 0
    for i in range(n):
        if x != 2:
            x = a[x-1]
            c += 1
            if c == n:
                return -1
        else:
            return c
print(f())

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input()) - 1 # 配列に対応
index = 0
trials = -1
for i in range(N):
    index = A[index]
    if index == 1:
        trials = i + 1
        break
print(trials)

Submission status: Accepted

Solution code:
N = int(input())
S = [int(input()) for _ in range(N)]
#print(S)

s = ()
nex = 1

for i in range(N):
    #print(S[nex-1])
    nex = S[nex-1]
    if nex == 2:
        print(i+1)
        break
    if i == N-1:
        print(-1)

Submission status: Accepted

Solution code:
import re
import copy

def accept_input():
    N = int(input())
    A = {}
    for i in range(1,N+1):
        A[i] = int(input())
    return N,A

def process(s):
    if s == "#":
        return 1
    else:
        return 0
N,A = accept_input()
cur = 1
kaisu = 0
for _ in range(N):
    cur = A[cur]
    kaisu += 1
    if cur == 2:
        break
if cur == 2:
    print(kaisu)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())

a = [int(input())-1 for i in range(N)]
owari = [0]*N

tmp = 0
count = 0

while owari[tmp] == 0 and tmp != 1:
    count += 1
    owari[tmp]=1
    tmp = a[tmp]

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

Submission status: Accepted

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

    pressed = 0
    i = 0
    t = 0
    while i != 1:
        i = A[i]
        if pressed > N:
            t = -1
            break
        pressed += 1
        t += 1

    print(t)
    
resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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

visit = set()
now = 1
for i in range(1, n + 10):
    if now not in visit:
        visit.add(now)
        now = a[now - 1]
    else:
        print(-1)
        break
    if now == 2:
        print(i)
        break

Submission status: Accepted

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

counter = 0
now = 1
visit = [False] * n
visit[0] = True
while True:
    counter += 1
    if a[now - 1] == 2:
        print(counter)
        break
    elif visit[a[now - 1]-1] == True:
        print(-1)
        break
    else:
        visit[a[now - 1]-1] = True
    now = a[now - 1]

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

N = k()
A = []
for i in range(N):
    a = k()
    A.append(a)
    
aaa = 0    
ans = A[0]
count = 1
 
for i in range(N):
    if ans == 2:
        aaa = 1
        break
    else:
        ans = A[ans-1]
        count += 1
if aaa == 1:
    print(count)
else:
    print(-1)

Submission status: Accepted

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

i = 0;
flg = False

for n in range(0,N):
  if l[i] == 1 :
    print(n+1)
    flg = True
    break
  else:
    i = l[i]


if not flg:
  print("-1")

Submission status: Accepted

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


def solve(N: int, a: "List[int]"):
    s = {1}
    i = 1
    c = 0
    while True:
        c += 1
        i = a[i-1]
        if i == 2:
            print(c)
            break
        if i in s:
            print(-1)
            break
        s.add(i)

    return


# Generated by 1.1.7.1 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())
A = []
for i in range(N):
    a = int(input())
    A.append(a)
b = 1
x = A[0]
while x != 2:
    x = A[x - 1]
    if b > N:
        print(-1)
        exit()
    b += 1

print(b)

Submission status: Accepted

Solution code:
n = int(input())
A = []
bool = [True] * n
for i in range(n):
    a = int(input())
    a -= 1
    A.append(a)
j = A[0]
cnt = 1
for i in range(n):
    if j == 1:
        print(cnt)
        exit()
    if bool[A[j]] == True:
        bool[A[j]] = False        
        j = A[j]
        cnt += 1
    else :
        print(-1)
        exit()

Submission status: Accepted

Solution code:
N=int(input())
a=[int(input())-1 for _ in range(N)]
x=0
ans=0
while True:
    ans+=1
    x=a[x]
    if x==1:
        print(ans)
        break
    if ans>N:
        print(-1)
        break;

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
# a = list(map(lambda x: int(x) - 1, a))	#すべての要素から-1

p = a[0]

if p == 2:
    print(1)
    exit()
    
cnt = 1

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

print(-1)

Submission status: Accepted

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

j = 1
cnt = 0
check = False

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

Submission status: Accepted

Solution code:
N = int(input())
A = {}
for i in range(N):
  A[i+1] = int(input())
B = [-1 for i in range(N+1)]
B[1] = 0
i = 1
while 1:
  j = A[i]
  if j == 2:
    print(B[i] + 1)
    break
  elif B[j] != -1:
    print(-1)
    break
  else:
    B[j] = B[i] + 1
    i = j

Submission status: Accepted

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

    answer = -1
    counter = 0

    i = 0
    while True:
        counter += 1
        if i == a[i]:
            answer = -1
            break
        elif a[i] == 1:
            answer = counter
            break
        elif a[i] == 0:
            answer = -1
            break
        else:
            temp = a[i]
            a[i] = 0
            i = temp

    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys
nyuryoku = [int(i) - 1 for i in sys.stdin]

N = nyuryoku[0] + 1
a_list = nyuryoku[1:]


cnt_push = 0
button_list = [False] * N
i = 0
#cur_but_posi = a_list[0]

while True:
    if button_list[i] == False:
        button_list[i] = True
        cnt_push += 1
        if a_list[i] == 1:
            break
        i = a_list[i]
    else:
        cnt_push = -1
        break

print(cnt_push)

"""
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 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:
import sys

l = [int(i) - 1 for i in sys.stdin]

N = l[0] + 1

a = l[1:]

done = [False] * N

i = 0

ans = 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())
V = [int(input()) for i in range(1,N+1)]
cnt = 1
val = V[0]
while True:
  if val == 2:
    print(cnt)
    break
  if (cnt >= N):
    print(-1)
    break
  cnt += 1
  val = V[val-1]

Submission status: Accepted

Solution code:
with open(0) as f:
    N, *a = map(int, f.read().split())

from itertools import count
p = 0
seen = {0}
for i in count(1,1):
    p = a[p]-1
    if p == 1:
        ans = i
        break
    if p in seen:
        ans = -1
        break
    seen.add(p)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
now = 1
while now != 2:
    if li[now] == 0:
        count = -1
        break
    else:
        a = li[now]
        li[now] = 0
        now = a
        count += 1

print(count)

Submission status: Accepted

Solution code:
import sys
N = int(input())
d = [int(input()) for i in range(N)]

cnt = 0
button = d[0]
while button != 2:
    button = d[button-1]
    cnt += 1

    if cnt > 100000:
        print(-1)
        sys.exit(0)

print(cnt+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush

sys.setrecursionlimit(10**6)
INF = 10**20

def mint():
    return map(int,input().split())
def lint():
    return map(int,input().split())

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import cProfile
input = sys.stdin.readline
def main():
    n=int(input())
    a=[int(input()) for _ in range(n)]
    l=[1]
    i=0
    aa=1
    while True:
        aa=a[aa-1]
        if aa == 2:
            print(i+1)
            sys.exit()
        if i>n:
            print(-1)
            sys.exit()
        i+=1
main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
a = [ri() for _ in range(n)]
idx = 1
cnt = 1
if a[0] == 2:
    print(1)
    exit()
while idx != -1:
    a[idx-1], idx = -1, a[idx-1]
    cnt += 1
    if a[idx-1] == 2:
        print(cnt)
        exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N = int(input())
A = [0] + [int(input()) for _ in range(N)]

cur = 1
ans = 0
pushed = [False] * (N + 1)
pushed[1] = True
ok = True
while True:
    pushed[cur] = True
    ans += 1
    cur = A[cur]
    if cur == 2:
        break
    if pushed[cur]:
        ok = False
        break

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

Submission status: Accepted

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

print(-1)