qyliang/CodeNanoFix huggingface dataset:
Buggy 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)

Fixed 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]
    if(next == 2):
        print(count)
        break
    start = next - 1
else:
    print(-1)
Buggy code:
import sys
N = int(input())
ls = []
for i in range(N):
    ls.append(int(input()))
i = 0
cnt = 1
while(cnt <= N):
    cnt += 1
    k = ls[i]
    if (ls[k-1] == 2):
        print(cnt)
        sys.exit()
    i = ls[k-1] - 1
print(-1)

Fixed code:
import sys
N = int(input())
ls = []
for i in range(N):
    ls.append(int(input()))
i = 0
cnt = 1
while(cnt <= N):
    k = ls[i]
    if (k == 2):
        print(cnt)
        sys.exit()
    cnt += 1
    i = k-1
print(-1)
Buggy code:
n = int(input())
lista = []
for i in range(n):
  lista.append(int(input()))

count = 1
fp = lista[0]
for i in range(n):
  fp = lista[fp]
  count += 1
  if fp == 2:
    print(count)
    exit()
print(-1)
       

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

count = 1
fp = lista[0]
if fp == 2:
  print(count)
  exit()

for i in range(n):
  fp = lista[fp-1]
  count += 1
  if fp == 2:
    print(count)
    exit()
print(-1)
Buggy code:
n = int(input())
li = [int(input()) for _ in range(n)]

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

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

light = 1
count = 0

for i in range(n):
  light = li[light - 1]
  count+=1
  if light == 2:
    print(count)
    exit()
print(-1)
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]
i = 1
c = 0
while True:
  if i == 2:
    print(c)
    quit(0)
  if a[i] == i:
    print(-1)
    quit(0)
  c += 1
  i, a[i] = a[i], i

Fixed code:
n = int(input())
a = [0] + [int(input()) for _ in range(n)]
i = 1
c = 0
while True:
  if i == 2:
    print(c)
    quit(0)
  if a[i] == i:
    print(-1)
    quit(0)
  c += 1
  tmp = a[i]
  a[i] = i
  i = tmp
Buggy code:
N=int(input())
a=[int(input())for i in range(N)]
p=1
ok=False
ans=0
for i in range(N):
    ans+=1
    if p==2:
        ok=True
        break
    p=a[p-1]
print(ans if ok else -1)

Fixed code:
N=int(input())
a=[int(input())for i in range(N)]
p=1
ok=False
ans=0
for i in range(N):
    if p==2:
        ok=True
        break
    p=a[p-1]
    ans+=1
print(ans if ok else -1)
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]
now = a[0]
c = 1

if now == 2:
    print(1)
    exit()

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

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

if now == 2:
    print(1)
    exit()

for i in range(n):
    now = a[now-1]
    c += 1
    if now == 2:
        print(c)
        exit()
print(-1)
Buggy code:
n=int(input())
a=[int(input()) for i in range(n)]
x=1
cnt=0
y=[]
while True:
    x=a[x-1]
    cnt+=1
    y.append(x)
    if x==2:
        print(cnt)
        break
    elif x==1 or x in y:
        print('-1')
        break
    else:
        continue


Fixed code:
n=int(input())
a=[int(input()) for i in range(n)]
x=1
cnt=0
y=[1]
while True:
    x=a[x-1]
    cnt+=1
    y.append(x)
    if x==2:
        print(cnt)
        break
    elif x==1:
        print('-1')
        break
    elif cnt==100000:
        print(-1)
        break
    else:
        continue
Buggy code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
p = 0
while True:
    if p == 1 or cnt >= n:
        break
    p = A[p-1]
    cnt += 1
if p == 1:
    print(cnt)
else:
    print(-1)

Fixed code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections


n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
p = 1
while True:
    p = A[p-1]
    cnt += 1
    if p == 2 or cnt > n:
        break
if p == 2:
    print(cnt)
else:
    print(-1)
