Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() (int, error) {
	return strconv.Atoi(nextString())
}

func main() {
	scanner.Split(bufio.ScanWords)

	n, _ := nextInt()
	m, _ := nextInt()

	arr := make([][]int, m)
	for i := range arr {
		a, _ := nextInt()
		b, _ := nextInt()
		arr[i] = []int{a, b}
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
	//	fmt.Println(arr)

	nGot := 0
	spent := 0
	for i := range arr {
		a := arr[i][0]
		b := arr[i][1]
		if a >= n {
			nGot++
		} else if a+b >= n {
			spent += n - a
			nGot++
		}
		if nGot >= m-1 {
			break
		}
	}
	fmt.Println(spent)
}

C++: #include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
void dainyu(int X[], int Y[ ],int n,int m) {
	for (int i = n; i <= m; i++) {
		Y[i] = X[i];
	}
}
void Dscanf(int X[], int n, int m) {
	for (int i = n; i <= m;i++) {
		scanf("%d", &X[i]);
	}
}
void Dprintf(int X[], int n, int m) {
	for (int i = n; i <= m; i++) {
		printf("%d\n", X[i]);
	}
}
void reverse(int X[], int Y[],int n,int m) {
	for (int i = n; i <= m; i++) {
		Y[i] = X[n + m - i];
	}
}

int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int N, M, A[4000], B[4000];
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= M; i++) {
		scanf("%d %d", &A[i], &B[i]);
	}
	for (int i = 1; i <= M; i++) {
		if (B[i] == max(M, B)) {
			B[i] = N;
			break;
		}
	}
	int x = 0;
	for (int i = 1; i <= M-1; i++) {
		if (B[i] > N) {
			x -= N - B[i];
		}
	}
	printf("%d\n", x);

	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {

	int min = all[1];
	for (int i = 2; i <= n; ++i) {
		if (min > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[1];
	for (int i = 2; i<=n; ++i) {
		if (max<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main() {
	int a, b, k[1005], l[1005], ans = 0, sum = 0;
	cin >> a >> b;
	for (int i = 0; i < b; ++i) {
		cin >> k[i] >> l[i];
		if (a - k[i] > 0) ans += a - k[i];
		if (a - k[i] > sum) sum = a - k[i];
	}
	cout << ans - sum << endl;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>

using namespace std;

int main (void)
{
	int n,m,a[100],b[100],cou,x;
	cin >> n >> m;
	cou = 0;
	x = 0;
	for (int i = 0; i < m; i++)
	{
		cin >> a[i] >> b[i];
		if (a[i] >= b[i])
		{
			cou++;
		}
		
	}
	
	if (cou >= m - 1)
	{
		cout << '0' << endl;
	}
	else
	{
		for (int i = 0; i < m; i++)
		{
			if (a[i] < b[i] && x < (b[i] - a[i]))
			{
				x = 0;
				x = b[i] - a[i];
			}
		}
	}
}

C++: #include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
void dainyu(int X[], int Y[ ],int n,int m) {
	for (int i = n; i <= m; i++) {
		Y[i] = X[i];
	}
}
void Dscanf(int X[], int n, int m) {
	for (int i = n; i <= m;i++) {
		scanf("%d", &X[i]);
	}
}
void Dprintf(int X[], int n, int m) {
	for (int i = n; i <= m; i++) {
		printf("%d\n", X[i]);
	}
}
void reverse(int X[], int Y[],int n,int m) {
	for (int i = n; i <= m; i++) {
		Y[i] = X[n + m - i];
	}
}

int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int N, M, A[4000], B[4000];
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= M; i++) {
		scanf("%d %d", &A[i], &B[i]);
	}
	for (int i = 1; i <= M; i++) {
		if (B[i] == max(M, B)) {
			B[i] = N;
			break;
		}
	}
	int x = 0;
	for (int i = 1; i <= M; i++) {
		if (B[i] > N) {
			x -= N - B[i];
		}
	}
	printf("%d\n", x);

	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {

	int min = all[1];
	for (int i = 2; i <= n; ++i) {
		if (min > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[1];
	for (int i = 2; i<=n; ++i) {
		if (max<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int ll;

int main(){
	int n,m; cin >> n >> m;
	int ans=0;
	int ma=0;
	for(int i=0;i<m;i++){
		int a,b; cin >> a >> b;
		int cnt=max(0,n-a);
		ans+=cnt;
		ma=max(ma,cnt);
	}
	cout << ans-ma << endl;
}

C++: #include <utility> // pair make_pair
using std::swap; // c++11
using std::pair;
using std::make_pair;
#include <tuple> // tuple(c++11) make_tuple(c++11)
using std::tuple; // get<address>(variable)
using std::make_tuple;
#include <string> // string stoi(c++11) stoll(c++11) to_string(c++11)
using std::string;
using std::stoi;
using std::stoll;
using std::to_string;
#include <queue> // queue priority_queue
using std::queue;
using std::priority_queue;
#include <stack> // stack
using std::stack;
#include <vector> // vector
using std::vector;
#include <map> // map
using std::map;
#include <set> // set
using std::set; // insert erase find count
#include <algorithm> // min max
using std::sort;
using std::lower_bound; // iterator,  >=
using std::upper_bound; // iterator,  >
using std::min;
using std::max;
using std::min_element; // iterator
using std::max_element; // iterator
#include <complex> // complex
using std::complex; // real imag
#include <iostream> // cin cout
using std::cin;
using std::cout;
using std::endl;
#include <iomanip> // setprecision fixed(manipulator)
using std::setprecision;
using std::fixed;
#include <cmath>
#include <cstdio>

#define lli long long int
#define INF (int)1e09
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EFOR(i, m, n) for (int i = m; i <= n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REFOR(i, m, n) for (int i = m; i >= n; i--)
#define VSORT(v) sort((v).begin(), (v).end()) // sort vector
#define VGSORT(v) sort((v).begin(), (v).end(), std::greater<int>()) // sort vector
typedef pair<int, int> PI;
typedef tuple<int, int, int> TI;
template <typename T> using Vec = vector<T>; // c++11

int main(void) {
    int n;
    int m;
    cin >> n >> m;
    int temp;
    Vec<int> table(m);
    FOR (i, 0, m) {
        cin >> table[i] >> temp;
    }
    VGSORT(table);
    int i = 0;
    while (1) {
        if (table[i] >= n) {
            i++;
        } else {
            break;
        }
    }
    /*
    FOR (i, 0, m) {
        cout << table[i] << " ";
    }
    cout << endl;
    cout << i << endl;
    */
    if (i >= m - 1) {
        cout << 0 << endl;
    } else {
        int count = 0;
        FOR (j, i, m - 1) {
            count += (n - table[j]);
        }
        cout << count << endl;
    }

    return 0;
}