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

time = 0
push = 0
while True:
    time += 1
    if bL[push] == 2:
        print(time)
        break
    if time == N:
        print("-1")
        break
    push = bL[push]-1

Submission status: Accepted

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

time = 1
push = 0
while True:
    
    if bL[push] == 2:
        print(time)
        break
    if time == N:
        print("-1")
        break
    
    push = bL[push]-1
    
    time += 1

Submission status: Accepted

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

j = 0
pointer = 0
b = False
for i in range(n):
    j += 1
    if a[pointer] == 1:
        b = True
        break
    else:
        pointer = a[pointer]

print(j) if b == True else print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
i = 0
while True:
    i = a[i]  - 1
    c += 1
    if i == 1:
        print(c)
        break
    if c > n:
        print(-1)
        break

Submission status: Accepted

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

while i != 2:
    if i in s:
        ans = -1
        break
    else:
        s.add(i)
        i, ans = a[i-1], ans+1
print(ans)

Submission status: Accepted

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

visited = set([])
cur = 0
ok = False

while True:
    if not (cur in visited):
        visited.add(cur)
    else:
        break
    cur = A[cur] - 1
    if cur == 1:
        ok = True
        break
if ok:
    print(len(visited))
else:
    print(-1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = [0]
a += [int(input()) for i in range(n)]

cnt = 0
ps = 1

for _ in range(1, n + 1):
    ps = a[ps]
    cnt += 1

    if ps == 2:
        print(cnt)
        break
else:
    print(-1)

Submission status: Accepted

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

num = -1
crr_btn = 1
for i in range(1, n + 1):
    if nxt_btn[crr_btn] == 2:
        num = i
        break
    crr_btn = nxt_btn[crr_btn]
print(num)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
b = [1] * n
i = 0
c = 0
while True:
    if b[i] < 1:
        break
    if i == 1:
        print(c)
        exit()
    b[i] = 0
    i = a[i] - 1
    c += 1
print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    s = []
    ans = 0
    now = 1
    for i in range(n):
        s.append(int(input()))
    for i in s:
        now = s[now-1]
        ans += 1
        if now == 2:
            print(ans)
            break
    else:
        print(-1)

Submission status: Accepted

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

count =0
x = 0
while x != 1 and count < 100000:
  count +=1
  x = list1[x]
  
if count < 100000:
  print(count)
else:
  print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(input()) - 1 for _ in range(n)]
s = set()
i = 0
ans = -1
while i not in s:
    if i == 1:
        ans = len(s)
        break
    s.add(i)
    i = a_list[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = [None]*n
for i in range(n):
    a[i] = int(input())-1
s = set()
c = 0
while True:
#     print(c)
    if c in s:
        ans = -1
        break
    s.add(c)
    c = a[c]
    if c==1:
        ans = len(s)
        break
print(ans)

Submission status: Accepted

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

visited = set()

cur = 0
visited.add(0)
ans = 0
while cur != 1:
    if a[cur] not in visited:
        visited.add(a[cur])
        cur = a[cur]
        ans += 1
    else:
        ans = -1
        break

print(ans)

Submission status: Accepted

Solution code:
s = input()
n = int(s)

nodes = []

for i in range(n):
    p = input()
    q = int(p)
    nodes.append(q)

answer = -1
m = []

x = nodes[0]
m.append(x)
for i in range(n-1):
    w = nodes[x-1]
    m.append(w)
    x = w

for i in range(len(m)):
    if m[i] == 2:
        answer = i + 1
        break

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
cnt = 0
btn = 1
while(True):
    btn = A[btn-1]
    cnt += 1
    if btn == 2:
        print(cnt)
        break
    if cnt > 10**6:
        print(-1)
        break

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
frag = [True for _ in range(n)]
frag[0] = False
ans = 0
now = 1
while(now != 2):
  ans += 1
  now = a[now-1]
  if(frag[now-1]):
    frag[now-1] = False
  else:
    ans = -1
    break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input())-1 for _ in range(n)]
