qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
keep=1
count=0
ababa=0
for i in range(n):
  count+=1
  keep=a[keep-1]
  if keep==2:
    ababa=1
    print(count)
    break
if ababa=0:
  print(-1)

Fixed code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
keep=1
count=0
ababa=0
for i in range(n):
  count+=1
  keep=a[keep-1]
  if keep==2:
    ababa=1
    print(count)
    break
if ababa==0:
  print(-1)
Buggy code:
n = int(input())
a_l = [ int(input()) for _ in range(n) ]

# N 1
ans = 0
next_idx = 0
for _ in range(n):
    ans += 1
    next_idx = a_l[next_idx]-1
    if next_idx == 1:
        break
print(ans)

Fixed code:
n = int(input())
a_l = [ int(input()) for _ in range(n) ]

# N 1
ans = 0
next_idx = 0
for _ in range(n):
    ans += 1
    next_idx = a_l[next_idx]-1
    if next_idx == 1:
        break
if next_idx != 1:
    ans = -1
print(ans)
Buggy code:
N = int(input())
a = {i + 1: int(input()) for i in range(N)}
ans = -1

count = 0
nxt = 1
while count < N:
  nxt = a[nxt]
  if nxt == 2:
    ans = count
    break
  count += 1

print(ans)

Fixed code:
N = int(input())
a = {i + 1: int(input()) for i in range(N)}
ans = -1

count = 1
nxt = 1
while count < N:
  nxt = a[nxt]
  if nxt == 2:
    ans = count
    break
  count += 1

print(ans)
Buggy code:
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(100**5)

def rec(n, a, seen):
	if seen[n-1]:
		return -1
	if a[n-1] == 2:
		return seen.count(True) + 1
	seen[n-1] = True
	return rec(a[n-1], a, seen)

def resolve():
	n = int(input())
	a = []
	seen = [False]*n
	for _ in range(n):
		a.append(int(input()))
	print(rec(1, a, seen))
resolve()

Fixed code:
import sys
from io import StringIO
import unittest

sys.setrecursionlimit(10**6)

def rec(n, a, seen):
	if seen[n-1]:
		return -1
	if a[n-1] == 2:
		return seen.count(True) + 1
	seen[n-1] = True
	return rec(a[n-1], a, seen)

def resolve():
	n = int(input())
	a = []
	seen = [False]*n
	for _ in range(n):
		a.append(int(input()))
	print(rec(1, a, seen))
resolve()
Buggy code:
n = int(input())
a = [int(input() for i in range(n)]

ans = 0
i = 0
while n > 0:
    ans += 1
    if a[i] == 2:
        print(ans)
        quit()
    else:
        i = a[i]-1
    n -= 1
print(-1)


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

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

Fixed code:
n=int(input())
a=[int(input())-1 for _ in range(n)]
ans=-1
l=0
for i in range(n):
    l=a[l]
    if l==1:
        ans=i+1
        break
print(ans)
Buggy code:
n = int(input())
a = list(int(input()) for i in range(n))
b = 1
ans = 0
while b != 2 and ans < n:
    ans += 1
    b = a[b-1]
if ans > n:
    print(-1)
else:
    print(ans)

Fixed code:
n = int(input())
a = list(int(input()) for i in range(n))
b = 1
ans = 0
while b != 2 and ans <= n:
    ans += 1
    b = a[b-1]
if ans > n:
    print(-1)
else:
    print(ans)
Buggy 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)


Fixed 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 == N:
    print(-1)
Buggy code:
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

for _ in range(n + 1):
    if button == 2:
        print(count)
        break
    button = a[button]
    count += 1

if count == n:
    print(-1)

Fixed code:
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

for _ in range(n):
    if button == 2:
        print(count)
        break
    button = a[button]
    count += 1
else:
    print(-1)
Buggy code:
import sys
from collections import deque

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

for _ in range(n + 1):
    if button == 2:
        print(count)
        break
    button = a[button]

if count == n:
    print(-1)

Fixed code:
import sys

n = int(input())
a = [0] + [int(sys.stdin.readline()) for _ in range(n)]

button = 1
count = 0

for _ in range(n):
    if button == 2:
        print(count)
        break
    button = a[button]
    count += 1

if count == n:
    print(-1)
Buggy code:
a=[input() for _ in range(int(input()))]
ans=1
c=a[0]
if(c==2):
    print(ans)
