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 = { (i + 1):int(input()) for i in range(n) }
s = 1
ans = 0
while True:
    if s == 2:
        break
    s = a[s]
    ans += 1
    if s == 1:
        ans = -1
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#ABC65 B
n = int(input())

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

bo = [1]
b = 1
c = 0
while 1:
    c += 1
    b = a[b-1]
    if b in bo:
        print(-1)
        quit()
    else:
        bo.append(b)
    if b == 2:
        break
print(c)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    i = 1
    c = 0
    v = [0]
    while A[i - 1] not in v:
        if A[i - 1] == 2:
            print(c + 1)
            return
        v.append(i)
        i = A[i - 1]
        c += 1
    print(-1)


main()

Submission status: Time Limit Exceeded

Solution code:
import sys
n=int(input())
a=[]
ans=0
for i in range(n):
    s=int(input())
    a+=[s]
i=0
while not(a[i]==2):
    e=i
    i=a[i]-1
    r=a[i]-1
    if e==r:
        print(-1)
        sys.exit()
    ans+=1
print(ans+1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

button = 1
count = 0
while(True):
    button = buttons[button - 1]
    count += 1
    if (button == 2):
        print(count)
        break
    elif(count > N):
        print(-1)

Submission status: Time Limit Exceeded

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

pushed = list()
button = 1
count = 0
while(True):
    button = buttons[button - 1]
    count += 1
    if (button == 2):
        print(count)
        break
    elif(button in pushed):
        print(-1)
        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()))

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

Submission status: Time Limit Exceeded

Solution code:
# 2019/6/25
import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
s = set()
cur = 1
ans = 0
while not cur in s:
    if a[cur-1] == 2:
        print(ans+1)
        sys.exit()
    else:
        cur = a[cur-1]
        ans += 1
print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
pikaa = 1
ans = 0
while 1:
    pikaa = a[pikaa - 1]
    ans += 1
    if pikaa == 1:
        print(-1)
        exit()
    elif pikaa == 2:
        print(ans)
        exit()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def main():
    a = [int(input()) for _ in range(N)]
    visited = []
    idx = 0
    ans = 0
    while True:
        n = a[idx] - 1
        ans += 1
        if n == 1:
            break
        if visited.count(n):
            ans = -1
            break
        visited.append(n)
        idx = n 
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 0
liuse = [1]
place = 1
for i in range(n):
  place = li[place-1]
  liuse.append(place)
  cnt += 1
  if place == 2:
    print(cnt)
    exit()
  if len(liuse) > len(set(liuse)):
    print(-1)
    exit()

Submission status: Time Limit Exceeded

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

cnt = 0
liuse = [1]
place = 1
while True:
  place = li[place-1]
  liuse.append(place)
  cnt += 1
  if place == 2:
    print(cnt)
    exit()
  if len(liuse) > len(set(liuse)):
    print(-1)
    exit()

Submission status: Time Limit Exceeded

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

lst = list(map(lambda x: x-1, lst))
counts = 0
lst_p = [0]
tmp = 0
while True:
    tmp = lst[tmp]
    counts += 1
    if tmp == 1:
        print(counts)
        break
    if tmp in lst_p:
        print(-1)
        break
    lst_p.append(tmp)

Submission status: Time Limit Exceeded

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

idx_li = []

count = 1
bu = li[0]
idx_li.append(bu)
if bu == 2:
  print(count)
  exit()
  
while True:
  bu = li[bu - 1]
  count += 1
  
  if bu == 2:
    print(count)
    exit()
  
  if bu in idx_li:
    print(-1)
    exit()
  else:
    idx_li.append(bu)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
B = [0] + [int(input()) for _ in range(N)]

x = 1
cnt = 0
while True:
  x = B[x]
  cnt += 1
  if x == 2:
    break
  if x == 1:
    cnt = -1
    break
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

li = []

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

check_li = [1]
s = 1
cnt = 0

while True:
    cnt += 1
    s = li[s-1]
    if s in check_li:
        print(-1)
        break
    if s == 2:
        print(cnt)
        break
    check_li.append(s)

