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:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    a = [I() - 1 for _ in range(N)]

    c = 0
    ans = 0
    is_ok = True
    while True:
        c = a[c]
        ans += 1
        if c == 0:
            is_ok = False
            break
        if c == 1:
            break

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

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

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

visited = [False] * n
f = 0
visited[f] = True
cnt = 0
while True:
    cnt += 1
    t = a[f]
    if t == 1:
        flg = True
        break
    if visited[t]:
        flg = False
        break
    f = t
print(cnt) if flg else print(-1)

Submission status: Time Limit Exceeded

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

visited = [False] * n
f = 0
cnt = 0
visited[f] = True
flg = True
while True:
    t = a[f]
    if visited[t]:
        flg = False
        break
    cnt += 1
    if t == 1:
        break
    f = t
print(cnt) if flg else print(-1)

Submission status: Time Limit Exceeded

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

visited = [False] * n
f = 0
cnt = 0
visited[f] = True
flg = True
while True:
    t = a[f]
    if visited[t]:
        flg = False
        break
    else:
        cnt += 1
        if t == 1:
            break
        f = t
print(cnt) if flg else print(-1)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

k = 1
while a.count(-1) <= n:
    t = a[k-1]
    a[k-1] = -1
    k = t
#    print(t)
#    print(a)
    if k == -1:
        break
    elif k == 2:
        break

if a.count(-1) >= n or k == -1:
    print(-1)
else:
    print(a.count(-1))

Submission status: Time Limit Exceeded

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

log = []
button = 1
count = 0
while button != 2:
    button = a[button-1]
    if (button in log) == True:
        count = -1
        break
    count += 1
    log.append(button)
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = [0] * n
for i in range(n):
    a[i] = int(input())

loop = [0] * n


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

tar_index = 0
loop[0] = 1
# 何回目で２に到達するか
count = 0

while True:
    tar_index = a[tar_index] - 1
    #print(count, tar_index)
    count += 1

    if (tar_index == 1):
        print(count)
        exit()

    # 一回行った場所に戻ると、循環してるのでOUT
    if (loop[tar_index] == 1):
        print(-1)
        exit()

Submission status: Time Limit Exceeded

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

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

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


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

	#print(A)

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




if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = []
for i in range(n):
    arr.append(int(input()))
visited = []
i = 0
c = 0
while i != 1 and i not in visited:
    visited.append(i)
    i = arr[i]-1
    c += 1
if i == 1:
    print(c)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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



done = []
light = 0
flag = 0
ans = -1
cnt = 0

while flag == 0:
  done.append(light)
  light = A[light]
  cnt += 1

  if light == 1:
    ans = cnt
    flag = 1
  if light in done:
    flag = 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))
 
pushed = [1]
 
while True:
  button = A[pushed[-1] - 1]
  if button in pushed:
    print("-1")
    break
  elif button == 2:
    print(len(pushed))
    break
  else:
    pushed.append(button)

Submission status: Time Limit Exceeded

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

pushed = [1]

while True:
  button = A[pushed[-1] - 1]
  if button in pushed:
    print("-1")
    break
  elif button == 2:
    print(len(pushed))
    break;
  else:
    pushed.append(button)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#/usr/bin/env python

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

lighted = [0] 
ans = 0 

while True:
    u = lighted[-1]
    v = a[u]-1
    ans += 1
    if v == 1:
        print(ans)
        exit()
    if v not in lighted:
        lighted.append(v)
    else:
        print(-1)
        exit()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

prev = []
next = 0
count = 0
while(next not in prev):
    prev.append(next)
    next = int(A[next])-1
    count += 1
    if(next == 1):
        print(count)
        break
else:
    print(-1)

Submission status: Time Limit Exceeded

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

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

    if cur in visited:
        print(-1)
        exit()

    visited.append(cur)

    if cur == 2:
        print(count)
        exit()

print(-1)

Submission status: Time Limit Exceeded

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

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

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

print(step)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = []
istwo = False
for i in range(N):
    a = int(input())
    if a == 2 and i != 1: istwo = True
    l.append(a-1)
