Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())

i=1
while 1:
    if (P/i)%int(P/i)==0:
        tmp=pow(P/i,1/N)
        if tmp%int(tmp)==0:
            break
    i+=1
print(int(tmp))

Fixed submission:
import copy
N, P = map(int, input().split())

x=[i+1 for i in range(int(P**(1/2))) if P%(i+1)==0]
for i in range(len(x)):
    if P%x[i]==0:
        x.append(int(P/x[i]))
x.sort(reverse=True)

for i in range(len(x)):
    tmp=round(x[i]**(1/N),10)
    if tmp%int(tmp)==0:
            break

print(int(tmp))

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())

i=1
while 1:
    tmp=pow(P/i,1/N)
    if tmp%int(tmp)==0:
        break
    i+=1
print(int(tmp))

Fixed submission:
import copy
N, P = map(int, input().split())

x=[i+1 for i in range(int(P**(1/2))) if P%(i+1)==0]
for i in range(len(x)):
    if P%x[i]==0:
        x.append(int(P/x[i]))
x.sort(reverse=True)

for i in range(len(x)):
    tmp=round(x[i]**(1/N),10)
    if tmp%int(tmp)==0:
            break

print(int(tmp))
Buggy python submission (verdict: Time Limit Exceeded):
S=input()
count=0
while 1:
    tmp=S.count('BW')
    if tmp==0:
        break

    count+=tmp
    S=S.replace('BW','WB')[S.find('B'):S.rfind('W')+1]
print(count)

Fixed submission:
S=input()

tmp=[i for i, x in enumerate(S) if x == 'W']
print(sum(tmp)-sum(range(len(tmp))))

Buggy python submission (verdict: Time Limit Exceeded):
S=raw_input()
count=0

while 1:
    tmp=S.count('BW')
    if tmp==0:
        break

    count=count+tmp
    S=S.replace('BW','WB')

print(count)

Fixed submission:
S=input()

tmp=[i for i, x in enumerate(S) if x == 'W']
print(sum(tmp)-sum(range(len(tmp))))

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
count=0

while 1:
    tmp=S.count('BW')
    if tmp==0:
        break

    count=count+tmp
    S=S.replace('BW','WB')

print(count)

Fixed submission:
S=input()

tmp=[i for i, x in enumerate(S) if x == 'W']
print(sum(tmp)-sum(range(len(tmp))))

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
count=0

while 'BW' in S:
    ind=S.index('BW')
    S=S[:ind]+'WB'+S[ind+2:]
    count=count+1

print(count)

Fixed submission:
S=input()

tmp=[i for i, x in enumerate(S) if x == 'W']
print(sum(tmp)-sum(range(len(tmp))))

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
count=0

def search_index(l, x):
    if x in l:
        return l.index(x)
    else:
        return False

while 'BW' in S:
    ind=search_index(S,'BW')
    S=S[:ind]+'WB'+S[ind+2:]
    count=count+1

print(count)

Fixed submission:
S=input()

tmp=[i for i, x in enumerate(S) if x == 'W']
print(sum(tmp)-sum(range(len(tmp))))
Buggy python submission (verdict: Runtime Error):
import collections

def conv(cc, ks, K):
    tm=max([sum(cc[j:j+(ks+1)]) for j in range(len(cc)-(ks-1))])
    if tm>=K:
        return ks
    else:
        return conv(cc, ks+1, K)

N, K=map(int, input().split())
h=[int(input()) for i in range(N)]

cc=list(collections.Counter(h).values())

print(conv(cc,0,K))

Fixed submission:
D=int(input())

if D==25:
  print('Christmas')
elif D==24:
  print('Christmas Eve')
elif D==23:
  print('Christmas Eve Eve')
else:
  print('Christmas Eve Eve Eve')
Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

b=[]
for i in range(N):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))

