Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n, m, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

C = [0]
D = [0]
for a in A:
    C.append(C[-1] + a)
for b in B:
    D.append(D[-1] + b)

p = n
for i in range(n + 1):
    if C[i] > k:
        p = i - 1
        break

_max = 0
keep = 0
for i in range(p, -1, -1):
    for j in range(keep, m + 1):
        if C[i] + D[j] > k:
            keep = j - 1
            break
        elif _max < i + j:
            _max = i + j
print(_max)

Fixed submission:
n, m, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

C = [0]
D = [0]
for a in A:
    C.append(C[-1] + a)
for b in B:
    D.append(D[-1] + b)

p = n
for i in range(n + 1):
    if C[i] > k:
        p = i - 1
        break

_max = p
keep = 0
for i in range(p, -1, -1):
    for j in range(keep, m + 1):
        if C[i] + D[j] > k:
            keep = j - 1
            break
        elif _max < i + j:
            _max = i + j
    if j == m:
        break
print(_max)

Buggy python submission (verdict: Time Limit Exceeded):
n, m, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

C = [0]
D = [0]
for a in A:
    C.append(C[-1] + a)
for b in B:
    D.append(D[-1] + b)

for i in range(n + 1):
    if C[i] > k:
        p = i - 1
        break
    p = n

_max = 0
for i in range(p, -1, -1):
    for j in range(m + 1):
        if C[i] + D[j] > k:
            break
        elif _max < i + j:
            _max = i + j
print(_max)

Fixed submission:
n, m, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

C = [0]
D = [0]
for a in A:
    C.append(C[-1] + a)
for b in B:
    D.append(D[-1] + b)

p = n
for i in range(n + 1):
    if C[i] > k:
        p = i - 1
        break

_max = p
keep = 0
for i in range(p, -1, -1):
    for j in range(keep, m + 1):
        if C[i] + D[j] > k:
            keep = j - 1
            break
        elif _max < i + j:
            _max = i + j
    if j == m:
        break
print(_max)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

ans = 0
for x in range(1, n+1):
    y = int(n / x)
    ans += y * (y + 1) * x // 2

print(ans)

Fixed submission:
n = int(input())

ans = 0
for x in range(1, n+1):
    y = int(n / x)
    ans += y * (y + 1) * x // 2

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))
num = [0] * (10 ** 5)

for a in A:
    num[a] += 1
_sum = sum(A)

result = []
q = int(input())
for i in range(q):
    b, c = list(map(int, input().split()))
    _sum += (c - b) * num[b]
    num[c] += num[b]
    num[b] = 0
    result.append(str(_sum))
print('\n'.join(result))

Fixed submission:
if input().isupper():
  print('A')
else:
  print('a')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))

count = {}
for a in A:
    count[a] = count.get(a, 0) + 1

q = int(input())
result = []
for i in range(q):
    b, c = list(map(int, input().split()))
    if count.get(b):
        count[c] = count.get(c, 0) + count.pop(b)
    sum = 0
    for key, value in count.items():
        sum += key * value
    result.append(str(sum))
print('\n'.join(result))

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
num = [0] * (10 ** 5 + 1)

for a in A:
    num[a] += 1
_sum = sum(A)

result = []
q = int(input())
for i in range(q):
    b, c = list(map(int, input().split()))
    _sum += (c - b) * num[b]
    num[c] += num[b]
    num[b] = 0
    result.append(str(_sum))
print('\n'.join(result))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))

count = {}
for a in A:
    if count.get(a):
        count[a] += 1
    else:
        count[a] = 1

q = int(input())
result = []
for i in range(q):
    b, c = list(map(int, input().split()))
    if count.get(b):
        count[c] = count.get(c, 0) + count.pop(b)
    sum = 0
    for key, value in count.items():
        sum += key * value
    result.append(str(sum))
print('\n'.join(result))

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
num = [0] * (10 ** 5 + 1)

for a in A:
    num[a] += 1
_sum = sum(A)

result = []
q = int(input())
for i in range(q):
    b, c = list(map(int, input().split()))
    _sum += (c - b) * num[b]
    num[c] += num[b]
    num[b] = 0
    result.append(str(_sum))