Buggy code:
N = int(input())
A = [int(input()) for _ in range(N)]

nb = 0
for i in range(N):
  nb = A[nb]
  if nb == 2:
    print(i+1)
    exit()

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

nb = 1
for i in range(N):
  nb = A[nb-1]
  if nb == 2:
    print(i+1)
    exit()
print(-1)
Buggy code:
N = int(input())
A = [int(input()) for i in range(N)]
Next = 0
for i in range(N):
  if Next == 2:
    Next = A[Next]
    p = i
    break;
  else:
    Next = A[Next-1]
    p = -1
print(p)

Fixed code:
N = int(input())
A = [int(input()) for i in range(N)]
Next = 1
for i in range(N):
  if Next == 2:
    Next = A[Next-1]
    p = i
    break;
  else:
    Next = A[Next-1]
    p = -1
print(p)
Buggy code:
N = int(input())
buttons = [int(input()) for i in range(N)]

button = 1
count = 0
while(True):
    button = buttons[button - 1]
    count += 1
    if (button == 2):
        print(count)
        break
    elif(count > N):
        print(-1)

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

button = 1
count = 0
while(True):
    button = buttons[button - 1]
    count += 1
    if (button == 2):
        print(count)
        break
    elif(count > N):
        print(-1)
        break
Buggy code:
N = int(input())
a = [int(input()) for i in range(N)]
count = 0
index = 1
end = 2
if end in a:
    print(-1)
else :
    while(True):
        count += 1
        index = a[index]
        if (index == end):
            print(count)
            break
        elif count > N:
            print(-1)
            break

Fixed code:
N = int(input())
a = [int(input()) for i in range(N)]
count = 0
index = 1
end = 2
if not end in a:
    print(-1)
else :
    while(True):
        count += 1
        index = a[index-1]
        if (index == end):
            print(count)
            break
        elif count > N:
            print(-1)
            break
Buggy code:
import sys 
N = list(map(int, sys.stdin))
bot = N[1]
for i in range(N[0]):
  if bot == 2:
    print(i)
    break
  bot == N[bot]
else:
  print(-1)

Fixed code:
import sys 
N = list(map(int, sys.stdin))
bot = N[1]
for i in range(N[0]):
  if bot == 2:
    print(i+1)
    break
  bot = N[bot]
else:
  print(-1)
Buggy code:
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
print(a)
cnt = 1
x = a[0]-1
if a[0] == 2:
    print(cnt)
    exit()
for _ in range(10**5+10):

    if a[x] == 2:
        cnt += 1
        print(cnt)
        exit()
    else:
        x = a[x]-1
        cnt += 1
print(-1)


Fixed code:
import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
# print(a)
cnt = 1
x = a[0]-1
if a[0] == 2:
    print(cnt)
    exit()
for _ in range(10**5+10):

    if a[x] == 2:
        cnt += 1
        print(cnt)
        exit()
    else:
        x = a[x]-1
        cnt += 1
print(-1)
Buggy code:
N = int(input())
a = [list() for i in range(N+1)]
for i in range(N):
    b = int(input())
    a[i+1].append(b)

ans = 0
check = []
tmp = 1
check.append(tmp)
flag = False
while(1):
    tmp = a[tmp]
    ans += 1
    if tmp==2:
        flag = True
        break
    if tmp in check:
        break
if flag:
    print(ans)
else:
    print(-1)

Fixed code:
N = int(input())
a = []
for i in range(N):
    b = int(input())
    a.append(b)
ans = 0
tmp = 1
flag = False
while(1):
    tmp = a[tmp-1]
    ans += 1
    if tmp==2:
        flag = True
        break
    if ans > N:
        break
if flag:
    print(ans)
else:
    print(-1)
Buggy 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)


Fixed 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(-1)
Buggy code:
# 2019/6/25
import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
s = set()
cur = 1
ans = 0
while not cur in s:
    if a[cur-1] == 2:
        print(ans+1)
        sys.exit()
    else:
        cur = a[cur-1]
        ans += 1
