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:
a = int(input())
ar = [0]
for i in range(a):
    l = int(input())
    ar.append(l)
count = 1
t = 1
br = []
while ar[t] != 2:
    if ar[t] in br:
        count = -1
        break
    else:
        br.append(ar[t])
        t = ar[t]
        count += 1
print(count)

Submission status: Time Limit Exceeded

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

if all([num != 2 for num in a]):
    print(-1)
    exit()

button = [1]
for _ in range(N):
    button.append(a[button[-1] - 1])
    current = button[-1]
    if current == 2:
        print(len(button) - 1)
        exit()
    if any([b == current for b in button[:-1]]):
        break

print(-1)

Submission status: Time Limit Exceeded

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

button = [1]
while True:
    button.append(a[button[-1] - 1])
    current = button[-1]
    if current == 2:
        break
    if any([b == current for b in button[:-1]]):
        print(-1)
        exit()

print(len(button) - 1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[]
for i in range(n):
	a.append(int(input()))
b=['']*n
b[0]='hoge'
c=0
x=0
while True:
	c+=1
	x=a[x]-1
	if b[x]=='hoge':
		c=-1
		break
	else:
		if x==1:
			break
print(c)

Submission status: Time Limit Exceeded

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

tmp=1
count=0
while(1):
  tmp=_list[tmp]
  count+=1
  if(tmp==2):
    print(count)
    break
  if(tmp==1):
    print(-1)
    break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = [0]
for _ in range(n):
    a_list.append(int(input()))
counter = 0
current_number = 1
already_pushed_list = []

while(True):
    counter += 1
    already_pushed_list.append(current_number)
    current_number = a_list[current_number]
    if current_number in already_pushed_list:
        print(-1)
        break
    if current_number == 2:
        print(counter)
        break

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

light=1
ans=0
while True:
    if light==2:
        break
    elif light==buttun[light]:
        ans=-1
        break
    else:
        light=buttun[light]
        ans+=1
    if light==1:
        ans=-1
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_a = []
for i in range(0,n):
    list_a.append(int(input()))
def search(number):
    for i in range(0,n):
        if list_a[i] == number:
            return i + 1
number = 2
count = 0
while True:
    number = search(number)
    count += 1
    if number == 1:
        print(count)
        break
    if count > n:
        print(-1)
        break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_a = []
for i in range(0,n):
    list_a.append(int(input()))
def search(number):
    for i in range(0,n):
        if list_a[i] == number:
            return list_a.index(number) + 1
number = 2
count = 0
while True:
    number = search(number)
    count += 1
    if number == 1:
        print(count)
        break
    if count > n:
        print(-1)
        break

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

i = 1
c = 0
r = [1]
while True:
    c += 1
    i = a[i]
    if i == 2:
        print(c)
        break
    else:
        if i in r:
            print(-1)
            break
        else:
            r.append(i)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [0]
for i in range(N):
    A.append(int(input()))
old = []
bo = 1
ind = 1
while True:
    if A[bo] == 2:
        print(ind)
        exit()
    else:
        ind += 1
        if bo in old:
            print(-1)
            exit()
        old.append(bo)
        bo = A[bo]

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=a[0];c=1;d=[b]
while b!=2 and b!=1 and len(set(d))==len(d):
  b=a[b-1]
  c+=1
  d.append(b)
if b==2:print(c)
else:print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=a[0];c=1;d=[b]
while b!=2 and len(set(d))==len(d):
  b=a[b-1]
  c+=1
  d.append(b)
if b==2:print(c)
else:print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=a[0];c=1;d=[a[0]]
while (not b==2) and (len(set(d))==len(d)):
  b=a[b-1]
  c+=1
  d.append(b)
if b==2:print(c)
else:print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=a[0];c=1;d=[a[0]]
while not (b==1 or b==2) and (len(set(d))==len(d)):
  b=a[b-1]
  c+=1
  d.append(b)
if b==2:print(c)
else:print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=a[0];c=1
while not (b==1 or b==2):
  c+=1
  b=a[b-1]
if b==1:print(-1)
else:print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
buttons = [input() for i in range(n)]
buttons_int = [int(b) - 1 for b in buttons]
button = 0
count = 0
history_list = []
i = 0
while i < n:
    if button == i:
        button = buttons_int[i]
        count += 1
        if button == 1:
            print(count)
            exit()
        if i in history_list:
            print(-1)
            exit()
        else:
            history_list.append(i)
        i = 0
    else:
        i += 1

Submission status: Time Limit Exceeded

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

button = 1
i = 0

while True:
    i += 1
    button = a[button]

    if button <= 2:
        if button == 1:
            print(-1)
        if button == 2:
            print(i)
        break

Submission status: Time Limit Exceeded

Solution code:
class UFT(object):
  def __init__(self,N):
    self.par={i:i for i in range(1,N+1)}
    self.h={i:0 for i in range(1,N+1)}
  def find(self,n):
    if self.par[n]==n:
      return n
    else:
      self.par[n]=self.find(self.par[n])
      return self.par[n]
  def unite(self,a,b):
    a=self.find(a)
    b=self.find(b)
    if self.h[a]>self.h[b]:
      self.par[b]=a
    else:
      self.par[a]=b
      if self.h[a]==self.h[b]:
        self.h[b]+=1
  def issame(self,a,b):
    return self.find(a)==self.find(b)

N=int(input())
obj=UFT(N)
d={i:0 for i in range(1,N+1)}
for i in range(N):
  b=int(input())
  d[i+1]=b
  obj.unite(i+1,b)

if obj.issame(2,1):
  c=1
  e=d[1]
  while e!=2:
    c+=1
    e=d[e]
  print(c)
else:
  print("-1")

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

light=1
count=0
while True:
    if light==2:
        print(count)
        quit()
    elif count>0 and light==1:
        print(-1)
        quit()
    else:
        light=A[light-1]
        count+=1

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

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

rm = {}
for i, x in enumerate(a):
  rm.setdefault(x, set()).add(i + 1)

p = set()
for i in range(n):
  if 1 in rm.get(2, []):
    print(i + 1)
    exit(0)
  d = rm.get(2, set()) - p
  if len(d) == 0: break
  n = reduce(lambda a, x: a.union(rm.get(x, set())), d, d)
  rm[2] = d.union(n)
  p = p.union(d)
print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
alist = []
for _ in range(N):
    alist.append(int(input()))
history = []
i = 1
while True:
    if i in history:
        print(-1)
        break
    history.append(i)
    i = alist[i - 1]
    if i == 2:
        print(len(history))
        break

Submission status: Time Limit Exceeded

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

reachable = []
i = 0
count = 1

while A[i] != 2:
    if A[i] in reachable:
        print(-1)
        exit()
    else:
        reachable.append(A[i])
        i = A[i] - 1
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = []
visited = []
place = 1
for i in range(N):
    a.append(int(input()))
for i in range(N):
    if place in visited:
        print(-1)
        exit()
    elif a[place - 1] == 2:
        print(i + 1)
        exit()
    else:
        visited.append(place)
        place = a[place - 1]

Submission status: Time Limit Exceeded

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

i = 1
rec2  = [1]
num = 0
while i != 2 and num < n:
    i = rec[i]
    if i not in rec2:
        rec2.append(i)
        num += 1
    else:
        num = -1
        break

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

Submission status: Time Limit Exceeded

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

i = 1
rec2  = [1]
num = 0
while i != 2:
    i = rec[i]
    if i not in rec2:
        rec2.append(i)
        num += 1
    else:
        num = -1
        break

print(num)

Submission status: Time Limit Exceeded

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

button_history = [1]
while True:
    next_button = a_list[button_history[-1] - 1]
    if next_button == 2:
        print(len(button_history))
        break
    elif next_button in button_history:
        print(-1)
        break
    else:
        button_history.append(next_button)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [0] * n
for i in range(n): a[i] = int(input()) - 1
i = 0
c = 0
ok = False
while i != a[i] and a[i] != 0:
    c += 1
    if a[i] == 1:
        ok = True
        break
    i = a[i]
if ok: print(c)
else: print(-1)

Submission status: Time Limit Exceeded

Solution code:
# ABC 065 B

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

i = 1
q = []
while i != 2:
    q.append(i)
    i = A[i-1]
    if i in q:
        print(-1)
        break
else:
    print(len(q))

Submission status: Time Limit Exceeded

Solution code:
# ABC 065 B

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

i = 1
q = []
while i != 2:
    q.append(i)
    if A[i-1] in q:
        print(-1)
        break
    i = A[i-1]
else:
    print(len(q))

Submission status: Time Limit Exceeded

Solution code:
# ABC 065 B

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

q = []
i = 1
while i != 2:
    if A[i-1] in q:
        print(-1)
        break
    q.append(i)
    i = A[i-1]
else:
    print(len(q))

Submission status: Time Limit Exceeded

Solution code:
# ABC 065 B

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

q = []
i = 0
while i != 1:
    if A[i] in q:
        print(-1)
        break
    q.append(A[i])
    i = A[i] - 1
else:
    print(len(q))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lis = [1]
cou = 0
ans = 0
bo = []
for i in range(n):
    bo.append(int(input()))
for i in range(n):
    a = bo[cou]
    if a not in lis:
        lis.append(a)
    else:
        print("-1")
        break
    cou = a-1
    ans += 1
    if a == 2:
        print(ans)
        break

Submission status: Time Limit Exceeded

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

q = [[0]]
while q:
    path = q.pop(0)
    n = path[-1]
    if n == 1:
        print(len(path) - 1)
        exit()
    else:
        for x in g[n]:
            if x not in path:
                new_path = path[:]
                new_path.append(x)
                q.append(new_path)
print(-1)

Submission status: Time Limit Exceeded

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

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

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

print(find(n))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
btn = []
checked = []
flag = True
j = 1
count = 0
for i in range(n):
  btn.append(int(input()))

while flag:
  if btn[j - 1] == 2:
    count += 1
    print(count)
    flag = False
  elif j - 1 in checked:
    print(-1)
    flag = False
  else:
    checked.append(j - 1)
    j = btn[j - 1]
    count += 1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())

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

