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 <climits>
using namespace std;

class Card {
    int hit, miss, diff;
    public:
    void set(int o, int x);
    int use_diff();
};

void Card::set(int o, int x) {
    hit = o;
    miss = x;
    diff = o - x;
}

int Card::use_diff() { return diff; }

int main() {
    int n, m; cin >> n >> m;

    int diff_worst = 0;
    int diff_bad_better = INT_MIN;
    int diff_sum = 0;
    Card cards[m];
    for (int i=0; i<m; i++) {
        Card card = cards[i];
        int a, b; cin >> a >> b;
        card.set(a, b);
        diff_worst = min(card.use_diff(), diff_worst);
        if (card.use_diff() < 0) {
            diff_bad_better = max(card.use_diff(), diff_bad_better);
            diff_sum += card.use_diff();
        }
    }

    cout << - ( diff_sum - diff_worst ) / 2 << endl;
}

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

C++: #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <utility>
#include <numeric>

using namespace std;
typedef long long llong;
typedef unsigned long long ullong;

const ullong MOD = 1000000000 + 7;

int main()
{
	int n, m;
	cin >> n >> m;

	vector<int> a, b;
	int cnt = 0;
	for (int i = 0; i < m; i++)
	{
		int x, y;
		cin >> x >> y;

		a.push_back(x);
		b.push_back(y);

		if (x >= n)
		{
			cnt++;
		}
	}

	int v = 0;
	if (cnt < m - 1)
	{
		sort(a.begin(), a.end());
		reverse(a.begin(), a.end());

		
		for (int i = 0; i < m; i++)
		{
			if (a.at(i) < n)
			{
				v += n - a.at(i);
				cnt++;
				if (cnt >= m - 1)
				{
					break;
				}
			}
		}
	}

	cout << v << endl;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <stack>
#include <queue>
#include <cmath>
 
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define repp(i,j,k) for(int i=j;i>=k;i--)
#define Sort(x) sort(x.begin(),x.end()); 
#define ll long long
int mod=1e9+7;
const int INF = 1e9;
 
using namespace std;
 
int main(void) {
	int n,m,count=0,ans=0;
	vector<int> v;
	cin>>n>>m;
	rep(i,0,m){
	    int a,b;
	    cin>>a>>b;
	    if(a>=b)count++;
	    else v.push_back((b-a)/2);
	}
	sort(v.begin(),v.end());
	
	if(count>=m-1){
	    cout<<0<<endl;
	    return 0;
	}else {
	    rep(i,0,m-1-count){
	        ans+=v[i];
	    }
	}
	
	cout<<ans<<endl;
	
 
 
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <stack>
#include <queue>
#include <cmath>
 
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define repp(i,j,k) for(int i=j;i>=k;i--)
#define Sort(x) sort(x.begin(),x.end()); 
#define ll long long
int mod=1e9+7;
const int INF = 1e9;
 
using namespace std;
 
int main(void) {
	int n,m,count=0,ans=0;
	vector<int> v;
	cin>>n>>m;
	rep(i,0,m){
	    int a,b;
	    cin>>a>>b;
	    if(a>=b)count++;
	    else v.push_back((b-a)/2);
	}
	
	if(count>=m-1){
	    cout<<0<<endl;
	    return 0;
	}else {
	    rep(i,0,m-1-count){
	        ans+=v[i];
	    }
	}
	
	cout<<ans<<endl;
	
 
 
	return 0;
}

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

using namespace std;

int main() {
	int n, m, a[1000], b, ans = 0;
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		cin >> a[i] >> b;
	}
	sort(a, a + m);
	for (int i = m - 1; i > 0; --i) {
		if (a[i] < n) ans += n - a[i];
	}
	cout << ans << endl;

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int cnt = 0;
    priority_queue<int, vector<int>, greater<>> q;
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        if (a >= b) {
            ++cnt;
        } else {
            q.push(n - a);
        }
    }
    int cost = 0;
    while (cnt < m - 1) {
        int p = q.top();
        q.pop();
        cost += p;
        ++cnt;
    }
    cout << cost << '\n';
    return 0;
}

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

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

signed main(){
    int N,M;
    cin>>N>>M;
    vint vec;
    rep(i,M){
        int a,b;
        cin>>a>>b;
        vec.pb(max(0ll,N-a));
    }
    sort(all(vec));
    int ans=0;
    rep(i,M-1)ans+=vec[i];
    cout<<ans<<endl;
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
int main()
{
	int n, m, a, b[1000], k, ans=0;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++)
	{
		scanf("%d%d", &a, &b[i]);
	}
	for (int i = 0; i < m; i++)
	{
		for (int j = i + 1; j < m; j++)
		{
			if (b[i] > b[j])
			{
				k = b[i];
				b[i] = b[j];
				b[j] = k;
			}
		}
	}
	for (int i = 0; i < m - 1; i++)
	{
		if(b[i]>n)
		ans += b[i]-n;
	}
	printf("%d\n", ans);
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<sstream>
using namespace std;
 
#define P(p) cout<<(p)<<endl
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(),greater<int>());
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return
#define lb(v,n) lower_bound(v.begin(),v.end(),n)
#define ub(v,n) upper_bound(v.begin(),v.end(),n)
#define mae(v) max_element(v.begin(),v.end())
#define mie(v) min_element(v.begin(),v.end())
#define INF 0x7FFFFFFF
#define mod 1000000007
typedef long long ll;
////////////////////////////////////////////////////////////

int main(){
	int n,m;
	cin >> n >> m;
	
	priority_queue<pair<int,int>> v;
	rep(i,0,m){
		int a,b;
		cin >> a >> b;
		v.push(pair<int,int> (a,b));
	}

	int ans = 0;
	rep(i,0,m-1){
		pair<int,int> p = v.top();
		v.pop();
		int a=p.first;
		int b=p.second;
		
		if( a < n ){
			ans += n-a;	
		}
		
	}
	
	cout << ans << endl;
	ret 0;
}