qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N = int(input())
button = list()
for i in range(N):
    button.append(int(input()))

pre  = 1
print(button)
i = 0
c = 1
rireki = [0]*N
while all: 
    if(button[i]==2):
        print(c)
        break
    rireki[i] +=1
    if(rireki[i]==2):
        print(-1)
        break
    i = button[i]-1
    c+=1

Fixed code:
N = int(input())
button = list()
for i in range(N):
    button.append(int(input()))

pre  = 1
# print(button)
i = 0
c = 1
rireki = [0]*N
while all: 
    if(button[i]==2):
        print(c)
        break
    rireki[i] +=1
    if(rireki[i]==2):
        print(-1)
        break
    i = button[i]-1
    c+=1
Buggy 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)

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

time = 1
push = 0
while True:
    
    if bL[push] == 2:
        print(time)
        break
    if time == N:
        print("-1")
        break
    
    push = bL[push]-1
    
    time += 1

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

time = 1
push = 0
while True:
    
    if bL[push] == 2:
        print(time)
        break
    if time == N:
        print("-1")
        break
    
    push = bL[push]-1
    
    time += 1
Buggy code:
#B
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
lamp = 0
ans=0
for i in range(n):
    if lamp == i:
        lamp = a[i] - 1
        ans += 1
        # print(i,lamp)
    if lamp == 1:
        # print(ans)
        sys.exit()
print(-1)

Fixed code:
#B
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
lamp = 1
ans=0
for i in range(n):
    if lamp - 1 == 1:
        print(i)
        sys.exit()
    lamp = a[lamp - 1]
print(-1)
Buggy code:
a = [int(input()) for _ in range(int(input()))]
c,s = 1,a[0]
if s != 2 and c < n: c,s = c+1,a[n-1]
print(c if c < n else "-1")

Fixed code:
n = int(input())
a = [int(input()) for _ 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:
n = int(input())
a = [int(input()) for _ in range(n)]

pushed = set()
cnt = 0
push= 1
while True:
    if push in pushed:
        cnt = -1
        break
    pushed.add(push)
    push = a[push]
    cnt += 1
    if push == 2:
        break

print(cnt)
    


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

pushed = set()
cnt = 0
push= 1
while True:
    if push in pushed:
        cnt = -1
        break
    pushed.add(push)
    push = a[push-1]
    cnt += 1
    if push == 2:
        break

print(cnt)
Buggy 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)


Fixed code:
import sys
n = int(input())
a = [int(input()) for _ in range(n)]
check = [0] * n
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)
Buggy code:
n=int(input())
lamp=[]
status=1
count=0
possible=1
for i in range(n):
  p=int(input())
  lamp.append(p)
while status!=2 or count==n:
  status=lamp[status-1]
  count+=1
if count!=n:
  print(count)
else:
  print(-1)

Fixed 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:
    var=lamp[status-1]
    lamp[status-1]=0
    status=var
    count+=1
  else:
    possible+=1
    break
if possible==0:
  print(count)
else:
  print(-1)
Buggy code:
N = int(input())
l = [int(input()) for i in range(N)]
index = 0
count = 0
for j in range(N):
    index = l[index]
    count += 1
    if index == 2:
        print(count)
        exit()
print(-1)

Fixed code:
N = int(input())
l = [int(input()) for i in range(N)]
index = 0
count = 0
for j in range(N):
    index = l[index] - 1
    count += 1
    if index == 1:
        print(count)
        exit()
print(-1)
Buggy code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*N

a=1
count=0

while True:
    if PressedButtons[a]==False:
        PressedButtons[a]=True
        count+=1
        a=lamps[a]
        if a==2:
            print(count)
    elif PressedButtons[a]==True:
        print("-1")
        exit()
    else:
        print("-1")

Fixed code:
N=int(input())
lamps=[]
for a in range(N):
    _=int(input())
    lamps.append(_)
lamps.insert(0,"aaa")