b.sort()
b=b[::-1]
for sp in range(0,N+1-K):
    if [len(bin(b[i])[2:]) for i in range(sp,sp+K)] == [len(bin(b[sp])[2:])for _ in range(sp,sp+K)]:
        break
s=b[sp:sp+K]
if K==1:
    tmp=s[0]
else:
    tmp=s[0]&s[1]
    for i in range(2,K):
        tmp=tmp&s[i]

print(tmp)

Fixed submission:
N = int(input())
a = list(map(int, input().split()))

mean = sum(a)/N
ind = None
min = None
for i in range(N):
    tmp=abs(mean-a[i])
    if min is None:
        min=tmp
        ind=i
    else:
        if tmp<min:
            min=tmp
            ind=i

print(ind)

Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

b=[]
for i in range(N):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))

b.sort()
b=b[::-1]
for sp in range(0,N+1-K):
    if [len(bin(b[i])[2:]) for i in range(sp,sp+K)] == [len(bin(b[sp])[2:])for i in range(sp,sp+K)]:
        break
s=b[sp:sp+K]
if K==1:
    tmp=s[0]
else:
    tmp=s[0]&s[1]
    for i in range(2,K):
        tmp=tmp&s[i]

print(tmp)

Fixed submission:
N = int(input())
a = list(map(int, input().split()))

mean = sum(a)/N
ind = None
min = None
for i in range(N):
    tmp=abs(mean-a[i])
    if min is None:
        min=tmp
        ind=i
    else:
        if tmp<min:
            min=tmp
            ind=i

print(ind)

Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

b=[]
for i in range(N):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))

b.sort(reverse=True)
s=b[:K]

Fixed submission:
N = int(input())
a = list(map(int, input().split()))

mean = sum(a)/N
ind = None
min = None
for i in range(N):
    tmp=abs(mean-a[i])
    if min is None:
        min=tmp
        ind=i
    else:
        if tmp<min:
            min=tmp
            ind=i

print(ind)

Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

b=[]
for i in range(N):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))

b.sort(reverse=True)
s=b[:K]
if K==1:
    tmp=s[0]
else:
    tmp=s[0]&s[1]
    for i in range(2,K):
        tmp=tmp&s[i]

print(tmp)

Fixed submission:
N = int(input())
a = list(map(int, input().split()))

mean = sum(a)/N
ind = None
min = None
for i in range(N):
    tmp=abs(mean-a[i])
    if min is None:
        min=tmp
        ind=i
    else:
        if tmp<min:
            min=tmp
            ind=i