print('\n'.join(result))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))

count = {}
for a in A:
    if count.get(a):
        count[a] += 1
    else:
        count[a] = 1

q = int(input())
B = []
C = []
for i in range(q):
    b, c = list(map(int, input().split()))
    B.append(b)
    C.append(c)
for i in range(q):
    b = B[i]
    c = C[i]
    if count.get(b):
        count[c] = count.get(c, 0) + count.pop(b)
    sum = 0
    for key, value in count.items():
        sum += key * value
    print(sum)

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
num = [0] * (10 ** 5 + 1)

for a in A:
    num[a] += 1
_sum = sum(A)

result = []
q = int(input())
for i in range(q):
    b, c = list(map(int, input().split()))
    _sum += (c - b) * num[b]
    num[c] += num[b]
    num[b] = 0
    result.append(str(_sum))
print('\n'.join(result))
Buggy python submission (verdict: Runtime Error):
x, n = [int(i) for i in input().split(' ')]
if n > 0:
    p = [int(i) for i in input().split(' ')]

    p.append(x)
    p.sort()
    i = p.index(x)

    y = x
    double = 0

    for j in range(1, i + 1):
        if i == j:
            low = x - j - 1
        elif not x - j + double == p[i - j]:
            if x == p[i - j]:
                double += 1
            else:
                low = x - j + double
                break
    double = 0
    for j in range(1, len(p) - i + 1):
        if j + i == len(p):
            high = x + j + 1
        elif not x + j - double == p[i + j]:
            if x == p[i + j]:
                double += 1
            else:
                high = x + j - double
                break

    if x - low > high - x:
        print(high)
    else:
        print(low)
else:
    print(x)
    exit()

Fixed submission:
x, n = [int(i) for i in input().split(' ')]

if n:
    p = [int(i) for i in input().split(' ')]
    if x in p:
        for i in range(1, 101):
            if not x - i in p:
                print(x - i)
                exit()
            elif not x + i in p:
                print(x + i)
                exit()
    else:
        print(x)
else:
    print(x)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(i) for i in input().split(" ")]

a.sort()
dp = [True for i in range(n)]

for i in range(n):
    if dp[i]:
        for j in range(i + 1, n):
            if not a[j] % a[i]:
                dp[j] = False
            if a[j] == a[i]:
                dp[i] = False

print(dp.count(True))

Fixed submission:
n = int(input())
a = [int(i) for i in input().split(" ")]

a.sort()
dp = [True for i in range(a[-1]+1)]
count=0

for i in range(n):
    if dp[a[i]]:
        count+=1
        for j in range(1,n-i):
            if a[i]!=a[i+j]:
                break
            elif j==1:
                count-=1
            dp[a[i+j]]=False
        for j in range(a[i], a[-1]+1, a[i]):
            dp[j]=False
        # for j in range(i + 1, n):
        #     if not a[j] % a[i]:
        #         dp[j] = False
        #     if a[j] == a[i]:
        #         dp[i] = False

#print(dp.count(True))
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(i) for i in input().split(" ")]

a.sort()
dp = [True for i in range(n)]

for i in range(n):
    if dp[i]:
        for j in range(i + 1, n):
            if not a[j] % a[i]:
                dp[j] = False
            if a[j] == a[i]:
                dp[i] = False

print(dp.count(True))

Fixed submission:
n = int(input())
a = [int(i) for i in input().split(" ")]

a.sort()
dp = [True for i in range(a[-1]+1)]
count=0

for i in range(n):
    if dp[a[i]]:
        count+=1
        for j in range(1,n-i):
            if a[i]!=a[i+j]:
                break
            elif j==1:
                count-=1
            dp[a[i+j]]=False
        for j in range(a[i], a[-1]+1, a[i]):
            dp[j]=False
        # for j in range(i + 1, n):
        #     if not a[j] % a[i]:
        #         dp[j] = False
        #     if a[j] == a[i]:
        #         dp[i] = False

#print(dp.count(True))
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
n, = (int(x) for x in input().split())
p = list(int(x) for x in input().split())

