#include<bits/stdc++.h>
using namespace std;
stack<int> a;
stack<int> b;
stack<int> c;
stack<int> d;
stack<int> e;
stack<int> f;
int i,j,k,n,p,m,s,l;
int main()
{
cin>>n>>p;
for(i=1;i<=n;i++)
{
cin>>j>>k;
switch(j)
{
case 1:
{
//cout<<k;
if(a.empty()==0)
{
if(a.top()<k)
{
a.push(k);
s++;
}
else
{
while(1)
{
if(a.top()>k)
{
//cout<<a.top()<<'\n';
a.pop();
s++;
}
else
{
break;
a.push(k);
s++;
}
}
}
}
else
{
a.push(k);
s++;
}
}
case 2:
{
//cout<<k;
if(b.empty()==0)
{
if(b.top()<k)
{
b.push(k);
s++;
}
else
{
while(1)
{
if(b.top()>k)
{
//cout<<b.top()<<'\n';
b.pop();
s++;
}
else
{
break;
b.push(k);
s++;
}
}
}
}
else
{
b.push(k);
s++;
}
}
case 3:
{
//cout<<k;
if(c.empty()==0)
{
if(c.top()<k)
{
c.push(k);
s++;
}
else
{
while(1)
{
if(c.top()>k)
{
//cout<<c.top()<<'\n';
c.pop();
s++;
}
else
{
break;
c.push(k);
s++;
}
}
}
}
else
{
c.push(k);
s++;
}
}
case 4:
{
if(d.empty()==0)
{
if(d.top()<k)
{
d.push(k);
s++;
}
else
{
while(1)
{
if(d.top()>k)
{
//cout<<d.top()<<'\n';
d.pop();
s++;
}
else
{
break;
d.push(k);
s++;
}
}
}
}
else
{
d.push(k);
s++;
}
}
case 5:
{
//cout<<k;
if(e.empty()==0)
{
if(e.top()<k)
{
e.push(k);
s++;
}
else
{
while(1)
{
if(e.top()>k)
{
//cout<<e.top()<<'\n';
e.pop();
s++;
}
else
{
break;
e.push(k);
s++;
}
}
}
}
else
{
e.push(k);
s++;
}
}
case 6:
{
//cout<<k;
if(f.empty()==0)
{
if(f.top()<k)
{
a.push(k);
s++;
}
else
{
while(1)
{
if(f.top()>k)
{
//cout<<f.top()<<'\n';
f.pop();
s++;
}
else
{
break;
f.push(k);
s++;
}
}
}
}
else
{
f.push(k);
s++;
}
}
}
}
cout<<s;
}