print(-1)

Fixed code:
# 2019/6/25
import sys
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
if not 2 in a:
    print(-1)
    sys.exit()
s = set()
cur = 1
ans = 0
while not cur in s:
    if a[cur-1] == 2:
        print(ans+1)
        sys.exit()
    else:
        s.add(cur)
        cur = a[cur-1]
        ans += 1
print(-1)
Buggy 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")


Fixed code:
#ABC065B
N = int(input())
ai = [int(input()) for _ in range(N)]

lightning = 1
count = 1
ok = False
for _ in range(N):
    lightning = ai[lightning-1]
    if lightning == 2:
        print(count)
        ok = True
        break
    else:
        count += 1

if not ok:
    print(-1)
Buggy code:
import sys
a = [int(i) for i in sys.stdin]
now = a[0]
count = 0
while True:
    count += 1
    if now == 2:
        print(count)
        exit()
        now = a[now]
print(-1)


Fixed code:
import sys
a = [int(i) for i in sys.stdin]
now = a[1]
count = 0
while count!=a[0]:
    count += 1
    if now == 2:
        print(count)
        exit()
    now = a[now]
print(-1)
Buggy code:
n = int(input())
buttons = [0]
target_index = 0
for _ in range(n):
    num = int(input())
    buttons.append(num)

count = 0
next_index = 2
while count < n:
    if next_index < 1 or len(buttons) - 1 <= next_index:
        count = -1
        break

    prev_index = buttons.index(next_index)
    count += 1
    if prev_index == 1:
        break
    else:
        next_index = prev_index

print(count if count < n else -1)


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

count = 0
prev_index = 1
while count < n:
    if prev_index < 1 or len(buttons) <= prev_index:
        count = -1
        break

    next_index = buttons[prev_index]
    count += 1
    if next_index == 2:
        break
    else:
        prev_index = next_index

print(count if count < n else -1)
Buggy code:
N = int(input())
a = [int(input()) for i in range(N)]
i = 0

while cnt <= N:
  lgt = a[i]
  cnt += 1
  if lgt == 2:
    print(cnt)
    break
  i = lgt - 1
else:
  print(-1)

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

while cnt <= N:
  lgt = a[i]
  cnt += 1
  if lgt == 2:
    print(cnt)
    break
  i = lgt - 1
else:
  print(-1)
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]
index = 1
count = 0
while index != 2 and count < n:
  index = a[index-1]
  count += 1
print(count)

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

Fixed code:
n=int(input())
a=[int(input()) for i in range(n)]
b=a[0]
for i in range(n+1):
  if b==2:
    print(i+1)
    break
  else:
    b=a[b-1]
  if i==n:
    print(-1)
Buggy code:
N = int(input())
A = [int(input()) for _ in range(N)]
pos = 1
ans = 0
while pos != 2:
  pos = A[pos-1]
  ans += 1
  if ans == 100000:
    break
print(ans)
  

Fixed code:
N = int(input())
A = [int(input()) for _ in range(N)]
pos = 1
ans = 0
while pos != 2:
  pos = A[pos-1]
  ans += 1
  if ans == 100000:
    ans = -1
    break
print(ans)
Buggy 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


Fixed code:
N = int(input())
a = [int(input()) for i in range(N)]
ch = [0] * (10**5 + 1)
numB = 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
Buggy code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0

out=0

while i!=1:
  out+=1
  i=a[i]
  if i>n:
    out=-1
    break

print(out)


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

out=0
kai=0
while i!=1:
  kai+=1
  out+=1
  i=a[i]
  if kai>n:
    out=-1
    break

print(out)
Buggy code:
n=int(input())
A=[int(input()) for _ in range(n)]
A=[0]+A
cnt=1
for ? in range(1,n+1):
  cnt=A[cnt]
  if cnt==2:
    print(?)
    exit()
  
print(-1)
  

