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: Wrong Answer

Solution code:
def trained(n):
    x = [ int(input()) for i in range(n)]
    pushing = min(x) - 1
    for i in range(n):
        if x[pushing] == 2:
            return i + 1
        pushing = x[pushing] - 1
    return -1
N = int(input())
print(trained(N))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
lst = [int(input())]
lst += [int(input()) for x in range(lst[0])]
answerExists = False
for i in range(lst[0]+1):
    if i == 0:
        el = lst[1]
    if lst[el] == 2:
        answerExists = True
        print(i+2); break
    el = lst[el]
if answerExists == False:
    print(-1)

Submission status: Wrong Answer

Solution code:
lst = [int(input())]
lst += [int(input()) for x in range(lst[0])]
answerExists = False
for i in range(lst[0]):
    if i == 0:
        el = lst[1]
    if lst[el] == 2:
        answerExists = True
        print(i+2); break
    el = lst[el]
if answerExists == False:
    print(-1)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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:
  if lamp[status-1]!=0:
    status=lamp[status-1]
    lamp[status-1]=0
    count+=1
  else:
    print(-1)
    break
print(count)

Submission status: Wrong Answer

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

for i in range(10**5):
    if a[p] == 2:
        ans += 1
        result = True
        break
    else:
        p = a[p]-1
        ans += 1

if result == True:
    print(ans)
else:
    print("-1")

Submission status: Wrong Answer

Solution code:
N = int(input())

a = []

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

done = [False] * N

i = 0

ans = 0

while done[i] == False:
    if done[a[i]] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input()) 
b = [int(input()) for i in range(n)]
cnt = 1
bs = b[0] - 1 
while cnt < n+1:
    cnt += 1
    if b[bs] == 2:

        print(cnt)
        exit()
    else:
        bs = b[bs] -1
print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]  # parser
A += [int(input()) for i in range(N)]
count = 1
flashed = A[1]
while True:
    flashed = A[flashed]
    count += 1
    if flashed == 2:
        print(count)
        exit(0)
    elif count>10**6:
        print(-1)
        exit(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]  # parser
A += [int(input()) for i in range(N)]
count = 1
flashed = A[1]
while True:
    flashed = A[flashed]
    count += 1
    if flashed == 2:
        print(count)
        exit(0)
    elif count>10**5:
        print(-1)
        exit(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]  # parser
A += [int(input()) for i in range(N)]
count = 1
befores = [0, 0, 0]
flashed = A[1]
befores[0] = A[1]
while True:
    flashed = A[flashed]
    befores[2] = befores[1]
    befores[1] = befores[0]
    befores[0] = flashed
    count += 1
    if flashed == 2:
        print(count)
        exit(0)
    elif count>10**5:
        print(-1)
        exit(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]  # parser
A += [int(input()) for i in range(N)]
count = 1
befores = [0, 0, 0]
flashed = A[1]
befores[0] = A[1]
while True:
    flashed = A[flashed]
    befores[2] = befores[1]
    befores[1] = befores[0]
    befores[0] = flashed
    count += 1
    if flashed == 2:
        print(count)
        exit(0)
    elif befores[2] == befores[0]:
        print(-1)
        exit(0)
    elif count>10**5:
        print(-1)
        exit(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]  # parser
A += [int(input()) for i in range(N)]
count = 1
befores = [0, 0, 0]
flashed = A[1]
befores[0] = A[1]
while True:
    flashed = A[flashed]
    befores[2] = befores[1]
    befores[1] = befores[0]
    befores[0] = flashed
    count += 1
    if flashed == 2:
        print(count)
        exit(0)
    elif befores[2] == befores[0]:
        print(-1)
        exit(0)

Submission status: Wrong Answer

Solution code:
n = int(input()) #3
b = [int(input()) for i in range(n)]
cnt = 1
bs = b[0] - 1 
while cnt < n:
    cnt += 1
    if b[bs] == 2:

        print(cnt)
        exit()
    else:
        bs = b[bs] -1
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input()) #3
b = [int(input()) for i in range(n)]
cnt = 1
bs = b[0] - 1 
while cnt <= n:
    cnt += 1
    if b[bs] == 2:

        print(cnt)
        exit()
    else:
        bs = b[bs] -1
