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:
n=int(input())
a=[0]*n
for _ in range(n):
  a[_]=int(input())
c=1
p=a[0]
while True:
  p=a[p-1]
  c+=1
  if p==2:
    break
  if p==1:
    c=-1
    break
print(c)

Submission status: Wrong Answer

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

stack = [2]

can = False
while True:
    if stack[len(stack)-1] == 1:
        can = True
        break
    c = stack[len(stack)-1]
    if c not in a:
        break
    i = a.index(c)+1
    if i in stack:
        break
    stack.append(i)

if can:
    print(len(stack)-1)
else:
    print(-1)

Submission status: Wrong Answer

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

i = 2
f = a[0]
while i< 10**6:
    f = a[f-1]
    if f == 2:
        print(i)
        break
    elif i>n+1:
        print(-1)
        break
    else:
        i+=1

Submission status: Wrong Answer

Solution code:
import functools


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    a = list(map(lambda x: int(x), inputs))
    a[1] = -1
    return wh(a, 2, 0)


def wh(a, search, count):
    counts = []

    if search == 1:
        return count

    for i, value in enumerate(a):
        if value == search:
            counts.append(wh(a, i + 1, count + 1))

    counts_ignore_minus = list(filter(lambda x: x != -1, counts))

    if len(counts_ignore_minus) == 0:
        return -1

    min = counts_ignore_minus[0]
    for c in counts_ignore_minus:
        if min > c:
            min = c
    return min

Submission status: Wrong Answer

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

    ans = -1
    ct = 0
    light = 1
    for _ in range(N):
        light = a[light]
        ct += 1
        if light == 2:
            ans = ct

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input())-1 for _ in range(n)]
cnt=[1]+[0]*(n-1)
ans=0
l=0
for i in range(n):
    l=a[l]
    cnt[l]+=1
    if l==1:
        ans=sum(cnt)-1
if ans==n:
    ans=-1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input())-1 for _ in range(n)]
cnt=[1]+[0]*(n-1)
ans=-1
l=0
for i in range(n):
    l=a[l]
    cnt[l]+=1
    if l==1:
      if sum(cnt)==1:
        ans=1
      else:
        ans=sum(cnt)-1
    if cnt[l]==2:
        break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#coding: utf-8

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

start = 0
count = 0

for i in range(N):
    count += 1
    next = a[start]
    start = a[next-1]
    if(start == 2):
        print(count)
        break
    start -= 1
else:
    print(-1)

Submission status: Wrong Answer

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

n = int(input())
a_list = [int(input()) for i in range(n)]
ans = 0

if 1 in a_list:
    idx = a_list.index(1)
else:
    idx = 0

for i in range(n):
    if a_list[idx] == 2:
        break
    ans += 1
    idx = a_list[idx]-1

if ans == n:
    ans = -1

print(ans)

Submission status: Wrong Answer

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

n = int(input())
a_list = [int(input()) for i in range(n)]
ans = 0

if 1 in a_list:
    idx = a_list.index(1)
else:
    idx = 0
    ans += 1

for i in range(n):
    if a_list[idx] == 2:
        break
    ans += 1
    idx = a_list[idx]-1

if ans == n:
    ans = -1

print(ans)

Submission status: Wrong Answer

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

data.insert(0, 0)

i = 1
cnt = 0

push_button = {1: "PUSH"}

while True:
  
  trans_value = data[i]
  
  cnt += 1

  push_button[trans_value] = "PUSH"
  
  if trans_value in push_button:
    cnt = -1
    break
    
  if trans_value == 2:
    break
    
print(cnt)

Submission status: Wrong Answer

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

data.insert(0, 0)

i = 1

push_button = {1: "PUSH"}

while True:
  
  trans_value = data[i]

  push_button[trans_value] = "PUSH"
  
  if trans_value in push_button:
    cnt = -1
    break
  
  cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
length = int(input())  
 
data = ([int(input()) for i in range(length)])

data.insert(0, 0)

i = 1
v = 0
cnt = 0

if data[i] == i:
  cnt = -1
else:
  while v != 2:
    v = data[i]
    if data[v] == i:
      cnt = -1
      break
    i = v
    cnt += 1
  
print(cnt)

Submission status: Wrong Answer

Solution code:
length = int(input())  
 
data = ([int(input()) for i in range(length)])

data.insert(0, 0)

i = 1
v = 0
cnt = 0

while v != 2:
  
  v = data[i]
  
  if data[v] == i:
    cnt = -1
    break
  
  i = v

  cnt += 1
  
