Problem:


Score : 200 points

Problem Statement

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

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

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

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

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

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

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

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

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3



Sample input:
3
3
1
2


Sample output:
2

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = []
for i in range(n):
	a_list.append(int(input()))
r = -1
num = 1
nums = [1]
count = 0
while True:
	num = a_list[num-1]
	count += 1
	if num in nums:
		break
	if num == 2:
		r = count
		break
	if count > n:
		break
	nums.append(num)
print(r)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = []
for i in range(n):
	a_list.append(int(input()))
r = -1
num = 1
nums = [1]
count = 0
while True:
	num = a_list[num-1]
	count += 1
	if num in nums:
		break
	if num == 2:
		r = count
		break
	nums.append(num)
print(r)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
i = a[0]
num = [1, i]
for k in range(n):
    if 2 in num:
        print(len(num) - 1)
        break
    j = a[i-1]
    if j in num:
        print(-1)
        break
    num.append(j)
    if 2 in num:
        print(len(num) - 1)
        break
    i = a[j-1]
    if i in num:
        print(-1)
        break
    num.append(i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
i = a[0]
num = [1, i]
for j in range(n):
    if 2 in num:
        print(len(num) - 1)
        exit()
    i = a[i-1]
    if i in num:
        print(-1)
        exit()
    num.append(i)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
b = [0] * n
ans = 0
c = 0
b[0] = 1
while True:
    c = a[c] - 1
    ans += 1
    if b[c] == 1:
        ans = -1
        break
    if c == 1:
        break
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
table = {}
for i in range(n):
    a = int(input())
    table[i] = a-1

is_visited = [ False for _ in range(n) ]
is_visited[0] = True

cnt = 0
now = 0
while True:
    if now == 1:
        print(cnt)
        break
    nxt = table[now]
    if is_visited[nxt]:
        print(-1)
        break
    now = nxt
    cnt += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

count = 0
btn = 1

while True:
    if btn == 2:
        print(count)
        break
    elif btn == 1 and count != 0:
        print(-1)
        break
    else:
        count += 1
        btn = btn_list[btn]

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

dic = {}
for n in range(1,N+1):
  dic[n] = int(input())
  
lis = []
now = 1
count = 0
flag = False
while True:
  #count += 1
  if now in lis:
    break
  if dic[now] == 2:
    flag = True
    break
  lis.append(now)
  now = dic[now]

if flag:
  print(len(lis)+1)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

dic = {}
for n in range(1,N+1):
  dic[n] = int(input())
  
lis = []
now = 1
count = 0
flag = False
while True:
  count += 1
  if now in lis:
    break
  if dic[now] == 2:
    flag = True
    break
  lis.append(now)
  now = dic[now]
  
if flag:
  print(count)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(input()) for i in range(n)]
ans = 1
light = 0
list = []

while True:
    if A[light] in list:
        ans = -1
        break
    elif A[light] == 2:
        break
    else:
        ans = ans + 1
        list.append(light + 1)
        light = A[light] - 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(input()) for i in range(n)]
ans = 1
light = 0
list = []

while True:
    if A[light] == 1:
        ans = -1
        break
    elif A[light] == 2:
        break
    else:
        ans = ans + 1
        light = A[light] - 1

print(ans)

Submission status: Time Limit Exceeded

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

#loopの定義→同じインデックスを使用したら
loop=[]