print(-1)

Submission status: Wrong Answer

Solution code:
def func():
    pass


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

    m = a[0]
    for i in range(1000):
        if m == 2:
            print(i + 1)
            return
        m = a[m - 1]
    print(-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

c = 0
x = 1
while(x != 2):
  x = a[x-1]
  if not x:
    c = -1
    break
  else:
    c += 1
    a[x - 1] = False

print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())

A = []
B = []
for i in range(n):
    A.append(int(input()))
    B.append(i+1)

count=1
ans=0
i = A[0]
C = []
while(count<=n):
    C.append(i)
    count+=1
    if(A[B.index(i)] == 2):
        print(count)
        ans=1
        break
    else:
        i = A[B.index(i)]
        if(B.index(i) in C):
            print(-1)
            ans=1
            break
if(ans==0):
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())

A = []
B = []
for i in range(n):
    A.append(int(input()))
    B.append(i+1)

count=1
ans=0
i = A[0]
C = []
while(count<=n):
    C.append(i)
    count+=1
    if(A[B.index(i)] == 2):
        print(count)
        ans=1
        break
    else:
        i = A[B.index(i)]
        if(i in C):
            print(-1)
            ans=1
            break
if(ans==0):
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())

A = []
B = []
for i in range(n):
    A.append(int(input()))
    B.append(i+1)

count=1
ans=0
i = A[0]
while(count<=n):
    count+=1
    if(A[B.index(i)] == 2):
        print(count)
        ans=1
        break
    else:
        i = A[B.index(i)]
if(ans==0):
    print(-1)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[0]*(N+1)
for i in range(N):
    a[i+1]=int(input())
    flag=False
j=1
cnt=1
while flag==False and cnt<=N+1:
    if a[a[j]]==2:
        flag=True
        cnt+=1
    else:
        j=a[j]
        cnt+=1
if flag==True:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[0]*(N+1)
for i in range(N):
    a[i+1]=int(input())
    flag=False
j=1
cnt=1
while flag==False and cnt<=N:
    if a[a[j]]==2:
        flag=True
        cnt+=1
    else:
        j=a[j]
        cnt+=1
if flag==True:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[0]*(N+1)
for i in range(1,N+1):
    a[i]=int(input())
    flag=False
j=1
cnt=1
while flag==False and cnt<=N:
    if a[a[j]]==2:
        flag=True
        cnt+=1
    else:
        j=a[j]
        cnt+=1
if flag==True:
    print(cnt)
else:
    print(-2)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[0]*(N+1)
for i in range(1,N+1):
    a[i]=int(input())
    flag=False
j=1
cnt=1
while flag==False and cnt<=N:
    if a[a[j]]==2:
        flag=True
        cnt+=1
    else:
        j=a[j]
        cnt+=1
if flag==True:
    print(cnt)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
buttons = [int(input()) for _ in range(N)]
count = 1
tmp = buttons[0]
flag = True
while count < N+2:
    tmp = buttons[tmp-1]
    count += 1
    if tmp == 2:
        print(count)
        flag = False
        break
if flag:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
buttons = [int(input()) for _ in range(N)]
count = 1
tmp = buttons[0]
flag = True
while count < N+1:
    tmp = buttons[tmp-1]
    count += 1
    if tmp == 2:
        print(count)
        flag = False
        break
if flag:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
buttons = [int(input()) for _ in range(N)]
count = 1
tmp = buttons[0]
flag = True
while count < N:
    tmp = buttons[tmp-1]
    count += 1
    if tmp == 2:
        print(count)
        flag = False
        break
if flag:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [0 for _ in range(n)]
for i in range(n):
    a[i] = int(input()) - 1
visited = [False for _ in range(n)]
visited[0] = True
next = a[0]
count = 1
while True:
    if not visited[a[next]]:
        visited[a[next]] = True
        count += 1
        next = a[next]
        if next == 1:
            print(count)
            exit()
    else:
        print(-1)
        exit()

