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)]
cd = 0
count = 0
while(True) :
    cd = a[cd] - 1
    count += 1
    if cd == 1 :
        print(count)
        break
    if count >= n :
        print(-1)
        break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def buttons(n, paths):
    cont = 0
    button = 0
    for _ in range(n):
        if button == 1:
            return cont
        cont += 1
        button = paths[button]
    return -1


if __name__ == '__main__':
    n = int(input())
    print(buttons(n, [int(input()) - 1 for _ in range(n)]))

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
a.insert(0,0)
count = 0
hold = 1
while hold != 2:
    count += 1
    if a[hold] != 2:
        hold = a[hold]
    elif a[hold] == 2:
        break
    if count > (len(a)-1):
        count = -1
        break
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] * n
for i in range(n): a[i] = int(input()) - 1
i = 0
c = 1
ok = False
s = set()
while a[i] != 1:
    c += 1
    if i in s:
        c = -1
        break;
    s.add(i)
    i = a[i]
print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, list(input() for _ in range(n))))
visited = list(False for _ in range(n))

if (2 in a) == False:
    print(-1)
    exit()
    
idx = 1
cnt = 0
while True:
    cnt += 1
    if a[idx - 1] == 2:
        print(cnt)
        exit()
    else:
        if visited[idx - 1] == True:
            print(-1)
            exit()
        else:
            visited[idx - 1] = True
            idx = a[idx - 1] ##次のボタン

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# ABC 065 B

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

i = 1
s = 0
while i != 2:
    s += 1
    A[i-1], i = 1, A[i-1]
    if i == 1:
        print(-1)
        break
else:
    print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
ans=0
p=1
chk=[0]*n
l=[]
for i in range(n):
    l.append(int(input()))

while 1:
    if chk[p-1]==1:
        print(-1)
        break
    chk[p-1]=1
    ans+=1
    if l[p-1]==2:
        print(ans)
        break
    p=l[p-1]

Submission status: Accepted

Solution code:
N = int(input())

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

ans = -1
btn = 1
for i in range(1, N + 1):
    btn = A[btn - 1]
    if btn == 2:
        ans = i
        break

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = [int(input()) for i in range(n)]
num,y,a = 0,set([1]),1
while num != len(y) and 2 not in y:
	num,a = len(y),x[a-1]
	y.add(a)
if 2 in y: print(len(y)-1)
else: print(-1)

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem B

if __name__ == '__main__':
    button_count = int(input())
    button_rules = {i + 1: int(input()) for i in range(button_count)}

    count = 1
    result = button_rules[count]

    while (result != 2) and (count < button_count):
        result = button_rules[result]
        count += 1

    if count < button_count:
        print(count)
    else:
        print(-1)

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem B

if __name__ == '__main__':
    button_count = int(input())
    button_rules = {i + 1: int(input()) for i in range(button_count)}

    count = 1
    result = button_rules[count]

    # HACK: More smarter.
    while (result != 2) and (count < 10 ** 5):
        result = button_rules[result]
        count += 1

    if count < 10 ** 5:
        print(count)
    else:
        print(-1)

Submission status: Accepted

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

N = int(input())

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

pushed = set([1])
button_next = dict[1]
while True:
    if button_next == 2:
        print(len(pushed))
        break
    elif button_next not in pushed:
        pushed.add(button_next)
        button_next = dict[button_next]
    else:
        print(-1)
        break

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
current = 1
count = 0
ans = -1

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lis = []
p = 1
s = -1
for i in range(n):lis.append(int(input()))
for m in range(n):
    p = lis[p-1]
    if p == 2:
        s = m + 1
        break
print(s)

Submission status: Accepted

Solution code:
n=int(input())
a = [int(input()) for x in [0]*n]
k=0
for i in range(n):
    if k==1:
        print(i)
        break
    k=a[k]-1
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n=int(input())
ai=[]
for i in range(n):
    ai.append(int(input()))
    
already=[0 for j in range(n)]
i=0
dist=0
while(1):
    if(dist>n):
        print(-1)
        sys.exit()
    i=ai[i]-1
    if(i==1):
        break
    dist+=1
print(dist+1)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
 
T=[0]*N
t=0
for i in range(N):
    if t==1: break
    T[t]+=1
    t=A[t]-1
 
 
if t==1: print(sum(T))
else: print(-1)

Submission status: Accepted

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

T=[0]*N
t=0
for i in range(N):
    if t==1: break
    T[t]+=1
    t=A[t]-1


if t==1: print(sum(T))
else: print(-1)