# tmp=a[0]
# if tmp==2:
#   print(1)
#   exit()
tmp=1
while True:  
  tmp=a[tmp-1]
  count+=1
  if tmp==2:
    print(count)
    exit()
  if tmp-1 in loop:
    print(-1)
    exit()
  loop.append(tmp-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

b = 1
cnt = 0
flag = False
for i in range(N):
    cnt += 1
    next_button = buttons[b]
    if next_button == 2:
        flag = True
        break
    pushed.append(b)
    b = next_button
    if b in pushed:
        flag = False
        break
        
if flag:
    print(cnt)
else :
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

b = 1
cnt = 0
while True:
    cnt += 1
    next_button = buttons[b]
    if next_button == 2:
        flag = True
        break
    pushed.append(b)
    b = next_button
    if b in pushed:
        flag = False
        break
        
if flag:
    print(cnt)
else :
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
# Author: cr4zjh0bp
# Created: Sun Mar 22 23:25:40 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
a = nin(n)

ans = 0
flag = True
i = 0
p = 0
while i != 1:
    if p == a[i] - 1:
        flag = False
        break
    p = i
    i = a[i] - 1
    ans += 1

print(ans if flag else -1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

a.insert(0,0)
ans = 0
visited = []
i = 1

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

Submission status: Time Limit Exceeded

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

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

B = [1]
i = 0
while(len(B) == len(set(B))):
    t = A[B[-1] - 1]
    i += 1
    if t == 2:
        break
    B.append(t)
if t == 2:
    print(i)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

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

    A=[int(input()) for i in range(N)]
    temp=[0]*N
    i = 0
    cnt=0

    while True:
        if A[i] not in temp:
            temp[cnt] = A[i]
            if A[i]==2:
                print(cnt+1)
                break
            i=A[i]-1
            cnt+=1

        else:
            print(-1)
            break
resolve()

Submission status: Time Limit Exceeded

Solution code:
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

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

    A=[int(input()) for i in range(N)]
    temp=[0]*N
    i = 0
    cnt=0

    while True:
        if A[i] not in temp:
            temp[cnt] = A[i]
            if A[i]==2:
                print(temp.index(A[i])+1)
                break
            i=A[i]-1
            cnt+=1

        else:
            print(-1)
            break
resolve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
light_position =1
light_position = a[light_position-1]
count = 1
while True:
    if light_position == 2:
        break
    elif light_position == 1:
        count = -1
        break        
    light_position = a[light_position-1]
    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
buttons = list(map(int, [input() for i in range(n)]))

indexs = [] #押したボタンの番号
a = 1 #押すボタンの番号
cnt = 0 #押した回数

for i in range(n):
    cnt += 1 #押した回数を増やす
    
    if buttons[a-1] == 2:
        print(cnt)
        exit()
    elif a in indexs:
        print(-1)
        exit()
    
    indexs.append(a) #押したボタンの番号を追加
    a = buttons[a-1]

print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
buttons = list(map(int, [input() for i in range(n)]))

indexs = [] #押したボタンの番号
a = 1 #押すボタンの番号
cnt = 0 #押した回数

while True:
    cnt += 1 #押した回数を増やす
    
    if buttons[a-1] == 2:
        print(cnt)
        exit()
    elif a in indexs:
        print(-1)
        exit()
    
    indexs.append(a) #押したボタンの番号を追加
    a = buttons[a-1]

Submission status: Time Limit Exceeded

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

hist = []
next_i = 0
ans = 0
for i in range(N):
    ans += 1
    hist.append(next_i)
    next_i = a[next_i]
    if next_i == 1:
        break
    elif next_i in hist:
        ans = -1
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dic = {}

for i in range(n):
    a = int(input())
    dic[i+1] = a
    
    if a==1 and max(dic.values())<=i+1:
        break
    
light = dic[1]
ans = 1

if len(dic) < n:
    ans = -1
else:
    while light != 2:
        ans += 1
        light = dic[light]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dic = {}

for i in range(n):
    dic[i+1] = int(input())
    
light = dic[1]
ans = 1

while light != 2:
    ans += 1
    light = dic[light]
    
    if light == 1:
        ans = -1
        break
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#ABC065

n = int(input())
a = [int(input()) for _ in range(n)]
b = []
count = 0
i = 1
for _ in range(n):
    if i in b:
        
        count = 0
        break
    if i == 2:
        break
    b.append(i)
    i = a[i-1]
    count += 1
    
        
if count != 0:
    print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
#ABC065

n = int(input())
a = [int(input()) for _ in range(n)]
b = []
count = 0
i = 1
while i != 2:
    if i in b:
        print(-1)
        count = 0
        break
        
    b.append(i)
    i = a[i-1]
    count += 1
    
        
if count != 0:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
#ABC065

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(input())
ans = 1
num = a[0]
path = []
while num != 2:
    if len({num} & set(path)) > 0:
        ans = -1
        break
    path.append(num)
    num = a[num-1]
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    btn = 1
    hist = []
    while True:
        if btn == 2:
            print(len(hist))
            break

        if btn in hist:
            print(-1)
            break

        hist.append(btn)
        btn = a[btn - 1]


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	a = int(input())
	al=[]
	for i in range(a):
		b = int(input())
		al.append(b)
		
	opened_list=set()
	st=0
	opened_list.add(st)
	count=0
	while st != 1:
		st = al[st]-1
		count+=1
		if st in opened_list:
			return -1
			
	return count
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Time Limit Exceeded

Solution code:
def main():
    buttons = int(input())
    next_lighten_up = [int(input()) for _ in range(buttons)]
    searched = [1]
    answer = 1
    while True:
        if next_lighten_up[searched[-1] - 1] == 2:
            break
        elif next_lighten_up[searched[-1] - 1] in searched:
            answer = -1
            break
        else:
            answer += 1
            searched.append(next_lighten_up[searched[-1] - 1])
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LMI(): return list(map(int, input().split()))

N = I()
A = [I() for _ in range(N)]

seen = {1}
i = 0
light = 1
while light != 2:
    light = A[light - 1]
    if light in seen:
        print(-1)
        exit(0)
    i += 1
print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
next = []
for i in range(N):
    next.append(int(input()))
count = 0
history = [1]
while True:
    light = next[history[-1] - 1]
    if light in history:
        print(-1)
        exit()
    count += 1
    if light == 2:
        print(count)
        exit()
    else:
        history.append(light)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
now_next = {}
for i in range(N):
    now_next[i + 1] = int(input())
count = 0
history = [1]
while True:
    light = now_next[history[-1]]
    if light in history:
        print(-1)
        exit()
    count += 1
    if light == 2:
        print(count)
        exit()
    else:
        history.append(light)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
b=[1]
buf=1
if 2 not in a:
    print(-1)
else:
    while True:
        a_buf=a[buf-1]
        if a_buf==2:
            print(len(b))
            break
        elif a_buf in b:
            print(-1)
            break
        b.append(a_buf)
        buf=a_buf

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
b=[1]
buf=1
if 2 not in a:
    print(-1)
else:
    while True:
        if a[buf-1]==2:
            print(len(b))
            break
        elif a[buf-1] in b:
            print(-1)
            break
        b.append(a[buf-1])
        buf=a[buf-1]

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
dic = {}
for i in range(N):
  a = int(input())
  dic[i + 1] = a
l = [1]
n = dic[1]
i = 1
flg = False
if n == 2:
  print(1)
else:
  while n not in l:
    l.append(n)
    n = dic[n]
    i += 1
    if n == 2:
      flg = True
      ans = i
      break
  if flg:
    print(ans)
  else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
dic = {}
for i in range(N):
  a = int(input())
  dic[i + 1] = a
l = [1]
n = dic[1]
i = 1
flg = False
if n == 2:
  print(1)
else:
  while n not in l:
    l.append(n)
    n = dic[n]
    i += 1
    if n == 2:
      flg = True
      ans = i
      break
  if flg:
    print(ans)
  else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
B = []
cnt = 0
i = 0
while i != 1:
    i = A[i]-1
    B.append(i)
    cnt += 1
    if len(B) != len(set(B)):
        print(-1)
        exit()
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
l=[]
c=1
i=0
f=True
for _ in range(100002):
    i+=1
    c=a[c-1]
    b=len(l)
    l.append(c)
    l=list(set(l))
    if len(l)==b:
        f=False
        break
    if c==2:break
if f:print(i)
else:print(-1)

Submission status: Time Limit Exceeded

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

f=True
while True:
    i+=1
    c=a[c-1]
    b=len(l)
    l.append(c)
    l=list(set(l))
    if len(l)==b:
        f=False
        break
    if c==2:break
if f:print(i)
else:print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
r = []
idx = 0
cnt = 0
for i in range(n):
    w = a[idx]
    cnt += 1
    if w in r:
        cnt = -1
        break
    elif w == 2:
        break
    elif i == n - 1:
        cnt = -1
        break
    else:
        r.append(w)
        idx = w - 1
print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0
check = []
tmp = 1
check.append(tmp)
flag = False
while(1):
    tmp = a[tmp][0]
    ans += 1
    if tmp==2:
        flag = True
        break
    if tmp in check:
        break
    check.append(tmp)
    if ans >N:
        break
if flag:
    print(ans)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

ans = 0
check = []
tmp = 1
check.append(tmp)
flag = False
while(1):
    tmp = a[tmp][0]
    ans += 1
    if tmp==2:
        flag = True
        break
    if tmp in check:
        break
    check.append(tmp)
if flag:
    print(ans)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

ans = 0
check = []
tmp = 1
check.append(tmp)
flag = False
while(1):
    tmp = a[tmp][0]
    ans += 1
    if tmp==2:
        flag = True
        break
    if tmp in check:
        break
if flag:
    print(ans)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
check = [0]
ans = 0
now = 1
while (1):
    now = l[now - 1]
    ans += 1

    if now == 2:
        print(ans)
        exit()
    if now in check:
        print(-1)
        exit()
    check.append(now)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
n = int(input())
a = [int(input())-1 for _ in range(n)]
# -1をしたので、if a[s] == 1になったら終了
#print(a)
cnt = 0
visit = [0] #出発点は訪れてる
i = 0
while cnt <= n:
    cnt += 1
    #print(visit)
    if a[i] in visit:#循環するのでbreak
        print(-1)
        exit(0)
    elif a[i] == 1: #ボタン２を押すので今までの回数を答える
        print(cnt)
        exit(0)
    else:# i を更新　visitに現在のボタンを追加する
        visit.append(a[i])
        i = a[i]

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
n = int(input())
a = [int(input())-1 for _ in range(n)]
# -1をしたので、if a[s] == 1になったら終了
#print(a)
cnt = 0
visit = [0] #出発点は訪れてる
i = 0
while True:
    cnt += 1
    #print(visit)
    if a[i] in visit:#循環するのでbreak
        print(-1)
        exit(0)
    elif a[i] == 1: #ボタン２を押すので今までの回数を答える
        print(cnt)
        exit(0)
    else:# i を更新　visitに現在のボタンを追加する
        visit.append(a[i])
        i = a[i]

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
n = int(input())
a = [int(input())-1 for _ in range(n)]
#print(a)
cnt = 0
visit = [0] #出発点は訪れてる
i = 0
while True:
    cnt += 1
    #print(visit)
    if a[i] in visit:
        print(-1)
        exit()
    elif a[i] == 1:
        print(cnt)
        exit()
    else:
        visit.append(a[i])
        i = a[i]

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

while True:
    current = A[current] - 1
    ans += 1
    if current == 0:
        print(-1)
        break

    if current == 1:
        print(ans)
        break

Submission status: Time Limit Exceeded

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

done = []
i = 0
cnt = 0

while True:
    if i not in done:
        done.append(i)
        cnt += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        cnt = -1
        break

print(cnt)

Submission status: Time Limit Exceeded

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

# 1: now, -1: visited, 0: none
b[0] = 1
now = 0
count = 0
while (b[1] != 1):
   next = a[now] - 1

   if (b[next] == -1):
      print(-1)
      exit()

   b[now] = -1
   b[next] = 1

   now = next
   count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
# 二回訪れたら終了、それまでに2に行ったらOK
# インデックスと値を入れ替えながらやってみる
n = int(input())
dic = []
for i in range(n):
    dic.append(int(input()))

used = []
tmp = 1
ans = 0
while True:
    target = dic[tmp - 1]
    if target == 2:
        print(ans + 1)  # 次の一手で決まるので
        exit()
    if target in used:
        print(-1)
        exit()
    tmp = target
    used.append(tmp)
    ans += 1

Submission status: Time Limit Exceeded

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

Done = []
check = 0
bot = 0
count = 0

while check == 0:
    light = A[bot]
    bot = light -1
    count += 1

    if light == 2:
        ans = count
        break
    
    if light in Done:
        ans = -1
        break

    Done.append(light)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
for _ in range(N):
    a = int(input())
    A.append(a - 1)
Flags = [False] * N
Flags[0] = True
now = 0
cnt = 0
while now != 1:
    cnt += 1
    next_node = A[now]
    if Flags[next_node]:
        print("-1")
        exit()
    now = next_node
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = [int(input()) for _ in range(n)]
blst = []
i = 1
n = 0
while i not in blst:
    blst.append(i)
    i = lst[i - 1]
    n += 1
    if i == 2:
        print(n)
        quit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
#Trained?
N = int(input())
a = [int(input()) for _ in range(N)]
judge2 = 0
judgeloop = 0
now = 1
tugi = a[0]
rireki = []
n = 1

if tugi == 2:
    judge2 += 1

while judge2 == 0 and judgeloop == 0:
    n += 1
    rireki.append(now)
    now = tugi
    tugi = a[tugi-1]
    if tugi in rireki:
        judgeloop += 1
    elif tugi == 2:
        judge2 += 1

if judgeloop == 1:
    print(-1)
elif judge2 == 1:
    print(n)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)
target = ["0"]+["1"]+["0"]*(N-2)
searched = {"".join(light):1}

flag = 0
num = 0
bottun = 0
for _ in range(N):
    if light == target:
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    if searched.get("".join(light)):
        break
    else:
        searched["".join(light)] = 1
    
if flag:
    print(num)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)