a = A[0]
i = 1
aset = set()
while a != 1:
    a = A[a]
    if not a in aset:
        aset.add(a)
        i += 1
    else:
        print(-1)
        break
else:
    print(i)

Submission status: Accepted

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

Submission status: Accepted

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

has_pushed_button = set()
pushed_button_num = 1
count = 0
can_push_2 = False
while not pushed_button_num in has_pushed_button:
    has_pushed_button.add(pushed_button_num)
    pushed_button_num = a_list[pushed_button_num - 1]
    count += 1
    if pushed_button_num == 2:
        can_push_2 = True
        break

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

Submission status: Accepted

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

pushed = set()

now = 0
ans = 0
while True:
    if now in pushed:
        ans = -1
        break
    pushed.add(now)
    ans += 1
    now = la[now]
    if now == 1:
        break
print(ans)

Submission status: Accepted

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

cnt = 0
num = 1

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
i=ans=1
while a[i-1]!=2 and ans<=n:
    ans+=1
    i=a[i-1]
print(ans if ans<=n else -1)

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())

pushed = []
button = [0] * (N + 1)

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

push_button = 1
for _ in range(1, N+1):
    if push_button == 2:
        print(len(pushed))
        exit(0)
    else:
        pushed.append(push_button)
        push_button = button[push_button]

print("-1")

Submission status: Accepted

Solution code:
N=int(input())

A=[0]*(N+1)
for k in range(1,N+1):
    A[k]=int(input())

T=[False]*(N+1)
x=1
I=0
while not T[x] and x!=2:
    T[x]=True
    I+=1
    x=A[x]
    
if x==2:
    print(I)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
button=[False]*(n+1)
button[1]=True
a={}
for i in range(1,n+1):
    ai=int(input())
    a[i]=ai
tmp=1
pushed={}
ans=0
while not button[2]:
    button[tmp]=False
    if tmp in pushed:
        print(-1)
        exit()
    pushed[tmp]=1
    tmp=a[tmp]
    button[tmp]=True
    ans+=1
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

v = [False]*n
v[0] = True

ans = 0
tmp = 0

while tmp != 1:
    ans += 1
    tmp = a[tmp]
    if v[tmp]:
        print(-1)
        exit()
    else:
        v[tmp] = not v[tmp]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = []

for i in range(N):
  a = int(input())
  A.append(a-1)
  
seen = set()

pos = 0
seen.add(0)
count = 0
while pos != 1:
  pos = A[pos]
  if pos in seen:
    count = -1
    break
  else:
    seen.add(pos)
  count += 1
  
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = 1
count = 0
while ans!=2:
    ans = a[ans-1]
    count += 1
    if count > len(a):
        print(-1)
        exit()
print(count)

Submission status: Accepted

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

light = 0
visited = [False] * N
count = 0
while True:
    if light == 1:
        break

    if visited[light]:
        print(-1)
        exit(0)

    visited[light] = True
    light = a[light]
    count += 1

print(count)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 1
    n = int(input())
    a = [int(input()) for _ in range(n)]
    c = [False]*n
    now = a[0]
    c[0] = True
    while now != 2:
        now = a[now-1]
        if c[now-1] == True:
            ans = -1
            break
        ans += 1
        c[now-1] = True
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

cnt = 0
light = 1
push = [0] * (n+1)

for i in range(1, len(a) + 1):
    if push[light] == 0:
        push[light] = 1
        light = a[light]
        cnt += 1
        if light == 2:
            print(cnt)
            exit()
    else:
        print(-1)
        exit()

Submission status: Accepted

Solution code:
N = int(input())
al = [[] for _ in range(N+1)]
for i in range(1,N+1):
    al[i] = int(input())
#print(al)

#初期値
n = 1
cnt = 1
visited = [0 for _ in range(N+1)]
visited[1] = 1
while al[n] != 2:
    cnt += 1
    n = al[n]
    if visited[n] == 1:
        print(-1)
        exit()
    else:
        visited[n] = 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0