x = 0
for i in range(n):
    for j in range(i+1):
        if p[i] > p[j]:
            break
        if i == j:
            x += 1
print(x)

Fixed submission:
n, = (int(x) for x in input().split())
p = list(int(x) for x in input().split())
 
x = 0
m = 1e6
for i in range(n):
    if p[i] <= m:
        x += 1
        m = p[i]
print(x)

Buggy python submission (verdict: Time Limit Exceeded):
n, = (int(x) for x in input().split())
p = list(int(x) for x in input().split())

x = 0
for i in range(n):
    if p[i] <= min(p[:i]+[p[i]]):
        x += 1
        pass
print(x)

Fixed submission:
n, = (int(x) for x in input().split())
p = list(int(x) for x in input().split())
 
x = 0
m = 1e6
for i in range(n):
    if p[i] <= m:
        x += 1
        m = p[i]
print(x)
Buggy python submission (verdict: Time Limit Exceeded):
wfwfe=input()

b=input().split()

if "0" in b:
    print (0)
else:

    count=1
    for i in b:
        count=count*int(i)
        
    if count>10**18:
        print (-1)
    else:
        print (count)


Fixed submission:
wfwfe=input()
check=0
b=input().split()
if "0" in b:
    print (0)
else:
    
    count=1
    for i in b:
        count=count*int(i)
        
        if count>10**18:
            print (-1)
            check=1
            break
    if check==0:
        print (count)

Buggy python submission (verdict: Time Limit Exceeded):
wfwfe=input()

b=input().split()



count=1
for i in b:
    count=count*int(i)
    
if count>10**18:
    print (-1)
else:
    print (count)


Fixed submission:
wfwfe=input()
check=0
b=input().split()
if "0" in b:
    print (0)
else:
    
    count=1
    for i in b:
        count=count*int(i)
        
        if count>10**18:
            print (-1)
            check=1
            break
    if check==0:
        print (count)

Buggy python submission (verdict: Time Limit Exceeded):
wfwfe=input()

b=input().split()

b=[int(p) for p in b]

count=1
for i in b:
    count=count*i
if count>10**18:
    print (-1)
else:
    print (count)


Fixed submission:
wfwfe=input()
check=0
b=input().split()
if "0" in b:
    print (0)
else:
    
    count=1
    for i in b:
        count=count*int(i)
        
        if count>10**18:
            print (-1)
            check=1
            break
    if check==0:
        print (count)
Buggy python submission (verdict: Runtime Error):
a=input().split()

a=[float(p) for p in a]

