Problem:


Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3



Sample input:
3
3
1
2


Sample output:
2

Submission status: Accepted

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

G = [1]
for i in range(N):
    G.append(A[G[-1]-1])
    
ans = 0
ok = False
for i in range(N+1):
    if G[i]==2:
        ok = True
        break
    ans += 1

if not ok:
    ans = -1
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

light = lst[1]
count = 1

while light != 2 and count < n:
    light = lst[light]
    count += 1

print(count if count < n else -1)

Submission status: Accepted

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

light = lst[1]
count = 1

while light != 2 and count < n+1:
    light = lst[light]
    count += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a1 = int(input())
    a.append(a1)
z = -1
s = 0
for h in range(n):
    if a[s] == 2:
        z = 'Yes'
        z = h+1
        break
    else:
        s = a[s]-1
print(z)

Submission status: Accepted

Solution code:
def resolve():
  N = int(input())
  A = [0]*(N+1)

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

  move_count = 0
  next_index = 1
  for _ in range(N):
    move_count += 1
    if A[next_index] == 2:
      print(move_count)
      return True
    elif A[next_index] == 0:
      print(-1)
      return True
    else:
      temp = next_index
      next_index = A[temp]
      A[temp] = 0

if __name__ == "__main__":
  resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC065 B:Trained?

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

step = 0
c = 0
while True:
    if step == 1:
        ans = c
        break
    if c > N:
        ans = -1
        break
    else:
        c += 1
        step = l[step]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [0]
for _ in range(N):
    a.append(int(input()))
memo = [-1] + [1] + [0] * (N-1)
next = 1
cnt = 0
while True:
    cnt += 1
    next = a[next]
    if next == 2:
        print(cnt)
        break
    if memo[next] == 1:
        print(-1)
        break
    memo[next] = 1

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list()
count = 0
value = 0
for i in range(N):
  a.append(int(input()))

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

Submission status: Accepted

Solution code:
ais = [int(raw_input()) for oo in range(int(raw_input()))]
seen = set([])
u = 0
while((u!= 1) and (u not in seen)):
	seen.add(u)
	u = ais[u] - 1

print len(seen) if u == 1 else -1

Submission status: Accepted

Solution code:
N=int(input())
a=[]
m=1
for i in range(N):
  t=int(input())
  a.append(t)
for j in range(N):
  m=a[m-1]
  if m==2:
    print(j+1)
    break
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

print(isOK)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)
import math
import itertools
INF = float('inf')
from heapq import heapify, heappop, heappush

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

    ans = 0
    button = 1
    for i in range(n):
        button = A[button-1]
        ans += 1
        if button == 2:
            print(ans)
            return
    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = {i + 1: int(input()) for i in range(N)}
ans = -1

count = 1
nxt = 1
while count < N:
  nxt = a[nxt]
  if nxt == 2:
    ans = count
    break
  count += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
num = 0
index = 0
a_lst = []
for _ in range(N):
  a_lst.append(int(input()))
past_index = [0] * N
while True:
  num += 1
  index = a_lst[index]-1
  if index == 1:
    break
  elif past_index[index] == 1:
    num = -1
    break
  else:
    past_index[index] = 1
print(num)

Submission status: Accepted

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

light = 0
ans = -1
for n in range(1,N+1):
  light = A[light]
  if light ==1:
    ans = n
    break
print(ans)

Submission status: Accepted

Solution code:
def train():
    n=int(input())
    al=[]
    for _ in range(n):
        a=int(input())
        al.append(a)
    id=0
    ans=1
    for _ in range(n):
        if al[id]==2:
            print(ans)
            return
        else:
            id=(al[id])-1
            ans+=1
    print(-1)
train()

Submission status: Accepted

Solution code:
n = int(input())
butnum = {}
for i in range(1,n+1):
    butnum[i] = int(input())
pushbut = 1
pushedbut =[]
jud = 0
for j in range(1,n+1):
    pushbut = butnum[pushbut]
    if pushbut == 2:
        print(j)
        jud = 1
        break
if jud == 0:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = 1
next = a[0]
while True:
  if next == 1:
    ans = -1
    break
  elif next == 2:
    break
  cur = next
  next = a[next - 1]
  a[cur - 1] = 1
  ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
now = 1
c = 0
p = set([1])
while True:
    if A[now-1] in p:
        print(-1)
        exit()
    p.add(A[now-1])
    c += 1
    if A[now-1] == 2:
        break
    now = A[now-1]
print(c)

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = [int(input()) for i in range(n)]
x = 0
cnt = 0
l = [False] * n
while x != 1:
    l[x] = True
    x = a[x]-1
    cnt += 1
    if l[x]:
        print(-1)
        sys.exit()
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
next = []
for i in range(N):
  next.append(int(input()) - 1)