print(cnt)

Submission status: Wrong Answer

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

index = 1
cnt = 0

for _ in range(N):
    index = a[index - 1]
    cnt += 1
    if index == 2 :
        break
    if index == 1 :
        cnt = -1
        break

print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(int(input()) for _ in range(n))
b=a[0]
c=0
for i in range(n-1):
    b=a[b-1]
    if b==2:
        c=i+2
        break
print(c if c!=0  else -1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(int(input()) for _ in range(n))
b=a[0]
c=0
for i in range(n):
    b=a[b-1]
    if b==2:
        c=i+2
        break
print(c if c!=0  else -1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
List = [int(input()) for _ in range(n)]
c, s = 1, List[0]
while s!=2 and c>=n:
  c+=1
  s = List[s-1]
print(-1 if c>=n else c)

Submission status: Wrong Answer

Solution code:
n = int(input())
List = [int(input()) for _ in range(n)]
c, s = 1, List[0]
while List[s-1]!=2 and c >= n:
  c+=1
  s = List[s-1]
print(-1 if c>=n else c)

Submission status: Wrong Answer

Solution code:
n = int(input())
List = [int(input()) for _ in range(n)]
c, s = 0,1
while List[s-1]!=2 and c >= n:
  c+=1
  s = List[s-1]
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
List = [int(input()) for _ in range(n)]
s = List[0]
count = 1
while s!=2 and count<n:
    count+=1
    s = List[s-1]
print(count if count>=n else -1)

Submission status: Wrong Answer

Solution code:
#ABC065B
N = int(input())
ai = [int(input()) for _ in range(N)]
if ai[0] == 2:
    print("0")
else:
    lightning = ai[0]
    count = 1
    ok = False
    for _ in range(N):
        lightning = ai[lightning-1]
        count += 1
        if lightning == 2:
            print(count)
            ok = True
            break

    if not ok:
        print("-1")

Submission status: Wrong Answer

Solution code:
#ABC065B
N = int(input())
ai = [int(input()) for _ in range(N)]
if ai[0] == 2:
    print("0")
else:
    lightning = ai[0]
    count = 1
    ok = False
    for _ in range(N):
        lightning = ai[lightning-1]
        count += 1
        if lightning == 2:
            print(count)
            ok = True
        break

    if not ok:
        print("-1")

Submission status: Wrong Answer

Solution code:
#ABC065B
N = int(input())
ai = [int(input()) for _ in range(N)]
if ai[0] == 2:
    print("0")
else:
    lightning = ai[0]
    count = 1
    ok = False
    lightning = ai[lightning-1]
    count += 1
    for _ in range(N):
        if lightning == 2:
            print(count)
            ok = True
        break

    if not ok:
        print("-1")

Submission status: Wrong Answer

Solution code:
#ABC065B
N = int(input())
ai = [int(input()) for _ in range(N)]
if ai[0] == 2:
    print("0")
else:
    lightning = ai[0]
    count = 1
    ok = False
    for _ in range(N):
        if lightning == 2:
            print(count)
            ok = True
        break
    lightning = ai[lightning-1]
    count += 1

    if not ok:
        print("-1")

Submission status: Wrong Answer

Solution code:
#ABC065B
N = int(input())
ai = [int(input()) for _ in range(N)]
lightning = ai[0]
count = 1
ok = False
for i in range(N):
    lightning = ai[lightning-1]
    count += 1
    if lightning == 2:
        print(count)
        ok = True
        break
if not ok:
    print("-1")

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
cnt = 1
button = 1
for _ in range(N):
    button = a[button - 1]
    cnt += 1
    if button == 2:
        print(cnt)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
cnt = 1
button = a[0]
print(button)
while button != 2:
    button = a[button - 1]
    cnt += 1
    if cnt > N:
        print(-1)
        break
else:
    print(cnt)

Submission status: Wrong Answer

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

count = 0
i = 1
for j in range(n+1):
    if a[i-1]==2:
        count += 1
        print(count)
        break
    else:
        i = a[i-1]
        print(a[i-1], i)
        count += 1
    if j ==n:
        print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
trace = [False for _ in range(n)]
trace[0] = True
pos = 0
push_count = 1

while True:
    next_pos = a[pos] - 1
    push_count +=1
    if a[next_pos]==2:
        print(push_count)
        break
    if not trace[next_pos]:
        trace[next_pos] = True
        pos = next_pos
    else:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
trace = [False for _ in range(n)]
trace[0] = True
pos = 1
push_count = 1

while True:
    next_pos = a[pos-1]
    push_count +=1
    if a[next_pos-1]==2:
        print(push_count)
        break
    if not trace[next_pos-1]:
        trace[next_pos-1] = True
        pos = next_pos
    else:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
trace = [False for _ in range(n)]
trace[0] = True
pos = 1
push_count = 1
while True:
    next_pos = a[pos-1]
    push_count +=1
    if a[next_pos-1]==2:
        print(push_count+1)
        break
    if not trace[next_pos-1]:
        trace[next_pos-1] = True
        pos = next_pos
    elif trace[next_pos-1]:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
trace = [False for _ in range(n)]
trace[0] = True
pos = 1
push_count = 1
while True:
    next_pos = a[pos-1]
    push_count +=1
    if a[next_pos-1]==2:
        print(push_count)
        break
    if not trace[next_pos-1]:
        trace[next_pos-1] = True
        pos = next_pos
    elif trace[next_pos-1]:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
N = int(input())

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

start = As[0]
steps = 1
while True:
    next = As[start - 1]
    steps += 1
    if next == 2:
        break
    else:
        if next == start or steps > N:
            print(-1)
            exit()

    start = next

print(steps)

Submission status: Wrong Answer

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

button = 1
for i in range(N):
    button = a[button-1]
    if button == 2:
        print(i)
else:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(N):
  if a[j] == 2:
    flag = True
    break
  else:
    count += 1
    j = a[j]-1


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

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

light = s[0]
for i in range(n + 1):
  light = s[light - 1]
  if light == 2:
    print(i + 2)
    exit()
  else:
    continue
print(-1)

Submission status: Wrong Answer

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

light = s[0]
for i in range(n + 1):
  light = s[light - 1]
  if light == 2:
    print(i + 2)
    exit()
  else:
    continue
print("-1")

Submission status: Wrong Answer

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

light = s[0]
for i in range(n - 1):
  light = s[light - 1]
  if light == 2:
    print(i + 2)
    exit()
  else:
    continue
print("-1")

Submission status: Wrong Answer

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

light = s[0]
for i in range(n):
  light = s[light - 1]
  if light == 2:
    print(i + 2)
    exit()
  else:
    continue
print("-1")

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [0 for _ in range(n)]
x[0] = 1
flag = True
for i in range(n):
    ai = int(input()) - 1
    if i != ai and x[i] > 0:
        if x[ai] > 0:
            x[ai] = min(x[ai], x[i] + 1)
        else:
            x[ai] = x[i] + 1
if x[1] > 0:
    print(x[1] - 1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [0 for _ in range(n)]
x[0] = 1
flag = True
for i in range(n):
    ai = int(input()) - 1
    if i != ai and x[i] > 0:
        if x[ai] > 0:
            x[ai] = min(x[ai], x[i] + 1)
        else:
            x[ai] = x[i] + 1
        x[i] = 0
if x[1] > 0:
    print(x[1] - 1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [0 for _ in range(n)]
x[0] = 1
flag = True
for i in range(n):
    ai = int(input()) - 1
    if i != ai and x[i] > 0:
        if x[ai] > 0:
            x[ai] = min(x[ai], x[i] + 1)
        else:
            x[ai] = x[i] + 1
        x[i] = 0
    if flag and x[1] > 0:
        print(x[1] - 1)
        flag = False
if flag:
    print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
hoge=[]
for i in range(n):
  hoge.append(int(input()))
ans=0
punch=0
while 1:
  ans+=1
  punch=hoge[punch-1]
  if(punch==1):
    print(ans)
    break
  if(ans>=100001):
    print(-1)
    break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

ind=0
cnt=1
while True:
    ind=A[ind]-1
    cnt+=1
    if A[ind]==2:
        break
    if cnt>n:
        cnt=-1
        break

print(cnt)

Submission status: Wrong Answer

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

memo = [0] * N
memo[0] = 1
ok = [0] * N
ok[1] = 1

cnt = 0
for j in range(N):
    memo[j] = 0
    memo[A[j] - 1] = 1
    cnt += 1
    if memo == ok:
        print(cnt)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("./logtest.log")
logger.addHandler(h)

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

if 2 in A:
    print(len(A) - A.count(2))
else:
    print(-1)

logger.info({"N": N})
logger.info({"A": A})

Submission status: Wrong Answer

Solution code:
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("./logtest.log")
logger.addHandler(h)

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

memo = set(A)
if 2 in memo:
    print(len(memo) - 1)
else:
    print(-1)

logger.info({"N": N})
logger.info({"A": A})

Submission status: Wrong Answer

Solution code:
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = logging.FileHandler("./logtest.log")
logger.addHandler(h)

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

if 2 in A:
    print(N-1)
else:
    print(-1)

logger.info({"N": N})
logger.info({"A": A})

Submission status: Wrong Answer

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

ind = 0
ans = 1
for i in range(1,N):
#     print("A[ind]",A[ind])

    ind = A[ind]-1
    ans +=1
#     print("IND",ind)
    if A[ind] ==2:
        print(ans)
        exit()
if A[ind] !=2:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int, [input() for i in range(N)]))
v = 1
for i in range(100000):
    if l[v - 1] == 2:
        print(i+1)
        break
    else:
        v = l[v - 1]
        if i == 99:
            print('-1')

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int, [input() for i in range(N)]))
v = 1
for i in range(100):
    if l[v - 1] == 2:
        print(i+1)
        break
    else:
        v = l[v - 1]
        if i == 99:
            print('-1')