c=a*b*100
print (c//100)



Fixed submission:
a=input().split()

b=int(a[0])
lst=list(a[1])

del lst[-3]

c=""
for i in lst:
    c=c+i
c=int(c)

print (int(b*c//100))
Buggy python submission (verdict: Runtime Error):
x = list(map(int,inpt().split()))
for i in range(5):
  if x[i] ==0:
    ans = i
    
print(ans)

Fixed submission:
x = list(map(int,input().split()))
for i in range(5):
    if x[i] ==0:
        ans = i+1
    
print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()

if s == Sunny:
    print('Cloudy')
elif s == 'Cloudy':
    print('Rainy')
else:
    print('Sunny')

Fixed submission:
s = input()

if s == 'Sunny':
    print('Cloudy')
elif s == 'Cloudy':
    print('Rainy')
else:
    print('Sunny')
Buggy python submission (verdict: Runtime Error):
n, k =map(int, input().split()) 
s = input()
count = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        count += 1
print(str(count+k*2)

Fixed submission:
n, k =map(int, input().split()) 
s = input()
count = 0
even =0
for i in range(n-1):
    if s[i] == s[i+1]:
        count += 1
    else:
        even +=1

for i in range(k):
        if even >= 2:
            count +=2
            even -= 2
        elif even ==1:
            count+=1
            even -=2
print(str(count))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
a.reverse()
count = 0
for i in range(n-1):
    if a[i+1] > a[i]:
        a[i+1] -= 1
for i in range(n-1):
    if a[i+1]>a[i]:
        count +=1
if count == 0:
    print('Yes')
else:
    print('No')

Fixed submission:
a, b, c = map(int, input().split())
c2 = c-(a-b)
if c2<0: c2=0
print(c2)
Buggy python submission (verdict: Time Limit Exceeded):
import collections
N = int(input())

counter = collections.Counter([input() for _ in range(N)])

answers = [k for k, v in counter.items() if v == max(counter.values())]
answers.sort()
for x in answers:
  print(x)

Fixed submission:
import collections
N = int(input())
counter = collections.Counter([input() for _ in range(N)])
maximum = max(counter.values())
answers = [k for k, v in counter.items() if v == maximum]
answers.sort()
for x in answers:
  print(x)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
N = int(input())
S = [input() for _ in range(N)]

counter = collections.Counter(S)

answers = [k for k, v in counter.items() if v == max(counter.values())]
answers.sort()
for x in answers:
  print(x)

Fixed submission:
import collections
N = int(input())
counter = collections.Counter([input() for _ in range(N)])
maximum = max(counter.values())
answers = [k for k, v in counter.items() if v == maximum]
answers.sort()
for x in answers:
  print(x)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = [input() for _ in range(N)]

dic = {}

for s in S:
  if s not in dic.keys():
    dic.setdefault(s, 0)
  dic[s] += 1

answers = [k for k, v in dic.items() if v == max(dic.values())]
answers.sort()
for x in answers:
  print(x)

Fixed submission:
import collections
N = int(input())
counter = collections.Counter([input() for _ in range(N)])
maximum = max(counter.values())
answers = [k for k, v in counter.items() if v == maximum]
answers.sort()
for x in answers:
  print(x)
Buggy python submission (verdict: Runtime Error):
import sys
import itertools
read = sys.stdin.buffer.read #全てまとめて文字列として格納
readline = sys.stdin.buffer.readline # 一行読む
readlines = sys.stdin.buffer.readlines # 全行リストに格納

H,N = map(int, readline().split())
A = list(map(int, readline().split()))

if sum(A) >= H:
  print('Yes')
  exit()
print('No')
eixt()

Fixed submission:
import sys
import itertools
read = sys.stdin.buffer.read #全てまとめて文字列として格納
readline = sys.stdin.buffer.readline # 一行読む
readlines = sys.stdin.buffer.readlines # 全行リストに格納

H,N = map(int, readline().split())
A = list(map(int, readline().split()))

if sum(A) >= H:
  print('Yes')
  exit()
else:
  print('No')
  exit()
Buggy python submission (verdict: Runtime Error):
n=int(input())
p = list(map(int, input().split()))
count=1
newlist = []
for i in range(1, n):
  newlist.append(p[i-1])
  if p[i] <= min(newlist):
    count+=1
print(count)

Fixed submission:
n,m = map(int, input().split())
if(n==m):
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
y = [num for num in range(1, n+1)]
permutation_n = list(itertools.permutations(y, 3))

a=b=0
count=1
for permu in permutation_n:
  if permu == p:
    a = count
  if permu == q:
    b = count
  if a != 0 and b != 0:
    break
  count+=1
print(abs(a-b))

Fixed submission:
k,x = map(int, input().split())
if 500*k >= x:
  print('Yes')
  exit()
print('No')
Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int, input().split())
for i in range(k):
  if a>=1:
    a-=1
  elif b>=1:
    b-=1
  else:
    print(0,0)
    exit()
print(a,b)

Fixed submission:
a,b,k = map(int, input().split())
if a>=k:
  print(a-k,b)
elif k>=a+b:
  print(0,0)
else:
  print(0,str(b-k+a))

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int, input().split())
for i in range(k):
  if a == 0 and b == 0:
    print(0,0)
    exit()
  if a>=1:
    a-=1
    continue
  elif b>=1:
    b-=1
print(a,b)

Fixed submission:
a,b,k = map(int, input().split())
if a>=k:
  print(a-k,b)
elif k>=a+b:
  print(0,0)
else:
  print(0,str(b-k+a))

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int, input().split())
for i in range(k):
  if a == 0 and b == 0:
    print(0,0)
    exit()
  if a>=1:
    a-=1
    continue
  elif b>=1:
    b-=1
print(a,b)

Fixed submission:
a,b,k = map(int, input().split())
if a>=k:
  print(a-k,b)
elif k>=a+b:
  print(0,0)
else:
  print(0,str(b-k+a))

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int, input().split())
for i in range(k):
  if a>=1:
    a-=1
    continue
  elif b>=1:
    b-=1
    continue
  if a == 0 and b == 0:
    print(0,0)
    exit()