now = 0
count = 0
judge = False
for i in range(N):
  now = next[now]
  count += 1
  if now == 1: 
    judge = True
    print(count)
    break
if not judge:
  print(-1)

Submission status: Accepted

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

visited={1,}
old=1
count=0
while 1:
  new = a[old-1]
  count += 1
  if new in visited:
    print(-1)
    exit()
  if new == 2:
    print(count)
    exit()
  old = new
  visited.add(new)

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*n
for i in range(n):
    a[i]=int(input())
f=1
count=0
while f!=2 and count<n+2:
    f=a[f-1]
    count=count+1
if count>n:
    count=-1
print(count)

Submission status: Accepted

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

button = [0] * N
button[0] = 1
next_button = 1
i = 0

while True:
  button[next_button - 1] = 2
  next_button = A[next_button - 1]
  if button[next_button - 1] == 2:
    print("-1")
    break
  elif next_button == 2:
    i += 1
    print(i)
    break
  else:
    button[next_button - 1] = 1
    i += 1

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input())-1 for _ in range(n)]
i=0
ct=0
while i!=1:
  if a[i]==-1:
    print("-1")
    break
  k=i
  i=a[i]
  a[k]=-1
  ct+=1
else:
  print(ct)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = [0] + [int(input()) for _ in range(N)]
    pushed = [False] * (N + 1)
    pushed[0] = True
    now = 1
    cnt = 0
    while now != 2:
        now = A[now]
        cnt += 1
        if pushed[now]:
            print(-1)
            return
        pushed[now] = True
    print(cnt)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [list(input()) for _ in range(n)]
#a = [int(input()) for _ in range(n)]

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

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

if (ans == n):
    print(-1)
else:
    print(ans+1)

Submission status: Accepted

Solution code:
N = int(input())
ls = []
ans = -1
for i in range(N):
    ls.append(int(input()))
on = 0
for i in range(N):
    on = ls[on]-1
    if on == 1:
        ans = i +1
        break
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

nextint = A[0]

for i in range(1, N + 1):
    if nextint == 2:
        print(i)
        exit()

    nextint = A[nextint - 1]

print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

State = [1]
for _ in range(N-1):
    State.append(0)

i=0
count=1
while 1:
    i = L[i]-1
    if State[i]==1:
        print(-1)
        exit()

    if i+1 == 2:
        print(count)
        exit()

    State[i]=1
    count+=1

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = []
    for _ in range(N):
        a = int(input())
        A.append(a)
    count = 0
    num = 1
    for i in range(N):
        num = A[num-1]
        count += 1
        if num == 2:
            print(count)
            exit()
    print("-1")
resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#/usr/bin/env python

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

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

print(-1)

Submission status: Accepted

Solution code:
#/usr/bin/env python

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

lighted = [0] 
ans = 0 

for i in range(n):
    u = lighted[-1]
    v = a[u]-1
    ans += 1
    if v == 1:
        print(ans)
        exit()
    lighted.append(v)

print(-1)

Submission status: Accepted

Solution code:
n = int(input())
f = [int(input()) for _ in range(n)]
f = [0] + f
vis = [0] * (n + 1)
s , flag , ans = 1 , 0 , 0
vis[s] = 1
while f[s] != 2:
	if vis[f[s]] == 1:
		flag = 1
		break
	s = f[s]
	vis[s] = 1
	ans+=1

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

Submission status: Accepted

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

prev = []
next = 0
count = 0
while(count < N):
    next = int(A[next])-1
    count += 1
    if(next == 1):
        print(count)
        break
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
b = a[0]
c = 1
d = set()
while b != 2:
    b = a[b - 1]
    if b in d:
        print(-1)
        break
    else:
        d.add(b)
        c += 1
else:
    print(c)

Submission status: Accepted

Solution code:
n=int(input())
li=[int(input()) for i in range(n)]
basyo=1
res=-1
for i in range(2*n):
    basyo=li[basyo-1]
    if basyo==2:
        res=i+1
        break
print(res)

Submission status: Accepted

Solution code:
N = int(input())
a_list = [int(input())-1 for i in range(N)]
next_i = 0
done_flag = False
for i in range(N):
    next_i = a_list[next_i]
    if  next_i == 1:
        done_flag = True
        break

if done_flag:
    print(i + 1)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


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

  next_ = A[0]
  cnt = 1
  lighten = set([0])
  while True:
    if next_ == 1:
      print(cnt)
      return
    if next_ in lighten:
      break
    lighten.add(next_)
    next_ = A[next_]
    cnt += 1

  print(-1)


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

Submission status: Accepted

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

cur = 1
for i in range(N):
    cur = buttons[cur-1]

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