ind = 0
flag = 0
for i in range(n):
    t = a[ind]
    if t == 2:
        flag = 1
        ans += 1
        break
    else:
        ind = t-1
        ans += 1

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

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

def main():
	n=II()
	A=[0]+[II() for _ in range(n)]

	a=1

	for i in range(n+3):
		if a==2:
			ans=i
			break
		a=A[a]
	else:
		ans=-1

	print(ans)









if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
push = [True] * n
for i in range(1, n+1):
  a = int(input())
  dic[i] = a
i = 1
cnt = 1
push[0] = False
while True:
  if dic[i] == 2:
    break
  else:
    k = dic[i]
    if push[k-1]:
      i = k
      cnt += 1
      push[k-1] = False
    else:
      cnt = -1
      break
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = [0]*n
count = 0
num = a[0]
index = 1
while ans[1] != 1:
    count += 1
    ans[index-1] += 1
    if ans[index-1] >= 2:
        print(-1)
        exit()
    index = num
    num = a[num-1]
print(count-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    now = 1
    check = [0]*N
    ans = 0
    while(1):
    # for k in range(100):
        if now == 2:
            break
        if check[now-1] == 1:
            print(-1)
            return
        else:
            check[now-1] = 1
        now = A[now-1]
        ans += 1
        # print(now)
    print(ans)
    return
        
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    now = 1
    check = [0]*N
    ans = 0
    # while(1):
    for k in range(N):
        if now == 2:
            print(ans)
            return
        # if check[now-1] == 1:
        #     print(-1)
        #     return
        else:
            check[now-1] = 1
        now = A[now-1]
        ans += 1
        # print(now)
    print(-1)
    return
        
        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

b = [True]*n
btn = 1
ans = 0
while b[btn - 1]:
    ans += 1
    b[btn - 1] = False
    btn = A[btn - 1]
    if btn == 2:
        b[1] = False
        print(ans)
        break
if b[1]:
    print(-1)

Submission status: Accepted

Solution code:
def readinput():
    n=int(input())
    a=[0]
    for _ in range(n):
        a.append(int(input()))
    return n,a

def main(n,a):
    button=[0]*(n+1)
    button[1]=1
    i=1
    count=0
    while(True):
        i=a[i]
        count+=1
        if i==2:
            return count
        if button[i]>0:
            return -1
        button[i]+=1

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

while count <= N:
    if num != 2:
        num = a[num-1]
        count += 1
    else:
        print(count)
        exit()
print(-1)

Submission status: Accepted

Solution code:
n=int(input())
lst=[]
for i in range(n):
    lst.append(int(input())-1)
ans=1
if lst[0]==1:
    print(ans)
    exit()
else:
    button=lst[0]
    lst[0]=None

while True:
    p=button
    button=lst[button]
    ans+=1
    if button==1:
        print(ans)
        break
    elif button==None:
        print(-1)
        break
    lst[p]=None

Submission status: Accepted

Solution code:
N = int(input())

button_dict = dict()

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

pushed_button_set = set()
i = 1
while 1:
    if i in pushed_button_set:
        print(-1)
        break
    elif i == 2:
        print(len(pushed_button_set))
        break
    else:
        pushed_button_set.add(i)
        i = button_dict[i]

Submission status: Accepted

Solution code:
N=int(input())
a=[]
for i in range(N):
    a.append(int(input()))
ans=0
position=1
flag=True
for i in range(N):
    if position==2:
        print(ans)
        flag=False
        break 
    else:
        position=a[position-1]
        ans+=1

if flag:
    print(-1)

Submission status: Accepted

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

for n in range(N):
  if l == 2:
    print(ans)
    exit()
  else:
    l = A[l-1]
    ans+=1
    
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

done = {}
ans = 0
now = A[1]
while True:
    ans += 1
    if now == 2:
        print(ans)
        exit(0)
    if done.get(now) == None:
        done[now] = True
        now = A[now]
    else:
        break
print(-1)

Submission status: Accepted

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


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

  set_ = set()
  set_.add(0)
  cur = 0
  next_ = 0
  cnt = 0

  while True:
    # push
    cnt += 1
    cur = next_
    next_ = A[cur] - 1
    if next_ == 1:
      print(cnt)
      break
    if next_ in set_:
      print(-1)
      break
    set_.add(next_)


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

Submission status: Accepted

Solution code:
N = int(input())

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

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

print(count)

Submission status: Accepted

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

next = 0
count = 0
for i in range(len(lst)):
    next = lst[next]
    count+=1
    if next == 1:
        print(count)
        sys.exit()
print(-1)

Submission status: Accepted

Solution code:
n = int(input())
li = [int(input()) for i in range(n)]
if 2 not in li:
  print(-1)
else:
  ans = 0
  st = 0
  while st != 1:
    if (st == 0 and ans > 0) or ans > n:
      ans = -1
      break
    st = li[st]-1
    ans += 1
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
cnt=0
button=0
flag=False
#ボタン1が最初光っている(インデックスは0)
for i in range(n):
    #ボタンを押す
    cnt+=1
    button=a[button]-1
    if button==1:
        flag=True
        break
if flag:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(500000)


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


#回数i,インデックスn
def button(i,n):
    n_new = a[n-1]
    
    if i == N:
        if n != 2:
            print(-1)
            return
    if n_new == 2:
        print(i)
        return 
    else:
        return button(i+1,n_new)

button(1,1)

Submission status: Accepted

Solution code:
N = int(input())

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

used = set()

i = 0
ans = 0
while True:
    a = A[i]

    if a == 2:
        print(ans)
        break

    if a in used:
        print(-1)
        break

    used.add(i)
    i = a
    ans += 1

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

N = getN()
A = getArray(N)
for i in range(N):
    A[i] -= 1
ignore = [0] * N
ignore[0] = 1

now = 0
cnt = 0
while True:
    now = A[now]
    cnt += 1
    if ignore[now] == 1:
        print(-1)
        break
    ignore[now] = 1
    if now == 1:
        print(cnt)
        break

Submission status: Accepted

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

#%%
idx = 0
visited = set()
count = 0
flag = False

for i in range(n):
    next_idx = A[idx]
    next_idx -= 1
    visited.add(idx)

    if idx == 1:
        flag = True
        break
    elif next_idx in visited:
        break
    count += 1
    idx = next_idx

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
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 ** 9)
INF = 10**9
mod = 10**9+7

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
s = [0] * N
c = 0
OK = False
n = 0
while True:
    c += 1
    n = a[n] - 1
    if n == 1:
        OK = True
        break
    if s[n] == 1:
        break
    s[n] = 1