Submission status: Wrong Answer

Solution code:
n = int(input())
edges = {i: int(input())-1 for i in range(n)}
mem = [[0 for _ in range(n)] for _ in range(n)]
d = 0


def has_edge(edges, v, g, d):
    target = edges[v]
    if mem[v][target] == 0:
        mem[v][target] = 1
        if target == g:
            return d
        else:
            return has_edge(edges, target, g, d+1)
    return d


d = has_edge(edges, 0, 1, d)
print(d+1) if d else print(-1)

Submission status: Wrong Answer

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

for i in range(n):
    print(''.join([str(e) for e in edges[i]]))


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: Wrong Answer

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)
for i in range(N):
  next_l = A[next_l]
  if next_l in past or next_l > N:
    print(-1)
    break
  if next_l == 2:
   print(len(past))
   break
  past.append(next_l)
print(-1)

Submission status: Wrong Answer

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)
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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = []
count = 1
for i in range(n):
    num_list.append(int(input())-1)
#[2,2,3,1,3]
#1があるindexに行くのが目標
ind = num_list[0]
for i in range(n-1):
    ind = num_list[ind]
    count += 1
    if ind == 1:
        print(count)
        break
if count > n-1:
   print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = []
count = 0
for i in range(n):
    num_list.append(int(input())-1)
#[2,2,3,1,3]
#1があるindexに行くのが目標
ind = num_list[0]
for i in range(n):
    ind = num_list[ind]
    count += 1
    if ind == 1:
        print(count+1)
        break
if count+1 > n-1:
   print(-1)

Submission status: Wrong Answer

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

if 2 in botan:
    print(-1)
    exit()

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

Submission status: Wrong Answer

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

tmp = 1
for i in range(1,n+1):
    current = a[tmp]
    ans += 1
    if current == 2:
        exit()
    else:
        tmp = current
        print(tmp)

print(a[0])

Submission status: Wrong Answer

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

tmp = 1
for i in range(1,n+1):
    current = a[tmp]
    ans += 1
    if current == 2:
        print(ans)
        exit()
    else:
        tmp = current
        print(tmp)

print(a[0])

Submission status: Wrong Answer

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

tmp = 1
for i in range(1,n+1):
    current = a[tmp]
    ans += i
    if current == 2:
        print(ans)
        exit()
    else:
        tmp = current

