#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <cstdio>
#define pii pair<int, int>
#define mp make_pair
//#define pp pair<int,int>
#define maxn 1050000
#define ll long long
#define INF 2e9
#define LINF 2e14
#define MOD 1000000007
#define mem(a,v) memset(a,v,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
using namespace std;
int n, k, pt[100010];
vector<ll> v[100010], v2;
set<pii> pq;
map<ll, ll> lt, gt;
ll ltt, gtt;
int ltcnt, gtcnt;
int main()
{
while(~scanf("%d%d", &k, &n)){
ll res = LINF;
int best;
for(int i = 0; i < 100010; i++)
v[i].clear();
memset(pt, 0, sizeof(pt));
pq.clear();
gt.clear(), gt.clear();
ltt = gtt = ltcnt = gtcnt = 0;
v2.clear();
for(int i = 0; i < n; i++){
int pos, t;
scanf("%d%d", &pos, &t);
v[--t].pb(pos);
v2.pb(pos);
}
for(int i = 0; i < k; i++){
v[i].pb(INF);
ll mid = (v[i][1]+v[i][0])/2;
pq.insert(mp(mid, i));
gt[v[i][0]] ++;
gtt += v[i][0];
}
gtcnt = k;
for(int i = 0; i < n; i++){
ll pos = v2[i];
while(gt.begin()->first < pos){
gtt -= gt.begin()->first*gt.begin()->second;
gtcnt -= gt.begin()->second;
ltt += gt.begin()->first*gt.begin()->second;
ltcnt += gt.begin()->second;
lt[gt.begin()->first] += gt.begin()->second;
gt.erase(gt.begin());
}
while(pq.begin()->first <= pos){
int type = pq.begin()->second;
pq.erase(pq.begin());
if(pt[type] < pos){
ltcnt--, ltt -= pt[type];
if(--lt[pt[type]] == 0)
lt.erase(pt[type]);
pt[type]++;
}
else{
gtcnt--, gtt-=pt[type];
if(--gt[pt[type]] == 0)
gt.erase(pt[type]);
pt[type]++;
}
if(pt[type] < pos){
ltcnt++, ltt += pt[type];
lt[pt[type]]++;
}
else{
gtcnt++, gtt+=pt[type];
++gt[pt[type]];
}
ll mid = (v[type][pt[type]] + v[type][pt[type]+1])/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tc2V0KHB0LCAwLCBzaXplb2YocHQpKTsKICAgICAgICBwcS5jbGVhcigpOwogICAgICAgIGd0LmNsZWFyKCksIGd0LmNsZWFyKCk7CiAgICAgICAgbHR0ID0gZ3R0ID0gbHRjbnQgPSBndGNudCA9IDA7CiAgICAgICAgdjIuY2xlYXIoKTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKXsKICAgICAgICAgICAgaW50IHBvcywgdDsKICAgICAgICAgICAgc2NhbmYoIiVkJWQiLCAmcG9zLCAmdCk7CiAgICAgICAgICAgIHZbLS10XS5wYihwb3MpOwogICAgICAgICAgICB2Mi5wYihwb3MpOwogICAgICAgIH0KICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgazsgaSsrKXsKICAgICAgICAgICAgdltpXS5wYihJTkYpOwogICAgICAgICAgICBsbCBtaWQgPSAodltpXVsxXSt2W2ldWzBdKS8yOwogICAgICAgICAgICBwcS5pbnNlcnQobXAobWlkLCBpKSk7CiAgICAgICAgICAgIGd0W3ZbaV1bMF1dICsrOwogICAgICAgICAgICBndHQgKz0gdltpXVswXTsKICAgICAgICB9CiAgICAgICAgZ3RjbnQgPSBrOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspewogICAgICAgICAgICBsbCBwb3MgPSB2MltpXTsKICAgICAgICAgICAgd2hpbGUoZ3QuYmVnaW4oKS0+Zmlyc3QgPCBwb3MpewogICAgICAgICAgICAgICAgZ3R0IC09IGd0LmJlZ2luKCktPmZpcnN0Kmd0LmJlZ2luKCktPnNlY29uZDsKICAgICAgICAgICAgICAgIGd0Y250IC09IGd0LmJlZ2luKCktPnNlY29uZDsKICAgICAgICAgICAgICAgIGx0dCArPSBndC5iZWdpbigpLT5maXJzdCpndC5iZWdpbigpLT5zZWNvbmQ7CiAgICAgICAgICAgICAgICBsdGNudCArPSBndC5iZWdpbigpLT5zZWNvbmQ7CiAgICAgICAgICAgICAgICBsdFtndC5iZWdpbigpLT5maXJzdF0gKz0gZ3QuYmVnaW4oKS0+c2Vjb25kOwogICAgICAgICAgICAgICAgZ3QuZXJhc2UoZ3QuYmVnaW4oKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd2hpbGUocHEuYmVnaW4oKS0+Zmlyc3QgPD0gcG9zKXsKICAgICAgICAgICAgICAgIGludCB0eXBlID0gcHEuYmVnaW4oKS0+c2Vjb25kOwogICAgICAgICAgICAgICAgcHEuZXJhc2UocHEuYmVnaW4oKSk7CiAgICAgICAgICAgICAgICBpZihwdFt0eXBlXSA8IHBvcyl7CiAgICAgICAgICAgICAgICAgICAgbHRjbnQtLSwgbHR0IC09IHB0W3R5cGVdOwogICAgICAgICAgICAgICAgICAgIGlmKC0tbHRbcHRbdHlwZV1dID09IDApCiAgICAgICAgICAgICAgICAgICAgICAgIGx0LmVyYXNlKHB0W3R5cGVdKTsKICAgICAgICAgICAgICAgICAgICBwdFt0eXBlXSsrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgICAgICBndGNudC0tLCBndHQtPXB0W3R5cGVdOwogICAgICAgICAgICAgICAgICAgIGlmKC0tZ3RbcHRbdHlwZV1dID09IDApCiAgICAgICAgICAgICAgICAgICAgICAgIGd0LmVyYXNlKHB0W3R5cGVdKTsKICAgICAgICAgICAgICAgICAgICBwdFt0eXBlXSsrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYocHRbdHlwZV0gPCBwb3MpewogICAgICAgICAgICAgICAgICAgIGx0Y250KyssIGx0dCArPSBwdFt0eXBlXTsKICAgICAgICAgICAgICAgICAgICBsdFtwdFt0eXBlXV0rKzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgZ3RjbnQrKywgZ3R0Kz1wdFt0eXBlXTsKICAgICAgICAgICAgICAgICAgICArK2d0W3B0W3R5cGVdXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGxsIG1pZCA9ICh2W3R5cGVdW3B0W3R5cGVdXSArIHZbdHlwZV1bcHRbdHlwZV0rMV0pLzI7CiAgICAgICAgICAgICAgICBwcS5pbnNlcnQobXAobWlkLCB0eXBlKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYocmVzID4gcG9zKihsdGNudC1ndGNudCktbHR0K2d0dCkKICAgICAgICAgICAgICAgIHJlcyA9IHBvcyoobHRjbnQtZ3RjbnQpLWx0dCtndHQsIGJlc3QgPSBwb3M7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiJWRcbiIsIGJlc3QpOwogICAgfQogICAgcmV0dXJuIDA7Cn0K