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_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)
        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 = [int(input())-1 for i in range(N)]

light = 0
first = 0
ans = 0

while True:
    if light == A[light]:
        print(-1)
        exit()
    light = A[light]
    ans += 1
    if light == 1:
        print(ans)
        exit()
    if light == first:
        print(-1)
        exit()

Submission status: Time Limit Exceeded

Solution code:
#2019/10/16
N, *a = map(int, open(0).read().split())
#print(N, a)
i=0
cnt=1
button=[]
while(True):
    #print("i=",i, "a[i]=",a[i])
    #print("button=", button)
    i=a[i]-1
    if i==1:
        print(cnt)
        break
    elif i in button:
        print("-1")
        break
    else:
        button.append(i)
        cnt+=1

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
N=int(input())
button=[]
for i in range(N):
  button.append(int(input()))

number=0
count=0
footprint=[0]
while number!=1:
  number=button[number]-1
  count+=1
  if number in footprint:
    print(-1)
    exit()
  footprint.append(number)
print(count)

Submission status: Time Limit Exceeded

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

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

check=[]
s=0
count=0

while s!=1:
  if s in check:
    break
  else:
    check.append(s)
    s=li[s]-1
    count+=1

if s==1:
  print(count)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[]
I=[]
for n in range(N):
    a=int(input())
    A.append(a)
I.append(1)
def t(I):
    #while 2 not in I:
    while 1:
        a=A[I[-1]-1]
        if a in I:
            return print(-1)
        if a==2:
            return print(len(I))
        I.append(a)
t(I)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[]
I=[]
for n in range(N):
    a=int(input())
    A.append(a)
I.append(1)
def t(I):
    while 2 not in I:
        a=A[I[-1]-1]
        if a in I:
            return print(-1)
        I.append(a)
    return print(len(I)-1)
t(I)

Submission status: Time Limit Exceeded

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

count=1
c=a[0]

while c != 2:
    c=a[c-1]
    if a[c-1] == b:
        print(-1)
        exit()
    b=c
    count +=1 

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(5 * 10**5)
input = sys.stdin.readline

N = int(input())

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

line = [0]
ans = -1

while True:
    if (a[line[-1]] - 1 in line):
        break
    else:
        line.append(a[line[-1]] - 1)

    if line[-1] == 1:
        ans = len(line) - 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
n= int(input())
a= [int(input()) for i in range(n)]
memo=[]
def solve(num,ans):
    if num==2:
        print(ans)
    else:
        ans+=1
        tmp=a[num-1]
        if tmp in memo:
            print('-1')
        else:
            memo.append(tmp)
            solve(tmp,ans)