print(a[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
route = [a[0]]
count = 1
flag = 0
for i in range(1, N):
    count += 1
    route.append(a[route[i-1]-1])
    if route[i] == 2:
        flag = 1
        break

if flag == 0:
    print(-1)
else:
    print(len(route))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
button = []
id = 1
times = 0

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

for i in range(n):
        times += 1
        id = button[id-1]
        if id == 2:
                print(id)
                break
else:
        print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(len(a)):
  if b==2:
    print(ans)
    exit()
  b = a[b-1]
  ans += 1
print

Submission status: Wrong Answer

Solution code:
import copy
N = int(input())
ans = 0
tentou = 1
re_ans = [-1]
for i in range(1, N+1):
    ai = int(input())
    
    if tentou == i:
        tentou = ai
        ans += 1
        
        if ai == 2:
            re_ans.append(copy.copy(ans))

if len(re_ans) >= 2:
    print(re_ans[1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
tentou = 1
hantei = 0
for i in range(1, N+1):
    ai = int(input())
    
    if tentou == i:
        tentou = ai
        ans += 1
        
        if ai == 2:
            hantei = 1
            print(ans)
            break

if hantei == 0:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
tentou = 1
re_ans = [-1]
for i in range(1, N+1):
    ai = int(input())
    
    if tentou == i:
        tentou = ai
        ans += 1
        
        if tentou == 2:
            re_ans.append(ans)

if len(re_ans) >= 2:
    print(re_ans[1])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
tentou = 1

for i in range(1, N+1):
    ai = int(input())
    
    if tentou == i:
        tentou = ai
        ans += 1
        
        if tentou == 2:
            print(ans)
            break
else:
    print(-1)

Submission status: Wrong Answer

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

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

x = 1
b = 0

for i in range(n+1):
	b = a[b]-1
	x +=1
	if a[b] == a[1]:
		print(x-1)
		break
else:
       print(-1)

Submission status: Wrong Answer

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

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

x = 0
b = 0

for i in range(n+1):
	b = a[b]-1
	x +=1
	if a[b] == a[1]:
		print(x)
		break
else:
       print(-1)

Submission status: Wrong Answer

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

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

x = 0
b = 0

for i in range(n):
	b = a[b]-1
	x +=1
	if a[b] == a[1]:
		print(x)
		break
else:
       print(-1)

Submission status: Wrong Answer

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)
  k = a[k]
  c += 1
print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = []

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

now = a[0]
count = 1

while now != 2 and count < n:
    count += 1
    now = a[now - 1]

    if now == 2:
        print(count)
        exit(0)

    if count >= n:
        print("-1")
        exit(0)

Submission status: Wrong Answer

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
        buttun[light]=1
    if light==1:
        ans=-1
        break
print(ans)

Submission status: Wrong Answer

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

light=1
ans=0
while True:
    if buttun[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: Wrong Answer

Solution code:
N = int(input())
A = []
for a in range(N) :
    a = int(input())
    A.append(a)
p = A[0]
answer = 1
for i in range(N-1) :
    if p == 2 :
        break
    elif i == (p-1) :
        p = A[i]
        answer += 1
if p == 2 :
    print(answer)
else :
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for a in range(N) :
    a = int(input())
    A.append(a)
p = A[0]
answer = 1
for i in range(N) :
    if p == 2 :
        break
    elif i == (p-1) :
        p = A[i]
        answer += 1
if p == 2 :
    print(answer)
else :
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
start = a[0]
counting = 1
coreect = 0
if start == 1:
    print(counting)

else:
    while N >= 1:
        
        N -= 1
        counting += 1
        start = a[start-1]
        if start == 2:
            coreect = 1
            print(counting)
            break

if coreect == 0:
    print(-1)

Submission status: Wrong Answer

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
for i in range(n):
    if button == i:
        button = buttons_int[i]
        count += 1
        if count >= n**2:
            print(-1)
            exit()
        if button == 1:
            print(count)
            exit()
print(-1)

Submission status: Wrong Answer

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
for i in range(n):
    if button == i:
        button = buttons_int[i]
        count += 1
        if button == 0:
            print(-1)
            exit()
        if button == 1:
            print(count)
            exit()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for i in range(0,n)]
j=0
ans=-1
for i in range(0,n):
    if a[j] == 2:
        ans = j
        break
    j = a[j]-1

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for i in range(0,n)]
j=0
ans=-1
for i in range(0,n):
    if a[j] == 2:
        ans = j
        break
    j = a[j]-1

print(ans)

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = [int(input()) for _ in range(n)]
    a_list.insert(0, 0)

    position = 1
    cnt = 0
    for i in range(n):
        position = a_list[position]
        cnt += 1
        if position == 2:
            print(cnt + 1)
            exit()
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = [int(input()) for _ in range(n)]
    a_list.insert(0, 0)

    position = a_list[1]
    cnt = 0
    for i in range(1000000):
        position = a_list[position]
        cnt += 1
        if position == 2:
            print(cnt + 1)
            exit()
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = [int(input()) for _ in range(n)]
    a_list.insert(0, 0)

    position = a_list[1]
    cnt = 0
    for i in range(100001):
        position = a_list[position]
        cnt += 1
        if position == 2:
            print(cnt + 1)
            exit()
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = [int(input()) for _ in range(n)]
    a_list.insert(0, 0)

    position = a_list[1]
    cnt = 0
    for i in range(100000):
        position = a_list[position]
        cnt += 1
        if position == 2:
            print(cnt + 1)
            exit()
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = [int(input()) for _ in range(n)]
    a_list.insert(0, 0)

    position = a_list[1]
    cnt = 0
    for i in range(10000):
        position = a_list[position]
        cnt += 1
        if position == 2:
            print(cnt + 1)
            exit()
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
d={i:0 for i in range(1,N+1)}
for i in range(N):
  d[i+1]=int(input())

s=set()
t=d[1]
c=1
while 1:
  c+=1
  if t in s:
    print("-1")
    break
  s.add(t)
  if d[t]==2:
    print(c)
    break
  t=d[t]

Submission status: Wrong Answer

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:
        break
print(len(history))

Submission status: Wrong Answer

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

pointer = input_list[0]
count = 1
index = 0

while True:
    pointer = input_list[pointer-1]
    count += 1
    index += 1
    if pointer == 2:
        print(count)
        break
    if index == 10**5 + 1:
        print(-1)
        break

Submission status: Wrong Answer

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

pointer = input_list[0]
count = 1

for _ in range(N):
    pointer = input_list[pointer-1]
    count += 1
    if pointer == 2:
        print(count)
        break
else:
    print(-1)

Submission status: Wrong Answer

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

flag = False
if a[0] == 2:
    print(1)
else:
    count = 0
    i = a[0]
    for _ in range(n):
        count += 1
        if a[i-1] == 2:
            flag = True
            break
        i = a[i-1]

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

Submission status: Wrong Answer

Solution code:
N=int(input())
hairetsu=[]
count=0
pos=0

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

while count<=10001:
    pos=hairetsu[pos]-1
    count+=1
    if pos==1:
        print(count)
        exit()
print(-1)

Submission status: Wrong Answer

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

for i in range(n):
    a = int(input())
    b[i+1] = b.get(i+1, 0) + a

key, t = b.get(1, 0), 1
key_set = {1, key}
while key != 0:
    key = b.get(key, 0)
    if key in key_set:
        print(-1)
        break

    key_set.add(key)
    t += 1

    if key == 2:
        print(t)
        break

else:
    print(-1)

Submission status: Wrong Answer

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

for i in range(n):
    a = int(input())
    b[i+1] = b.get(i+1, 0) + a

key, t = b.get(1, 0), 1
key_set = {1}
while key != 0:
    key = b.get(key, 0)
    if key in key_set:
        print(-1)
        break

    key_set.add(key)
    t += 1

    if key == 2:
        print(t)
        break

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
b, p = {1}, 0

for i in range(len(a)):
    if i+1 in b:
        b.remove(i+1)
        b.add(a[i])
        p += 1

    if 2 in b:
        print(p)
        break

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input())-1 for _ in range(n)]
now = 0
checked = set()
for i in range(1, n+1):
  now = a[now]
  if now==1:
    print(i)