print(a,b)

Fixed submission:
a,b,k = map(int, input().split())
if a>=k:
  print(a-k,b)
elif k>=a+b:
  print(0,0)
else:
  print(0,str(b-k+a))

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int, input().split())
for i in range(k):
  if a>=1:
    a-=1
  elif b>=1:
    b-=1
  if a == 0 and b == 0:
    print(0,0)
    exit()
print(a,b)

Fixed submission:
a,b,k = map(int, input().split())
if a>=k:
  print(a-k,b)
elif k>=a+b:
  print(0,0)
else:
  print(0,str(b-k+a))

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int, input().split())
for i in range(k):
  if a>=1:
    a-=1
  elif b>=1:
    b-=1
  if a == 0 and b == 0:
    print(0,0)
    exit()
print(a,b)

Fixed submission:
a,b,k = map(int, input().split())
if a>=k:
  print(a-k,b)
elif k>=a+b:
  print(0,0)
else:
  print(0,str(b-k+a))

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int, input().split())
for i in range(k):
  if a>=1:
    a-=1
  elif b>=1:
    b-=1
  if a == b == 0:
    print(0,0)
    exit()
print(a,b)

Fixed submission:
a,b,k = map(int, input().split())
if a>=k:
  print(a-k,b)
elif k>=a+b:
  print(0,0)
else:
  print(0,str(b-k+a))
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=list(input())
result=[]
for i in range(n):
  total=0
  for j in range(n):
    if j == i: continue
    if i < j and s[j] == 'E': total+=1
    if i > j and s[j] == 'W': total+=1
  result.append(total)
print(min(result))

Fixed submission:
n = int(input())
s = list(input())

# W,E各々の累積和を求めておく
cumulative_e = [0] * (n+1)
cumulative_w = [0] * (n+1)
for i in range(1, n+1):
  if s[i-1] == 'W':
    cumulative_w[i] = cumulative_w[i-1] + 1
    cumulative_e[i] = cumulative_e[i-1]
  elif s[i-1] == 'E':
    cumulative_e[i] = cumulative_e[i-1] + 1
    cumulative_w[i] = cumulative_w[i-1]

# 誰がリーダーに適任か調べる
result = [0] * n
for j in range(n):
  result[j] = cumulative_w[j] + ( cumulative_e[n] - cumulative_e[min(n,j+1)])

print(min(result))
Buggy python submission (verdict: Runtime Error):
H,W = map(int, input().split())
S = [list(input()) for _ in range(H)] #データ形式はS[i][j]

eight = [[-1,-1],[0,-1],[1,-1], # 周囲8マスの定義
         [-1,0],[1,0],
         [-1,1],[0,1],[1,1]]

for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      for e in eight:
        target = S[i+e[0]][j+e[1]]
        if target and target != '#':
          if target == '.' :
            S[i+e[0]][j+e[1]] = 1
          else :
            S[i+e[0]][j+e[1]] += 1

for i in range(H):
  for j in range(W):
    print(S[i][j], end='')
  if i < H: print('')

Fixed submission:
H,W = map(int, input().split())
S = [list(input()) for _ in range(H)] #データ形式はS[i][j]

eight = [[-1,-1],[0,-1],[1,-1], # 周囲8マスの定義
         [-1,0],[1,0],
         [-1,1],[0,1],[1,1]]

for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      for e in eight:
        if (0 <= i+e[0] < H) and (0 <= j+e[1] < W) :
          target = S[i+e[0]][j+e[1]]
          if target != '#' :
            if target == '.' :
              S[i+e[0]][j+e[1]] = 1
            else :
              S[i+e[0]][j+e[1]] += 1