cnt = 0
b = [1]
a = aa[0]
while 1:
    cnt += 1
    if a == 2:
        print cnt
        exit()
    a = aa[a-1]
    if a in b:
        print -1
        exit()
    b.append(a)
    if cnt == n:
        print -1
        exit()

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())

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

cnt = 0
b = [1]
a = aa[0]
while 1:
    cnt += 1
    if a == 2:
        print cnt
        exit()
    a = aa[a-1]
    if a in b:
        print -1
        exit()
    b.append(a)
    if cnt == n:
        print -1

Submission status: Time Limit Exceeded

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

index = 0
count = 0

while (index != 1):
    index = a[index]
    count += 1
    if count >= int(x):
        print(-1)

print(count)

Submission status: Time Limit Exceeded

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

x = 1
y = []

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

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

Submission status: Time Limit Exceeded

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

x = 1
y = []

while x not in y and x != 2:
    y.append(x)
    x = a[x-1]

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

Submission status: Time Limit Exceeded

Solution code:

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque


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

#print(d)

q = []
q.append(1)

point = 1
count = 1

while True:
    if d[point] in q:
        print(-1)
        break
    if d[point] == 2:
        print(count)
        break
        
    q.append(d[point])
    point = d[point]
    count += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque


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

#print(d)

q = []
q.append(1)