Submission status: Wrong Answer

Solution code:
n,ans=int(input()),-1
a,m=[int(input())for i in range(n)],1
for i in range(n):
  m=a[m-1]
  if m==2:ans=i+1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
i = 1
c = 0
cnt = 0
while c < n+1:
    b = a[i-1]
    a[i-1] = False
    if a[b-1]:
        cnt += 1
    else:
        break
    
    if a[b-1] == 2:
        print(cnt+1)
        exit()
    i = b
    c += 1
    
print(-1)

Submission status: Wrong Answer

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

def push(i):
    return A[i] - 1

index = 0
cnt = 0
for _ in range(N):
    index = push(index)
    cnt += 1
    if index == 1:
       print(cnt)
       exit()
print

Submission status: Wrong Answer

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

def push(i):
    return A[i] - 1

print(A)

index = 0
cnt = 0
for _ in range(N):
    index = push(index)
    cnt += 1
    if index == 1:
       print(cnt)
       exit()
print(-1)

Submission status: Wrong Answer

Solution code:
def train(n):
    x = [int(input()) for i in range(n)]
    idx = min(x) - 1
    for i in range(n*10+1):
        if x[idx] == 2:
            return i + 1
        idx = x[idx] - 1
    return -1
n = int(input())
print(train(n))