Submission status: Accepted

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

a = [0]+A
visit = [False for _ in range(N+1)]
q = [1]
length = 0
while True:
    to = q.pop()
    if visit[to]:
        print('-1')
        break
    if to==2:
        print(length)
        break
    visit[to] = True
    q.append(a[to])
    length += 1

Submission status: Accepted

Solution code:

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

i = 1
visited = set()
steps = 0
while i!= 2 and i not in visited:
    visited.add(i)
    i = d[i]
    steps += 1

if i == 2:
    print(steps)
else:
    print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a = [int(input()) for x in [0]*n]
k=0
for i in range(n):
    if k==1:
        print(i)
        break
    k=a[k]-1
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
a = {(i+1): input() for i in range(N)}
p = 1
ans = -1
for k in range(N):
    p = a[p]
    if p == 2:
        ans = k+1
        break
print ans

Submission status: Accepted

Solution code:
N = int(input())

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

now = 1
Ans = -1
count = 0

for i in range(len(lis)):
	now = lis[now-1]
	count += 1
	if now == 2:
		Ans = count
		break

print(Ans)

Submission status: Accepted

Solution code:
N = int(input())
button_list = []
for _ in range(N):
    button_list.append(int(input()))
ans = -1
p = 1
s = 0
for i in range(N):
    p = button_list[p-1]
    s += 1
    if p == 2:
        ans = s
        break
print(ans)

Submission status: Accepted

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

    visited_flag = [False for _ in range(N)]
    cnt = 1
    i = 0
    while 1:
        if A[i] == 2:
            print(cnt)
            break
        else:
            i = A[i]  - 1
            if visited_flag[i] == True:
                print(-1)
                break
            visited_flag[i] = True
            cnt += 1


solve()

Submission status: Accepted

Solution code:
n = int(input())
btn = []
checked = set()

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

def find(n):
  global btn, checked
  j = 1
  for i in range(n):
    if btn[j - 1] == 2:
      # count += 1
      return len(checked) + 1
    elif j - 1 in checked:
      return -1
    else:
      checked.add(j - 1)
      j = btn[j - 1]
  return -1

print(find(n))

Submission status: Accepted

Solution code:
a, v, c = [input() for _ in xrange(input())], {1}, 1
while True:
    c = a[c - 1]
    if c in v:
        print -1
        exit()
    if c == 2:
        print len(v)
        exit()
    v.add(c)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    a = [0 for i in range(N)]
    for i in range(N):
        a[i] = int(input())
    on_light = 1
    cnt = 0
    for i in range(N):
        on_light = a[on_light-1]
        cnt += 1
        if on_light == 2:
            print(cnt)
            return
    print(-1)
    return
    
    
solve()

Submission status: Accepted

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

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

dp = [False]*(N+1)

ans = 0
ind = 1
while True:
    if ind == 2:
        print(ans)
        break
    else:
        if dp[ind]:
            print(-1)
            break
        else:
            dp[ind] = True
            ans += 1
            ind = a[ind]

Submission status: Accepted

Solution code:


num = int(input())

array = []
for i in range(num):
    array.append(int(input()))

res = -1
if 2 in array:
    judge = array[0]
    roop = 1
    while judge != 2 and roop != 100001:
        roop += 1
        judge = array[judge-1]
        if judge == 2:
            break
else:
    judge = 10000000

if judge == 2:
    res = roop

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = []
for i in range(n):
    A.append(int(input()))
flag = -1
b = 0
for i in range(n):
    if A[b] == 2:
        flag = i+1
        break;
    else:
        b = A[b]-1
print(flag)

Submission status: Accepted

Solution code:
n = int(input())
a = []
a.append(1)
for i in range(n):
	x = int(input())
	a.append(x)
y = 0
check = True
for i in range(n+1):
	y = a[y]
	if a[y] == 2:
		print(i+1)
		check = False
		break
if check:
	print('-1')

Submission status: Accepted

Solution code:
N = int(raw_input())
a = [int(raw_input()) for i in xrange(N)]
i = 0
ans = 0
while True:
    if a[i] == -1:
        break
    ni = a[i] - 1
    a[i] = -1
    i = ni
    ans += 1
    if i == 1:
        break
print ans if a[i] != -1 else -1

Submission status: Accepted

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


n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
if 2 not in a:
    print(-1)
else:
    cnt = 0
    before_num = 1
    lighted_bottons = [0 for i in range(n + 1)]
    while True:
        cnt += 1
        if a[before_num - 1] == 2:
            print(cnt)
            break
        else:
            if lighted_bottons[a[before_num - 1]] == 1:
                print(-1)
                break
            before_num = a[before_num - 1]
            lighted_bottons[before_num] = 1