solve(a[0],1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
s = [1,a[0]] #shone buttons
while True: #unitl 2 appears in s or a loop generates 
    if s[-1] == 2: #if 2 appears
        break
    if len(set(s)) != len(s): #if a loop generates: mathematical induction
        break 
    s += [a[s[-1]-1]]
# print(s)
if s[-1] == 2:
    print(len(s)-1)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

visited = [0]
steps = 0

while steps<N:
  curr_i = visited[-1]
  curr_val = A[curr_i]
  if curr_i == 1:
    print(steps)
    exit()
  else:
    next_i = curr_val - 1
    if next_i in visited:
      print(-1)
      exit()
    else:
      steps+=1
      visited.append(curr_val -1)
print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
 
n = ni()
a = [int(ns()) for i in range(n)]
pushed = []

next_b = 1
count = 0
for _ in range(0,n):
  if next_b == 2:
    break
  count += 1
  next_b = a[next_b-1]
  if next_b in pushed:
    count = -1
    break
  pushed.append(next_b)
  
if count == n:
  count = -1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
 
n = ni()
a = [int(ns()) for i in range(n)]
pushed = []

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

visitedlist = [0]
from_ = 0
for _ in range(n):
    to = alist[from_]
    if to in visitedlist:
        print(-1)
        break
    elif to == 1:
        print(len(visitedlist))
        break
    else:
        visitedlist.append(to)
        from_ = to

Submission status: Time Limit Exceeded

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

visitedlist = [0]
from_ = 0
while True:
    to = alist[from_]
    if to in visitedlist:
        print(-1)
        break
    elif to == 1:
        print(len(visitedlist))
        break
    else:
        visitedlist.append(to)
        from_ = to

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
prev=[1]  
for _ in range(N):
  if pos == 2:
    print(len(prev)-1)
    exit(0)
  pos = a[pos]
  if pos not in prev:
    prev.append(pos)
  

print(- 1)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
prev=[1]  
while(pos!= 2):
  pos = a[pos]
  if pos in prev:
    print(-1)
    exit(0)
  else:
    prev.append(pos)
print(len(prev) - 1)

Submission status: Time Limit Exceeded

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

prev=[2]  
count=0
button=2
while(button!=1):
  if len(b[button]) != 0:
    button = b[button].pop()
    if button not in prev:
      prev.append(button)
    else:
      button=prev[-1]
  else:
    prev.pop()
    if len(prev) == 0:
      print(-1)
      exit(0)
    button = prev[-1]
    continue

  
print(len(prev) - 1)

Submission status: Time Limit Exceeded

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

prev=[2]  
count=0
button=2
while(button!=1):
  if len(b[button]) != 0:
    button = b[button].pop()
    if button not in prev:
      prev.append(button)
    else:
      button=prev[-1]
  else:
    if len(prev) == 0:
        print(-1)
        exit(0)
    if prev[-1]==button:
      prev.pop()
      if len(prev) == 0:
        print(-1)
        exit(0)
      button = prev[-1]
      continue
    
  
print(len(prev) - 1)

Submission status: Time Limit Exceeded

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

prev=[2]  
count=0
button=2
while(button!=1):
  if len(b[button]) != 0:
    button = b[button].pop()
    if button not in prev:
      prev.append(button)
    else:
      button=prev[-1]
  else:
    if prev[-1]==button:
      prev.pop()
      if len(prev) == 0:
        print(-1)
        exit(0)
      button = prev[-1]
      continue
  
print(len(prev) - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys;sys.setrecursionlimit(9**9);*A,=map(int,open(0))
def f(i,t):
 if i==2:return len(t)
 if i in t:return -1
 return f(A[i],t+[i])
print(f(1,[]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s = 1
B = [s]
ans = 0

while 2 not in B:
    s = A[s-1]
    if s not in B:
        B.append(s)
        ans += 1
    else:
        ans = -1
        break

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

c = 0
n = 1
while True:
    n = A[n-1]
    c += 1
    if n == 1:
        ok = False
        break
    if n == 2:
        ok = True
        break
if ok:
    print(c)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

a = []
i = 1
cnt = 0
while i != 2:
  a.append(i)
  cnt += 1
  i = A[i-1]
  if i == 2 or i in a:
    break

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

Submission status: Time Limit Exceeded

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

a = []
i = 1
cnt = 0
while i != 2 and i not in a:
  a.append(i)
  cnt += 1
  i = A[i-1]
  if i == 2:
    break

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

Submission status: Time Limit Exceeded

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

a = []
i = 1
cnt = 0
while i != 2 and i not in a:
  a.append(i)
  i = A[i-1]
  cnt += 1

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N = int(input())
A = deque([])
for _ in range(N):
    A.append(int(input())-1)
b = 0
cnt = 0
pushed = deque([0])
while b != 1:
    cnt += 1
    b = A[b]
    if b in pushed:
        print(-1)
        break
    pushed.append(b)
else:
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
for _ in range(N):
    A.append(int(input())-1)
b = 0
cnt = 0
pushed = [0]
while b != 1:
    cnt += 1
    b = A[b]
    if b in pushed:
        print(-1)
        break
    pushed.append(b)
else:
    print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [int(input()) for _ in range(n)]
t = list(range(1,n+1))
i = 1
count = 0

while(i in t):
    count += 1
    t.remove(i)
    i = s[i-1]
    if i==2:
        print(count)
        break
else:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
while True:
  if next == 2:
    print(count)
    exit()
  elif count > n:
    print(-1)
    exit()
  else:
    next = L[next-1]
    count += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
while True:
  if next == 2:
    print(count)
    exit()
  elif count > n:
    print(-1)
    exit()    
  next = L[next-1]
  count += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
for i in range(n):
  if next == 2:
    print(i)
    exit()
  next = L[next-1]
print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
for i in range(n):
  if next == 2:
    print(i)
    quit()
  next = L[next-1]
print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 0
for i in range(len(L)):
  if L[next] == 2:
    print(i+1)
    quit()
  next = L[next]-1
print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
count = 0
next = 0
for i in range(len(L)):
  if L[next] == 2:
    print(count+1)
    quit()
  next = L[next]-1
  count += 1
print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
count = 0
next = 0
NG = [1]
while True:
  if L[next] in NG:
    print(-1)
    quit()
  elif L[next] == 2:
    print(len(NG))
    quit()
  else:
    NG.append(L[next])
    next = L[next]-1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
count = 0
next = 0
NG = [1]
while True:
  if L[next] in NG:
    print(-1)
    quit()
  elif L[next] == 2:
    print(count+1)
    quit()
  else:
    NG.append(L[next])
    next = L[next]-1
    count += 1

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
  n = int(input())
  a = [0]*n
  for n in range(n):
    a[n]=int(sys.stdin.readline())-1
  sw = [0]
  while True:
    nextsw = a[sw[-1]]
    if nextsw == 1:
      print(len(sw))
      return
    if nextsw in sw:
      print(-1)
      return
    sw.append(nextsw)
if __name__ == '__main__':    
  main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  a = []
  for n in range(n):
    a.append(int(input())-1)
  sw = [0]
  while True:
    nextsw = a[sw[-1]]
    if nextsw == 1:
      print(len(sw))
      return
    if nextsw in sw:
      print(-1)
      return
    sw.append(nextsw)
                                
main()

Submission status: Time Limit Exceeded

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

#TLEチーンコ
i=0
j=0
b=[]
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b:
        print(-1)
        break
    else:
        b.append(i)
        i=a[i]

Submission status: Time Limit Exceeded

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

#TLEチーンコ
i=0
j=0
b=[]
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b:
        print(-1)
        break
    else:
        i=a[i]

Submission status: Time Limit Exceeded

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

#TLEチーンコ
i=0
j=0
b=[]
while True:
    if i==1:
        print(j)
        break
    elif i in b:
        print(-1)
        break
    else:
        b.append(i)
        i=a[i]
        j+=1

Submission status: Time Limit Exceeded

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

#TLEチーンコ
i=0
j=0
b=[]
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b:
        print(-1)
        break
    else:
        b.append(i)
        i=a[i]

Submission status: Time Limit Exceeded

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

#TLEチーンコ
i=0
j=0
b=[]
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b:
        print(-1)
        break
    else:
        b.append(a[i])
        i=a[i]

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(sys.stdin.readline())
a = [int(sys.stdin.readline()) for i in range(n)]
a = [-1] + a

def cond(a):
    if 2 not in a:
        return -1

    visited = []
    current = 1
    count = 0

    while True:
        if current == 2:
            return count
        if current in visited:
            return -1
        count += 1
        visited.append(current)
        current = a[current]

print(cond(a))

Submission status: Time Limit Exceeded

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

num = a[0]
count = 0

if 2 not in a:
    print(-1)
else:
    while num != 2:
        b = num
        num = a[num - 1]
        count += 1
        if a[num - 1] == b:
            print(-1)
            sys.exit()
    print(count + 1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
count =1 #ボタンを押した回数用 
s=0 #スタートするボタン　これを操作ボタンにしていく
while True:
    if a[s]==2:
        print(count)
        sys.exit()
    elif count>1 and s==0:
        print(-1)
        sys.exit()
    else:
        count+=1
        s=a[s]-1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, a: "List[int]"):
    visited = [False]*N
    curr = 1
    counter = 0
    while curr != 2:
        curr = a[curr-1]
        counter += 1
        if curr == 1:
            print(-1)
            return
    print(counter)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = [int(input()) for _ in range(n)]
checked = []
num = 0
count = 0

while num not in checked:
    checked.append(num)
    num = a_list[num] - 1
    count += 1
    if num == 1:
        break

if num == 1:
    print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    if on in on_li:
        print(-1)
        exit()
    else:
        on_li.append(on)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(int(input()) for _ in range(n))

push=[0 for _ in range(n)]
index=0
cnt=1
flag=True

while flag:
    push[index]+=1
    if s[index]==2:
        print(cnt)
        flag=False
    else:
        if push.count(2)!=0:
            print(-1)
            flag=False
        else: 
            index=s[index]-1
            cnt+=1

Submission status: Time Limit Exceeded

Solution code:
# ABC 65, B - trained
n = int(input())
A = [int(input()) for i in range(n)]
went = []
now = 1
for i in range(n):
    if now in went:
        print(-1)
        break
    if now == 2:
        print(i)
        break
    went.append(now)
    now = A[now-1]

Submission status: Time Limit Exceeded

Solution code:
# ABC 65, B - trained
n = int(input())
A = [int(input()) for i in range(n)]

togo = [1]
went = []

while togo:
    for i in togo:
        togo.remove(i)
        went.append(i)
        if A[i-1] not in went:
            togo.append(A[i-1])
if 2 in went:
    print(went.index(2))
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
check = []
cnt = 0
i = 0
while True:
    if i in check:
        print(-1)
        break
    if i not in check:
        check.append(i)
        i = a[i]-1
    if cnt >= n/2:
        print(-1)
        break
    if i == 1:
        print(len(check))
        break
    cnt += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
check = []
cnt = 0
i = 0
while True:
    if i not in check:
        check.append(i)
        i = a[i]-1
    elif i in check:
        print(-1)
        break
    elif cnt >= n:
        print(-1)
        break
    if i == 1:
        print(len(check))
        break
    cnt += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
check = []
cnt = 0
i = 0
while True:
    if i not in check:
        check.append(i)
        i = a[i]-1
    elif cnt >= n:
        print(-1)
        break
    if i == 1:
        print(len(check))
        break
    cnt += 1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

N = int(input())
a = [0]

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

i=1

button = []
for j in range(N):
    if(i == 2):
        print(len(button))
        sys.exit()
    elif(i in button):
        print(-1)
        sys.exit()
    else:
        button.append(i)
        i = a[i]

print(-1)

Submission status: Time Limit Exceeded

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

#x=dict(zip(range(1,N+1),a))
#print(x)
route=[1,]
#for _ in range(N):
while 2 not in route:
  #print(x[route[-1]])
  if route[-1]==2:
    break
  elif x[route[-1]-1] not in route:
    route.append(x[route[-1]-1])
  else:
    print(-1)
    exit()
    
print(len(route)-1)

Submission status: Time Limit Exceeded

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

#x=dict(zip(range(1,N+1),a))
#print(x)
route=[1,]
for _ in range(N):
#while 2 not in route:
  #print(x[route[-1]])
  if route[-1]==2:
    break
  elif x[route[-1]-1] not in route:
    route.append(x[route[-1]-1])
  else:
    print(-1)
    exit()
        
print(len(route)-1)

Submission status: Time Limit Exceeded

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

#x=dict(zip(range(1,N+1),a))
#print(x)
route=[1,]
for _ in range(N):
#while 2 not in route:
  #print(x[route[-1]])
  if 2 in route:
    break
  elif x[route[-1]-1] not in route:
    route.append(x[route[-1]-1])
  else:
    print(-1)
    exit()
        
print(len(route)-1)

Submission status: Time Limit Exceeded

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

#x=dict(zip(range(1,N+1),a))
#print(x)
route=[1,]
while 2 not in route:
  #print(x[route[-1]])
  if x[route[-1]-1] not in route:
    route.append(x[route[-1]-1])
  else:
    print(-1)
    exit()
print(len(route)-1)

Submission status: Time Limit Exceeded

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

#x=dict(zip(range(1,N+1),a))
route=[1,]
while 2 not in route:
  #print(x[route[-1]])
  if x[route[-1]-1] not in route:
    route.append(x[route[-1]-1])
  else:
    print(-1)
    exit()
print(len(route)-1)

Submission status: Time Limit Exceeded

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

x=dict(zip(range(1,N+1),a))
#print(x)
route=[1,]
while 2 not in route:
  #print(x[route[-1]])
  if x[route[-1]] not in route:
    route.append(x[route[-1]])
  elif len(route)-1>N:
    print(-1)
    exit()
  else:
    print(-1)
    exit()
print(len(route)-1)

Submission status: Time Limit Exceeded

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

x=dict(zip(range(1,N+1),a))

route=[1,]
while 2 not in route:
  #print(x[route[-1]])
  if x[route[-1]] not in route:
    route.append(x[route[-1]])
  else:
    print(-1)
    exit()
print(len(route)-1)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    a = []
    for _ in range(n):
        a.append(read_int(1)[0])
    return a


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    dummy = -1
    buttons_flash_info = list(a)
    buttons_flash_info.insert(0, dummy)

    index = 1
    visited = set()
    visited.add(index)
    push_count = 1
    while True:
        next_index = buttons_flash_info[index]
        index = next_index
        if index == 2:
            return push_count
        if index in visited:
            return -1
        push_count += 1
    return -1


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
button_list =[]
for i in range(1,N+1):
  button_list.append([i,int(input())])
bond_list = [button_list[0][0]]
dest = 0
for i in range(N):
  bond_list.append(button_list[button_list[dest][1]-1][0])
  bond_list.append(button_list[button_list[dest][1]-1][1])
  dest = button_list[button_list[dest][1]-1][1]-1
  if 2 in bond_list:
    print(bond_list.index(2))
    break;
  if len(bond_list) >=3 and bond_list[len(bond_list)-1] == bond_list[len(bond_list)-3]:
    print(-1)
    break;

Submission status: Time Limit Exceeded

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

reached = []
pos = 0#indexで指定
while 1:
    if pos not in reached:
        reached.append(pos)
    else:
        break
    pos = A[pos]-1
#print(reached)

if 1 in reached:
    print(reached.index(1))
else:
    print(-1)

Submission status: Time Limit Exceeded

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

pushed = []

def main(start=0):
     count = 0
     ls = start
     while True:
          if ls == 1:
               return count
          elif ls in pushed:
               return -1

          pushed.append(ls)
          count += 1
          ls = A[ls]

print(main())

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [int(input())-1 for i in range(N)]
ans = 0
index = 0
b = [False]*N
b[0] = True
while index!=1:
    index = a[index]
    if b[index]:
        ans = -1
        break
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

cnt=0
tmp=1
already=[]
while True:
    cnt+=1
    already.append(tmp)
    tmp=a[tmp-1]
    if tmp==2:
        print(cnt)
        break
    elif tmp in already:
        print(-1)
        break

Submission status: Time Limit Exceeded

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

def get_ans(a):
    mods = list()
    
    if 2 not in a:
        print(-1)
        return True
    
    count = 0
    mod = 1
    while(True):
        mod = a[mod-1]
        count += 1
        if (mod == 2):
            print(count)
            return True
        elif mod in mods:
            print(-1)
            return True
        else:
            mods.append(mod)

ret = get_ans(a)