Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
l, r = map(int,input().split())
min_val = float('inf')
for i in range(l,r):
  for j in range(l+1, r+1):
    tmp = i*j%2019
    if min_val>tmp:
      min_val = tmp
    if min_val == 0:
      break
print(min_val)

Fixed submission:
l, r = map(int,input().split())
min_val = float('inf')
for i in range(l,r):
  for j in range(l+1, r+1):
    tmp = i*j%2019
    if min_val>tmp:
      min_val = tmp
    if min_val == 0:
      break
  if min_val == 0:
    break
print(min_val)

Buggy python submission (verdict: Time Limit Exceeded):
l, r = map(int,input().split())
min_val = float('inf')
for i in range(l,r):
  for j in range(l+1, r+1):
    min_val = min(min_val, (i*j)%2019)
    if r-l>=2019 and min_val==0:
      break
print(min_val)

Fixed submission:
l, r = map(int,input().split())
min_val = float('inf')
for i in range(l,r):
  for j in range(l+1, r+1):
    tmp = i*j%2019
    if min_val>tmp:
      min_val = tmp
    if min_val == 0:
      break
  if min_val == 0:
    break
print(min_val)
Buggy python submission (verdict: Runtime Error):
n, x = map(int,input().split())
L = list(map(int,input().split()))
d, cnt = 0, 1
for i in range(1,n+2):
  d = d + L[i-1]
  if d <= x:
    cnt+=1
  else:
    break
print(cnt)

Fixed submission:
n, x = map(int,input().split())
L = list(map(int,input().split()))
D = [0]*(n+1)
cnt = 1

for i in range(n):
  D[i+1] = D[i] + L[i]
  if D[i+1]<=x:
    cnt+=1

print(cnt)
Buggy python submission (verdict: Runtime Error):
n, m = map(int,input().split())
a = [int(input()) for _ in range(m)]
dp = [0]*(n+2)
mod = int(1e9+7)
for i in range(n, -1, -1):
  if i==n and a[-1]!=i:
    dp[i] = 1
  elif i!=n:
    if i in a:
      dp[i] = 0
    else:
      dp[i] = dp[i+1] + dp[i+2]
  else:
    break

print(dp[0]%mod)


Fixed submission:
n, m = map(int,input().split())
broken = [0]*(n+1)
for _ in range(m):
  a = int(input())
  broken[a]=1

dp = [0]*(n+2)
mod = int(1e9+7)
dp[n] = 1
for i in range(n-1, -1 , -1):
  if broken[i]:
    dp[i] = 0
  else:
    dp[i] = (dp[i+1] + dp[i+2]) % mod

print(dp[0])
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int,input().split())
ans = [0]*n

for _ in range(m):
  l, r = map(int,input().split())
  for i in range(l-1, r):
    ans[i] += 1
print(ans.count(m))

Fixed submission:
n, m = map(int,input().split())
maxL, minR = 1, n
L = []
R = []
for i in range(m):
  l, r = map(int,input().split())
  maxL = max(maxL, l)
  minR = min(minR, r)

ans = minR - maxL + 1
print(max(ans, 0))
Buggy python submission (verdict: Time Limit Exceeded):
r, g, b, n = map(int, input().split())
cnt = 0
for i in range(n+1):
    for j in range(n+1):
        v = r*i + g*j
        if n>=v and (n-v)%b==0: cnt+=1
print(cnt)


Fixed submission:
r, g, b, n = map(int, input().split())
cnt = 0
for i in range(n+1):
    for j in range(n+1):
        v = r*i + g*j
        if n>=v and (n-v)%b==0: cnt+=1
print(cnt)
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = list(input())
cnt = 0
if S.count('#')>S.count('.'):
  word = '#'
else:
  word = '.'
ans=""
for i in range(N):
  if i==0:
    ans+=S[i]
    pass
  if ans[-1]=='#' and S[i]=='.':
    cnt+=1
    ans+=word
  else:
    ans+=S[i]
  print(ans)
print(cnt)

Fixed submission:
N = int(input())
S = list(input())
b = S.count("#")
w = S.count(".")
ans = min(b,w)
if b>=1 and w>=1:
  cost = N-w
  for i in range(N-1, -1, -1):
    if S[i]=='.':
      cost+=1
    else: 
      cost-=1
      ans = min(ans,cost)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, Q = map(int,input().split())