Fixed code:
n=int(input())
A=[int(input()) for _ in range(n)]
A=[0]+A
cnt=1
for i in range(1,n+1):
  cnt=A[cnt]
  if cnt==2:
    print(i)
    exit()
  
print(-1)
Buggy code:
n = int(input())
a = [int(input()) for _ in range(n)]
lis = ["0"]*n
bot = a[0]
lis[0] == "1"
cnt = 1
while True:
    if bot == 2:
        print(cnt)
        break
    elif lis[bot-1] == "1":
        print(-1)
        break
    else:
        cnt += 1
        bot = a[bot-1]
        lis[bot-1] = "1"

Fixed code:
n = int(input())
a = [int(input()) for _ in range(n)]
lis = ["0"]*n
bot = a[0]
lis[0] = "1"
cnt = 1

while True:
    if bot == 2:
        print(cnt)
        break
    elif bot != 2 and lis[bot-1] == "1":
        print(-1)
        break
    else:
        cnt += 1
        lis[bot-1] = "1"
        bot = a[bot-1]
Buggy code:
n = int(input())
a = [int(input()) for i in range(n)]

ans = 0
tmp = 0

for i in range(n):
    ans += 1
    if a[tmp] == 2:
        break;
    tmp = a[tmp] - 1
if ans == 0:
    ans = -1

print(ans)


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

ans = 0
tmp = 0

for i in range(n):
    ans += 1
    if a[tmp] == 2:
        break;
    tmp = a[tmp] - 1
if ans == n:
    ans = -1

print(ans)
Buggy code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))

idx = 1
flag = False
for i in range(1, N+1):
  idx = A[idx - 1]
  if idx == 2:
    flag = True
    break

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

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

idx = 1
flag = False
for i in range(1, N+1):
  idx = A[idx - 1]
  if idx == 2:
    flag = True
    break

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

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

Fixed code:
n = int(input())
data = [int(input()) for i in range(n)]
count = 0
temp = 1
for i in range(n):
    temp = data[temp-1]
    count += 1
    if temp == 2:
        print(count)
        break
else:
    print(-1)
Buggy code:
N=int(input())
a=[int(input()) for i in range(N)]
X=1
i=1
while a[i-1]!=2 and X<=n:
    X+=1
    i=a[i-1]
if X>N:
  print(-1)
else:
  print(X)

Fixed code:
N=int(input())
a=[int(input()) for _ in range(N)]
X=1
i=1
while a[i-1]!=2 and X<=N:
    X+=1
    i=a[i-1]
if X>N:
  print(-1)
else:
  print(X)
Buggy code:
import sys
n = int(input())
a = []
ans = 0
lamp = 1
for _ in range(n):
  A = int(input())
  a.append(a)
  
for i in range(n):
  if lamp -1 == 1:
    print(i)
    sys.exit()
  lamp = a[lamp - 1]
print(-1)

Fixed code:
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:
N = int(input())
a = []
for _ in range(N):
    a.append(int(input()))
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]
for _ in range(N):
    a.append(int(input()))
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:
# -*- 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
	if tugi-1 == 1:
	print(i)
	sys.exit()
	while i < N+1:
		i += 1
		tugi = btnList[tugi-1]
		if tugi-1 == 1:
			print(i)
			sys.exit()
	print(-1)
	
if __name__ == "__main__":
	main()
	

Fixed 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
	if tugi-1 == 1:
		print(i)
		sys.exit()
	while i < N+1:
		i += 1
		tugi = btnList[tugi-1]
		if tugi-1 == 1:
			print(i)
			sys.exit()
	print(-1)
	
if __name__ == "__main__":
	main()
Buggy code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = []
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif A[push] in count:
      break
      
    else:
      　count.append(push)
        push = A[push]
        
print(ans)

Fixed code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = set()
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif A[push] in count:
      break
      
    else:
      count.add(push)
      push = A[push]
        
print(ans)
Buggy code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = [False]*N
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif count[push]:
      break
      
    else:
        push = A[push]
        count[push] = True
      