else:
    while(c!=2):
        ans+=1
        c=a[c-1]
        if(ans>len(a)):
            print(-1)
            exit()
    print(ans)

Fixed code:
a=[int(input()) for _ in range(int(input()))]
ans=1
c=a[0]
if(c==2):
    print(ans)
else:
    while(c!=2):
        ans+=1
        c=a[c-1]
        if(ans>len(a)):
            print(-1)
            exit()
    print(ans)
Buggy 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)

Fixed code:
n = int(input())
a = list(int(input())-1 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)
Buggy 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()

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

if __name__ == '__main__':
    main()
Buggy code:
# -*- coding: utf-8 -*-


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

Fixed 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 + 1)]
    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
Buggy code:
n=int(input())
a=[int(input()) for _ in range(n)]
ans=-1
b=a[0]
if b==2:
    print(1)
    break
else:
    for i in range(n):
        b=a[b-1]
        # print(f"b={b}")
        if b==2:
            ans=i+2
            break
    print(ans) 

Fixed code:
n=int(input())
a=[int(input()) for _ in range(n)]
ans=-1
b=a[0]
if b==2:
    print(1)
else:
    for i in range(n):
        b=a[b-1]
        # print(f"b={b}")
        if b==2:
            ans=i+2
            break
    print(ans)
Buggy code:
n=int(input())
a=[int(input()) for i in range(n)]

count=1

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

print(count)    

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

count=1

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

print(-1)
Buggy code:
N = int(input())
S = [int(input()) for i in range(N)]
count=1
i=0
while True:
  if S[i]==2 or count==N-1:
    break
  else:
    count+=1
    i=S[i]-1
if count==N-1: print(-1)
else: print(count)

Fixed code:
N = int(input())
S = [int(input()) for i in range(N)]
count=1
i=0
while True:
  if S[i]==2 or count==N:
    break
  else:
    count+=1
    i=S[i]-1
if count==N: print(-1)
else: print(count)
Buggy code:
n = int(input())
buttons = []
for _ in range(n):
    buttons.append(int(input()))

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


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

light = buttons[0]
for i in range(n):
    if light == 2:
        print(i+1)
        break
    light = buttons[light-1]
else:
    print(-1)
Buggy code:
N = int(input())
a = [0] + [int(input()) for _ in range(N)]
x = 1
visited = set([1])
answer = 0

while True:
  x = A[x]
  if x in visited:
    answer = -1
    break
  visited.add(x)
  answer += 1
  if x == 2:
    break
    
print(answer)


Fixed code:
N = int(input())
a = [0] + [int(input()) for _ in range(N)]
x = 1
visited = set([1])
answer = 0

while True:
  x = a[x]
  if x in visited:
    answer = -1
    break
  visited.add(x)
  answer += 1
  if x == 2:
    break
    
print(answer)
Buggy code:
# B - Trained?
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    cnt = 0
    x = 1

    if 2 in a:
        for _ in range(n-1):
            x = a[x-1]
            cnt += 1

            if x == 2:
                print(cnt)
                exit()

    else:
        print(-1)
        exit()
        

if __name__ ==  "__main__":
    main()

Fixed code:
# B - Trained?
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    cnt = 0
    x = 1

    if 2 in a:
        for _ in range(n-1):
            x = a[x-1]
            cnt += 1

            if x == 2:
                print(cnt)
                exit()
        else:
            print(-1)
    else:
        print(-1)
        

if __name__ ==  "__main__":
    main()
Buggy code:
if __name__ == "__main__":
    N = int(input())
    a = [int(input()) for _ in range(N)]

    index = 0
    count = 0
    number = 1
    flags = [0 for _ in range(N)]
    flags[index] = 1
    while(number != 2):
        number = a[index]
        index = number - 1
        count += 1
        print(flags)
        if flags[index] == 1:
            break
        else:
            flags[index] = 1

    if number == 2:
        print(count)
    else:
        print(-1)


Fixed code:
if __name__ == "__main__":
    N = int(input())
    a = [int(input()) for _ in range(N)]

    index = 0
    count = 0
    number = 1
    flags = [0 for _ in range(N)]
    flags[index] = 1
    while(number != 2):
        number = a[index]
        index = number - 1
        count += 1
        if flags[index] == 1:
            break
        else:
            flags[index] = 1

    if number == 2:
        print(count)
    else:
        print(-1)
