#include <bits/stdc++.h>
using namespace std;
long long n,p,res;
deque<int>a[10];
int main () {
freopen("gitara.in","r",stdin);
freopen("gitara.out","w",stdout);
cin >> n >> p;
for (int i=1;i<=n;i++) {
int x,y;
cin >> x >> y;
while (a[x].size()&&a[x].back()>y) {
res++;
a[x].pop_back();
}
if (a[x].size()==0||a[x].back()!=y) {
a[x].push_back(y);
res++;
}
}
cout << res <<endl;
return 0;
}