S = input()
for _ in range(Q):
  l, r = map(int,input().split())
  print(S[l-1:r].count('AC'))

Fixed submission:
import sys
input = sys.stdin.readline
 
N, Q = map(int,input().split())
S = input()
nums = [0] * (N + 1)
for i in range(N):
  nums[i+1] = nums[i] + (1 if S[i:i+2]=='AC' else 0)
    
for j in range(Q):
  l, r = map(int,input().split())
  print(nums[r-1] - nums[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, Q = map(int,input().split())
S = input()
for _ in range(Q):
  l, r = map(int,input().split())
  a = S[l-1:r]
  print(a.count('AC'))

Fixed submission:
import sys
input = sys.stdin.readline
 
N, Q = map(int,input().split())
S = input()
nums = [0] * (N + 1)
for i in range(N):
  nums[i+1] = nums[i] + (1 if S[i:i+2]=='AC' else 0)
    
for j in range(Q):
  l, r = map(int,input().split())
  print(nums[r-1] - nums[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int,input().split())
S = input()
for i in range(Q):
  l, r = map(int,input().split())
  a = S[l-1:r]
  print(a.count('AC'))

Fixed submission:
import sys
input = sys.stdin.readline
 
N, Q = map(int,input().split())
S = input()
nums = [0] * (N + 1)
for i in range(N):
  nums[i+1] = nums[i] + (1 if S[i:i+2]=='AC' else 0)
    
for j in range(Q):
  l, r = map(int,input().split())
  print(nums[r-1] - nums[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int,input().split())
S = input()
for i in range(Q):
  a = ''
  ans = 0
  l, r = map(int,input().split())
  b = S[l-1:r]
  for j in b:
    if j == 'A' and len(a)==0:
      a += j
    if j == 'C' and len(a)!=0:
      ans += 1
      a = ''
  print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
 
N, Q = map(int,input().split())
S = input()
nums = [0] * (N + 1)
for i in range(N):
  nums[i+1] = nums[i] + (1 if S[i:i+2]=='AC' else 0)
    
for j in range(Q):
  l, r = map(int,input().split())
  print(nums[r-1] - nums[l-1])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
monster = set(map(int,input().split()))
monster = sorted(list(monster))
if len(monster)==1:
  print(monster[0])
  exit()

while True:
  tmp = int(monster[-1]/monster[-2])
  monster[-1]-=monster[-2]*tmp
  monster = sorted(monster)
  if monster[-2]==0:
    print(monster[-1])
    exit()

Fixed submission:
def gcd(a, b):
  while b:
    a, b = b, a % b
 
  return a

n = int(input())
monster = sorted(list(map(int,input().split())))
ans = []
for i in range(n-1):
  g = gcd(monster[i], monster[i+1])
  ans.append(g)
print(min(ans))

Buggy python submission (verdict: Runtime Error):
n = int(input())
monster = sorted(list(map(int,input().split())))
min_HP = min(monster)
cnt=0
while True:
  tmp = int(monster[-1]/monster[-2])
  monster[-1]-=monster[-2]*tmp
  if monster[-1]<=0:
    monster.pop(-1)
  monster = sorted(monster)
  if min_HP==monster[0]:
    cnt+=1
    if cnt>=2:
      print(min_HP)
      exit()
  else:
    min_HP = monster[0]
    cnt=0

Fixed submission:
def gcd(a, b):
  while b:
    a, b = b, a % b
 
  return a

n = int(input())
monster = sorted(list(map(int,input().split())))
ans = []
for i in range(n-1):
  g = gcd(monster[i], monster[i+1])
  ans.append(g)
print(min(ans))

Buggy python submission (verdict: Runtime Error):
n = int(input())
monster = sorted(list(map(int,input().split())))
min_HP = min(monster)
cnt=0
while True:
  tmp = int(monster[-1]/monster[-2])
  monster[-1]-=monster[-2]*tmp
  if monster[-1]<=0:
    monster.pop(-1)
  monster = sorted(monster)
  if min_HP==monster[0]:
    cnt+=1
    if cnt>=2:
      print(min_HP)
      exit()
  else:
    min_HP = monster[0]
    cnt=0
  print(monster)

Fixed submission:
def gcd(a, b):
  while b:
    a, b = b, a % b
 
  return a

n = int(input())
monster = sorted(list(map(int,input().split())))
ans = []
for i in range(n-1):
  g = gcd(monster[i], monster[i+1])
  ans.append(g)
print(min(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
monster = sorted(list(map(int,input().split())))
while True:
  tmp = int(monster[-1]/monster[-2])
  monster[-1]-=monster[-2]*tmp
  if monster[-1]<=0:
    monster.pop(-1)
  monster = sorted(monster)
  if len(monster)==1:
    print(monster[0])
    exit()


Fixed submission:
def gcd(a, b):
  while b:
    a, b = b, a % b
 
  return a

n = int(input())
monster = sorted(list(map(int,input().split())))
ans = []
for i in range(n-1):
  g = gcd(monster[i], monster[i+1])
  ans.append(g)
print(min(ans))
Buggy python submission (verdict: Runtime Error):
d = int(input())
c = 'Christmas'
e = 'Eve'
a = 25-d
if a>=1:
  b=' '
print(c+(b+e)*a)

Fixed submission:
d = int(input())
c = 'Christmas'
e = 'Eve'
if d==25:
  print(c)
elif d==24:
  print(c, e)
elif d==23:
  print(c, e, e)
else:
  print(c, e, e, e)
Buggy python submission (verdict: Runtime Error):
n,k =map(int,input().split())
trees = sorted([int(input()) for _ in range(n)])
ans = float('inf')
for i in range(n):
  if i+2==n:
    break
  else:
    temp = abs(trees[i]-trees[i+k-1])
    if temp<ans:
      ans = temp
      if ans==0:
        break

print(ans)

Fixed submission:
n,k =map(int,input().split())
trees = sorted([int(input()) for _ in range(n)])
ans = float('inf')
for i in range(n):
  if i+k>n:
    print(ans)
    exit()
  else:
    ans = min(trees[i+k-1]-trees[i], ans)
    if ans==0:
      print(ans)
      exit()
Buggy python submission (verdict: Runtime Error):
N, T = map(int,input().split())
cost=float('inf')
for i in range(N):
  c,t=map(int,input().split())
  if T>=t:
    cost = min(cost,c)
  if cost==float('inf'):
    cost='TLE'
print(cost)

Fixed submission:
N, T = map(int,input().split())
cost=float('inf')
for i in range(N):
  c,t=map(int,input().split())
  if T>=t:
    cost = min(cost,c)
  if i==N-1 and cost==float('inf'):
    cost='TLE'
print(cost)
Buggy python submission (verdict: Runtime Error):
s=list(input())
k=int(input())
if s[0]=='1':
  print(s[1])
  
else:
  print(s[0])

Fixed submission:
s = input()
k = int(input())
for i in range(k):
    if s[i]!='1':
        break
print(s[i])
Buggy python submission (verdict: Runtime Error):
s=list(input())
t=list(input())
flag=False
cnt=0
if sorted(s)==sorted(t):
  a=s.index(t[0])
  for i in range(len(s)):
    b=i+a
    print(b)
    if b>=len(s):
      b-=len(s)
    if s[b]==t[i]:
      cnt+=1

if cnt==len(S):
  flag=True
print('Yes' if flag else 'No')

Fixed submission:
s=list(input())
t=list(input())
flag=False
cnt=0
if sorted(s)==sorted(t):
  a=s.index(t[0])
  for i in range(len(s)):
    b=i+a
    if b>=len(s):
      b-=len(s)
    if s[b]==t[i]:
      cnt+=1

if cnt==len(s):
  flag=True
print('Yes' if flag else 'No')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
S = list(input())
ans = float('inf')
for i in range(n):
  tmp = S[:i].count('W') + S[i+1::].count('E') 
  ans = min(tmp, ans)
print(ans)

Fixed submission:
n = int(input())
S = input()
E = [0]*n
W = [0]*n
ans = n
for i in range(n):
  if S[i]=='E':
    E[i] = 1
  else:
    W[i] = 1

for i in range(1, n):
  W[i] += W[i-1]
  E[i] += E[i-1]

for i in range(n):
  tmp = 0
  if i:
    tmp += W[i-1]
  tmp += E[n-1] - E[i]
  ans = min(ans, tmp)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
X = int(input())
num = [1]
for i in range(1,X):
  temp = i
  while temp*i<=X:
    temp=temp*i
    num.append(temp)
    
print(num[-1])

Fixed submission:
X = int(input())
num = [1]
for i in range(2,X):
  temp = i
  while temp*i<=X:
    temp=temp*i
    num.append(temp)
    
print(sorted(num)[-1])

Buggy python submission (verdict: Runtime Error):
X = int(input())
num = []
for i in range(1,X):
  temp = i**2
  if temp<=X:
    num.append(temp)
    
print(num[-1])

Fixed submission:
X = int(input())
num = [1]
for i in range(2,X):
  temp = i
  while temp*i<=X:
    temp=temp*i
    num.append(temp)
    
print(sorted(num)[-1])

Buggy python submission (verdict: Runtime Error):
X = int(input())
num = []
for i in range(1,X+1):
  temp = i**2
  if temp<X:
    num.append(temp)
    
print(num[-1])

Fixed submission:
X = int(input())
num = [1]
for i in range(2,X):
  temp = i
  while temp*i<=X:
    temp=temp*i
    num.append(temp)
    
print(sorted(num)[-1])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
b = set(a)
ans = 0
for i in b:
  tmp = a.count(i)
  if tmp!=i:
    if tmp<i:
      ans+=tmp
    else:
      ans+=tmp-i
print(ans)

Fixed submission:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
counter = Counter(a)
ckey = list(counter.keys())
cval = list(counter.values())
ans = 0

for i in range(len(ckey)):
  if ckey[i]>cval[i]:
    ans+=cval[i]
  elif ckey[i]<cval[i]:
    ans+=cval[i]-ckey[i]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
b = set([i for i in a])
ans = 0
for i in b:
  tmp = a.count(i)
  if tmp!=i:
    if tmp<i:
      ans+=tmp
    else:
      ans+=tmp-i
print(ans)

Fixed submission:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
counter = Counter(a)
ckey = list(counter.keys())
cval = list(counter.values())
ans = 0

for i in range(len(ckey)):
  if ckey[i]>cval[i]:
    ans+=cval[i]
  elif ckey[i]<cval[i]:
    ans+=cval[i]-ckey[i]
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int,input().split())
ball = input().split()
ball_list = sorted(list(set(ball)))
nums = len(ball_list)

if K >= nums:
  print("0")
  exit()

balls = []
for i in range(nums):
  balls.append(ball.count(ball_list[i]))

balls = sorted(balls, reverse=True)
print(N - sum(balls[:K]))

Fixed submission:
N, K = map(int,input().split())
ball = input().split()
d = {}

if K >= len(set(ball)):
  print("0")
  exit()

for i in ball:
  if not i in d:
    d[i] = 1
  else:
    d[i] += 1

d_val = sorted(d.values(),reverse=True)
print(N - sum(d_val[:K]))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = sorted(list(map(int,input().split())))
B = sorted(list(map(int,input().split())))
C = sorted(list(map(int,input().split())))
cnt = 0

for i in range(n):
  for j in range(n):
    if C[i]>B[j]:
      for k in range(n):
        if B[j]>A[k]:
          cnt+=1

print(cnt)

Fixed submission:
import bisect
n = int(input())
A = sorted(list(map(int, input().split())))
B = list(map(int, input().split()))
C = sorted(list(map(int, input().split())))
cnt = 0
for i in range(n):
    a = bisect.bisect_left(A, B[i])
    c = bisect.bisect_right(C, B[i])
    cnt += a * (n-c)
print(cnt)
Buggy python submission (verdict: Runtime Error):
s=input().replace('\n', '')
t=input().replace('\n', '')
begin, end = len(s) - len(t), len(s)
ans=''
while begin>=0:
    origin = s[begin:end]
    flag=1
    for i in range(len(t)):
        if origin[i]!=t[i] and origin[i]!='?':
            flag=0
            break
    if flag:
        ans = s[:begin]+t+s[end:]
    begin-=1
    end-=1
print("UNRESTORABLE" if flag else ans.replace('?', 'a'))


Fixed submission:
s=input().replace('\n', '')
t=input().replace('\n', '')
begin, end = len(s) - len(t), len(s)
ans=''
while begin>=0:
    origin = s[begin:end]
    flag=1
    
    for i in range(len(t)):
        if origin[i]!=t[i] and origin[i]!='?':
            flag=0
            break
    if flag:
        ans = s[:begin]+t+s[end:]
        break
    begin-=1
    end-=1
print("UNRESTORABLE" if not len(ans) else ans.replace('?', 'a'))

Buggy python submission (verdict: Runtime Error):
s=input().replace('\n', '')
t=input().replace('\n', '')
begin, end = len(s) - len(t), len(s)
ans=''
while begin>=0:
    origin = s[begin:end]
    print(origin)
    flag=1
    for i in range(len(t)):
        if origin[i]!=t[i] and origin[i]!='?':
            flag=0
            break
    if flag:
        ans = s[:begin]+t+s[end:]
    begin-=1
    end-=1
print("UNRESTORABLE" if flag else ans.replace('?', 'a'))


Fixed submission:
s=input().replace('\n', '')
t=input().replace('\n', '')
begin, end = len(s) - len(t), len(s)
ans=''
while begin>=0:
    origin = s[begin:end]
    flag=1
    
    for i in range(len(t)):
        if origin[i]!=t[i] and origin[i]!='?':
            flag=0
            break
    if flag:
        ans = s[:begin]+t+s[end:]
        break
    begin-=1
    end-=1
print("UNRESTORABLE" if not len(ans) else ans.replace('?', 'a'))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = [int(input()) for _ in range(N)]
b = set(a)
cnt = 0
for i in b:
    if a.count(i)%2==1:
        cnt+=1
print(cnt)


Fixed submission:
from collections import Counter
N = int(input())
a = [int(input()) for _ in range(N)]
ans = [i for i in Counter(a).values() if i%2==1]
print(len(ans))
Buggy python submission (verdict: Runtime Error):
n = int(input())
p = list(map(int, input().split()))
cnt = 0
pp = [1 if p[i-1] == i else 0 for i in range(1, n+1)]
for i in range(n):
    if pp[i]==1:
        p[i], p[i+1] = p[i+1], p[i]
        cnt+=1
print(cnt)


Fixed submission:
n = int(input())
p = list(map(int, input().split()))
flag = 0
cnt = 0
for i in range(n):
    if flag: flag=0
    elif p[i]==i+1:
        flag=1
        cnt+=1
print(cnt)
Buggy python submission (verdict: Runtime Error):
s=sorted(list(set(input())))
flag=True
for i in range(26):
  a=i+97
  if int(ord(s[i]))-a!=0:
    flag=False
    break
print('None' if flag else chr(a))

Fixed submission:
s=sorted(list(set(input())))
flag=True
for i in range(26):
  a=i+97
  if s.count(chr(a))==0:
    flag=False
    break
print('None' if flag else chr(a))
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
nums = []
dup = []
for i in range(N):
  nums.append(int(input()))
  
temp = 0
for j in range(len(nums)):
  if nums[temp]==2:
    print(j+1)
    exit()
  elif nums[temp]!=2 and nums[temp] in dup:
    print(-1)
    exit()
  else:  
    dup.append(nums[temp])
    temp=nums[temp]-1

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

temp = 0
for j in range(N):
  if nums[temp]==2:
    print(j+1)
    exit()
  else:
    temp = nums[temp]-1

print(-1)
Buggy python submission (verdict: Runtime Error):
N = int(input())
h = [int(input()) for _ in range(N)]
ans = sum(h)
if ans%10!=0:
    print(ans)
else:
    s = [i for i in s if s%10]
    print(ans - min(s))


Fixed submission:
N = int(input())
h = [int(input()) for _ in range(N)]
ans = sum(h)
if ans%10!=0:
    print(ans)
else:
    s = [i for i in h if i%10]
    print(ans - min(s) if len(s) else 0)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
cnt = 0
for i in range(n):
    if m<=2:
        break
    cnt+=1
    m-=2
print(int(m//4)+cnt)


Fixed submission:
n, m = map(int, input().split())
mm = m//2
print(mm if n>mm else (mm-n)//2+n)
Buggy python submission (verdict: Time Limit Exceeded):
K,S=map(int,input().split())
cnt=0
for x in range(K+1):
  for y in range(K+1):
    z=S-x-y
    if S==x+y+z and 0<=z and z<=K:
      cnt+=1
print(cnt)

Fixed submission:
K,S=map(int,input().split())
cnt=0
for x in range(K+1):
  for y in range(K+1):
    z=S-x-y
    if 0<=z and z<=K:
      cnt+=1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
K,S=map(int,input().split())
x=y=z=0
cnt=0
for l in range(K+1):
  for m in range(K+1):
    for n in range(K+1):
      if l+m+n==S:
        cnt+=1
print(cnt)

Fixed submission:
K,S=map(int,input().split())
cnt=0
for x in range(K+1):
  for y in range(K+1):
    z=S-x-y
    if 0<=z and z<=K:
      cnt+=1
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
N, x = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
    while a[i] + a[i+1] > x:
        a[i+1]-=1
        cnt+=1
print(cnt)


Fixed submission:
N, x = map(int, input().split())
a = [0] + list(map(int, input().split()))
cnt = 0
for i in range(N):
    if a[i]+a[i+1]>x:
        tmp = a[i]+a[i+1]-x
        a[i+1] -= tmp
        cnt+=tmp
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N, x = map(int, input().split())
a = list(map(int, input().split()))
b = []
cnt = 0
for i in a:
    if i>x:
        tmp = abs(x-i)
        i-=tmp
        cnt+=tmp
    b.append(i)
for j in range(N-1):
    while b[j] + b[j+1] > x:
        b[j+1]-=1
        cnt+=1
print(cnt)

Fixed submission:
N, x = map(int, input().split())
a = [0] + list(map(int, input().split()))
cnt = 0
for i in range(N):
    if a[i]+a[i+1]>x:
        tmp = a[i]+a[i+1]-x
        a[i+1] -= tmp
        cnt+=tmp
print(cnt)
Buggy python submission (verdict: Runtime Error):
N,K = map(str,input().split())
D = list(map(int,input().split()))
nums = [x for x in range(10)]
nots = [i for i in nums if i not in D]
ans = ""
for j in range(len(N)):
  for k in nots:
    if int(N[j])<=k:
      ans+=str(k)
      break
print(int(ans))

Fixed submission:
N,K = map(int,input().split())
D = input().split()
for i in range(N, 10*N+1):
  for j in str(i):
    if j in D:
      break
  else:
    print(i)
    exit()
Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())

a=list(map(int,input().split()))

visited=[0]*n
if k<=n:
    now=0
    nex=0
    loop_start=0
    target=0

    for i in range(n):
        now=nex
        visited[now]+=1
        nex=a[now]-1
        if i+1==k:
            print(nex+1)
            exit()
now=0
nex=0
loop_start=0
target=0
lis=[]
flag=False

for i in range(2*n):
    now=nex
    visited[now]+=1
    nex=a[now]-1
    if flag:
        if now+1 not in lis:
           lis.append(now+1)
           continue
        else:
            break
    if visited[now]==2:
        loop_start=i
        target=now+1
        lis.append(now+1)
        flag=True


dif=len(lis)
first=loop_start-dif
k-=first
print(lis[k%dif])






Fixed submission:
n,k=map(int,input().split())

a=list(map(int,input().split()))

visited=[0]*n
if k<=n:
    now=0
    nex=0
    loop_start=0
    target=0

    for i in range(n):
        now=nex
        visited[now]+=1
        nex=a[now]-1
        if i+1==k:
            print(nex+1)
            exit()
now=0
nex=0
loop_start=0
target=0
lis=[]
s=set()
flag=False

for i in range(2*n):
    now=nex
    visited[now]+=1
    nex=a[now]-1
    if flag:
        if now+1 not in s:
           lis.append(now+1)
           s.add(now+1)
           continue
        else:
            break
    if visited[now]==2:
        loop_start=i
        target=now+1
        lis.append(now+1)
        s.add(now+1)
        flag=True


dif=len(lis)
first=loop_start-dif
k-=first
print(lis[k%dif])
Buggy python submission (verdict: Runtime Error):
vn,m,q=map(int,input().split())
lis=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    lis.append((a,b,c,d))
    
from itertools import combinations_with_replacement

co=list(combinations_with_replacement(range(1,m+1),n))

ans=0
for status in co:
    tmp_score=0
    for a,b,c,d in lis:
        if status[b-1]-status[a-1]==c:
            tmp_score+=d
    ans=max(ans,tmp_score)
print(ans)


Fixed submission:
n,m,q=map(int,input().split())
lis=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    lis.append((a,b,c,d))
    
from itertools import combinations_with_replacement

co=list(combinations_with_replacement(range(1,m+1),n))

ans=0
for status in co:
    tmp_score=0
    for a,b,c,d in lis:
        if status[b-1]-status[a-1]==c:
            tmp_score+=d
    ans=max(ans,tmp_score)
print(ans)