print(-1)

Submission status: Accepted

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

for i in range(N):
    id = int(input()) - 1
    a[id].add(i)

step = -1
#already = a[1]
now = a[1]
next = set()
for i in range(N-1):
    #already = already.union(now)
    if 0 in now:
        step = i+1
        break
    for j in now:
        next = next.union(a[j])
    now = next
    next = set()

print(step)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
num=1
res=False
time=1
for j in range(n-1):
    if a[num-1]==2:
        res=True
        time+=j
        break
    else:
        num=a[num-1]

if res:
    print(time)
else:
    print(-1)

Submission status: Accepted

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

i = 0

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

print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
l = [(int(input())-1) for _ in range(N)]
flag = [0]*N
flag[0] = 1
flag[1] = 2
cnt = 0
cur_b = 0
while True:
    cnt += 1
    next_b = l[cur_b]
    if flag[next_b] == 1:
        print(-1)
        break
    elif flag[next_b] == 2:
        print(cnt)
        break
    else:
        flag[next_b] = 1
        cur_b = next_b
else:print(-1)

Submission status: Accepted

Solution code:
N = int(input())
l = [(int(input())-1) for _ in range(N)]
flag = [0]*N
flag[0] = 1
flag[1] = 2
cnt = 0
cur_b = 0
for i in range(N):
    cnt += 1
    next_b = l[cur_b]
    if flag[next_b] == 1:
        print(-1)
        break
    elif flag[next_b] == 2:
        print(cnt)
        break
    else:
        flag[next_b] == 1
        cur_b = next_b
else:print(-1)

Submission status: Accepted

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

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

print('-1')

Submission status: Accepted

Solution code:
N = (int(input()))
A = [0]+[int(input()) for T in range(0,N)]
Now = 1
Count = 0
Flag  = False
for T in range(0,N):
    Next = A[Now]
    Count += 1
    if Next==2:
        Flag = True
        break
    else:
        Now = Next
if Flag:
    print(Count)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
an = [int(input()) for num in range(n)]

answer = False
count = 1
index = 1
while(True):
  if count > len(an):
    break
  if an[index-1] == 2:
    answer = True
    break
  index = an[index-1]
  count += 1

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

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]
A.insert(0,0)
i=1
count=0
while True:
  if count>N:
    print(-1)
    break
  lamp=A[i]
  count+=1
  if lamp==2:
    print(count)
    break
  i=lamp

Submission status: Accepted

Solution code:
n = int(input())
arr = []
for i in range(0 , n):
	arr.append(int(input()))
gp =[]
p=0
for i in range(0 ,n):
	arr[i]=arr[i]-1
cnt =0
vis =[0 for i in range(0 , n+1)]
while True:
	if p==1:
		print (cnt)
		break
	if cnt>=n:
		print(-1)
		break
	cnt = cnt+1
	p = arr[p]

Submission status: Accepted

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

a0=a[0]
cnt=0
f=True

if a0==2:
  cnt=1
  f=False

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

if cnt==n:
  cnt=-1

print(cnt)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    A = list(int(s) - 1 for s in read().split())

    ans = 0
    x = 0
    lit = [False] * N
    lit[0] = True
    while True:
        x = A[x]
        ans += 1
        if lit[x] or x == 1:
            break
        lit[x] = True

    if x != 1:
        ans = -1

    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = 0
l = [True]*(n+1)
a = [0]
idx = 1
for i in range(n):
  a.append(int(input()))

while True:
  if idx == 2:
    print(c)
    break
  else:
    if l[idx]:
      c += 1
      l[idx] = False
      idx = a[idx]
    else:
      print(-1)
      break

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,sys.stdin.read().split()))
check_a=[False for _ in range(n)]
c=0
i=0
while True:
  if i == 1:
    print(c)
    exit()
  nex=a[i]-1
  if check_a[i]:
    break
  check_a[i]=True
  i=nex
  c+=1
print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
history = set()
now = 0
cnt = 0
while(True):
    if now in history:
        cnt = -1
        break
    else:
        cnt += 1
        history.add(now)
        now = a[now]-1
        if now == 1:
            break
print(cnt)

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())
used = [False]*(N+1)
used[1] = True
a = [None]*(N+1)
for i in range(1,N+1):
    x = int(input())
    a[i] = x
x = a[1]
cnt = 1
while True:
    if used[x] == True:
        print(-1)
        break
    if x == 2:
        print(cnt)
        break
    used[x] = True
    x = a[x]
    cnt += 1

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
li = [int(input()) for _ in range(N)]
i = 1
for n in range(N):
    i = li[i-1]
    if i == 2:
        print(n+1)
        break
else:print(-1)

Submission status: Accepted

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

