n,m=list(map(int,input().split()))

p=[list(map(int,input().split()))for i in range(m)]

c=[]

for i in p:

	a,b=i

	if a>=n:o=0

	else:o=n-a

	c.append(o)

c.sort()

c.pop()

print((sum(c)))
n,m=list(map(int,input().split()))

p=[list(map(int,input().split()))for i in range(m)]

c=[n-a if n>a else 0for a,b in p]

print((sum(c)-max(c)))
n, m = list(map(int, input().split()))
p = [list(map(int, input().split())) for i in range(m)]
c = []
for i in p:
    a, b = i
    if a >= n:
        o = 0
    else:
        o = n - a
    c.append(o)
c.sort()
c.pop()
print((sum(c)))

n, m = list(map(int, input().split()))
p = [list(map(int, input().split())) for i in range(m)]
c = [n - a if n > a else 0 for a, b in p]
print((sum(c) - max(c)))

-c = [] -for i in p: -    a, b = i -    if a >= n: -        o = 0 -    else: -        o = n - a -    c.append(o) -c.sort() -c.pop() -print((sum(c))) +c = [n - a if n > a else 0 for a, b in p] +print((sum(c) - max(c)))