if OK:
    print(c)
else:
    print(-1)

Submission status: Accepted

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

while(True):
    count[i]+=1
    i = a[i]-1
    if i == 1:
        print(sum(count))
        break
    if count[i] == 1:
        print(-1)
        break

Submission status: Accepted

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

while o != 2:
    if len(t) > n:
        print(-1)
        exit(0)
    t.append(o)
    o = a[o-1]

print(len(t))

Submission status: Accepted

Solution code:
N = int(input())
a_line = []
a_con = []
flag = True

for _ in range(N):
  a_line.append(int(input()))
  a_con.append(0)
  
ans = 1
place = 0

for i in range(N):
  if a_con[place] == 1:
    flag = False
    break
  if a_line[place] == 2:
    break
  a_con[place] += 1
  place = a_line[place] - 1
  ans += 1

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

Submission status: Accepted

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

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

print(-1)

Submission status: Accepted

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

    flag = False
    on = 0
    cnt = 0
    for i in range(n):
        if a[on] == 2:
            cnt += 1
            flag = True
            break
        else:
            cnt += 1
            on = a[on] - 1

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



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, *aa = map(int, open(0).read().split())

d = [0]*n

cur = 1

ans = 0

while 1:
    d[cur-1] = 1
    cur = aa[cur-1]
    ans += 1
    if cur == 2:
        print(ans)
        break
    if d[cur-1]:
        print(-1)
        break

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
count, current = 0, 1
while current != 2:
    current = A[current - 1]
    count += 1
    if count > n:
        count = -1
        break