else:
  print(-1)

Submission status: Wrong Answer

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

N = int(input())

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

pushed = set([1])
button_next = dict[1]
while True:
    if button_next not in pushed:
        pushed.add(button_next)
        button_next = dict[button_next]
        if button_next == 2:
            print(len(pushed))
            break
    else:
        print(-1)
        break

Submission status: Wrong Answer

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

N = int(input())

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

pushed = []
button_next = dict[1]
while True:
    if button_next not in pushed:
        pushed.append(button_next)
        button_next = dict[button_next]
        if button_next == 2:
            fail = False
            break
    else:
        fail = True
        break
if fail:
    print(-1)
else:
    print(len(pushed)+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
lights = [int(raw_input()) for i in range(int(raw_input()))]
lamps = [0]*len(lights)
lamps[0] = 1
lampco = len(set(lights))
for i in xrange(len(lamps)):
    if (i > 0):
        lamps[int(lights[i])-1] += 1
    if lamps[1] == 1:
        print i
        exit()
print -1

Submission status: Wrong Answer

Solution code:
3
3
1
2

Submission status: Wrong Answer

Solution code:
lights = [int(raw_input()) for i in range(int(raw_input()))]
lamps = [0]*len(lights)
lamps[0] = 1
lampco = len(set(lights))
for i in xrange(len(lamps)):
    lamps[int(lights[i])-1] += 1
    if lamps[1] == 1:
        print i
        exit()
print -1