target = ["0"]+["1"]+["0"]*(N-2)
searched = {"".join(light):1}

flag = 0
num = 0
bottun = 0
while True:
    if light == target:
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    if searched.get("".join(light)):
        break
    else:
        searched["".join(light)] = 1
    
if flag:
    print(num)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

light = "1"+"0"*(N-1)
target = "0"+"1"+"0"*(N-2)
searched = {light:1}

flag = 0
num = 0
bottun = 0
while True:
    if light == target:
        flag = 1
        break
    num += 1
    idx = bottun
    light = light[:idx]+"0"+light[idx+1:]
    idx = light_rule[bottun]
    light = light[:idx]+"1"+light[idx+1:]
    bottun = light_rule[bottun]
    if searched.get(light):
        break
    else:
        searched[light] = 1
    
if flag:
    print(num)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

light = "1"+"0"*(N-1)
target = "0"+"1"+"0"*(N-2)
searched = {light:1}

def replace_str(source_s, to_s, idx):
    return source_s[:idx]+to_s+source_s[idx+1:]

flag = 0
num = 0
bottun = 0
while True:
    if light == target:
        flag = 1
        break
    num += 1
    light = replace_str(light, "0", bottun)
    light = replace_str(light, "1", light_rule[bottun])
    bottun = light_rule[bottun]
    if searched.get(light):
        break
    else:
        searched[light] = 1
    