flag = [0]*N
flag[0] = 1
flag[1] = 2
cnt = 0
cur_b = 0
while istwo:
    cnt += 1
    next_b = l[cur_b]
    if flag[next_b] == 1:
        print(-1)
        break
    elif flag[next_b] == 2:
        print(cnt)
        break
    else:
        flag[next_b] == 1
        cur_b = next_b

if istwo == False:
    print(-1)

Submission status: Time Limit Exceeded

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

if istwo == False:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

num=1
cnt=0
i=0

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

Submission status: Time Limit Exceeded

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

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

cnt=0
i=0

while True:
  i=button[i]-1 #←ここが理解できない。。。
  cnt+=1
  if i==1:
    print(cnt)
    break
  if cnt>N:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

num = 0
crr_btn = 1
while True:
    num += 1
    if nxt_btn[crr_btn] == 1:
        num = -1
        break
    if nxt_btn[crr_btn] == 2:
        break
    crr_btn = nxt_btn[crr_btn]
print(num)

Submission status: Time Limit Exceeded

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

visited = set()

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

N = int(input())

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

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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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


print(ans)

Submission status: Time Limit Exceeded

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

done_list = [1]
ans = 0
tmp = 1
for _ in range(N):
    if A[tmp-1] in done_list:
        print(-1)
        exit(0)
    done_list.append(A[tmp-1])
    tmp = A[tmp-1]
    ans += 1
    if tmp == 2:
        print(ans)
        exit(0)

Submission status: Time Limit Exceeded

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

done_list = [1]
ans = 0
tmp = 1
while True:
    if A[tmp-1] in done_list:
        print(-1)
        exit(0)
    done_list.append(A[tmp-1])
    tmp = A[tmp-1]
    ans += 1
    if tmp == 2:
        print(ans)
        exit(0)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

print(len(t))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
sec = []
count, cur = 0, 1
while cur not in sec:
    sec.append(cur)
    if cur == 2:
        break
    cur = A[cur - 1]
    count += 1