Submission status: Accepted

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

checked = [0 for _ in range(n)]

cnt = 0
light = 0

while(True):
    if checked[light]:
        print(-1)
        break
    checked[light] = True
    light = inp[light] - 1
    cnt += 1
    if light == 1:
        print(cnt)
        break

Submission status: Accepted

Solution code:
n = int(raw_input())

aa = []
for i in range(n):
    t = int(raw_input())
    aa.append(t)

cnt = 0
b = dict({1: True})
a = aa[0]
while 1:
    cnt += 1
    if a == 2:
        print cnt
        exit()
    a = aa[a-1]
    if a in b:
        print -1
        exit()
    b[a] = True
    if cnt == n:
        print -1
        exit()

Submission status: Accepted

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

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

i=1
c=0

while(1):
    if i == 2:
        print c
        break
    elif c >= N:
        print -1
        break
    c+=1
    i=a[i-1]

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    N = int(input())
    A = []
    for x in range(N):
        y = int(input())
        y -= 1
        A.append(y)
    pos = 0
    cnt = 0
    visited = set()
    while (cnt <= N + 1):
        visited.add(pos)
        if (pos == 1):
            print (cnt)
            exit()
        pos = A[pos]
        cnt += 1
        if pos in visited:
            break

    print (-1)

Submission status: Accepted

Solution code:
import sys
N = int(input())
ls = []
for i in range(N):
    ls.append(int(input()))
i = 0
cnt = 1
while(cnt <= N):
    k = ls[i]
    if (k == 2):
        print(cnt)
        sys.exit()
    cnt += 1
    i = k-1
print(-1)

Submission status: Accepted

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

index = 0
count = 0


while True:
    count += 1
    check[index] = 0
    index = a[index]

    if check[index] == 0:
        print(-1)
        break
    if index == 1:
        print(count)
        break

Submission status: Accepted

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

past = [0 for _ in range(n)]

cnt = 0
now = 1

while(True):
	if past[now-1] == 1:
		print(-1)
		exit(0)
	if now == 2:
		print(cnt)
		exit(0)
	past[now-1] = 1
	cnt += 1
	now = a[now-1]

Submission status: Accepted

Solution code:
#x,a,b=[int(i) for i in input().split()]

N=int(input())
a=[int(input()) for i in range(N)]
light=[0 for i in range(N)]
light[0]=1
push=0
botton=0

for k in range(N):

 light[botton]=0
 light[a[botton]-1]=1

 botton=a[botton]-1
 push=push+1
 if light[1]==1:
   print(push)
   break




if light[1]!=1:
  print(-1)

Submission status: Accepted

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

now = 0
cnt = -1
for i in range(1, N):
  now = a[now]
  if(now == 1):
    cnt = i
    break

print(cnt)

Submission status: Accepted

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

used = set()
now = 1
ret = 0
while True:
    if now == 2:
        print(ret)
        exit()
    elif now in used:
        print(-1)
        exit()
    used.add(now)
    now = a[now - 1]
    ret += 1

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://abc065.contest.atcoder.jp/tasks/abc065_b

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    N = int(input())
    a = [int(input()) for _ in range(N)]
    a.insert(0, '@')            #  1起算にするためのダミー


    possible = False
    next_btn = a[1]
    visited = set([1])
    while next_btn not in visited:
        if next_btn == 2:
            possible = True
            break
        visited.add(next_btn)
        next_btn = a[next_btn]
    if possible:
        print(len(visited))
    else:
        print(-1)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    lst = [int(input())-1 for _ in[0]*n]
    visit = set()
    current = 0
    while True:
        visit.add(current)
        current = lst[current]
        if current == 1:
            print(len(visit))
            break
        if current in visit:
            print(-1)
            break

if __name__=="__main__":
    solve()

Submission status: Accepted

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

btn = 0
hist = set()
ans = 0
while True:
    if btn == 1:
        break

    if btn in hist:
        ans = -1
        break

    hist.add(btn)
    btn = a[btn]
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
num = a[0]
ans = 0
check = -1
for n in range(0, N+2):
    if num == 2:
        ans = n + 1
        check = 1
        break
    num = a[num - 1]
if check != 1:
    print(check)
else:
    print(ans)

Submission status: Accepted

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

n = 1
saw = [0] * N
now = 1