print(ans)

Fixed code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input())-1)
    
ans = -1
push = 0
count = [False]*N
 
for i in range(N+1):
 
    if A[push] == 1:
        ans = i+1
        break
        
    elif count[push]:
      break
      
    else:
        count[push] = True
        push = A[push]
        
      
print(ans)
Buggy code:
import sys
n = int(input())
ns = [int(input()) for _ in range(n)]
if not 1 in ns:
    print(-1)
    sys.exit(0)
i = 0
cnt = 0
pushed = [0]*n
flg = False
while(True):
    cnt += 1
    i = ns[i] - 1
    if pushed[i] == 1:
        break
    pushed[i] = 1
    if i == 1:
        flg = True
        break
if flg:
    print(cnt)
else:
    print(-1)

Fixed code:
import sys
n = int(input())
ns = [int(input()) for _ in range(n)]
if not 2 in ns:
    print(-1)
    sys.exit(0)
i = 0
cnt = 0
pushed = [0]*n
flg = False
while(True):
    cnt += 1
    i = ns[i] - 1
    if pushed[i] == 1:
        break
    pushed[i] = 1
    if i == 1:
        flg = True
        break
if flg:
    print(cnt)
else:
    print(-1)
Buggy code:
n = int(input())
a = [int(input()) for i in range(n)]
b = 0
cnt = 0
for i in range(1, n + 1):
    cnt += 1
    if a[b] == 2:
        break
    b = a[b] - 1
print(cnt)

Fixed code:
n = int(input())
a = [int(input()) for i in range(n)]
b = 0
cnt = 0
for i in range(1, n + 1):
    cnt += 1
    if a[b] == 2:
        print(cnt)
        exit()
    b = a[b] - 1
print(-1)
Buggy code:
n=int(input())
ans=1
a=[]
for i in range(n):
  a.append(int(input()))
x=a[0]
if x=-2:
  print(ans)
  exit()
for i in range(n):
  x=a[x-1]
  if x==2:
    print(ans+1)
    exit()
  else:
    ans+=1
print(-1)

Fixed code:
n=int(input())
ans=1
a=[]
for i in range(n):
  a.append(int(input()))
x=a[0]
if x==2:
  print(ans)
  exit()
for i in range(n):
  x=a[x-1]
  if x==2:
    print(ans+1)
    exit()
  else:
    ans+=1
print(-1)
Buggy code:
n = int(input())
buttons = [int(input()) for i in range(n)]
pushing = 1
counter = 0
while counter >= n:
    pushing = buttons[pushing]
    counter += 1
    if pushing == 2:
        print(counter)
        break
else:
    print(-1)


Fixed code:
n = int(input())
buttons = [int(input()) for i in range(n)]
pushing = 1
counter = 0
while counter <= n:
    pushing = buttons[pushing - 1]
    counter += 1
    if pushing == 2:
        print(counter)
        break
else:
    print(-1)
Buggy 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)


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

light=1
ans=0
while True:
    if buttun[light]==2:
        break
    elif light==buttun[light]:
        ans=-1
        break
    else:
        light=buttun[light]
        ans+=1
    if light==1:
        ans=-1
        break
print(ans)

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

light=1
ans=0
while True:
    if light==2:
        break
    elif light==buttun[light]:
        ans=-1
        break
    else:
        tmp=light
        light=buttun[light]
        ans+=1
        buttun[tmp]=1
    if light==1:
        ans=-1
        break
print(ans)
Buggy code:
n = int(input())
l = list(int(input()) for _ in range(n))
num = 1
ans = -1

for i in range(n):
    num = l[num-1]
    if num == 2: 
        ans = i
        break
print(ans)

Fixed code:
n = int(input())
l = list(int(input()) for _ in range(n))
num = 1
ans = -1

for i in range(n):
    if num == 2: 
        ans = i
        break
    num = l[num-1]
print(ans)