print(count)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(input()) for _ in range(N)]
dp=[0]*(10**5)
dp[0]=1
c=0
ans=0
for i in range(N):
  ans+=1
  if a[c]-1==1:
    print(ans)
    exit()
  if dp[a[c]-1]==1:
    print(-1)
    exit()
  dp[a[c]-1]=1
  c=a[c]-1

Submission status: Accepted

Solution code:
import sys

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())


def main():
    N = ii()
    a = [ii() for i in range(N)]
    now = 0
    for i in range(N+1):
        if now == 1:
            print(i)
            return
        now = a[now]-1
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

N = int(input())
button = [0]*N

for i in range(N):
    button[i] = int(input())-1
count, now = 0, 0

while count <= N:
    now = button[now]
    count += 1
    if now == 1:
        print(count)
        sys.exit()
print(-1)

Submission status: Accepted

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

now = 0
cnt = 0
while now != 1:
    now = A[now] - 1
    cnt += 1
    if cnt > N:
        cnt = -1
        break
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

flag = [True]*n
t = a[0]
ans = 1
while t != 2:
  if flag[t-1]:
    flag[t-1] = False
    t = a[t-1]
    ans += 1
  else:
    print(-1)
    exit()
print(ans)

Submission status: Accepted

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

def solve(n,a):
  memo = [0]*(n+1)
  def f(a,present=1,cnt=0):
    next = a[present]
    if memo[next]:
      return -1
    elif next == 2:
      return cnt+1
    else:
      memo[present] = 1
      return f(a,next,cnt+1)
  return f(a)
      

N = int(input())
a = [0] + [int(input()) for _ in range(N)]
print(solve(N,a))

Submission status: Accepted

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

N = int(input())

a = []

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

ans = 0
b_now = 1
loop = []

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

print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
button = [[i+1, int(input())] for i in range(N)]
button.insert(0, [0, 0])
cnt = 0
times = 0
q = deque([1])

while q:
    p = q.popleft()
    l = button[p][1]
    cnt += 1
    if l == 1:
        print(-1)
    elif l == 2:
        print(cnt)
    else:
        q.append(l)

    times += 1
    if times >= N:
        print(-1)
        exit()

Submission status: Accepted

Solution code:
import numpy as np
def resolve():
    N = int(input())
    A = np.zeros(N + 1, dtype = int)
    for i in range(1, N + 1):
        A[i] = int(input())
    current_num = 1
    ans = 0
    Found = False
    for _ in range(N):
        next_num = A[current_num]
        ans += 1 
        if next_num == 2:
            Found = True
            break
        current_num = next_num
    if (Found):
        print(ans)
    else:
        print(-1)
resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
  a_ = int(input())
  a.append(a_)
res = []
i = 1
for _ in range(n):
  res.append(a[i-1])
  i = a[i-1]
  if i == 2:
    break

if 2 in res:
  print(len(res))
else:
  print(-1)

Submission status: Accepted

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

a = 1
cnt = 0
while a != 2 and not selected[a - 1]:
    selected[a - 1] = True
    a = A[a - 1]
    cnt += 1
if a == 2:
    print(cnt)
else:
    print('-1')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import  sys
N=int(input())
a = [int(input()) for _ in range(N)]
cnt=1
t=1
if a[t - 1] == 2:
    print(cnt)
    sys.exit()
for i in range(10**5):
    cnt+=1
    t=a[t-1]
    if a[t-1]==2:
        print(cnt)
        sys.exit()

print(-1)