print(ind)
Buggy python submission (verdict: Runtime Error):
X, Y = map(int, input())
print(X+Y//2)

Fixed submission:
X, Y = map(int, input().split())
print(X+Y//2)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[int(input()) for _ in range(N)]

A.sort()

B=[]
while A!=[]:
  B.append(A.pop())
  A=A[::-1]
  
if B[0]>B[-1]:
  B.insert(0,B[-1])
  B=B[:-1]
  
ans=0
for i in range(N-1):
  ans+=abs(B[i]-B[i+1])
  
print(ans)

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

A.sort(reverse=True)
if len(A)%2==0:
  print(A[len(A)//2-1]-A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:]))
else:
  tmp1=-A[len(A)//2]-A[len(A)//2+1]+2*sum(A[:len(A)//2])-2*sum(A[len(A)//2+2:])
  tmp2=A[len(A)//2-1]+A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:])
  print(tmp1 if tmp1>tmp2 else tmp2)

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=[int(input()) for _ in range(N)]

A.sort(reverse=True)

ans=0
while A!=[]:
  ans+=abs(A.pop(0)-A[-1])
  A[::-1]
  
print(ans)

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

A.sort(reverse=True)
if len(A)%2==0:
  print(A[len(A)//2-1]-A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:]))
else:
  tmp1=-A[len(A)//2]-A[len(A)//2+1]+2*sum(A[:len(A)//2])-2*sum(A[len(A)//2+2:])
  tmp2=A[len(A)//2-1]+A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:])
  print(tmp1 if tmp1>tmp2 else tmp2)
Buggy python submission (verdict: Runtime Error):
N, M, X, Y = map(int, input().split())
x=list(map(int, input().split()))
y=list(map(int, input().split()))

if max(x)<min(y):
  for i in range(max(x)+1,min(y)+1):
    if i>X and i<=Y:
      print('No War')
      break
  else
    print('War')
else:
  print('War')

Fixed submission:
N, M, X, Y = map(int, input().split())
x=list(map(int, input().split()))
y=list(map(int, input().split()))

if max(x)<min(y):
  for i in range(max(x)+1,min(y)+1):
    if i>X and i<=Y:
      print('No War')
      break
  else:
    print('War')
else:
  print('War')

Buggy python submission (verdict: Runtime Error):
N, M, X, Y = map(int, input().split())
x=list(map(int, input().split()))
y=list(map(int, input().split()))

if max(x)<min(y):
  for i in range(max(x)+1,min(y)+1)
    if i>X and i<=Y:
      print('No War')
      break
  else
    print('War')
else:
  print('War')

Fixed submission:
N, M, X, Y = map(int, input().split())
x=list(map(int, input().split()))
y=list(map(int, input().split()))

if max(x)<min(y):
  for i in range(max(x)+1,min(y)+1):
    if i>X and i<=Y:
      print('No War')
      break
  else:
    print('War')
else:
  print('War')
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
T = input()

U = [[S[i],T[i]] for i in range(len(S))]
U.sort()

tmp=''
g_tmp=[]
for a in U:
  if tmp!=a[0]:
    tmp=a[0]
    tmp_s=[]
    if a[1] in g_tmp:
      print('No')
      break
    g_tmp.append(a[1])
    
  tmp_s.append(a[1])
  if len(set(tmp_s))!=1:
    print('No')
    break
else:
  print('Yes')

Fixed submission:
S = input()
T = input()

R={}
R_={}

for a in zip(S,T):
  if a[0] not in R:
    R[a[0]]=a[1]
  else:
    if R[a[0]]!=a[1]:
      print('No')
      break
      
  if a[1] not in R_:
    R_[a[1]]=a[0]
  else:
    if R_[a[1]]!=a[0]:
      print('No')
      break
      
else:
  print('Yes')
Buggy python submission (verdict: Runtime Error):
N=int(input())

for i in range(1,N+1):
  tmp=0
  t=i**(1/2)
  for j in range(1,int(t)+1):
    if i%j==0:
      tmp+=1
      
  if t-int(t)<=1e-3:
    if tmp*2-1==8:
      ans+=1
      
  else:
    if tmp*2==8:
      ans+=1
      
print(ans)    

Fixed submission:
N=int(input())

ans=0
for i in range(1,N+1,2):
  tmp=0
  t=i**(1/2)
  for j in range(1,int(t)+1):
    if i%j==0:
      tmp+=1
      
  tmp=tmp*2
  if t-int(t)<=1e-3:
    tmp-=1

  if tmp==8:
    ans+=1
      
print(ans)

Buggy python submission (verdict: Runtime Error):
N=int(input())

for i in range(1,N+1):
  tmp=0
  for j in range(1,int(sqrt(i))+1):
    if i%j==0:
      tmp+=1
      
  if sqrt(i)-int(sqrt(i))<=1e-3:
    if tmp*2-1==8:
      ans+=1
      
  else:
    if tmp*2==8:
      ans+=1
      
print(ans)    

Fixed submission:
N=int(input())

ans=0
for i in range(1,N+1,2):
  tmp=0
  t=i**(1/2)
  for j in range(1,int(t)+1):
    if i%j==0:
      tmp+=1
      
  tmp=tmp*2
  if t-int(t)<=1e-3:
    tmp-=1

  if tmp==8:
    ans+=1
      
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
K = int(input())

D=5000000000000000

tmp=0
for s in S:
  t=1
  for _ in range(K):
    t=t*int(s)
    if tmp+t>=K:
      print(s)
      break
  else:
    tmp+=t
    continue
  break

Fixed submission:
S = input()
K = int(input())

D=5000000000000000

tmp=0
for s in S:
  if s=='1':
    tmp+=1
    if tmp>=K:
      print(s)
      break
    continue
    
  t=1
  for _ in range(D):
    t=t*int(s)
    if tmp+t>=K:
      print(s)
      break
  else:
    tmp+=t
    continue
  break

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
K = int(input())

D=5000000000000000

tmp=0
for s in S:
  t=1
  for _ in range(D):
    t=t*int(s)
    if tmp+t>=K:
      print(s)
      break
  else:
    tmp+=t
    continue
  break

Fixed submission:
S = input()
K = int(input())

D=5000000000000000

tmp=0
for s in S:
  if s=='1':
    tmp+=1
    if tmp>=K:
      print(s)
      break
    continue
    
  t=1
  for _ in range(D):
    t=t*int(s)
    if tmp+t>=K:
      print(s)
      break
  else:
    tmp+=t
    continue
  break

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
K = int(input())

D=5000000000000000

tmp=0
for s in S:
  t=1
  for _ in range(D):
    t=t*int(s)
    if tmp+t>=K:
      print(s)
      break
  else:
    continue
  break

Fixed submission:
S = input()
K = int(input())

D=5000000000000000

tmp=0
for s in S:
  if s=='1':
    tmp+=1
    if tmp>=K:
      print(s)
      break
    continue
    
  t=1
  for _ in range(D):
    t=t*int(s)
    if tmp+t>=K:
      print(s)
      break
  else:
    tmp+=t
    continue
  break

Buggy python submission (verdict: Runtime Error):
S = int(input())
K = int(input())

D=5000000000000000

tmp=0
for s in S:
  tmp+=int(s)*5000000000000000 if s!='1' else 1
  if tmp>K:
    print(s)

Fixed submission:
S = input()
K = int(input())

D=5000000000000000

tmp=0
for s in S:
  if s=='1':
    tmp+=1
    if tmp>=K:
      print(s)
      break
    continue
    
  t=1
  for _ in range(D):
    t=t*int(s)
    if tmp+t>=K:
      print(s)
      break
  else:
    tmp+=t
    continue
  break
Buggy python submission (verdict: Runtime Error):
N,K=map(int, inpur().split())

if K%N==0:
  print(0)
else:
  print(1)

Fixed submission:
N,K=map(int, input().split())

if N%K==0:
  print(0)
else:
  print(1)
Buggy python submission (verdict: Runtime Error):
N=int(input())

if N%2==0:
  ans='0'
else:
  ans='1'
  
i=2
while abs(tmp)<=abs(N):
  if N%((-2)**i)==0:
    ans='0'+ans
  else:
    ans='1'+ans
    
print(ans)

Fixed submission:
N=int(input())

if N%2==0:
  ans='0'
else:
  ans='1'
  N-=1
  
i=2
while abs(N)>0:
  if N%((-2)**i)==0:
    ans='0'+ans
  else:
    ans='1'+ans
    N-=(-2)**(i-1)
  i+=1
    
print(ans)
Buggy python submission (verdict: Runtime Error):
S = input()
T = input()

for i in range(len(S)):
    s=S[-i:]+S[0:-i]
    if s==T:
        print('Yes')
        break
else:
    print('No')

Fixed submission:
A = list(map(int, input().split()))

A.sort()
A.reverse()

print(abs(A[0]-A[1])+abs(A[1]-A[2]))
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
m=[list(map(int, input().split())) for _ in range(M)]
m=sorted(m, key=lambda x: x[1])
p=0
for _ in range(M):
    if m!=[]:
        tmp=max(m)[0]
        m=[l for l in m if l[1]<=tmp]
        p+=1
    else:
        break
print(p)

Fixed submission:
N, M = map(int,input().split())
m=[list(map(int, input().split()))[::-1] for _ in range(M)]
m.sort()

b=[]
for i in m:
    if len([tmp for tmp in b if tmp>=i[1]])==0:
        b.append(i[0]-1)
print(len(b))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
m=[list(map(int, input().split())) for _ in range(M)]
m=sorted(m, key=lambda x: x[1])
p=0
while m!=[]:
    tmp=max(m)[0]
    m=[l for l in m if l[1]<=tmp]
    p+=1
print(p)

Fixed submission:
N, M = map(int,input().split())
m=[list(map(int, input().split()))[::-1] for _ in range(M)]
m.sort()

b=[]
for i in m:
    if len([tmp for tmp in b if tmp>=i[1]])==0:
        b.append(i[0]-1)
print(len(b))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
m=[list(map(int, input().split())) for _ in range(M)]
m=sorted(m, key=lambda x: x[1])
p=0
while m!=[]:
    tmp=max(m)[0]
    while m!=[] and m[-1][1]>tmp:
        m.pop()
    p+=1
print(p)

Fixed submission:
N, M = map(int,input().split())
m=[list(map(int, input().split()))[::-1] for _ in range(M)]
m.sort()

b=[]
for i in m:
    if len([tmp for tmp in b if tmp>=i[1]])==0:
        b.append(i[0]-1)
print(len(b))
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = int(input())

if N%500=<A:
  print('Yes')
else:
  print('No')


Fixed submission:
N = int(input())
A = int(input())

if N%500<=A:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
s = input()
t = input()
s.sort()
t.sort(reverse=True)
print('Yes' if s<t else 'No')

Fixed submission:
s = input()
t = input()
s=sorted(s)
t=sorted(t,reverse=True)
print('Yes' if s<t else 'No')
Buggy python submission (verdict: Runtime Error):
K, S=map(int,input().split)

answer=0
for x in range(K+1):
  for y in range(K+1):
    if S-(x+y)<=K:
      answer+=1
      
print(answer)

Fixed submission:
K, S=map(int,input().split())

answer=0
for x in range(K+1):
  for y in range(K+1):
    if S-(x+y)>=0 and S-(x+y)<=K:
      answer+=1
      
print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
strings = []
for _ in range(N):
    strings.append(sorted(list(input().rstrip('\n'))))

count = 0
for i in range(len(strings)):
    for j in range(i + 1, len(strings)):
        if strings[i] == strings[j]:
            count += 1

print(count)


Fixed submission:
from math import factorial

N = int(input())
strings = {}
for _ in range(N):
    string = str(sorted(list(input().rstrip('\n'))))
    if string in strings:
        strings[string] += 1
    else:
        strings[string] = 1

count = 0
for value in strings.values():
    if value >= 2:
        count += factorial(value) // 2 // factorial(value - 2)

print(count)
Buggy python submission (verdict: Runtime Error):
    input()
    p = [int(x) for x in input().split()]
     
    p_sorted = p.sorted()
     
    diff = 0
    for p_elem, p_sorted_elem in zip(p, p_sorted):
      	if p_elem !+ p_sorted_elem:
          	diff += 1
     
    if diff == 0 or diff == 2:
      	print('YES')
    else:
      	print('NO')

input()
p = [int(x) for x in input().split()]

p_sorted = sorted(p)

diff = 0
for p_elem, p_sorted_elem in zip(p, p_sorted):
  	if p_elem !+ p_sorted_elem:
      	diff += 1

if diff == 0 or diff == 2:
  	print('YES')
else:
  	print('NO')



Fixed submission:
input()
p = [int(x) for x in input().split()]
    
p_sorted = sorted(p)
    
diff = 0
for p_elem, p_sorted_elem in zip(p, p_sorted):
    if p_elem != p_sorted_elem:
        diff += 1
    
if diff == 0 or diff == 2:
    print('YES')
else:
    print('NO')

Buggy python submission (verdict: Runtime Error):
input()
p = [int(x) for x in input().split()]

p_sorted = p.sorted()

diff = 0
for p_elem, p_sorted_elem in zip(p, p_sorted):
  	if p_elem !+ p_sorted_elem:
      	diff += 1

if diff == 0 or diff == 2:
  	print('YES')
else:
  	print('NO')

Fixed submission:
input()
p = [int(x) for x in input().split()]
    
p_sorted = sorted(p)
    
diff = 0
for p_elem, p_sorted_elem in zip(p, p_sorted):
    if p_elem != p_sorted_elem:
        diff += 1
    
if diff == 0 or diff == 2:
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Runtime Error):
n = int(input().rstrip())
p = [int(x) for x in input().rstrip().split()]

count = 0
for i in range(1, n - 1):
    if (p[i] > p[i - 1] and p[i] < p[i + 1]) or \
        (p[i] < p[i - 1] and p[i] > p[i + 1]):
        count += 1
print(count)


Fixed submission:
from collections import defaultdict

S = input().rstrip()

char_dict = defaultdict(int)
for char in S:
    char_dict[char] += 1

kind = len(char_dict.keys())
val_set = set(char_dict.values())

if kind == 2 and val_set == {2}:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
, K = map(int, input().rstrip().split())

print(N - K)


Fixed submission:
N, K = map(int, input().rstrip().split())

if K == 1:
    print(0)
else:
    print(N - K)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3

def switch_on_off(n, keta):
    bin_str = format(n, f'0{keta}b')
    return list(bin_str)

def check_light(n, N, s, index, P):
    switch = switch_on_off(n, N)
    sum = 0
    for v in s:
        sum += int(switch[int(v) - 1])
    if sum % 2 == P[index - 1]:
        return 1
    return 0

N, M = map(int, input().rstrip().split())
K = []
S = []
for _ in range(M):
    tmp = list(map(int, input().rstrip().split()))
    K.append(tmp[0])
    S.append(tmp[1:])
P = list(map(int, input().rstrip().split()))


ans = 0
for n in range(2 ** N):
    sum = 0
    for index, s in enumerate(S):
        sum += check_light(n, N, s, index, P)
    if sum == M:
        ans += 1

print(ans)


Fixed submission:
#!/usr/bin/env python3

def switch_on_off(n, keta):
    bin_str = format(n, '0{}b'.format(keta))
    return list(bin_str)

def check_light(n, N, s, index, P):
    switch = switch_on_off(n, N)
    sum = 0
    for v in s:
        sum += int(switch[int(v) - 1])
    if sum % 2 == P[index]:
        return 1
    return 0

N, M = map(int, input().rstrip().split())
K = []
S = []
for _ in range(M):
    tmp = list(map(int, input().rstrip().split()))
    K.append(tmp[0])
    S.append(tmp[1:])
P = list(map(int, input().rstrip().split()))


ans = 0
for n in range(2 ** N):
    sum = 0
    for index, s in enumerate(S):
        sum += check_light(n, N, s, index, P)
    if sum == M:
        ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys

N, M = map(int, input().rstrip().split())

L = []
R = []
for _ in range(M):
    l, r = map(int, input().rstrip().split())
    L.append(l)
    R.append(r)

Ans = set(range(L[0], R[0] + 1))
for i in range(1, len(L)):
    Ans = set(range(L[i], R[i] + 1)) & Ans
    if not Ans:
        print(0)
        sys.exit(0)

print(len(Ans))


Fixed submission:
#!/usr/bin/env python3
import sys

N, M = map(int, input().rstrip().split())

L = []
R = []
for _ in range(M):
    l, r = map(int, input().rstrip().split())
    L.append(l)
    R.append(r)

Ans = min(R) - max(L) + 1
if Ans < 0:
    Ans = 0
print(Ans)
Buggy python submission (verdict: Runtime Error):
N=int(input())
List=list(map(int,input().split()))
List.sort(reverse=True)
ans=0
for i in range(N-1):
  k = List[i]/(2**(i+1))
  ans += k
l = List[N]/(2**(N-1))
ans += l
print(ans)
          
  

Fixed submission:
N = int(input())
v = list(map(int, input().split()))
v.sort()
v.reverse()
result = 0

for i, x in enumerate(v):
    if i == N - 1:
        i = N - 2
    result += x / pow(2, i + 1)

print(result)
Buggy python submission (verdict: Runtime Error):
k,x=map(int,input().split())
for i in range(x-k+1,x+K):
  print(i, end=' ')

Fixed submission:
k,x=map(int,input().split())
for i in range(x-k+1,x+k):
  print(i, end=' ')
Buggy python submission (verdict: Runtime Error):
a,b=map(int,input().split())
if (a+b) % 4 ==0:
  print(int((a+b)/4))
  else:
    print("IMPOSSIBLE")

Fixed submission:
a,b=map(int,input().split())
if (a+b) % 2 ==0:
  print(int((a+b)/2))
else:
  print("IMPOSSIBLE")
Buggy python submission (verdict: Runtime Error):
N=input()
List=[int(i) for i in input().split()]
count=0
for i in range(N):
  if i != List[i]:
    count += 1
if count <=2:
  print("YES")
else:
  print("NO")
    

Fixed submission:
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(n):
    if p[i] != i + 1:
        count += 1
if count <= 2:
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Runtime Error):
a,b=map(int,input().split())
c=(a//(2b+1))+1
print(c)

Fixed submission:
r=int(input())
print(3*r**2)

Buggy python submission (verdict: Runtime Error):
r=input()
print(3*r*r)

Fixed submission:
r=int(input())
print(3*r**2)
Buggy python submission (verdict: Runtime Error):
a,b=map(int,input().split())
c=a//(2b+1)+1
print(c)

Fixed submission:
n, d = map(int, input().split())
if n%(2*d+1) == 0:
    print(n//(2*d+1))
else:
    print(n//(2*d+1)+1)
Buggy python submission (verdict: Runtime Error):
a,b=input().split()
c=input()
c[b-1]=c[b-1].upper()
print(c)


Fixed submission:
n,k=map(int,input().split())
s=input()
print(s[:k-1]+s[k-1].lower()+s[k:])
Buggy python submission (verdict: Runtime Error):
N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q): l, r = map(int, input().split())
print(t[r-1] - t[l-1])

Fixed submission:
N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
	t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
	l, r = map(int, input().split())
	print(t[r-1] - t[l-1])
Buggy python submission (verdict: Runtime Error):
a,b = input()
if b%a ==0:
  print(a+b)
else:
  print(b-a)


Fixed submission:
x,y=map(int,input().split())
if y%x ==0:
  print(x+y)
else:
  print(y-x)

Buggy python submission (verdict: Runtime Error):
a, b = input()
if b%a ==0:
  print(a+b)
  else:
    print(b-a)

Fixed submission:
x,y=map(int,input().split())
if y%x ==0:
  print(x+y)
else:
  print(y-x)
Buggy python submission (verdict: Runtime Error):
N=int(input())
h = list(map(int , input().split()))
dp = [10**9+1]*N
dp[0] = 0
dp[1] = abs(h[0]-h[1])
for i in range(2,N):
    for j in range(1, i-1):
        dp[i] = min(dp[i-j]+abs(h[i]-h[i-j]), dp[]+abs(h[i]-h[i-j]))
print(dp[N-1])

Fixed submission:
N, K = map(int,input().split())
H = list(map(int,input().split()))

dp = [1e9]*N
dp[0] = 0
for i, b in enumerate(H):
  if i != 0:
    x = max(0, i-K)
    dp[i] = min([abs(b-d)+p for d, p in zip(H[x:i], dp[x:i])])
print(dp[-1])