PressedButtons=[False]*int(N+1)

a=1
count=0

if lamps.count(2)==0:
    print("-1")
    exit()

while PressedButtons[a]==False :
    PressedButtons[a]=True
    count+=1
    a=lamps[a]
    if a==2:
        print(count)
        exit()
print('-1')
Buggy code:
import sys
N = int(input())
a = [int(input()) for i in range(N)]
d = [0]
b = 0
c = 0
while True:
    if b == 1:a
        print(c)
        sys.exit()
    c += 1
    b = a[b] - 1
    if b in d:
        print('-1')
        sys.exit()
    d.append(b)

Fixed code:
import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
b = [0] * N
i = 0
c = 1
while True:
    if b[i] == 1:
        print(-1)
        sys.exit()
    b[i] = 1
    i = a[i] - 1
    if i == 1:
        print(c)
        sys.exit()
    c += 1
Buggy 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)

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

data.insert(0, 0)

i = 1
cnt = 0

push_button = {}

while True:
  
  trans_value = data[i]
  i = trans_value
  
  cnt += 1
  
  if trans_value == 2:
    break
  
  elif trans_value in push_button:
    cnt = -1
    break
    
  else:
    pass
  
  push_button[trans_value] = "PUSH"

print(cnt)
Buggy code:
=int(input())
a=[]
for i in range(n):
    ai=int(input())
    a.append(ai)
finish=False
for i in range(n):
    if i==0:
        next=a[a[0]-1]
    else:
        next=a[next-1]
    if next==2:
        print(i+2)
        finish=True
        break
if not finish:
    print(-1)

Fixed code:
n=int(input())
a=[]
for i in range(n):
    ai=int(input())
    a.append(ai)
finish=False
for i in range(n):
    if a[0]==2:
        print(1)
        finish=True
        break
    if i==0:
        next=a[a[0]-1]
    else:
        next=a[next-1]
    if next==2:
        print(i+2)
        finish=True
        break
if not finish:
    print(-1)
Buggy code:
N = int(input())
a = list(map(int, [input() for i in range(N)]))

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

print(-1)


Fixed code:
N = int(input())
a = list(map(int, [input() for i in range(N)]))

current_index = 1
for i in range(N - 1):
    current_index = a[current_index - 1]
    if current_index == 2:
        print(i + 1)
        exit()

print(-1)
Buggy code:
import collections
N=int(input())
a=[-1]*(N+1)
checker = collections.defaultdict(int)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
prev=[1]  
for _ in range(N):
  if pos == 2:
    print(len(checker.values())-1)
    exit(0)
  pos = a[pos]
  if pos not in checker:
    checker[pos] = 1
  

print(- 1)


Fixed code:
import collections
N=int(input())
a=[-1]*(N+1)
checker = collections.defaultdict(int)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
checker[1] = 1
for _ in range(N):
  if pos == 2:
    print(len(checker.values()) - 1)
    exit(0)
  pos = a[pos]
  if pos not in checker:
    checker[pos] = 1
  else:
    break
  

print(- 1)
Buggy code:
Row = int(input())
List = []
for i in range (Row):
  List.append(int(input()))
s = 1
res = 0
flag = False
for i in range(Row):
  s = List[s]
  res += 1
  if s == 2:
    flag=True
    break
if not flag:
  print(-1)
else:
  print(res)

Fixed code:
Row = int(input())
List = []
for i in range (Row):
  List.append(int(input()))
s = 1
res = 0
flag = False
for i in range(Row):
  s = List[s-1]
  res += 1
  if s == 2:
    flag=True
    break
if not flag:
  print(-1)
else:
  print(res)
Buggy code:
N = int(input())
A = [int(input()) for i in range(N)]
B = 1
for j in(A):
  if j == 2:
    print(B)
    break
  elif j == [-1]:
    print('-1')
  else:
    B += 1