if flag:
    print(num)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
import sys
input = sys.stdin.readline
a = []
for i in range(1,N+1):
    a.append(int(input()))
ans = 1
check = a[0]
pre_index = 1
while True:
    if check == 2:
        print(ans)
        exit()
    if pre_index == a[check-1]:
        print(-1)
        exit()
    pre_index = check
    check = a[check-1]
    ans += 1

Submission status: Time Limit Exceeded

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

#print(l)

check = [0]*n
go = 0
ans = 0

for i in range(n):
	#print(i)
	check[i] = go
	ans = ans + 1
	go = l[go]-1
	#print(go)
	#print(check)
	
	if go == 1:
		break
	
	if go in check:
		ans = -1
		break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())

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

index = 1
counter = 0
log = [0]

while True :
  
  find = bisect.bisect_left(log,index)
  
  if  find != len(log) and log[find]==index :
    print(-1)
    exit()
  else:
  	bisect.insort(log,index)
  
  nextindex = line[index]
  counter += 1
  index  = nextindex
  
  if index == 2 :
    print(counter)
    exit()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

index = 1
counter = 0
log = []
while True :

  if line[index] in log :
    print(-1)
    exit()
  else:
  	log.append(index)
  
  index = line[index]
  counter += 1

  
  if index == 2 :
    print(counter)
    exit()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