Submission status: Wrong Answer

Solution code:
def train(n):
    x = [int(input()) for i in range(n)]
    idx = min(x) - 1
    for i in range(n*10):
        if x[idx] == 2:
            return i + 1
        idx = x[idx] - 1
    return -1
n = int(input())
print(train(n))

Submission status: Wrong Answer

Solution code:
def train(n):
    x = [int(input()) for i in range(n)]
    idx = x[0] - 1
    for i in range(n):
        if x[idx] == 2:
            return i + 1
        idx = x[idx] - 1
    return -1
n = int(input())
print(train(n))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
numlist = [int(input()) for i in range(N)]
count = 1
num = numlist[0]
check = False
for n in range(N):
	num=numlist[num-1]
	if num==2:
		count+=1
		check = True
		break
	count+=1
if check == True:
	print(count)
else:
	print(-1)

Submission status: Wrong Answer

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

cnt = 1
adr = 0

for i in range(N):
    cnt += 1
    adr = a[adr] - 1
    if a[adr] == 2:
        break
else:
    cnt = -1

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, [input() for _ in range(N)]))
a.insert(0, 0)
cnt = 1
num = 1
prev = []

while(1):
    prev.append(num)
    num = a[num]
    if a[num] == 2:
        break
    elif a[num] in prev:
        cnt = -1
        break
    cnt += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(lambda x:x - 1, map(int, [input() for _ in range(N)])))

cnt = 1
num = 0
prev = []

while(1):
    prev.append(num)
    num = a[num]
    cnt += 1
    if a[num] == 1:
        break
    elif a[num] in prev:
        cnt = -1
        break

print(cnt)

Submission status: Wrong Answer

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

b = 1
ans = None
for i in range(N):
    b = a_s[b - 1]
    if b == 2:
        ans = i + 1