point = 1
count = 1

while True:
    if d[point] in q:
        print(-1)
        break
    elif d[point] == 2:
        print(count)
        break
        
    q.append(d[point])
    point = d[point]
    count += 1

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque


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

#print(d)

q = deque()
q.append(1)

point = 1
count = 1

while True:
    if d[point] in q:
        print(-1)
        break
    elif d[point] == 2:
        print(count)
        break
        
    q.append(d[point])
    point = d[point]
    count += 1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

pushed = [0]
while 1:
	next_button = trans[pushed[-1]]
	if next_button  == 2:
		print(len(pushed))
		break
	elif next_button - 1 in pushed:
		print(-1)
		break
	else:
		pushed.append(next_button - 1)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

ans = 0
i = 0
while True:
    if ans != 0 and i in [0, 1]:
        break
    i = A[i]
    ans += 1
if i == 0:
    ans = -1
print ans

Submission status: Time Limit Exceeded

Solution code:
def solve():
    n = int(input())
    l = []

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

    a = 1
    hist = []
    while(True):
        if(a-1 in hist):
            return -1
        if(a-1 == 1):
            return len(hist)
        hist.append(a-1)
        a = l[a-1]

if(__name__ == "__main__"):
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
import sys

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

count = 0
saw = []
now = 1

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

print(-1)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
import sys
N = int(input())
a = [ int(input()) for tmp in range(N) ]
ans = 0
index = 0
while True:
    sav = index #last index
    index = a[sav]-1 #new index
    ans += 1
    if index == 1:
        break
    if a[index] == sav+1: #Loop
        print("-1")
        sys.exit()
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=[int(input()) for i in range(N)]
b=[]
j=1
k=1
if 2 not in a:
    print(-1)
else:
    while j in range(1,N+1):
      if a[k-1] not in b:
         b.append(a[k-1])
         if a[k-1]==1:
                 break
         elif a[k-1]==2:
                 break
         else:
            j += 1
            k = a[k-1]
      else:
          break
    if 2 not in b:
        print(-1)
    else:
        print(len(b))

Submission status: Time Limit Exceeded

Solution code:
def training():
	N = int(input())

	A = [0]

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

	counter = 1
	index = 1
	judge = [1]
	if A[index] == 2:
		print(counter)
		return
	counter += 1
	index = A[index]
	for i in range(N):
		if A[index] in judge:
			print("-1")
			return
		if A[index] == 2:
			print(counter)
			return
		counter += 1
		judge.append(A[index])
		index = A[index]
	

training()

Submission status: Time Limit Exceeded