Fixed code:
N = int(input())
A = [int(input()) for i in range(N)]
B = 0
C = 1
for j in range(N):
  if A[B] == 2:
    print(C)
    break
  elif j == N-1:
    print('-1')
  else:
    B = A[B]-1
    C += 1
Buggy code:
N=int(input())
b=list(int(input()) for i in range(N))

n=0
ans=0
for j in range(N):
    n=b[n]-1
    ans += 1
    if n==1:
        break
print(ans)

Fixed code:
N=int(input())
b=list(int(input()) for i in range(N))

n=0
ans=0
for j in range(N+1):
    n=b[n]-1
    ans += 1
    if n==1:
        break
if ans<N+1:
    print(ans)
else:
    print(-1)
Buggy code:
n = int(input())
alist = [int(input())-1 for _ in range(n)]

now = 0
count = 0
while True:
    if now == 1:
        print(c)
        break
    elif c >= n:
        print(-1)
        break
    else:
        count += 1
        now = alist[now]

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

now = 0
count = 0
while True:
    if now == 1:
        print(count)
        break
    elif count >= n:
        print(-1)
        break
    else:
        count += 1
        now = alist[now]
Buggy 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)


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


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

Fixed code:
N=int(input())
aN=[int(input()) for _ in range(N)]
c=0
i=1
while True:
  c+=1
  if aN[i-1]==2:
    print(c)
    break
  elif aN[i-1]==0:
    print(-1)
    break
  t=aN[i-1]
  aN[i-1]=0
  i=t
Buggy code:
n = int(input())
arr = []
passed = []
for i in range(n):
  a = int(input())
  arr.append(a)
  
next_position = 1
ans = 0

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

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

while(1):
  next_position = arr[next_position-1]
  #print(position)
  if ans == n:
    print(-1)
    break
  #passed.append(next_position)
  ans += 1
  if next_position == 2:
    print(ans)
    break
Buggy code:
n = int(input())

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

i = 0
count = 0

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

Fixed code:
n = int(input())

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

i = 0
count = 0

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

if count == n + 1:
  count = -1
  
print(count)
Buggy 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:
    j = a[j]
    count += 1

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


Fixed code:
N = int(input())
a = []
for i in range(N):
  a.append(int(input()))
j = 0
count = 1
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")
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]

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

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

count = 0
i = 0
while count <= 10e+5:
    count += 1
    if a[i] == 2:
        print(count)
        break
    else:
        i = a[i]-1
else:
    print(-1)
Buggy 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])

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


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

visited = set()

cur = 0
visited.add(0)
ans = 0
while cur != 1:
    if a[cur] not in visited:
        visited.add(a[cur])
        cur = a[cur]
        ans += 1
    else:
        ans = -1

print(ans)


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

visited = set()

cur = 0
visited.add(0)
ans = 0
while cur != 1:
    if a[cur] not in visited:
        visited.add(a[cur])
        cur = a[cur]
        ans += 1
    else:
        ans = -1
        break

print(ans)
Buggy code:
N = int(input())
D = {i+1:int(input()) for i in range(N)}
d = D{1}
ans = 1


for i in range(N):
  if d==2:
    print(ans)
    exit()
    
  d = D[d]
  ans +=1
  
  
print(-1)

Fixed code:
N = int(input())
D = {i+1:int(input()) for i in range(N)}
d = D[1]
ans = 1


for i in range(N):
  if d==2:
    print(ans)
    exit()
    
  d = D[d]
  ans +=1
  
  
print(-1)
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]
pos=0
for i in range(n):
  print(pos,a[pos])
  if a[pos] == 2:
    print(i+1)
    exit()
  pos = a[pos]-1
print(-1)

Fixed code:
n = int(input())
a = [int(input()) for _ in range(n)]
pos=0
for i in range(n+1):
  if a[pos] == 2:
    print(i+1)
    exit()
  pos = a[pos]-1
print(-1)
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]

i = 0
cnt = 0
while not i == 1:
    i = a[i] - 1
    cnt += 1
    if i >= n or cnt > 100000:
        exit("-1")
