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: Runtime Error

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

def solve(i, n):
    if n == 2:
        print(i)
    elif i == N:
        print(-1)
    else:
        return solve(i+1, a[n-1])

solve(0, 1)

Submission status: Runtime Error

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]
print(-1)

Submission status: Runtime Error

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

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        exit(1)
print("-1")

Submission status: Runtime Error

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

cnt = 0
t = 1
for i in a:
    t = a[t-1]
    cnt += 1
    if t == 2:
        print(cnt)
        exit(1)
print(-1)

Submission status: Runtime Error

Solution code:
def func(i=0, ans=0):
    if B[i] is True:
        return -1
    else:
        B[i] = True
    
    ans += 1
    if A[i] != 1:
        return func(A[i], ans)
    else:
        return ans



n = int(input())
A = [int(input())-1 for i in range(n)]
B = [False] * n
print(func())

Submission status: Runtime Error

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

Submission status: Runtime Error

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

num = 0
count = 1
while num != 2:
  num = a[num]
  count += 1
  if count >= n:
    count = -1
    
print(count)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())

A = []

for i in range(N):
    A.append(int(input()))
count=1
num=0
ans = A.index(2)
st = A[0]
for i in range(N):
    if(A[0] == 2):
        print(count)
        break
    count+=1
    if(st==ans+1):
        print(count)
        num=1
        break
    st = A[st-1]

if(num!=1):
    print(-1)

Submission status: Runtime Error

Solution code:
N = int(input())

A = []

for i in range(N):
    A.append(int(input()))
count=1
num=0
ans = A.index(2)
st = A[0]
for i in range(N):
    count+=1
    if(st==ans+1):
        print(count)
        num=1
        break
    st = A[st-1]

if(num!=1):
    print(-1)

Submission status: Runtime Error

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


def has_edge(edges, v, g, d):
    target = edges.get(v)
    if target is not None and mem.get(v) is None:
        mem[v] = 1
        if target == g:
            return d
        else:
            return has_edge(edges, target, g, d+1)
    return -2


print(has_edge(edges, 0, 1, d)+1)

Submission status: Runtime Error

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


def has_edge(edges, v, g, d):
    target = edges.get(v)
    if target is not None and mem.get(v) is None
        mem[v] = 1
        if target == g:
            return d
        else:
            return has_edge(edges, target, g, d+1)
    return -2


print(has_edge(edges, 0, 1, d)+1)

Submission status: Runtime Error

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


def has_edge(edges, v, g, d):
    target = edges.get(v)
    if target is not None and mem[v] == 0:
        mem[v] = 1
        if target == g:
            return d
        else:
            return has_edge(edges, target, g, d+1)
    return -2


print(has_edge(edges, 0, 1, d)+1)

Submission status: Runtime Error

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


def has_edge(edges, v, g, d):
    target = edges[v]
    if mem.get(v) is None:
        mem[v] = 1
        if target == g:
            return d
        else:
            return has_edge(edges, target, g, d+1)
    return -2


print(has_edge(edges, 0, 1, d)+1)

Submission status: Runtime Error

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


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


print(has_edge(edges, 0, 1, d)+1)

Submission status: Runtime Error

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: Runtime Error

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

if 2 is not 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: Runtime Error

Solution code:
n = int(input())
a = [int(input())-1 for x in range(n)]
ans = 1
ok = False
i = a.index(1)
for x in range(n):
    if i == 0:
        ok = True
        break
    i = a.index(i)
    ans += 1

print([-1, ans][ok])

Submission status: Runtime Error

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:

Submission status: Runtime Error

Solution code:
n = int(input())
a=[]
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: Runtime Error

Solution code:
n = int(input())
a = list(int(input()) for i in range(n))
s = 0
c = 0

while s != 1 and c < n:
    c += 1
    s = a[s]
if c < n:
    print(c)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [0]
for _ in range(n+1):
  a.append(int(input()))
visited = [False]*(n+1)
visited[1] = True
cnt = 0
but = 1
while True:
  nextb = a[but]
  if visited[nextb]:
    cnt = -1
    break
  elif nextb == 2:
    cnt += 1
    break
  else:
    but = nextb
    cnt += 1
    visited[but] = True
print(cnt)

Submission status: Runtime Error

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

c = 1
for i in range(n):
  if c == 2:
    print(i + 1)
    exit(0)
  c = a[c]
