#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL); using ll = int64_t; /*-----------------------------------------------------------------------------------------------------*/
constint N = 5e5+25; ll bit[N]; voidmod(int p, int val){ for(; p<N; p+=p&-p) bit[p] += val; } ll sum(int p){ ll res = 0; for(; p; p-=p&-p) res += bit[p]; return res; }
signedmain(){ EmiliaMyWife int n, m; cin >> n >> m; vector<int> arr(n+1), vis(n+1); for(int i = 1, a, b; i <= m; i++) cin >> a >> b, arr[a] = b, vis[b] = 1; int it = 1; for(int i = 1; i <= n; i++) { if(arr[i]) continue; while(vis[it]) it++; arr[i] = it++; } ll ans = 0; for(int i = 1; i <= n; i++) { ans += sum(n)-sum(arr[i]); mod(arr[i], 1); } cout << ans << '\n';