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: Wrong Answer

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:
            res = roop
            break


print(res)

Submission status: Wrong Answer

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 <= 100000:
        roop += 1
        judge = array[judge-1]
        if judge == 2:
            res = roop
            break


print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 = []
    while True:
        cnt += 1
        if a[before_num - 1] == 2:
            print(cnt)
            break
        else:
            if a[before_num - 1] in lighted_bottons:
                print("li")
                print(-1)
                break
            before_num = a[before_num - 1]
            lighted_bottons.append(before_num)

Submission status: Wrong Answer

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 = []
    while True:
        cnt += 1
        if a[before_num - 1] == 2:
            print(cnt)
            break
        else:
            if before_num - 1 in lighted_bottons:
                print(-1)
                break
            before_num = a[before_num - 1]
            lighted_bottons.append(before_num - 1)

Submission status: Wrong Answer

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 = []
    while True:
        cnt += 1
        if a[before_num - 1] == 2:
            print(cnt)
            break
        else:
            if before_num in lighted_bottons:
                print(-1)
                break
            before_num = a[before_num - 1]
            lighted_bottons.append(before_num - 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

a_n=list(map(int,input().split()))

if 2 not in a_n:
    print(-1)
else:
    for i in range(N):
        if a_n[i]==2:
            print(i+1)
            break

Submission status: Wrong Answer

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

now = 1
count = 0
loop = int(N * (N -1)/2)

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

if count == loop:
    print(-1)

Submission status: Wrong Answer

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

s = a[0]

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

Submission status: Wrong Answer

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

push_list=[0]
next_push = 0
count=1
while(True):
    next_push = a[next_push]
    if a[next_push] == 1:
        count+=1
        print(count)
        break
    if next_push in push_list:
        print("-1")
        break
    push_list.append(next_push)
    count+=1

Submission status: Wrong Answer

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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
N = int(input())
trans = [int(input()) for i in range(N)]

pushed = [0]
for i in range(10^5 + 1):
	next_button = trans[pushed[-1]]
	if next_button  == 2:
		print(len(pushed))
		break
	elif next_button - 1 in pushed:
		print(-1)
		break
	else:
		pushed.append(next_button - 1)

Submission status: Wrong Answer

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
    i = A[i]
    used.add(i)
    ans += 1
if i != 1:
    ans = -1
print ans

Submission status: Wrong Answer

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

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

act = a[0]
count = 0

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

Submission status: Wrong Answer

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

count = 1
n = a[0]
if n == 2:
    print(0)
    sys.exit()

a[0] = -1

while 1:
    if a[n-1] == -1:
        count = -1
        break
    elif a[n-1] == 2:
        count += 1
        break
    else:
        tmp = a[n-1]
        a[n-1] = -1
        n = tmp
        count += 1

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
buttons = [int(input()) for i in range(n)]
pushing = 1
counter = 0
while counter >= n:
    pushing = buttons[pushing - 1]
    counter += 1
    if pushing == 2:
        print(counter)
        break
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
buttons = [int(input()) for i in range(n)]
pushing = 1
counter = 0
while counter >= n:
    pushing = buttons[pushing]
    counter += 1
    if pushing == 2:
        print(counter)
        break
else:
    print(-1)

Submission status: Wrong Answer

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

current = buttons[1]

for i in range(n):
    counter += 1
    if current == 2:
        print(counter)
        break
    elif current == 0:
        print(-1)
        break
    next_button = buttons[current]
    buttons[current] = 0
    current = next_button

Submission status: Wrong Answer

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

current = buttons[1]

for i in range(n):
    counter += 1
    next_button = buttons[current]
    buttons[current] = 0
    if next_button == 0:
        print(-1)
        break
    elif next_button == 2:
        print(counter)
        break
    current = next_button

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = []
for i in range(N):
	a.append(raw_input())
#print a
for i in range(N):
	a[i] = int(a[i])
cnt = 0
trn = 0
while trn != 1:
	if trn == -2:
		cnt = -1
		break
	print a

	hako = trn
	trn = a[trn] - 1
	a[hako] = -1
	cnt += 1
	
print cnt

Submission status: Wrong Answer

Solution code:
import sys


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

    def push(x):
        return buttons[x - 1]

    seq = [push(1)]

    for _ in range(n):
        if seq[-1] == 2:
            print(len(seq))
            return 0
        seq += [push(seq[-1])]

    return 0


if __name__ == '__main__':
    sys.exit(main())

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
x = [False]*N
n,i = (0, 0)
while x[n] == False:
    x[n] = True
    n = a[n]-1
    i+=1
    if n==2:
        print(i)
        break
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = input()
b = []
b_old = []
tmp = input()
b.append(int(tmp))
n_b = b[0]
flag = 0
if n_b == 2:
    ans = 1
    flag = 1
count = 1
add = 0
for i in range(int(n)-1):
    tmp = input()
    b.append(int(tmp))
    if i+1 > n_b and flag == 0:
        count += 1
        n_b = b[n_b - 1]
        if n_b == 2:
            ans = count
            flag = 1
        elif n_b in b_old:
            ans = -1
        else:
            b_old.append(n_b)
    else:
        add += 1
if flag == 0:
    for i in range(add):
        count += 1
        n_b = b[n_b - 1]
        if n_b == 2:
            ans = count
            flag = 1
        elif n_b in b_old:
            ans = -1
        else:
            b_old.append(n_b)

    if flag == 0:
        ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
if not(2 in a):
    print(-1)

x = np.zeros(N+1, dtype=np.int8)
x[0] = a[0]
for i in range(N):
    if x[i] == 2: break
    if a[x[i]-1] in x:
        i = N
        break
    x[i+1] = a[x[i]-1]
if i+1 < N: print(i+1)
else: print(-1)

Submission status: Wrong Answer

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

tmp=0
i=0
cnt=0
 
while i != 1 and cnt < 10000:
    tmp = a[i]
    cnt += 1
    i = tmp-1

if cnt<10000:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

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

tmp=0
i=0
cnt=0
 
while i != 1 and cnt < 1000:
    tmp = a[i]
    cnt += 1
    i = tmp-1

if cnt<1000:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
cnt = 0
for i in range(n-1):
    x = int(input())
    if x == 2:
        break
    else:
        cnt += 1
if cnt == n:
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
ch = [0] * (10**5 + 1)
if a[0] == 1 or a[1]  == 2:
    print(-1)
numB = 1
ch[1] = numB
c = 1
while True:
    numB = a[numB-1]
    if numB == 2:
        print(c)
        break
    if ch[numB] == 1:
        print(-1)
        break
    c += 1
    ch[numB] = 1

Submission status: Wrong Answer

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

count = 1
s = b[0]
if s == 2:
    print(count)
b_old.append(s)
n_b = s
flag = 0
for i in range(int(n)):
    count += 1
    n_b = b[n_b-1]
    if n_b == 2:
        print(count)
        flag = 1
        break
    elif n_b in b_old:
        print("-1")
        flag = 1
        break
    else:
        b_old.append(n_b)

if flag == 0:
    print("-1")

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

num_button = 1
now_light = a[0]-1
pushed = set()
while True:
    if now_light in pushed:
        print(-1)
        break

    pushed.add(now_light)
    now_light = a[now_light]-1
    num_button += 1
    if now_light == 1:
        print(num_button)
        break

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
  N = int(input())
  al = [0,]
  for _ in range(N):
    al.append(int(input()))
  bl = [False]*(N+1)
  i = 1
  nb = 0
  while bl[i] == False:
    bl[i] = True
    i = al[i]
    nb += 1
  if bl[2] == False:
    print(-1)
  else:
    print(nb-1)

main()

Submission status: Wrong Answer

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

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

b=[]
for i in range(N+1):
    b.append(1)

index=1
counter=1
while True:
    counter+=1
    b[index]=-1
    index = a[index]
    if b[index]==-1:
        print(-1)
        break
    if a[index]==2:
        print(counter)
        break

Submission status: Wrong Answer

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

while flag == 0:
	a.append(A[a[i]-1])
	i += 1
	if a[i] == 2:
		print( i+1 )
		flag = 1
	else:
		b = set(a)
		if len(a) != len(b):
			print( -1 )
			flag = 1

Submission status: Wrong Answer

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

count = 1
s = b[0]
if s == 2:
    print(count)
b_old.append(s)
n_b = s
while(True):
    count += 1
    n_b = b[n_b-1]
    if n_b == 2:
        print(count)
        break
    elif n_b in b_old:
        print("-1")
        break
    else:
        b_old.append(n_b)

Submission status: Wrong Answer

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

    index = 0
    count = 0
    number = 1
    flags = [0 for _ in range(N)]
    flags[index] = 1
    while(number != 2):
        number = a[index]
        index = number - 1
        count += 1
        print(flags)
        if flags[index] == 1:
            break
        else:
            flags[index] = 1

    if number == 2:
        print(count)
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
c = 1
tf = False
for i in range(N):
    c = a[c-1]
    if c == 2:
        print(i+1)
        tf = True
if not tf:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
al = [0]
for _ in range(N):
  al.append(int(input()))
bl = [0]*(N+1)
i = 1
nb = 0
while bl[i] == 0:
  bl[i] = 1
  i = al[i]
  nb += 1
if bl[2] == 0:
  print(-1)
else:
  print(nb-1)

Submission status: Time Limit Exceeded

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

btn = 0
for _ in range(n):
  if btn in visited:
    break
  visited.append(btn)
  btn = a[btn]-1
  count += 1
  if btn==1:
    ans = count
    break
    
print(ans)

Submission status: Time Limit Exceeded

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

btn = 0
while True:
  if btn in visited:
    print('-1')
    break
  visited.append(btn)
  btn = a[btn]-1
  count += 1
  if btn==1:
    print(count)
    break

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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

    num_count = 1

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

        if current_num == 1:
            print(-1)
            return

        if current_num == 2:
            print(num_count)
            return

        num_count += 1

run()

Submission status: Time Limit Exceeded

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

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

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

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

    num_history = [1]

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

        if current_num == 1:
            print(-1)
            return

        if current_num == 2:
            print(len(num_history))
            return

        num_history.append(current_num)

run()

Submission status: Time Limit Exceeded

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

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

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

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

    num_history = [1]

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

        if current_num in num_history:
            print(-1)
            return

        if current_num == 2:
            print(len(num_history))
            return

        num_history.append(current_num)

run()

Submission status: Time Limit Exceeded

Solution code:
def main():
  n=int(input())
  A=[int(input()) for i in range(n)]
  L = [1]
  crt=1
  for i in range(n):
    crt = A[crt-1]
    if crt in L:
      print(-1)
      break
    if crt == 2:
      print(len(L))
      break
    L.append(crt)
main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
L = [1]
crt=1
for i in range(n):
  crt = A[crt-1]
  if crt in L:
    print(-1)
    break
  if crt == 2:
    print(len(L))
    break
  L.append(crt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
L = [1]
crt=1
for i in range(n):
  crt = A[crt-1]
  if crt in L:
    print(-1)
    break
  if crt == 2:
    print(len(L))
    break
  L.append(crt)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

tmp = 0
count = 1
tmp = a[0]

while tmp != 2 and tmp != 1:
    count += 1
    tmp = a[tmp-1]

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

Submission status: Time Limit Exceeded

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

    pressed = []
    i = 0
    t = 0
    while i != 1:
        i = A[i]
        if i in pressed:
            t = -1
            break
        pressed.append(i)
        t += 1

    print(t)
    
resolve()

Submission status: Time Limit Exceeded

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

while True:
    if now == 1:
        print(-1)
        break
    if now == 2:
        print(ans)
        break
    now = a[now-1]
    ans += 1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
import sys
nyuryoku = [int(i) for i in sys.stdin]

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

"""
a_list=[]

for _ in range(N):
    a_list.append(int(input()))
"""


cnt_push = 1
button_list = []
cur_but_posi = a_list[0]

while True:
    #cur_but_posi = a_list[cur_but_posi - 1]
    if cur_but_posi == 2:
        print(cnt_push)
        break
    elif cur_but_posi in button_list:
        print(-1)
        break
    else:
        button_list.append(cur_but_posi)
        cur_but_posi = a_list[cur_but_posi - 1]    
        cnt_push += 1

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
"""
a_list=[]

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


cnt_push = 1
button_list = []
cur_but_posi = a_list[0]

while True:
    #cur_but_posi = a_list[cur_but_posi - 1]
    if cur_but_posi == 2:
        print(cnt_push)
        break
    elif cur_but_posi in button_list:
        print(-1)
        break
    else:
        button_list.append(cur_but_posi)
        cur_but_posi = a_list[cur_but_posi - 1]    
        cnt_push += 1

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a_list=[]

for _ in range(N):
    a_list.append(int(input()))

cnt_push = 1
button_list = []
cur_but_posi = a_list[0]

while True:
    #cur_but_posi = a_list[cur_but_posi - 1]
    if cur_but_posi == 2:
        print(cnt_push)
        break
    elif cur_but_posi in button_list:
        print(-1)
        break
    else:
        button_list.append(cur_but_posi)
        cur_but_posi = a_list[cur_but_posi - 1]    
        cnt_push += 1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

a = A[0]
while a != 2: 
  if a == 1:
    print(-1)
    exit()
  a = A[a-1]
  cnt += 1
  if a == 1:
    print(-1)
    exit()
print(cnt)

Submission status: Time Limit Exceeded

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

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

a = A[0]
while a != 2: 
  a = A[a-1]
  cnt += 1
  if a == 1:
    print(-1)
    exit()
print(cnt)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [0 for _ in range(n)]
already = []
next = 1
c = 0
for i in range(n):
  a = int(input())
  l[i] = a
while c <= n:
  next = l[next-1]
  c += 1
  if next == 2:
    print(c)
    break
  elif next in already:
    print(-1)
    break
  already.append(next)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = []
already = []
next = 1
c = 0
for i in range(n):
  a = int(input())
  l.append(a)
while True:
  next = l[next-1]
  c += 1
  if next == 2:
    print(c)
    break
  elif next in already:
    print(-1)
    break
  already.append(next)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = []
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif A[push] in count:
      break
      
    else:
      count.append(push)
      push = A[push]
        
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N+1):

    if A[push] == 1:
        ans = i+1
        break
        
    elif A[push] in count:
      break
      
    else:
        push = A[push]
        count.append(push)
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

count = []
n = 0
ans = 1
for _ in range(N+1):
  if A[n] == 1:
    break
 
  elif A[n] in count:
    ans = -1
    break
  
  else:
    count.append(A[n])
    ans += 1
    n = A[n]
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
for _ in range(N):
  A.append(int(input())-1)
#[int(input())-1]
 
count = []
n = 0
ans = 1
for k in range(N+1):
  if A[n] == 2-1:
    break
 
  elif A[n] in count:
    ans = -1
    break
  
  else:
    count.append(A[n])
    ans += 1
    n = A[n]
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
for _ in range(N):
  A.append(int(input())-1)
#[int(input())-1]
 
count = [-1]*N
n = 0
ans = 1
for k in range(N+1):
  if A[n] == 2-1:
    break
 
  elif A[n] in count:
    ans = -1
    break
  
  else:
    count[n] = A[n]
    ans += 1
    n = A[n]
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
# A[0]
A = list(map(lambda x: int(x)-1, [input() for n in range(N)]))
#[int(input())-1]
 
count = [-1]*N
n = 0
ans = 1
for k in range(N+1):
  if A[n] == 2-1:
    break

  elif A[n] in count:
    ans = -1
    break
  
  else:
    count[n] = A[n]
    ans += 1
    n = A[n]
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
# A[0]
A = [int(input())-1]
 
count = [-1]*N
n = 0
ans = 1
for k in range(N+1):
  if A[n] == 2-1:
    break
  
  elif A[n]+1 > len(A):
    for _ in range(A[n]+1 - len(A)):
      A.append(int(input()) -1)
    continue
  
  
  elif A[n] in count:
    ans = -1
    break
  
  else:
    count[n] = A[n]
    ans += 1
    n = A[n]
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
# A[0]
A = [int(input())-1]
 
count = [-1]*N
n = 0
ans = 1
for k in range(N+1):
  if A[n] == 2-1:
    break
  
  if A[n]+1 > len(A):
    for _ in range(A[n]+1 - len(A)):
      A.append(int(input()) -1)
    continue
  
  n = A[n]
  if n in count:
    ans = -1
    break
  count[n] = n
  ans += 1
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
# A[0]
A = [int(input())-1]

count = [-1]*N
n = 0
ans = 1
while A[n] != 2-1:
  
  if A[n]+1 > len(A):
    for _ in range(A[n]+1 - len(A)):
      A.append(int(input()) -1)
    continue
  
  n = A[n]
  if n in count:
    ans = -1
    break
  count[n] = n
  ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
#A = [0]*N
A = list(map(lambda x: int(x)-1, [input() for n in range(N)]))

count = [-1]*N
n = 0
ans = 1
while A[n] != 2-1:
  n = A[n]
  if n in count:
    ans = -1
    break
  count[n] = n
  ans += 1
  
print(ans)

Submission status: Time Limit Exceeded

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

for n in range(N):
  A[n] = int(input()) -1
  
count = [-1]*N
n = 0
ans = 1
while A[n] != 2-1:
  n = A[n]
  if n in count:
    ans = -1
    break
  count[n] = n
  ans += 1
  
print(ans)

Submission status: Time Limit Exceeded

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