print(-1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
"""
https://abc065.contest.atcoder.jp/tasks/abc065_b
"""
n=int(input())
a="S"
for i in range(n):
    a=a+input()
#print(a)
t=1
c=0
for i in range(n):
    t=int(a[t])
    c=c+1
    if t==2:
        print(c)
        break
if t!=2:
    print(-1)

Submission status: Runtime Error

Solution code:

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    static int[] g;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        g = new int[n+1];
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt();
            g[i] = a;
        }
        Set<Integer> s = new HashSet<>();
        int current = 1;
        while(!s.contains(current)) {
            s.add(current);
            if(!s.contains(g[current])) {
                current = g[current];
            }
        }

        if(s.contains(2)) {
            System.out.println(2);
        }
        else {
            System.out.println(-1);
        }
    }
}

Submission status: Runtime Error

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

def find(ad, nexti, cnt, s):
    cnt += 1
    s.add(nexti)
    if ad[nexti] == 2:
        return cnt
    elif ad[nexti] in s:
        return -1
    else:
        return find(ad, ad[nexti], cnt, s)

print(find(a, 1, 0, set()))

Submission status: Runtime Error

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

def find(a, nexti, cnt, s):
    global n
    if cnt > n:
        return -1
    cnt += 1
    s.add(nexti)
    if a[nexti] == 2:
        return cnt
    elif a[nexti] in s:
        return -1
    else:
        return find(a, a[nexti], cnt, s)

print(find(a, 1, 0, set()))

Submission status: Runtime Error

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

def find(a, nexti, cnt, s):
    cnt += 1
    s.add(nexti)
    if a[nexti] == 2:
        return cnt
    elif a[nexti] in s:
        return -1
    else:
        return find(a, a[nexti], cnt, s)

print(find(a, 1, 0, set()))

Submission status: Runtime Error

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

if a[0] == 2:
    print(0)
else:
    count = 0
    flag = False
    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: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define gc getchar
int getint() { unsigned int c; int x = 0; while (((c = gc()) - '0') >= 10) { if (c == '-') return -getint(); if (!~c) exit(0); } do { x = (x << 3) + (x << 1) + (c - '0'); } while (((c = gc()) - '0') < 10); return x; }
int getstr(char *s) { int c, n = 0; while ((c = gc()) <= ' ') { if (!~c) exit(0); } do { s[n++] = c; } while ((c = gc()) > ' ' ); s[n] = 0; return n; }
template<class T> inline bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T> inline bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

int as[100011];

int main () {
    int n = getint();
    for (auto i = 0; i < n; ++i) as[i] = getint() - 1;
    auto now = 0;

    for (auto step = 0; step < n; ++step) {
        now = as[now];
        if (now == 1) {
            cout << step + 1 << endl;
            return 0;
        }
    }
    puts("-1");
    return 0;
}

Submission status: Runtime Error

Solution code:


num = int(input())

array = []
for i in range(num):
    array.append(int(input()))

res = -1
if 2 in array:
    judge = array[0]
    roop = 1
    while judge != 2 and roop != 100001:
        roop += 1
        judge = array[judge-1]
        if judge == 2:
            break
if judge == 2:
    res = roop

print(res)

Submission status: Runtime Error

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


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

Submission status: Runtime Error

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

index = 0



while True:
    check[index] = 0
    index = a[index]
    if check[index] == 0:
        print(-1)
        break
    if index == 1:
        print(count)
        break

Submission status: Runtime Error

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

index = 0



while True:
    check[index] = 0
    index = a[index]
    if check[index] == 0:
        print(-1)
    if index == 1:
        print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
lst = [int(input()) for _ in[0]*n]
visit = set()
current = 0
while True:
  visit.add(current)
  current = lst[current]
  if current == 2:
    print(len(visit) + 1)
    break
  if current in visit:
    print(-1)
    break

Submission status: Runtime Error

Solution code:
N = int(input())

a_n=list(map(int,input().split()))
import sys

i=1
count=0
for k in range(N):
    if a_n[i-1]==2:
        count+=1
        print(count)
        sys.exit()
    else:
        i=a_n[i-1]
        count+=1

print(-1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) if(i) else 0 for i in range(n+1)]
distance = [None] * (n+1)

def dfs(p, d):
    if(p==2):
        return d
    if(distance[p]!=None):
        return -1
    ans = 100001
    distance[p] = d
    nextp = a[p]
    ans = min(ans, dfs(nextp, d + 1))
    return ans