while True:
    if a[now-1] == 2:
        break
    else:
        if saw[a[now-1]-1] == 1:
            n = -1
            break
        else:
            now = a[now-1]
            saw[now-1] = 1
            n += 1

print(n)

Submission status: Accepted

Solution code:
import sys

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

x = 1
i = 0

while i < n and x != 2:
    x = a[x-1]
    i += 1

if x == 2:
    print(i)
else:
    print(-1)

Submission status: Accepted

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

x = 1
i = 0

while i < n and x != 2:
    x = a[x-1]
    i += 1

if x == 2:
    print(i)
else:
    print(-1)

Submission status: Accepted

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

x = 1
y = [0] * n

while y[x-1] != 2 and x != 2:
    y[x-1] += 1
    x = a[x-1]

if x == 2:
    print(y.count(1))
else:
    print(-1)

Submission status: Accepted

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

x = 1
i = 0

while i < 100000 and x != 2:
    x = a[x-1]
    i += 1

if x == 2:
    print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:

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

s = set()
v = 1
for i in range(1,N+1):
    if v == 2:
        print(len(s))
        break
    elif v in s or i == N:
        print(-1)
        break
    else:
        s.add(v)
        v = a[v]

Submission status: Accepted

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

    idx, flag = 1, 1
    for i in range(1, n+1):
        idx = a_list[idx-1]
        if idx == 2:
            print(i)
            flag = 0
            break

    if flag: print(-1)
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

a_n=[int(input()) for i in range(N)]
import sys

i=1
count=0
for k in range(N):
    if a_n[i-1]==2:
        count+=1
        print(count)
        sys.exit()
    else:
        i=a_n[i-1]
        count+=1

print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) if(i) else 0 for i in range(n+1)]
distance = [None] * (n+1)
queue = [(1, 0)]
while(len(queue)):
    p, d = queue.pop()
    if(p==2):
        print(d)
        break
    if(distance[p]!=None or d==100001):
        print(-1)
        break
    distance[p] == d
    nextp = a[p]
    queue.append((nextp, d+1))

Submission status: Accepted

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

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

Submission status: Accepted

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

now = 1
count = 0

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

if count == N:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

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

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

i = 0
j = 0
count = 0
ans = 0

while i >= 0:
    if i == 1:
        ans += 1
        break
    else:
        j = i
        i = li[i] - 1
        count += 1
        li[j] = 0
if ans == 1:
    print(count)
else:
    print(-1)

Submission status: Accepted

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

    a = list(A)
    ans = 0
    try:
        idx = a.index(1)
    except:
        idx = 0
        ans += 1

    while True:
        btn = a[idx]
        if btn == 0:
            ans = -1
            break
        elif btn == 2:
            break
        a[idx] = 0
        idx = btn - 1
        ans += 1

    print(ans)

main()

Submission status: Accepted

Solution code:
import collections

N = int(input())
a = collections.deque()
for i in range(N):
    a.append(int(input()))
    
s = a[0]
cnt = 1

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

Submission status: Accepted

Solution code:
import sys

N = int(input())
a = [int(i) for i in sys.stdin]
    
s = a[0]
cnt = 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = 0
s = a[0]

if s == 2:
    print(cnt + 1)
else:
    for i in range(N):
        s = a[s-1]
        cnt += 1
        if s ==  2:
            print(cnt + 1)
            break
        if i == N-1:
            print('-1')

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
bot = [ int(i) for i in sys.stdin ]
now = bot[1]
count = 1
for i in range(bot[0]):
    if now == 2:
        print(count)
        exit()
    count += 1
    now =  bot[now]
print(-1)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys

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

#print(d)

q = deque()
q.append(1)

point = 1
count = 1

while N > 0:
    if d[point] == 2:
        print(count)
        sys.exit()
        
    q.append(d[point])
    point = d[point]
    count += 1
    N -= 1

print(-1)

Submission status: Accepted

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

while True:
    
    if light_offset == 1:
        print(cnt)
        break
    light_offset = buttons[light_offset] - 1
    cnt += 1
    
    if cnt > N:
        print(-1)
        break

Submission status: Accepted

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))

n = ri()
s = [-1]
ans = 0
light = 1
flag = False
for i in range(n):
    s.append(ri())
for i in range(n):
    ans += 1
    light = s[light]
    if(light == 2):
        flag = True
        break
print(ans if(flag) else -1)

Submission status: Accepted

Solution code:
n=int(input())
arr=[]
for _ in range(n):
    arr.append(int(input())-1)
cur=0
for i in range(n):
    nxt = arr[cur]
    if nxt==0:
        print(-1)
        break
    elif nxt==1:
        print(i+1)
        break
    cur=nxt