print(cnt)

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

i = 0
cnt = 0
while not i == 1:
    i = a[i] - 1
    cnt += 1
    if i >= n or cnt > 100000:
        print(-1)
        exit()
print(cnt)
Buggy code:
n = int(input())
a = [int(input())-1 for i in range(n)]
print(a)

light = a[0]
count = 0
while count <= n:
  #print(light)
  light = a[light]
  count +=1  
  if light == 1:
   # print('b')
    ans = count+1
    break
else:
  ans = -1

print(ans)    


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

light = 0
count = 0
while count < n:
  #print(light)
  light = a[light]
  count +=1  
  if light == 1:
   # print('b')
    ans = count
    break
else:
  ans = -1

print(ans)
Buggy code:
n=int(input())
l=[int(input()) for i in range(n)]

num=l[0]
cnt=1
for i in range(10**5):
  num=l[num-1]
  cnt+=1
  if num==2:
    break
    
if cnt==10**5:
  cnt=-1
  
print(cnt)

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

num=l[0]
cnt=0
for i in range(10**5):
  if num==2:
    cnt+=1
    break
  else:
    num=l[num-1]
    cnt+=1
    
if cnt==10**5:
  cnt=-1
  
print(cnt)
Buggy code:
#1
n = int(input())
a = [0]

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

b = a[1]
cnt = 0

for j in range(n+1):
    b = a[b]
    cnt += 1
    
    if b == 2:
        print(cnt)
        break
    if cnt == n:
        print(-1)


Fixed code:
#1
n = int(input())
a = [0]

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

cnt = 0
b = 1
ans = 0

for j in range(n+1):
    cnt += 1
    b = a[b]
    
    if b == 2:
        ans = 1
        break
if ans == 1:
    print(cnt)

else:
    print("-1")
Buggy code:
n=int(input())
s=[]
k=s[0]
ans=0
for i in range(n):
  a=int(input())
  s.append(a)
for j in range(n+1):
  if s[k]==2:
    ans+=1
    print(ans)
    exit()
  else:
    ans+=1
    k=s[s[k]]
print(-1)  

Fixed code:
n=int(input())
s=[]
ans=0
for i in range(n):
  a=int(input())
  s.append(a)
k=s[0]
for j in range(n+1):
  if k==2:
    ans+=1
    print(ans)
    exit()
  else:
    ans+=1
    k=s[k-1]
print(-1)
Buggy code:
# -*- coding:utf-8 -*-
import sys

a=[int(i)-1 for i in sys.stdin]
N=l[0]+1
a=a[1:]

done=[False]*N

ans=0
i=0

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

  
  else:
    ans=-1
    break
    
print(ans)
    

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

a=[int(i)-1 for i in sys.stdin]
N=a[0]+1
a=a[1:]

done=[False]*N

ans=0
i=0

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

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

Fixed code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input())-1)
 
light=0
 
for i in range(1,n+1):
    light=a[light]
    if light == 1:
        print(i)
        break
else:
    print(-1)
Buggy code:
#ABC065.B
N = int(input())
a = [int(input() for i in range(N))]
x = 0
for i in range(N):
    x = a[x]
    if a[x] == 2:
        print(i+1)
        break
else:
    print(-1)

Fixed code:
#ABC065.B
N = int(input())
a = [int(input()) for i in range(N)]
x = 1
for i in range(N):
    x = a[x-1]
    if x == 2:
        print(i+1)
        break
else:
    print(-1)
Buggy code:
n=int(input())
d=[int(input())-1for i in range(n)]
s=set(0)
ans=0
b=0
while 1>0:
  if d[b] in s:
    ans=-1
    break
  b=d[b]
  s+=d[b]
  ans+=1
  if b==1:
    break
print(ans)