Submission status: Time Limit Exceeded

Solution code:
def process(a):
    tmp = a[0]
    pushed = []
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.append(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1

if __name__ == '__main__':
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print(process(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

buttons = []

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

index = 1
cnt = 0
history = []

while index != 2:
    if index in history:
        cnt = -1
        break

    history.append(index)        
    index = buttons[index-1]
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=[int(input())-1 for _ in range(N)]
B=[]
i=0
while(1):
    if i not in B:
        B.append(i)
        i=A[i]
        if i==1:
            print(len(B))
            break
    else:
        print(-1)
        break

Submission status: Time Limit Exceeded

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

match = []
counter = 0
now = 1
for i in range(n):
    if now == 2:
        print(counter)
        exit()

    elif now not in match:
        match += [now]
        now = a[now]
        counter += 1

    else:
        print("-1")
        exit()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
i=0
a=[]
while i<N:
    a.append(int(input()))
    i+=1
num=1
count=0
while num!=2:
    num=a[num-1]
    if num==1:
        count=-1
        break
    else:
        count+=1
print(count)

Submission status: Time Limit Exceeded

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

index = 1
cnt = 0

while True:
    index = a[index - 1]
    cnt += 1
    if index == 2 :
        break
    if index == 1 :
        cnt = -1
        break

print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

res = [1]
i = 1

while i <= n:
  res.append(t[res[-1]-1])
  if res[-1] == 2:
    print(len(res)-1)
    break
  elif len(res) != len(set(res)):
    print(-1)
    break
  i += 1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

num = 0
now = 0
been = set()
been.add(0)

while now != 1:
    now = a[now] - 1
    if now in been:
        num = -1
        break
    else:
        num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:

def read_input():
    n = int(input())
    alist = [-1]

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


    return n, alist

def submit():
    n, alist = read_input()

    through = {}
    curr_button = 1
    steps = 0
    while curr_button not in through.keys():
        through[curr_button] = 1
        curr_button = alist[curr_button]
        steps += 1

        if curr_button == 2:
            break

    if curr_button == 2:
        print(steps)
    else:
        print(-1)



if __name__ == '__main__':
    submit()

Submission status: Time Limit Exceeded

Solution code:

def read_input():
    n = int(input())
    alist = [-1]

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


    return n, alist

def submit():
    n, alist = read_input()

    through = []
    curr_button = 1
    steps = 0
    while curr_button not in through:
        through.append(curr_button)
        curr_button = alist[curr_button]
        steps += 1

        if curr_button == 2:
            break

    if curr_button == 2:
        print(steps)
    else:
        print(-1)



if __name__ == '__main__':
    submit()

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N = int(raw_input())
    arr = []
    for i in range(N):
        arr.append(int(raw_input()))
    turning_on = 1
    count = 0
    turned_on_set = [1]
    while turning_on != 2:
        count += 1
        turning_on = arr[turning_on - 1]
        if turning_on in turned_on_set:
            count = -1
            break
        turned_on_set.append(turning_on)
    print str(count)

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
n,*a=map(int,open(0).read().split())
f=lambda i:a[i]
c=0
h=[]
while True:
  if f(c)==2:break
  if c in h:
    print(-1)
    exit()
  h.append(c)
  c=f(c)-1
print(len(h)+1)

Submission status: Time Limit Exceeded

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

button = [0]
on = 0
ans = 0

while True:
    now = a[on]-1
    ans += 1
    if now not in button:
        if now == 1:
            break
        else:
            on = now
            button.append(now)
    else:
        ans = -1
        break

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
b=[0]
for i in range(n):
    b.append(int(input()))
x=1
r=[1]
while 1:
    i=b[x]
    if i==2:
        print(len(r))
        break
    elif i in r:
        print(-1)
        break
    x=i
    r.append(i)

Submission status: Time Limit Exceeded

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

while(1):
  next_position = arr[next_position-1]
  #print(position)
  if next_position in passed:
    print(-1)
    break
  passed.append(next_position)
  ans += 1
  if next_position == 2:
    print(ans)
    break

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
b=[0]
for i in range(n):
    b.append(int(input()))
x=1
r=[0]
while 1:
    i=b[x]
    if i==2:
        print(len(r))
        break
    elif i in r:
        print(-1)
        break
    x=i
    r.append(i)

Submission status: Time Limit Exceeded

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

pnext = 1
pvisited = []
while True:
	pvisited.append(pnext)
	pnext = A[pnext - 1]
	if pnext == 2:
		print(len(pvisited))
		exit()
	if pnext in pvisited:
		print(-1)
		exit()

Submission status: Time Limit Exceeded

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

pnext = 0
pvisited = []
while True:
	pvisited.append(pnext)
	pnext = A[pnext] - 1
	if pnext == 1:
		print(len(pvisited))
		exit()
	if pnext in pvisited:
		print(-1)
		exit()

Submission status: Time Limit Exceeded

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

count = 0
i = 0
while count <= 10e+7:
    count += 1
    if a[i] == 2:
        print(count)
        break
    else:
        i = a[i]-1
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(500000)
N=int(input())
A=[int(input())-1 for _ in range(N)]
result=[]
app=result.append
def next(k):
	newlight=A[k]
	if k==1:
		return(1)
	elif k in result:
		return(0)
	else:
		app(k)
		return(next(newlight))
if not next(0):
	print(-1)
else:print(len(result))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

cnt = 0
lit = 0

while lit != 1 and (lit != 0 or cnt == 0):
    lit = a[lit] - 1
    cnt += 1

if lit == 1:
    print(cnt)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
data = [int(input()) for i in range(N)]
i = 0
a_i = None
res = 1
pre_i = 0
while data[i] != 2:
    a_i = data[i]
    if pre_i == a_i:
        res = -1
        break

    pre_i = i + 1
    i = data[i] -1
    res += 1
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
i = 0
cnt = 1
while a[i]!=2:
    t = a[i]
    if i == a[t-1]-1:
        print(-1)
        exit()
    i = t-1
    cnt += 1
else:
    print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

B = [1]
i = 0
flag = False
for _ in range(N):
    if 2 in B:
        flag = True
        break
    elif A[i] in B:
        break
    B.append(A[i])
    i = A[i] - 1

print(len(B) - 1) if flag else print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input())-1)  
    
index = 0
count = 0
flag = True
check = []
while flag:
    index = A[index]
    check.append(index)
    count += 1
    if len(list(set(check))) != len(check):
        flag = False
        count = -1
    if index == 1:
        flag = False
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
x = [0] + [int(input()) for i in range(n)]
cnt = 1
idx = min(x[1:])
while True:
    if x[idx] == 2:
        ans = cnt
        break
    elif x[idx] == x.index(x[idx]):
        ans = -1
        break
    elif x.index(x[idx]) == x.index(min(x[1:])):
        ans = -1
        break
    idx = x[idx]
    cnt += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
x = [0] + [int(input()) for i in range(n)]
cnt = 1
idx = min(x[1:])
while True:
    if x[idx] == 2:
        ans = cnt
        break
    elif x.index(x[idx]) == x.index(min(x[1:])):
        ans = -1
        break
    idx = x[idx]
    cnt += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
hako = []
count = 0
flag = True
i = 1

while True:
    hako.append(a[i-1])
    if hako.count(a[i-1]) == 2:
        flag = False
        break
    i = a[i-1]
    count += 1
    if i == 2:
        break

if flag:
    print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [0] + [int(input()) for i in range(N)]
record = [0] * (N + 1)
loop = True
pushing = min(A[1:])
ans = -1
while loop:
    record[A[pushing]] += 1
    if record[2] == 1:
        ans = sum(record)
        loop = False
    if record.count(2) == True:
        loop = False
    pushing = A[pushing]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

i = 0
count = 0

pushed = []

while(1):
  pushed.append(i)
  i = a[i]
  count += 1
  if i == 1:
    break
  elif i in pushed:
    count = -1
    break
    
print(count)

Submission status: Time Limit Exceeded

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

i = 0
count = 0

pushed = []

while(1):
  pushed.append(i)
  i = a[i]
  count += 1
  if i == 1:
    break
  elif i in pushed:
    count = -1
    break
    
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

i = 0
count = 0

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

i = 0
count = 0

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [0] + [int(input()) for i in range(n)]
c = a[1]
p = 1
while c > 2:
    c = a[c]
    p += 1

if c == 2:
    print(p)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lamp=[]
status=1
count=0
possible=1
for i in range(n):
  p=int(input())
  lamp.append(p)
if 2 in set(lamp):
  while status!=2 or count==n:
    status=lamp[status-1]
    count+=1
  if count!=n:
    print(count)
  else:
    print(-1)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lamp=[]
status=1
count=0
possible=1
for i in range(n):
  p=int(input())
  lamp.append(p)
while status!=2 or count==n:
  status=lamp[status-1]
  count+=1
if count!=n:
  print(count)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lamp=[]
status=1
count=0
possible=1
for i in range(n):
  p=int(input())
  lamp.append(p)
while status!=2 or count==n:
  status=lamp[status-1]
  count+=1
  if count==n and status!=2:
    possible=0
if possible==1:
  print(count)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
l = []
for i in range(a):
    t = int(input())
    l.append((i,t-1))
dic=[0]
k=l[0][1]
ans=1
for i in range(a):
    if k == 1:
        print(ans)
        break
    elif k not in dic:
        dic.append(k)
        k=l[k][1]
        ans+=1
    else:
        print(-1)
        break

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
a = [int(input()) for i in range(N)]
b=1
cnt=0
tmp=list()
for i in range(N+1):
    cnt+=1
    if a[b-1]==2:
        break
    if a[b-1] in tmp:
        print(-1)
        sys.exit(0)
    else:
        tmp.append(a[b-1])
        b=a[b-1]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
a = [int(input()) for i in range(N)]
b=1
cnt=0
tmp=list()
while(1):
    cnt+=1
    if a[b-1]==2:
        break
    if a[b-1] in tmp:
        print(-1)
        sys.exit(0)
    else:
        tmp.append(a[b-1])
        b=a[b-1]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
buttons = []
tap_count = 0
buttons_tapped = [0]
for _ in range(N):
    button = int(input())-1
    buttons.append(button)
while True:
    next_button = buttons[buttons_tapped[-1]]
    if next_button in buttons_tapped:
        tap_count = -1
        break
    tap_count += 1
    buttons_tapped.append(next_button)
    if next_button == 1:
        break
print(tap_count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
listA=[]
listIndexStock=[0]
index=0
while index < n:
    a = int(input())
    listA.append(a)
    index+=1

buttonLighted=0
pushCounter=0
index=0
while 1:
    pushCounter+=1
    if listA[buttonLighted] == 2:
        print(pushCounter)
        break
    elif listA[buttonLighted]-1 in listIndexStock:
        print(-1)
        break
    else:
        buttonLighted = listA[buttonLighted]-1
        listIndexStock.append(buttonLighted)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
buttons = [0]
pushed = [0] * (n + 1)
ans = 0
for i in range(n):
  buttons.append(int(input()))
push = 1
while(push != 2):
  if ((buttons[push] not in buttons) or (pushed[push] == 1)):
    print(-1)
    sys.exit()
  else:
    pushed[push] = 1
    push = buttons[push]
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
int_n = int(input())
ar_int = list()
for i in range(int_n):
    ar_int.append(int(input()))

target = 2
counter = 0

while counter < int_n:
    if target in ar_int:
        target = ar_int.index(target) + 1
        counter += 1
        if target == 1:
            print(counter)
            exit(0)
print(-1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    aa = [int(input()) - 1 for _ in range(n)]
    i = 0
    res = 0
    while True:
        if aa[i] == 1:
            print(res + 1)
            break
        if aa[i] == 0:
            print(-1)
            break
        i = aa[i]
        res += 1


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

a_dict = {}

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

answer = 0
now_point = 1
check_list = [1]

for i in range(N):
	now_point = a_dict[now_point]
	answer += 1
	if now_point == 2:
		break

	if now_point in check_list:
		answer = -1
		break
	check_list.append(now_point)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
edges = [[0 for _ in range(n)] for _ in range(n)]
mem = [[0 for _ in range(n)] for _ in range(n)]
dist = []

for i in range(n):
    edges[i][int(input())-1] = 1

def has_edge(edges, v, t, d):
    targets = [i for i, e in enumerate(edges[v]) if e != 0]
    for target in targets:
        if mem[v][target] == 0:
            mem[v][target] = 1
            if target == t:
                dist.append(d)
                return True
            else:
                return has_edge(edges, target, t, d+1)
    return False


print(min(dist)+1) if has_edge(edges, 0, 1, 0) else print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
B = [int(input()) for i in range(N)]
A = [0]
A.extend(B)
past = [1]
next_l = 1
if not 2 in A:
  print(-1)
  exit(0)
for i in range(N):
  next_l = A[next_l]
  if next_l in past or next_l > N:
    print(-1)
    exit(0)
  if next_l == 2:
    print(len(past))
    exit(0)
  past.append(next_l)
print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
B = [int(input()) for i in range(N)]
A = [0]
A.extend(B)
past = [1]
next_l = 1
for i in range(N):
  next_l = A[next_l]
  if next_l in past:
    print(-1)
    break
  if next_l == 2:
   print(len(past))
   break
  past.append(next_l)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[0]+[int(input())for i in range(n)]
p=[1]
k=0
while n:
    if s[p[k]]==2:
        print(k+1)
        break
    elif k==len(set(p)):
        print(-1)
        break
    else:
        p.append(s[p[k]])
        k+=1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
nums = []
dup = []
for i in range(N):
  nums.append(int(input()))
  
temp = 0
for j in range(len(nums)):
  if nums[temp]==2:
    print(j+1)
    exit()
  elif nums[temp]!=2 and nums[temp] in dup:
    print(-1)
    exit()
  else:  
    dup.append(nums[temp])
    temp=nums[temp]-1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

pushed = []

button = 0
count = 0
flag = False

while button not in pushed:
	if button == (2 - 1):
		flag = True
		break
	
	pushed.append(button)
	button = a[button] -1
	count += 1
	
if flag:
	print(count)
else:
	print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
botan = [int(input()) for i in range(n)]
light = 1 # 1番目のボタンが初め光ってる
count = 1
exi = [1]

if 2 in botan:

    while True:
        light = botan[light - 1]
        if light in exi:
            print(-1)
            exit()
        exi.append(light)
    
        if 2 in exi:
            print(count)
            exit()
        count += 1

else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
botan = [int(input()) for i in range(n)]
light = 1 # 1番目のボタンが初め光ってる
count = 1
exi = [1]
while True:
    light = botan[light - 1]
    if light in exi:
        print(-1)
        exit()
        break
    exi.append(light)
    
    if 2 in exi:
        print(count)
        exit()
        break
    count += 1

Submission status: Time Limit Exceeded

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

tmp = 1
while 1:
    current = a[tmp]
    ans += 1
    if tmp == current or current == 1:
        print(a[0])
        exit()
    elif current == 2:
        print(ans)
        exit()
    else:
        tmp = current

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = []
for i in range(N) :
    L.append(int(input()))
A = [0]
flag = 0
button = 1
ans = 0
while flag == 0 :
    button = L[button-1]
    ans += 1
    if (button-1) == 1 :
        print(ans)
        flag = 1
    elif (button-1) in A :
        print(-1)
        flag = 1
    else :
        A.append(button-1)

Submission status: Time Limit Exceeded

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

A = []
for i in range(N+1) :
    if i == 0 :
        button = 1
        ans = 0
    elif button == 2 :
        break
    else :
        button = L[button-1]
        if button in A :
            ans = -1
            break
        else :
            A.append(button)
            ans += 1
print(ans)