Buggy code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    A=[]
    check=[0]*n
    temp=0
    ct=1
    for i in range(n):
        A.append((INT()-1))
    check[0]=1
    for i in range(n):
        if A[temp]==1:
            print(ct)
            break
        elif check[A[temp]]==1:
            print(-1)
            break
        else:
            temp=A[temp]
            check[A[temp]]=1
            ct+=1
            
if __name__ == '__main__':
    do()

Fixed code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    A=[]
    check=[0]*n
    temp=0
    ct=1
    for i in range(n):
        A.append((INT()-1))
    check[0]=1
    for i in range(n):
        if A[temp]==1:
            print(ct)
            break
        elif check[A[temp]]==1:
            print(-1)
            break
        else:
            temp=A[temp]
            check[temp]=1
            ct+=1
            
if __name__ == '__main__':
    do()
Buggy code:
# ABC065 B - Trained?
N = int(input())
# A = [[int(input()),int(i+1),False] for i in range(N)]
A = [[int(input()),False] for i in range(N)]
print(A)
idx = 0
A[0][1] == True
cnt = 1

while cnt != N:
    if A[idx][1]:
        print(-1)
        exit()
    if A[idx][0] == 2:
        print(cnt)
        exit()
    A[idx][1] = True
    idx = A[idx][0]-1
    cnt += 1

Fixed code:
# ABC065 B - Trained?
N = int(input())
# A = [[int(input()),int(i+1),False] for i in range(N)]
A = [[int(input()),False] for i in range(N)]
idx = 0
A[0][1] == True
cnt = 1

while cnt != 2*N:
    if A[idx][1]:
        print(-1)
        exit()
    if A[idx][0] == 2:
        print(cnt)
        exit()
    A[idx][1] = True
    idx = A[idx][0]-1
    cnt += 1
Buggy code:
n = int(input())
listA = [int(input()) for i in range(n)]
a = listA[0]
b = 0
for j in range(n):
  if a != 2:
    a = listA[a]
    b = b+1
  elif a == 2:
    b = b+1
    break
if b == n:
  print(-1)
else:
  print(b)

  

Fixed code:
n = int(input())
listA = [int(input()) for i in range(n)]
a = listA[0]
b = 0
for j in range(n):
  if a != 2:
    a = listA[a-1]
    b = b+1
  elif a == 2:
    b = b+1
    break
if b == n:
  print(-1)
else:
  print(b)
Buggy code:
N = int(input())
x = [int(input()) for i in range(N)]
cnt = 1
num = x[0]
if num != 2:
    cnt = 2
    for i in range(N):
        num = x[num-1]

        if(num == 2):
            break
        cnt += 1
print(cnt)

Fixed code:
N = int(input())
x = [int(input()) for i in range(N)]
cnt = 1
num = x[0]
if num != 2:
    cnt = 2
    for i in range(N):
        num = x[num-1]

        if(num == 2):
            break
        cnt += 1
if cnt == N + 2:
    cnt = -1
print(cnt)
Buggy code:
N = int(input())
a = []
for i in range(0, N):
  a.append(int(input()))
  a[i] -= 1;

ans = 0
n = 0
flag = 0
for i in range(0, N):
  if(a[n] == 1):
    flag = 1
    break
  else:
    ans += 1
    n = a[n]
if flag == 0:
  ans = -1
print(ans)

Fixed code:
N = int(input())
a = []
for i in range(0, N):
  a.append(int(input()))
  a[i] -= 1;

ans = 1
n = 0
flag = 0
for i in range(0, N):
  if(a[n] == 1):
    flag = 1
    break
  else:
    ans += 1
    n = a[n]
if flag == 0:
  ans = -1
print(ans)
Buggy code:
n = int(input())
an = [int(input()) for i in range(n)]
c, s = 1, a[0]
while s != 2 and c < n:
    c, s = c + 1, a[s - 1]
print(c if c < n else -1)


Fixed code:
n = int(input())
an = [int(input()) for i in range(n)]
c, s = 1, an[0]
while s != 2 and c < n:
    c, s = c + 1, an[s - 1]
print(c if c < n else -1)
Buggy 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

Fixed 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==1):
    print(ans)
    break
  if(ans>=100001):
    print(-1)
    break
Buggy 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)

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

cnt = 0
i = 1

while cnt <= N:
    for _ in range(N):
        i = a[i - 1]
        cnt += 1
        if i == 2:
            print(cnt)
            exit()
print(-1)
Buggy code:
n = int(input())
a = [0,int(input()) for i in range(n)]
cnt = 1
next_a = a[1]
while cnt <= 10**5:
    next_a = a[next_a]
    cnt += 1
    if next_a == 2:
        break