for i in range(H):
  for j in range(W):
    print('0' if S[i][j] == '.' else str(S[i][j]), end='')
  if i < H: print('')

Buggy python submission (verdict: Runtime Error):
H,W = map(int, input().split())
S = [list(input()) for _ in range(H)] #データ形式はS[i][j]

eight = [[-1,-1],[0,-1],[1,-1], # 周囲8マスの定義
         [-1,0],[1,0],
         [-1,1],[0,1],[1,1]]

for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      for e in eight:
        target = S[i+e[0]][j+e[1]]
        if target and target != '#':
          if target == '.' :
            S[i+e[0]][j+e[1]] = 1
          else :
            S[i+e[0]][j+e[1]] += 1

for i in range(H):
  for j in range(W):
    print(S[i][j], end='')
  print('')

Fixed submission:
H,W = map(int, input().split())
S = [list(input()) for _ in range(H)] #データ形式はS[i][j]

eight = [[-1,-1],[0,-1],[1,-1], # 周囲8マスの定義
         [-1,0],[1,0],
         [-1,1],[0,1],[1,1]]

for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      for e in eight:
        if (0 <= i+e[0] < H) and (0 <= j+e[1] < W) :
          target = S[i+e[0]][j+e[1]]
          if target != '#' :
            if target == '.' :
              S[i+e[0]][j+e[1]] = 1
            else :
              S[i+e[0]][j+e[1]] += 1

for i in range(H):
  for j in range(W):
    print('0' if S[i][j] == '.' else str(S[i][j]), end='')
  if i < H: print('')
Buggy python submission (verdict: Runtime Error):
n=int(input())
a = [int(input()) for _ in range(n)]
position = a.index(2)
if position == False:
  print(str(-1))
  exit()
position = position + 1
for i in range(n+1): # 最高でもn+!回だけ探してやれば十分
  position = a.index(position) + 1
  if position==1:
    print(str(i+2))
    exit()
print(str(-1))

Fixed submission:
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 python submission (verdict: Runtime Error):
n=int(input())
a = [int(input()) for _ in range(n)]
position = a.index(2)
if position == False:
  print(-1)
  exit()
position = position + 1
for i in range(n+1): # 最高でもn+!回だけ探してやれば十分
  position = a.index(position) + 1
  if position==1:
    print(i+2)
    exit()
print(-1)
exit()

Fixed submission:
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 python submission (verdict: Runtime Error):
n=int(input())
a = [int(input()) for _ in range(n)]
position = a.index(2) + 1
if position == False:
  print(-1)
  exit()
for i in range(n+1): # 最高でもn+!回だけ探してやれば十分
  position = a.index(position) + 1
  if position==1:
    print(i+2)
    exit()
print(-1)
exit()

Fixed submission:
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 python submission (verdict: Runtime Error):
k, s = map(int, input().split())
p = 0
for x in range(0,k+1):
  for y in range(0,k+1):
    for z in range(0,k+1):
      if (x+y+z) == s:
        p+=1
      if (x+y+z) >= s:
        break
    if (x+y+Z) >= s:
      break
print(p)

Fixed submission:
k, s = map(int, input().split())
p = 0
for x in range(0,k+1):
  for y in range(0,k+1):
    z = s - x - y
    if z >= 0 and z <= k:
      p += 1
print(p)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
p = 0
for x in range(0,k+1):
  for y in range(0,k+1):
    for z in range(0,k+1):
      if (x+y+z) == s:
        p+=1
      if (x+y+z) >= s:
        break
print(p)

Fixed submission:
k, s = map(int, input().split())
p = 0
for x in range(0,k+1):
  for y in range(0,k+1):
    z = s - x - y
    if z >= 0 and z <= k:
      p += 1
print(p)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
p = 0
for x in range(0,k+1):
  for y in range(0,k+1):
    for z in range(0,k+1):
      if (x+y+z) == s:
        p+=1
print(p)

Fixed submission:
k, s = map(int, input().split())
p = 0
for x in range(0,k+1):
  for y in range(0,k+1):
    z = s - x - y
    if z >= 0 and z <= k:
      p += 1
print(p)