else:
    print(-1)

Submission status: Accepted

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

import sys
import re
import math
import collections
import itertools
import functools

DEBUG = True
DEBUG = False

def dbg(*args):
    if DEBUG:
        print("DBG: ", file=sys.stderr, end="")
        print(*args, file=sys.stderr)



def main():
    N = int(input())

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

    pressed = [False] * (N+1)
    button = 1
    cnt = 0
    while not pressed[button]:
        pressed[button] = True
        cnt += 1
        button = A[button]
        if button == 2:
            print(cnt)
            return

    print(-1)

if __name__ == "__main__": main()

Submission status: Accepted

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

count=0
next_push=0
while(next_push != 1 and count < 1e5+1):
    next_push = a[next_push]
    count += 1

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

Submission status: Accepted

Solution code:
import sys
a = [int(i) for i in sys.stdin]
now = a[1]
count = 0
while count!=a[0]:
    count += 1
    if now == 2:
        print(count)
        exit()
    now = a[now]
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    N = int(input())
    a_list = [int(input()) for _ in range(N)]
    used = set()
    now = 1
    ans = 0
    while True:
        if now == 2:
            print(ans)
            return
        now = a_list[now - 1]
        if now in used:
            print(-1)
            return
        used.add(now)
        ans += 1

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
i = 0
used = set([0, 1])
while True:
    if ans != 0 and i in used:
        break
    used.add(i)
    i = A[i]
    ans += 1
if i != 1:
    ans = -1
print ans

Submission status: Accepted

Solution code:
N = int(input())

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

now = 0
ans = 0
for i in range(N):
    if now == 1:
        ans = i
        break
    now = jmps[now]

ans = ans or -1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

now = 0
ans = 0
for i in range(N):
    if now == 1:
        ans = i
        break
    now = jmps[now]

ans = ans or -1
print(ans)

Submission status: Accepted

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

idx = 0
success = False
for i in range(N):
    idx = a[idx]-1
    if idx == 1:
        print("%d"%(i+1))
        success = True
        break
        
if success == False:
    print("%d"%(-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = []

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

i = 0
now = 0
while True:
    if now == 1:
        print(i)
        break
    if i == N:
        print("-1")
        break
    now = a[now] - 1
    i = i + 1

Submission status: Accepted

Solution code:
# coding: utf-8

n = int(input())
ls = []
for i in range(n):
    ls.append(int(input()))
lighted = [False for i in range(n)]
inx = 1
lighted[0] = True
cnt = 0
while 1:
    dest = ls[inx - 1]
    if not lighted[dest - 1]:
        if dest == 2:
            cnt += 1
            break
        else:
            lighted[dest - 1] = True
    else:
        cnt = -1
        break
    inx = dest
    cnt += 1
    
print(cnt)

Submission status: Accepted

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

flag = False
push = button[0]
next_b = button[push-1]
cnt = 0
for i in range(n):
    cnt += 1
    if push == 2:
        flag = True
        break
    push = button[push-1]
    next_b = button[push-1]

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

Submission status: Accepted

Solution code:

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

visited[0] = True
cur = 1
cnt = 0
while True:
    cur = a[cur-1]
    cnt = cnt + 1
    if cur == 2:
        print(cnt)
        break
    elif visited[cur-1]:
        print(-1)
        break
    else:
        visited[cur-1] = True

Submission status: Accepted

Solution code:
from sys import stdin 
a = [0]
n = int(stdin.readline())
for _ in xrange(n):
 a.append(int(stdin.readline()))
ans = -1
cur = 0
x = 1
s = set()
while True:
 if x in s:
  break
 if x==2:
  ans = cur
  break
 s.add(x)
 cur+=1
 x = a[x]
print ans

Submission status: Accepted

Solution code:
def solve(nexts):
    pushed = {0}
    now_on = 0
    count = 0
    while True:
        now_on = nexts[now_on]
        count += 1
        if now_on in pushed:
            return -1
        if now_on == 1:
            return count
        pushed.add(now_on)


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

Submission status: Accepted

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

vis = [False for _ in range(n)]

ans = 0
now = 0
while True:
    if vis[now]:
        ans = -1
        break

    vis[now] = True
    ans += 1
    now = a[now]
    if now == 1:
        break

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) - 1 for _ in range(N)]
now = 0
se = set([])
ans = 0

while True:
    if now == 1:
        break
    now = A[now]
    if now in se:
        print(-1)
        exit()
    se.add(now)
    ans += 1
print(ans)