if cnt < 10**5:
    print(cnt)
else:
    print(-1)

Fixed code:
n = int(input())
a = [0] + [int(input()) for i in range(n)]
cnt = 1
next_a = a[1]

if a[1] == 2:
    print(cnt)

else:
    while cnt <= 10**5:
        next_a = a[next_a]
        cnt += 1
        if next_a == 2:
            break

    if cnt <= 10**5:
        print(cnt)
    else:
        print(-1)
Buggy code:
N = int(input())
a = [int(input()) for i in range(N)]
anow = 1
count = 0
for i in range(N):
    anow = a[anow-1]
    count += 1
    if anow == 2:
        break
print('count' if anow==2 else '-1')

Fixed code:
N = int(input())
a = [int(input()) for i in range(N)]
anow = 1
count = 0
for i in range(N):
    anow = a[anow-1]
    count += 1
    if anow == 2:
        break
print(count if anow==2 else '-1')
Buggy code:
n=int(input())
l=[0]*n
for i in range(n):
    l[i]=int(input())

vi=[0]*n
vi[0]=1
ans=1
st=l[0]
f=0
while vi[1]==0:
    vi[st]+=1
    if vi[st]>1:
        f=1
        break
    st=l[st-1]
    ans+=1
if f==0:
    print(ans)
else:
    print(-1)
    
    
  
  
    

Fixed code:
n=int(input())
l=[0]*n
for i in range(n):
    l[i]=int(input())

vi=[0]*n
vi[0]=1
ans=0
st=l[0]
f=0
while vi[1]==0:
    vi[st-1]+=1
    if vi[st-1]>1:
        f=1
        break
    st=l[st-1]
    ans+=1
if f==0:
    print(ans)
else:
    print(-1)
Buggy code:
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)

flag = 0
num = 0
bottun = 0
for _ in range(N):
    if light[2] == "1":
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    
if flag:
    print(num)
else:
    print(-1)

Fixed code:
N = int(input())
light_rule = {n:int(input())-1 for n in range(N)}

light = ["1"]+["0"]*(N-1)

flag = 0
num = 0
bottun = 0
for _ in range(N):
    if light[1] == "1":
        flag = 1
        break
    num += 1
    # print(bottun, light_rule[bottun])
    light[bottun] = "0"
    light[light_rule[bottun]] = "1"
    bottun = light_rule[bottun]
    # print(light)
    
if flag:
    print(num)
else:
    print(-1)
Buggy code:
n = int(input())
lst = [0]
for _ in range(n):
    lst.append(int(input()))

light = lst[1]
count = 1

while light != 2 and count < n:
    light = lst[light]
    count += 1

print(count if count < n+1 else -1)

Fixed code:
n = int(input())
lst = [0]
for _ in range(n):
    lst.append(int(input()))

light = lst[1]
count = 1

while light != 2 and count < n+1:
    light = lst[light]
    count += 1

print(count if count < n+1 else -1)
Buggy code:
n=int(input())
a=[int(input()) for i in range(n)]
c,s=1,a[0]
while S!=2 and c<n:
  c,s=c+1,a[s-1]
print(c if c<n else -1)

Fixed code:
n= int(input())
a= [int(input()) for i in range(n)]
c,s = 1,a[0]
while s!=2 and c<n:
    c,s=c+1,a[s-1]
print(c if c<n else -1)
Buggy code:
# coding: utf-8
# Your code here!

N = int(input())
a = [int(input()) for i in range(N)]
pos = 0

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

Fixed code:
# coding: utf-8
# Your code here!

N = int(input())
a = [int(input()) for i in range(N)]
pos = 0

for i in range(N):
    if pos == 1:
        print(i)
        break
    else:
        pos = a[pos] - 1
else:
    print(-1)
Buggy code:
n = int(input())
a_list = []
for i in range(n):
	a_list.append(int(input()))
r = -1
num = 1
count = 0
nums = []
while True:
	num = a_list[num-1]
	if num in nums:
		break
	nums.append(num))
	count += 1
	if num == 2:
		r = count
		break
print(r)

Fixed code:
n = int(input())
a_list = []
for i in range(n):
	a_list.append(int(input()))
r = -1
num = 1
count = 0
while True:
	num = a_list[num-1]
	count += 1
	if num == 2:
		r = count
		break
	if count == n:
		break