print(-1 if ans is None else ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for _ in range(n):
  a += [int(input())-1]
s=set()
i=0
n=0
while True:
  n+=1
  i = a[i]
  if i==1:
    break
  elif i in s:
    print(-1)
    break
  else:
    s|={i}
else:
  print(n)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
check = [0] * n
print(check)
count = 0
button = a[0]
while(True):
    count += 1
    if button == 2:
        break
    if check[button-1] == 1:
        print(-1)
        sys.exit()
    check[button-1] = 1
    button = a[button-1]
print(count)

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
A = [int(input()) for i in range(N)]
now = A[0]
count = 1

for i in range(N):
    now = A[now-1]
    count += 1
    if(now==2):
        print(count)
        sys.exit()

print("-1")

Submission status: Wrong Answer

Solution code:
n=int(input())
m = int(pow(n,1/2)) + 1
k = 0
for i in range(m):
  if n <= pow(i,2):
    break
  if k <= pow(i,2):
    k = pow(i,2)  
  for z in range(3,i):
    if n <= pow(i,z):
      break
    elif k <= pow(i,z):
      k = pow(i,z)

print(k)

Submission status: Wrong Answer

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	i = 1
	while i < N+1:
		i += 1
		tugi = btnList[tugi-1]
		if tugi-1 == 1:
			print(i)
			sys.exit()
	print(-1)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	lists = [0]
	i = 0
	while i < N+1:
		i += 1
		lists.append(tugi-1)
		tugi = btnList[tugi-1]
		if tugi-1 == 1:
			print(len(lists))
			sys.exit()
	print(-1)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	lists = [0]
	i = 0
	while i < N+1:
		i += 1
		lists.append(tugi-1)
		tugi = btnList[tugi-1]
		if tugi-1 == 1:
			print(len(lists))
			sys.exit()
		if (tugi-1) in lists:
			print(-1)
			sys.exit()
	print(-1)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	lists = [0]
	i = 0
	while i < 100000:
		i += 1
		lists.append(tugi-1)
		tugi = btnList[tugi-1]
		if tugi-1 == 1:
			print(len(lists))
			sys.exit()
		if (tugi-1) in lists:
			print(-1)
			sys.exit()
	print(-1)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	lists = [0]
	while True:
		lists.append(tugi-1)
		tugi = btnList[tugi-1]
		if tugi-1 == 1:
			print(len(lists))
			sys.exit()
		if (tugi-1) in lists:
			print(-1)
			sys.exit()
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	lists = [0]
	while True:
		lists.append(tugi-1)
		tugi = btnList[tugi-1]
		if tugi == 2:
			print(len(lists))
			sys.exit()
		if tugi-1 in lists:
			print(-1)
			sys.exit()
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

def main():
	N = int(input())
	
	btnList = []
	for _ in range(N):
		tmp = int(input())
		btnList.append(tmp)
	
	tugi = btnList[0]
	lists = [0]
	while True:
		tugi = btnList[tugi-1]
		if tugi == 2:
			print(len(lists)+1)
			sys.exit()
		if tugi in lists:
			print(-1)
			sys.exit()
		else:
			lists.append(tugi)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[int(input()) for i in range(n)]
nxt=l[0]
d=[nxt]
flag=False
for i in range(n):
  if l[nxt-1] == 2:
    d.append(l[nxt-1])
    flag=True
    break
  elif d.count(l[nxt-1]):
    break
  nxt = l[nxt-1]
  d.append(nxt)
print(-1 if flag==False else len(d))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[int(input()) for i in range(n)]
d=[l[0]]
nxt=l[0]
flag=False
for i in range(n-1):
  if l[nxt-1] == 2:
    d.append(l[nxt-1])
    flag=True
    break
  elif d.count(l[nxt-1]):
    break
  nxt = l[nxt-1]
  d.append(l[nxt-1])
print(-1 if flag==False else len(d))

Submission status: Wrong Answer

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(d)
    d = a[d] - 1

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0] + [int(input()) for i in range(N)]
pushing = min(A[1:])
ans = -1
for i in range(1, N + 1):
    if A[pushing] == 2:
        ans = i
        break
    pushing = A[pushing]
print(ans)

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(1,n+1):
        if x[pushing] == 2:
            return i
        pushing = x[pushing]-1
    return -1
N = int(input())
print(trained(N))

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
        if (x[pushing] == (i + 1)) and (x[pushing] != 2):
            return -1
        pushing = x[pushing]-1
    return -1
N = int(input())
print(trained(N))

Submission status: Wrong Answer

Solution code:
def trained(n):
    x = [0] + [ 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]
    return -1
N = int(input())
print(trained(N))

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+1):
        if x[pushing] == 2:
            return i + 1
        pushing = x[pushing] - 1
    return -1
N = int(input())
print(trained(N))