btn=1
cnt=0

while True:
  btn=a[btn-1]
  cnt+=1
  if btn==2:
    print(cnt)
    break
  if cnt>N:
    print(-1)
    break

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log,gcd
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
'''
#Pythonを用いる場合
#numpy 1.18.0, scipy 1.4.1, scikit-learn 0.22, numba 0.47.0, networkx 2.4
 
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix
'''
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=[ii() for _ in range(n)]

chk=False
tmp=0
ans=0
for _ in range(n):
    if chk==False:
        ans+=1
    p=arr[tmp]
    if p==2:
        chk=True
    
    tmp=p-1

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

Submission status: Accepted

Solution code:
import sys
from collections import deque, Counter
def I(): return int(sys.stdin.readline().rstrip())

if __name__=='__main__':
    n = I()
    l = []
    for _ in range(n):
        l.append(I())
    c = [0]*n
    now = 0
    c[now] = 1
    for i in range(1,n+1):
        now = l[now]-1
        c[now] += 1
        if now==1:
            print(i)
            exit()
        if c[now]==2:
            print(-1)
            exit()
    exit()

Submission status: Accepted

Solution code:
import sys
from collections import deque, Counter
def I(): return int(sys.stdin.readline().rstrip())

if __name__=='__main__':
    n = I()
    l = []
    for _ in range(n):
        l.append(I())
    c = Counter()
    now = 0
    for i in range(1,n+1):
        now = l[now]-1
        c[now] += 1
        if now==1:
            print(i)
            exit()
        if c[now]==2:
            print(-1)
            exit()
    exit()

Submission status: Accepted

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

k = 1
ans = 0
m = 0
while k != 2 and m < n:
    k = lis[k - 1]
    m += 1
if k == 2:
    print(m)
else:
    print("-1")

Submission status: Accepted

Solution code:

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

import numpy as np

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return np.array(x)

N = int(input())

a = int_mtx(N)
visit = [0]*(N+1)
now = 1
visit[now] = 1

ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
Pushed = [False]*N
Pushed[0] = True
i = 0
cnt = 0
while True:
    i = A[i] - 1
    cnt += 1
    if Pushed[i]:
        print(-1)
        break
    Pushed[i] = True
    if i == 1:
        print(cnt)
        break

Submission status: Accepted

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

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

i = 1
ans = 0
flg = False
while A[i] != 0 and not flg:
    if i == 2:
        flg = True
    else:
        ans += 1
        tmp = i
        i = A[i]
        A[tmp] = 0

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
seen = set()
p = 1
while True:
    if not p in seen:
        seen.add(p)
        if p == 2:
            print(len(seen) - 1)
            break
        p = A[p-1]
    else:
        print(-1)
        break

Submission status: Accepted

Solution code:
from sys import exit

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

visited = [False] * n
res = 0
button = 0
while True:
    visited[button] = True
    button = a[button] - 1
    res += 1

    if visited[button]:
        print(-1)
        exit()
    
    if button == 1:
        print(res)
        exit()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
 
i = 0
m = 1
while(1):
    if a[i] == 2:
        ans = m
        break
 
    if m >= N:
        ans = -1
        break
 
    i = a[i] - 1 
    m += 1

print(ans)

Submission status: Accepted

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

i = 0
m = 1
while(1):
    if a[i] == 2:
        ans = m
        break

    if m >= N:
        ans = -1
        break

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    buttons = {}
    for i in range(N):
        buttons.update({i+1:int(input())})
    # print(buttons)
    p = 1
    ans = 0
    while ans <= N:
        p = buttons[p]
        ans += 1
        if p == 2:
            print(ans)
            return
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
Row = int(input())
List = []
for i in range (Row):
  List.append(int(input()))
s = 1
res = 0
flag = False
for i in range(Row):
  s = List[s-1]
  res += 1
  if s == 2:
    flag=True
    break
if not flag:
  print(-1)
else:
  print(res)

Submission status: Accepted

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

i = 0
c = 0
flag = 0
for _ in range(N):
    i = a[i]
    c += 1
    if i == 1:
        flag = 1 
        break

ans = c if flag else -1
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = [int(input()) - 1 for _ in range(N)]
    flgs = [False for _ in range(N)]
    flgs[0] = True
    i = A[0]
    flgs[i] = True
    cnt = 1
    while not flgs[1]:
        i = A[i]
        if flgs[i]:
            print(-1)
            return
        else:
            flgs[i] = True
            cnt += 1
    else:
        print(cnt)


resolve()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *a = map(int, read().split())
    r = 0
    x = 1
    while x != 2:
        r += 1
        x = a[x - 1]
        if x == 1 or r >= n:
            r = -1
            break
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = []

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

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

print(-1)