print(dfs(1, 0))

Submission status: Runtime Error

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

now = 1
count = 0

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

if count == loop:
    print(-1)

Submission status: Runtime Error

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

    a = list(A)
    idx = a.index(1)
    ans = 0
    while True:
        btn = a[idx]
        if btn == 0:
            ans = -1
            break
        elif btn == 2:
            break
        a[idx] = 0
        idx = btn - 1
        ans += 1

    print(ans)

main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

light=1

for i in range(n):
    light=a[light]
    if light == 2:
        print(i)
        break
else:
    print(-1)

Submission status: Runtime Error

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

light=1

for i in range(n):
    light=a[light]
    if light = 2:
        print(i)
        break
else:
    print(-1)

Submission status: Runtime Error

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

light=1

for i in range(n):
    light=a[light]
    if light = 2:
        print(i)
        break
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from sys import stdin 
a = [0]
n = int(stdin.readline())
for _ in xrange(n):
 a.append(int(stdin.readline())
ans = -1
cur = 0
x = 1
s = set()
while True:
 if x in s:
  break
 if x==2:
  ans = cur
  break
 s.add(x)
 cur+=1
 x = a[x]
print ans

Submission status: Runtime Error

Solution code:

def f(l, a, hist):
    if(a in hist):
        return -1
    if(a == 1):
        return len(hist)
    hist.append(a)
    return f(l,l[a]-1,hist)

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

    for i in range(n):
        l.append(int(input()))
        
    print(f(l,l[0]-1,[0]))

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

Submission status: Runtime Error

Solution code:

def f(l, a, hist):
    if(l[a] in hist):
        return -1
    if(l[a] == 1):
        return len(hist)
    hist.append(l[a])
    return f(l,a,hist)

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

    for i in range(n):
        l.append(int(input()))
        
    print(f(l,l[0],[]))

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input())
a=[int(input()) for i in range(N)]
b=[]
def c(j):
    if j==1:
        return 1
    else:
        return a[c(j-1)-1]
j=1
if 2 not in a:
    print(-1)
else:
    while a[c(j)-1] not in b:
        b.append(a[c(j)-1])
        j += 1
    if 2 not in b:
        print(-1)
    else:
        print(b.index(2)+1)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
if not(2 in a):
    print(-1)

x = [False]*N
n,i = (1, 0)
while x[n] == False:
    x[n] = True
    n = a[n-1]
    i+=1
    if n==2:
        print(i)
        break