print(count if 2 in sec else -1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
sec = []
cur = 1
while cur not in sec:
    sec.append(cur)
    if cur == 2:
        break
    cur = A[cur - 1]
print(len(sec) - 1 if 2 in sec else -1)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

while not now in course:
    course.append(now)
    now = button[now]
    count += 1
    if now == 1:
        print(count)
        sys.exit()
print(-1)

Submission status: Time Limit Exceeded

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

N = int(input())

a = []

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

ans = 0
b_now = 1
loop = []

if 2 in a:
    for i in range(1, N + 1):
        loop.append(b_now)
        b_now = a[b_now - 1]
        ans += 1
        if b_now == 2:
            print(ans)
            exit()
        elif b_now in loop:
            print(-1)
            exit()
else:
    print(-1)

Submission status: Time Limit Exceeded

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

N = int(input())

a = []

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

ans = 0
b_now = 1
loop = []

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S={n:int(input()) for n in list(range(1,N+1))}
a=1
cnt=0
while True:
    a=S[a]
    cnt+=1
    if a==2:
        print(cnt)
        break
    if a==1:
        print(-1)
        break

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  a = [int(input()) for i in range(n)]
  check_id=1
  check_list=[]
  while True:
    if check_id in check_list:
      print(-1)
      break
    if check_id==2:
      print(len(check_list))
      break      
    check_list.append(check_id)
    check_id=a[check_id-1]  
      
main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
Alist=[0]+[int(input()) for i in range(n)]

now=1
Hist=[1]

for i in range(n):
    now=Alist[now]
    if now==2:
        print(i+1)
        break
    if now in Hist:
        print(-1)
        break
    Hist.append(now)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
Alist=[0]+[int(input()) for i in range(n)]

now=1
count=0
Hist=[1]

while now!=2:
    now=Alist[now]
    count+=1
    if now in Hist:
        count=-1
        break
    Hist.append(now)
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
Alist=[0]+[int(input()) for i in range(n)]

now=1
count=0
Hist=[]

while now!=2:
    now=Alist[now]
    count+=1
    if now in Hist:
        count=-1
        break
    Hist.append(now)
print(count)

Submission status: Time Limit Exceeded

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

out=0

while i!=1:
  out+=1
  i=a[i]
  if i>n:
    out=-1
    break

print(out)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0
seen=[]
out=0
if 1 not in a:
  out=-1
  i=1
while i!=1:
  out+=1
  i=a[i]
  if i in seen:
    out=-1
    break
  seen.append(i)
print(out)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0
seen=[]
out=0
while i!=1:
  out+=1
  i=a[i]
  if i in seen:
    out=-1
    break
  seen.append(i)
print(out)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0
seen=[]
out=0
while i!=1:
  out+=1
  i=a[i]
  if i in seen:
    out=-1
    break
  seen.append(i)
print(out)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
button = [0]*n
button1 = a[0] - 1
count = 1
try:
    fst = a.index(2)
    if fst == 0:
        print('1')
        exit()
except ValueError:
    print('-1')
    exit()

for i in range(n):
    button[button1] += 1
    button1 = a[button1] - 1
    if button[1] == 1:
        print(count)
        break
    try:
        button.remove(2)
        print('-1')
        break
    except:
        count += 1
        pass

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
button = [0]*n
button1 = a[0] - 1
count = 1
try:
    _ = a.index(2)
except:
    print('-1')
    exit()
while True:
    button[button1] += 1
    button1 = a[button1] - 1
    if button[1] == 1:
        print(count)
        break
    try:
        button.remove(2)
        print('-1')
        break
    except:
        count += 1
        pass

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
button = [0]*n
button1 = a[0] - 1
count = 1
while True:
    button[button1] += 1
    button1 = a[button1] - 1
    if button[1] == 1:
        print(count)
        break
    try:
        button.remove(2)
        print('-1')
        break
    except:
        count += 1
        pass

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
x=1
cnt=0
while True:
    x=a[x-1]
    cnt+=1
    if x==2:
        print(cnt)
        break
    elif x==1:
        print('-1')
        break
    else:
        continue

Submission status: Time Limit Exceeded

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

pushed = [False] * N
pushed[0] = True
now = 0
cnt = 0
while True:
    next = A[now] - 1
    if pushed[next]:
        cnt = -1
        break

    if next != 1:
        now = next
        cnt += 1

    if next == 1:
        cnt += 1
        break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
T = [False for i in range(n)]
tmp = 1
T[0] = True
count = 1
while True:
    tmp = a[tmp-1]
    if T[tmp -1]:
        print(-1)
        exit()
    if tmp == 2:
        print(count)
        break
    count+=1

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
ls = [int(input()) for _ in range(n)]
if 2 not in ls:
    print(-1)
    sys.exit()
i = 1
k = 0
di = []
for j in range(n):
    i = ls[i-1]
    if i in di:
        print(-1)
        break
    di.append(i)
    k += 1
    if i == 2:
        print(k)
        break

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
ls = [int(input()) for _ in range(n)]
if 2 not in ls:
    print(-1)
    sys.exit()
i = 1
k = 0
di = []
for j in range(n):
    i = ls[i-1]
    if i in di:
        print(-1)
        sys.exit()
    di.append(i)
    k += 1
    if i == 2:
        break
print(k)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
ls = [int(input()) for _ in range(n)]
if 2 not in ls:
    print(-1)
    sys.exit()
i = 1
k = 0
di = []
while True:
    i = ls[i-1]
    if i in di:
        print(-1)
        sys.exit()
    di.append(i)
    k += 1
    if i == 2:
        break
print(k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

M = [1]
i = 1
while True:
  M.append(A[i-1])
  i = A[i-1]
  if 2 in M:
    print(len(M)-1)
    break
  else:
    for j in range(len(M)):
      if M.count(M[j]) > 1:
        print("-1")
        exit()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
b = [0]
target = 0
count = 0
while(True):
    target = a[target] - 1
    count += 1
    if target in b:
        print("-1")
        break
    else:
        b.append(target)
    if target == 1:
        print(count)
        break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
b = [0]
target = 0
count = 0
while(True):
    target = a[target] - 1
    count += 1
    if target in b:
        print("-1")
        break
    else:
        b.append(target)
    if target == 1:
        print(count)
        break

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Jun  4 22:35:46 2020

@author: naoki
"""

import sys

N = int(input()) # ボタンの個数
a = [int(input()) for i in range(N)] #k番目のボタンに書いてある番号
a = [0] + a #配列の番号合わせ
count = 0 #ボタンを押す回数
light = 1 # 光っているボタンの番号
if 2 not in a: # 2と書かれたボタンがない場合は不可能なので-1を出力
    print(-1)
    sys.exit()
else: # 2と書かれたボタンがある場合
    while light != 2 or count >= N : # 光っているボタンが2になるか、ボタンを押した回数がNを超えてしまったら終了
        count += 1 
        light = a[light]#左から数えたときに光っているボタンの番号の順番にあるボタンに書かれている数字が次に光るボタンの番号である。

if light != 2:
    print(-1)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
ans = 0
light = 0
memo = [0]
while light != 1:
    if a[light]-1 in memo:
        print(-1)
        exit()
    memo.append(a[light]-1)
    light = a[light]-1
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
i=0
count=1
while(1):
  if a[i]==1:
    frag=-1
    break
  elif a[i]==2:
    frag=count
    break
  else:
    i=a[i]-1
    count+=1
print(frag)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    dict={}
    for i in range(n):
        dict[i] = int(input())

    list2=[]
    count = 0
    now =1
    for i in range(n):
        if now in list2:
            print(-1)
            break
        else:
            if dict[now-1] ==2:
                count+=1
                print(count)
                break
            else:
                list2.append(now)
                count+=1
                now = dict[now-1]

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    list =[]
    for i in range(n):
        list.append(int(input()))

    list2=[]
    count = 0
    now =1
    for i in range(n):
        if now in list2:
            print(-1)
            break
        else:
            if list[now-1] ==2:
                count+=1
                print(count)
                break
            else:
                list2.append(now)
                count+=1
                now = list[now-1]

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    list =[]
    for i in range(n):
        list.append(int(input()))

    list2=[]
    count = 0
    now =1
    while True:
        if now in list2:
            print(-1)
            break
        else:
            if list[now-1] ==2:
                count+=1
                print(count)
                break
            else:
                list2.append(now)
                count+=1
                now = list[now-1]

Submission status: Time Limit Exceeded

Solution code:
def li():
    return list(map(int, input().split()))
def mi():
    return map(int, input().split())
def ii():
    return int(input())
def lim(N):
    return [int(input()) for i in range(N)]

N = ii()
a = lim(N)

current = 1
visited = [1]
result = 1

while True:
    if a[current - 1] in visited:
        result = -1
        break
    elif a[current - 1] == 2:
        break
    visited.append(a[current - 1])
    current = a[current - 1]
    result += 1

print(result)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
pos = 1
cnt = 0
passed = []
TF = True
while TF:
  pos -= 1
  if a[pos] in passed:
    TF = False
    print(-1)
  elif a[pos] == 2:
    print(cnt+1)
    TF = False
  else:
    passed.append(a[pos])
    pos = a[pos]
    cnt += 1

Submission status: Time Limit Exceeded

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

record = []
index = 0
count = 0
tf = True
while True:
    index=a[index]-1
    record.append(index)
    if record.count(index) == 2:
        print(-1)
        tf = False
        break
    else:
        count += 1
        if index == 1:
            break
if tf:
    print(count)

Submission status: Time Limit Exceeded

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

record = []
index = 0
count = 0
tf = True
while True:
    index=a[index]-1
    record.append(index)
    if record.count(index) == 2:
        print(-1)
        tf = False
        break
    else:
        count += 1
        if index == 1:
            break
if tf:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
now = 1
count = 0
past = []
while True:
    if now == 2:
        print(count)
        exit()
    if A[now - 1] in past:
        print(-1)
        exit()
    count += 1
    past.append(A[now - 1])
    now = A[now - 1]

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    btn = [int(input())-1 for _ in range(N)]
    i = 0
    li = [0]
    #print(btn)
    
    for _ in range(N):
        k = btn[i]
        #print(k, li)
        if k in li:
            print('-1')
            break
        else:
            if k != 1:
                li.append(k)
            else:
                print(len(li))
                break
        i = k
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
btn = [int(input())-1 for _ in range(N)]
i = 0
li = [0]

for _ in range(N):
    k = btn[i]
    #print(k, li)
    if k in li:
        print('-1')
        break
    else:
        if k != 1:
            li.append(k)
        else:
            print(len(li))
            break
    i = k