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<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

i_i c[1000];

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int2(n, m);
    rep(i, m)scanf("%d %d", &c[i].X, &c[i].Y);
    sort(c, c + m);
    //pri2(c[0].X, c[0].Y);
    int res = 0;
    rep1(i, m){
      res += max(0, n - c[i].X);
    }
    pri(res);

/*/

//*/ break;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;

int main()
{
	int n, m, bmax = 0, s = 0;
	int a[1000], b[1000];

	cin >> n >> m;

	for (int i = 0; i < m; i++)
		cin >> a[i] >> b[i];

	for (int i = 0; i < m; i++) {
		if (b[i] > n) {
			if (bmax < b[i])
				bmax = b[i];
			s += b[i] - n;
		}
	}

	cout << s - bmax + n << endl;

	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;

int n, m, a[1000], b[1000];

int main() {
  scanf("%d%d", &n, &m);
  int mina = 100000;
  int sum = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a[i], &b[i]);
    mina = min(mina, a[i]);
    if (a[i] < n) sum += n - a[i];
  }
  if (mina < n) sum -= n - mina;
  printf("%d\n", sum);
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
	long n,m,i,j,ans=0;
	cin >> n >> m;
	vector<long> a(m);
	for(i=0; i<m; i++){
		cin >> a[i] >> a[i];
	}
	sort(a.begin(),a.end());
	for(i=0; i<m-1; i++){
		ans+=max(long(0),a[i]-n);
	}
	cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int M,N,i;
    cin>>N>>M;
    int sum=0,full=0,okane;
    for(i=0;i<M;i++){
        int A,B;
        cin>>A>>B;
        okane=max(N-A,0);
        if(okane>full)full=okane;
        sum+=okane;
    }
    cout<<sum-full<<endl;
    return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n, m;
    int ans = 0;
    int card[1001];
    int cnt = 0;
    int dummy;

    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        cin >> card[i] >> dummy;
    }

    sort(card, card + m);
    reverse(card, card + m);

    for (int i = 0; i < m-1; i++) {
        if (n - card[i] > 0) {
            ans += n - card[i];
        }
    }
    cout << ans << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  int A[M], B[M];
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
  }
  int cost = 0;
  int maxV = 0;
  for (int i = 0; i < M; i++) {
    cost += max(0, N - A[i]);
    maxV = max(maxV, N - A[i]);
  }
  cost -= maxV;
  cout << cost << "\n";
  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
 
//const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
//const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
 
int main()
{
	int n, m;
	cin >> n >> m;
	vector<int> b(m);

	REP(i, m){
		int tmp;
		cin >> tmp;
		cin >> tmp;
		b[i] = tmp;
	}

	sort(ALL(b));
	
	int ans = 0;
	int count = 0;
	for (auto x : b){
		if (count == m-1) break;
		count++;
		if (x <= n) continue;
		ans += (x - n);
	}

	cout << ans << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int a[m] = {0}, b[m] = {0};
  int i;
  for (i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }

  vector<int> res(m);
  for (i = 0; i < m; i++) {
    res[i] = (a[i] >= n ? 0 : n - a[i]);
  }
  sort(res.begin(), res.end());
  int cost = 0;
  for (i = 0; i < m - 1; i++) {
    cost += res[i];
  }
  cout << cost << endl;
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
  int n,m;
  int atari,hazure;
  vector<int> result;
  int total=0;
  
  cin>>n>>m;

  for(int i=0;i<m;++i){
    cin>>atari>>hazure;
    if(atari>=n)
      result.push_back(0);
    else
      result.push_back((hazure-atari)/2);
  }

  sort(result.begin(),result.end());

  for(int i=0;i<m-1;++i)
    total+=result[i];

  cout<<total<<endl;
}

C++: #include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;

typedef pair<int, int> P;

int main() {
	int n, m;
	cin >> n >> m;
	vector<int> cd;
	int a, b;
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		if (a < n) {
			cd.push_back(n - a);
		}
	}
	sort(cd.begin(), cd.end());
	int ans = 0;
	for (int i = 0; i + 1 < cd.size(); i++) {
		ans += cd[i];
	}
	cout << ans << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m,a,b,sum=0,mx=0;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		sum+=max(n-a,0);
		mx=max(max(n-a,0),mx);
	}
	cout<<sum-mx<<endl;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,m,a[1000],b[1000],i,x;
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++) scanf("%d %d",&a[i],&b[i]);
	sort(a,a+m);
	x=0;
	for(i=1;i<m;i++){
		if(a[i]>=n) break;
		x=n-a[i]+x;
	}
	printf("%d\n",x);
	return 0;
}

C++: #include <iostream>
using namespace std;

int main()
{
	int n, m;
	int a[1000], b[1000];

	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i];
	}

	int k = 0, max = 0;

	for (int i = 0; i < m; i++) {
		if (max < b[i]) {
			k = i;
			max = b[i];
		}
	}

	int cost = 0;
	for (int i = 0; i < m; i++) {
		if (i != k) {
			if (a[i] < n) {
				cost += n - a[i];
			}
		}
	}

	cout << cost << endl;

	return 0;
}