index = 1
counter = 0
log = []
while True :

  if line[index] in log :
    print(-1)
    exit()
  else:
  	log.append(index)
  
  nextindex = line[index]
  counter += 1
  index  = nextindex
  
  if index == 2 :
    print(counter)
    exit()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
log_l = [0]
i = 0
count = 0
flag = False
while i != 1: 
    i = a[i]-1
    count +=1
    if i in log_l: break
    else: log_l.append(i)
    if i == 1:
        flag = True
        break
if flag: print(count)
else: print(-1)

Submission status: Time Limit Exceeded

Solution code:
buttons = int(input())
next = []
impossible = []
for i in range(buttons):
    next.append(int(input()))
now_button = 1
ans = 1
while next[now_button - 1] != 2:
    if next[now_button - 1] not in impossible:
        impossible.append(next[now_button - 1])
        now_button = next[now_button - 1]
        ans += 1
    else:
        ans = -1
        break
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=[int(input()) for i in range(N)]
M=[0]*N
m=1
i=1
ans=0
while True:
    m=a[m-1]
    if m not in M:
        if m==2:
            print(i)
            break
        else:
            M[i]=m
            i+=1
    else:
        print(-1)
        break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(int(input()) for _ in range(n))
count = 0
x = L[0]
A = []
while True:
    if x == 2:
        count += 1
        print(count)
        break
    elif x in A:
        print(-1)
        break
    else:
        A.append(x)
        x = L[x-1]
        count += 1

Submission status: Time Limit Exceeded

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

now = 1
flag = 1
memo = []
cnt = 0
for i in range(N):
    next = a_lis[now-1]
    if next in memo:
        print(-1)
        flag = 0
        break
    else:
        memo.append(next)
        now = next
    cnt+=1
    if next ==2:
        print(cnt)
        break

Submission status: Time Limit Exceeded

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

now = 1
flag = 1
memo = []
cnt = 0
while flag:
    next = a_lis[now-1]
    if next in memo:
        print(-1)
        flag = 0
        break
    else:
        memo.append(next)
        now = next
    cnt+=1
    if next ==2:
        print(cnt)
        break