#include<bits/stdc++.h>
#define int long long
using namespace std;
ifstream fin("gitara.in");
ofstream fout("gitara.out");
auto mread = [](){
int x;
fin >> x;
return x;
};
const int N = 5e5 + 5;
int n = mread(), p = mread(), a[N], b[N];
vector<int> v[7];
priority_queue<int> q[7];
signed main(){
for(int i = 1; i <= n; i ++){
a[i] = mread(), b[i] = mread();
v[a[i]].push_back(b[i]);
}
int ans = 0;
for(int i = 1; i <= 6; i ++){
for(int x : v[i]){
while(q[i].size() && q[i].top() > x){
ans ++;
q[i].pop();
}
if(q[i].empty() || q[i].top() < x){
ans ++;
q[i].push(x);
}
}
}
fout << ans;
return 0;
}