Fixed code:
n=int(input())
d=[int(input())-1for i in range(n)]
s=set()
s.add(0)
ans=0
b=0
while 1>0:
  if d[b] in s:
    ans=-1
    break
  b=d[b]
  s.add(b)
  ans+=1
  if b==1:
    break
print(ans)
Buggy code:
N = int(input())
A = []
for i in range(N):
    a = int(input())
    A.append(a)
check = 0
count = 0
botton = 1
print(A[1 -1])
for i in range(N):
    count += 1
    if A[botton - 1] == 2:
        check = 1
        break
    botton = A[botton - 1]

if check == 0:
    print(-1)
else:
    print(count)

Fixed code:
N = int(input())
A = []
for i in range(N):
    a = int(input())
    A.append(a)
check = 0
count = 0
botton = 1
for i in range(N):
    count += 1
    if A[botton - 1] == 2:
        check = 1
        break
    botton = A[botton - 1]

if check == 0:
    print(-1)
else:
    print(count)
Buggy 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)

Fixed 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):
  nex = a[x-1]
  if not nex:
    c = -1
    break
  else:
    c += 1
    a[x-1] = False
  x = nex
 
print(c)
Buggy 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)



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

cnt, i = 0, 0
while cnt < N:
    print(i)
    cnt += 1
    if A[i] == 1:
        break
    i = A[i]
print(cnt if cnt < N else -1)

Fixed code:
N = int(input())
A = [int(input()) - 1 for _ in range(N)]

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

ans, nex = 1, a[0]
while ans < N or nex == 2:
	 nex = a[nex - 1]
	 ans += 1
	 
print(ans if ans != N else -1)

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

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

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

for i in range(N+1):
  nxt = a[i]-1
  cnt += 1
  if a[nxt] == 2:
    print(cnt)
    exit()
  else:
    nxt = a[i] - 1

print(-1)

Fixed code:
N = int(input())
a = [int(input()) for _ in range(N)]
nxt = a[0]-1
cnt = 1
if a[0]-1 == 1:
  print(cnt)
  exit()

for i in range(1, N+1):
  if a[nxt] == 2:
    cnt += 1
    print(cnt)
    exit()
  else:
    nxt = a[nxt] - 1
    cnt += 1
else:    
  print(-1)
Buggy code:
n=int(input())
a=[input() for i in range(n)]
bu=0
end=-1
for i in range(n):
   if bu==1:
    end=i-1
    break
  bu=a[bu]-1
print(end)

Fixed code:
n=int(input())
a=[int(input()) for i in range(n)]
bu=0
end=-1
for i in range(n):
  if bu==1:
    end=i
    break
  bu=a[bu]-1
print(end)
Buggy code:
n=int(input())
lst=[int(input()) for i in range(n)]

lst = list(map(lambda x: x-1, lst))
lst_p = [0]
tmp = 0
flag =True
for i in range(n+1):
    tmp = lst[tmp]
    if tmp == 1:
        print(i)
        flag = False
        break
if flag:
    print(-1)

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

lst = list(map(lambda x: x-1, lst))
lst_p = [0]
tmp = 0
flag =True
for i in range(n+1):
    tmp = lst[tmp]
    if tmp == 1:
        print(i+1)
        flag = False
        break
if flag:
    print(-1)
Buggy code:
import sys
input = sys.stdin.readline
n=int(input())

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


def fun(k):
    if B[k] == 1:
     	return False                                                    
    else:
        return B[k]
ans = 0
cnt = 0
for i in range(n):
    cnt = fun(cnt)
    ans +=1
    if cnt ==False:
        print(ans)
        sys.exit() 
print(-1)
sys.exit()

Fixed code:
import sys
input = sys.stdin.readline
n=int(input())

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


def fun(k):
    if B[k] == 1:
     	return -5                                                    
    else:
        return B[k]
ans = 0
cnt = 0
for i in range(n+1):
    cnt = fun(cnt)
    ans +=1
    
    if cnt ==-5:
        print(ans)
        sys.exit() 
print(-1)
sys.exit()