else:
    print(-1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

    if a not in index_dict.keys():
        index_dict[a] = [i+1]
    else:
        index_dict[a].append(i+1)

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

    if index == 1:
        return loop + 1

    if index not in index_dict.keys():
        return -1

    if loop > N:
        return -1

    min_loop = N + 1
    for i_next in index_dict[index]:
        loop_ = rec_search(i_next, loop+1)
        min_loop = min(min_loop, loop_) if loop_ != -1 else min_loop
    loop_dict[index] = min_loop
    return min_loop

min_loop = N + 1
for i in index_dict[2]:
    loop = rec_search(i, 0)
    min_loop = min(min_loop, loop) if loop != -1 else min_loop

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

Submission status: Runtime Error

Solution code:
def push(i, count):
  already.append(i + 1)
  if buttons[i] == 2:
    print(count)
  elif buttons[i] in already:
    print('-1')
  else:
    push(buttons[i] - 1, count + 1)

N = int(input())

buttons = []
already = []

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

push(0, 1)

Submission status: Runtime Error

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


btn2 = a.index(2)
cnt = 1
idx = 1
for i in range(n+1):
    if a[idx] == btn2:
        cnt += 1
        print(cnt)
        break

    else:
        idx = a[idx]
        cnt += 1
else:
    print(-1)

Submission status: Runtime Error

Solution code:
# coding: utf-8
a=[]
b=[0]*100000
n = int(input())
for i in range(n):
    a.append(int(input()))
cnt=0
temp = a[0]
b[0] = 1
b[1] = 1
while True:
    temp = a[temp-1]
    if b[temp+1] == 1:
        print(-1)
        break
    elif temp==2:
        cnt+=1
        print(cnt)
        break
    b[temp+1] = 1
    cnt+=1

Submission status: Runtime Error

Solution code:
# coding: utf-8
a=[]
b=[0]*100000
n = int(input())
for i in range(n):
    a.append(int(input()))
cnt=0
print(a)
temp = a[0]
b[0] = 1
b[1] = 1
while True:
    temp = a[temp-1]
    if b[temp+1] == 1:
        print(-1)
        break
    elif temp==2:
        cnt+=1
        print(cnt)
        break
    b[temp+1] = 1
    cnt+=1

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
a = np.zeros(N)
for i in range(N):
    a[i+1] = int(input())
i = 1
n = 0
for j in range(N):
    n = n+1
    i = a[i]
    if i == 2:
        print(n)
        break
if i!=2:
    print(-1)

Submission status: Runtime Error

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


btn2 = a.index(2)
cnt = 1
idx = 1
for i in range(n):
    if a[idx] == btn2:
        if cnt != 1:
            cnt += 1
        print(cnt)
        break
    else:
        idx = a[idx]
        cnt += 1
else:
    print(-1)

Submission status: Runtime Error

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


btn2 = a.index(2)
cnt = 1
idx = 1
for i in range(n):
    if a[idx] == btn2:
        cnt += 1
        print(cnt)
        break
    else:
        idx = a[idx]
        cnt += 1
else:
    print(-1)

Submission status: Runtime Error

Solution code:
# coding: utf-8
a=[]
b=[0]*100000
n = int(input())
for i in range(n):
    a.append(int(input()))
cnt=0
temp = a[1]
b[0] = 1
b[1] = 1
while True:
    temp = a[temp-1]
    if b[temp+1]==1:
        print(-1)
        break
    else:
        b[temp+1]=1
    cnt+=1
    if temp-1==1:
        cnt +=1
        print(cnt)
        break

Submission status: Runtime Error

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

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

if cnt<1000:
    print(cnt)
else:
    print(-1)

Submission status: Runtime Error

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


btn2 = a.index(2)
cnt = 1
idx = 1
for i in range(1, n + 1):
    if a[idx] == btn2:
        cnt += 1
        print(cnt)
        break
    else:
        idx = a[idx]
        cnt += 1
else:
    print(-1)

Submission status: Runtime Error

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

tmp=0
i=0
cnt=0

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

if cnt<100:
    print(cnt)
else:
    print(-1)

Submission status: Runtime Error

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

b = 1
c = [1]
for i in range(N):
	b = a[b-1]
	if b in c:
		break
	c.append(b)
print(c.index(2))

Submission status: Runtime Error

Solution code:
# coding: utf-8
a=[]
b=[0]*100000
n = int(input())
for i in range(n):
    a.append(int(input()))
cnt=0
temp = a[0]
b[0] = 1
b[1] = 1
while True:
    temp = a[temp-1]
    if b[temp+1]==1:
        print(-1)
        break
    else:
        b[temp+1]=1
    cnt+=1
    if temp-1==1:
        cnt +=1
        print(cnt)
        break

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
ch = [0] * (10**5 + 1)
if a[0] == 1 or a[1]  == 2:
    print(-1)
ch[1] = numB
c = 1
while True:
    numB = a[numB-1]
    if numB == 2:
        print(c)
        break
    if ch[numB] == 1:
        print(-1)
        break
    c += 1
    ch[numB] = 1

Submission status: Runtime Error

Solution code:
import sys

def push(i, count):
  if buttons[i] == 2:
    print(count)
    sys.exit()
  elif buttons[i] in already:
    print('-1')
    sys.exit()
  else:
    already.append(i + 1)
    push(buttons[i] - 1, count + 1)

N = int(input())

buttons = []
already = []
count = 0

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

push(0, 1)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]
ch = [0] * 10**5
ch[1] = 1

if a[0] == 1 or a[1]  == 2:
    print(-1)

numB = 1
c = 1
while True:
    numB = a[numB-1]
    if numB == 2:
        print(c)
        break
    if ch[numB] == 1:
        print(-1)
        break
    c += 1
    ch[numB] = 1

Submission status: Runtime Error

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

for i in range(n):
  a[i+1]=int(input())

cnt=0
needle=1
while not(needle==2 or cnt >10007):
  needle=a[needle]
  cnt+=1

if cnt>10001:
  cnt=-1
print(cnt)

Submission status: Runtime Error

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

for i in range(n):
  a[i+1]=int(input())

cnt=0
needle=1
while not(needle==2 or cnt >10007):
  needle=a[needle]
  cnt+=1