print(r)
Buggy code:
n = int(input())
a = [int(input()) for i in range(n)]
now = 0
ans = -1
for i in range(n):
  value = a[now]
  if value == 2:
    ans = i+1
    break
  now = value
print(ans)

Fixed code:
n = int(input())
a = [int(input()) for i in range(n)]
now = 0
ans = -1
for i in range(n):
  value = a[now]
  if value == 2:
    ans = i+1
    break
  now = value - 1
print(ans)
Buggy code:
N = int(input())

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

v = l[0]
print(l)
print(v)
ans = 0
while v != 2 and v != -1:
  tmp = l[v-1]
  l[v-1] = -1
  v = tmp
  ans += 1

if v == 2:
  print(ans)
else:
  print(-1)


Fixed code:
N = int(input())

l = []
for i in range(N):
  v = int(input())
  l.append(v)
  
v = l[0]
ans = 1
while v != 2 and v != -1:
  tmp = l[v-1]
  l[v-1] = -1
  v = tmp
  ans += 1
  
if v == 2:
  print(ans)
else: 
  print(-1)
Buggy code:
N = int(input())
A = [int(input()) for i in range(N)]

cnt = 0
pos = 0

while True:
    if pos == 1:
        print(cnt)
    if cnt >= N:
        print(-1)

    pos = A[pos] - 1
    cnt += 1

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

cnt = 0
pos = 0

while True:
    if pos == 1:
        print(cnt)
        break
    if cnt >= N:
        print(-1)
        break

    pos = A[pos] - 1
    cnt += 1
Buggy code:
N = int(input())
A = [int(input()) for _ in range(N)]
cnt = 0
tmp = 1
for i in A:
    tmp = A[i-1]
    cnt += 1
    if tmp == 2:
        print(cnt)
        exit()
print("-1")


Fixed code:
N = int(input())
A = [int(input()) for _ in range(N)]
cnt = 0
tmp = 1
for i in A:
    tmp = A[tmp-1]
    cnt += 1
    if tmp == 2:
        print(cnt)
        exit()
print("-1")
Buggy code:
n = int(input())
l = []
ans = 1
for _ in range(n) :
	l.append(int(input()))
if l[0] == 2 :
	print(1)
else :
    i = l[0] - 1
	while True :
		if l[i] == 2 :
			ans += 1
			break
		i = l[i] - 1
		ans += 1
		if ans == n+1 :
			print(-1)
			exit()
	print(ans)

Fixed code:
n = int(input())
l = []
ans = 1
for _ in range(n) :
	l.append(int(input()))
if l[0] == 2 :
	print(1)
else :
    i = l[0] - 1
    while True :
        if l[i] == 2 :
            ans += 1
            break
        i = l[i] - 1
        ans += 1
        if ans == n+1 :
            print(-1)
            exit()
    print(ans)
Buggy 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)

Fixed 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(n + 1)
        break
else:
    print(-1)
Buggy code:
import sys


def main():
    n = int(input())
    buttons = [int(input()) for _ in range(n)]

    def push(x):
        return buttons[x - 1]

    seq = [push(1)]

    for _ in range(n):
        if seq[-1] == 2:
            print(len(seq))
            return 0
        seq += [push(seq[-1])]

    return 0


if __name__ == '__main__':
    sys.exit(main())


Fixed code:
import sys


def main():
    n = int(input())
    buttons = [int(input()) for _ in range(n)]

    def push(x):
        return buttons[x - 1]

    seq = [push(1)]

    for _ in range(n):
        if seq[-1] == 2:
            print(len(seq))
            return 0
        seq += [push(seq[-1])]

    print(-1)

    return 0


if __name__ == '__main__':
    sys.exit(main())
Buggy code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))

x = 0
num = 0
while( num <= N):
    if x == 1:
        print(num)
    elif num == N:
        print(-1)
    num += 1
    x = a[x] - 1


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

x = 0
num = 0
while( num <= N):
    if x == 1:
        print(num)
        break
    elif num == N:
        print(-1)
    num += 1
    x = a[x] - 1
Buggy code:
N = int(input())
A = [int(input())-1 for _ in range(N)]
i = 0
for n in range(1, N+1):
    i = A[i]
    if i == 2:
        print(n)
        break
else:
    print(-1)

Fixed code:
N = int(input())
A = [int(input())-1 for _ in range(N)]
i = 0
for n in range(1, N+1):
    i = A[i]
    if i == 1:
        print(n)
        break
else:
    print(-1)