Solution code:
def training():
	N = int(input())

	A = [0]

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

	counter = 1
	index = 1
	judge = [1]
	if A[index] == 2:
		print(counter)
		return
	counter += 1
	index = A[index]
	while True:
		if A[index] in judge:
			print("-1")
			return
		if A[index] == 2:
			print(counter)
			return
		counter += 1
		judge.append(A[index])
		index = A[index]
	

training()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

tmp=0
i=0
cnt=0
r=[]

while i != 1 and i not in r:
    tmp = a[i]
    r.append(i)
    i = tmp - 1
    cnt += 1
    if i in r:
        break
if i in r:
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

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

history = []
i = 1
count = 0
while True:
    count += 1
    history.append(i)
    i = data_input[i-1]
    if i in history:
        print(-1)
        break
    if i == 2:
        print(count)
        break

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
import sys
l = [int(i) for i in sys.stdin]
used = []
add = used.append
now = 1
count = 0
while True:
  if now == 2:
    break
  if now in used:
    print(-1)
    exit()
  add(now)
  now = l[now]
  count += 1
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a_list = [None]
index_list = list()
loop_dict = dict()

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

    if a == 2:
        index_list.append(i+1)

def rec_search(index, loop):
    if index in loop_dict.keys():
        return loop_dict[index]

    if index == 1:
        return loop + 1

    if loop > N:
        return -1

    i_next_list = [i for i in range(1, N+1) if a_list[i] == index]
    min_loop = N + 1
    for i_next in i_next_list:
        loop_ = rec_search(i_next, loop+1)
        min_loop = min(min_loop, loop_) if loop_ != -1 else min_loop
    loop_dict[index] = min_loop
    return min_loop
        
min_loop = N + 1
for i in index_list:
    loop = rec_search(i, 0)
    min_loop = min(min_loop, loop) if loop != -1 else min_loop

if min_loop == N + 1:
    print(-1)
else:
    print(min_loop)

Submission status: Time Limit Exceeded

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

tmp=0
i=0
cnt=0
r=[]

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

    while i != 1 and i not in r:
        tmp = a[i]
        r.append(i)
        cnt += 1
        i = tmp-1

    if i not in r:
        print(cnt)
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
#Python
import sys

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

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

i=1
#cnt=0
b=[]

while a[i-1]!=2:
	#cnt+=1
	#print a[i-1]
	if i in b:
		print "-1"
		sys.exit()
	b.append(i)
	i=a[i-1]
	#print i,a[i]

#print b
print len(b)+1
#print cnt+1

Submission status: Time Limit Exceeded

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

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

if i not in r:
    print(cnt)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
light = 1
count = 0
a = []
his = []
for i in range(n):
    a.append(int(input()))
while light not in his:
    his.append(light)
    light = a[light-1]
    count += 1
    if light == 2:
        print(count)
        quit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
#coding=UTF-8

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

for idx in range(0,N,1):
    a.append(int(input())-1)

visited=[]
lit=0
length=0
ans=None
while ans==None:
    lit=a[lit] #press
    length+=1
    if lit == 2-1:
        ans=True
    elif lit in visited:
        #ethernal loop
        ans=False
        

    visited.append(lit)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a_list = [None]
index_list = list()

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

    if a == 2:
        index_list.append(i+1)

def rec_search(index, loop):
    if index == 1:
        return loop + 1

    if loop > N:
        return -1

    i_next_list = [i for i in range(1, N+1) if a_list[i] == index]
    min_loop = N + 1
    for i_next in i_next_list:
        loop_ = rec_search(i_next, loop+1)
        min_loop = min(min_loop, loop_) if loop_ != -1 else min_loop
    return min_loop
        
min_loop = N + 1
for i in index_list:
    loop = rec_search(i, 0)
    min_loop = min(min_loop, loop) if loop != -1 else min_loop

if min_loop == N + 1:
    print(-1)
else:
    print(min_loop)

Submission status: Time Limit Exceeded

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


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

    if c>N:
        print -1

Submission status: Time Limit Exceeded

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

r=[]#履歴メモリ
tmp=0
i=0
cnt=0

while i != 1 and i not in r:
    tmp = a[i]
    r.append(i)
    cnt += 1
    i = tmp-1
    if i in r:
        break
if i in r:
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
import sys
l = [int(i) for i in sys.stdin]
used = []
now = 1
count = 0
while True:
  if now == 2:
    break
  if now in used:
    print(-1)
    exit()
  used.append(now)
  now = l[now]
  count += 1
print(count)

Submission status: Time Limit Exceeded

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

b = 1
count = 0
c = []
while(b != 2):
	if(b in c):
		print("-1")
		exit()
	c.append(b)
	b = a[b-1]
	count += 1
print(count)