#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 N, M; cin >> N >> M;
  map<i64, vector<i64>> PC; // 
  unordered_set<i64> reachable;
  REP(0, M, i) {
    i64 X, Y; RI(X); RI(Y);
    PC[X].pb(Y);
  }

  reachable.insert(N);
  for(auto &pc: PC) {
    if(pc.first == 0) {
      continue;
    }

    vi &xs = pc.second;
    vi add, remove;

    for(auto &x: xs) {
      if(IN(reachable, x)) {
        remove.pb(x);
      }
      if(IN(reachable, x-1) || IN(reachable, x+1)) {
        add.pb(x);
      }
    }

    for(auto &r: remove) {
      auto it = reachable.find(r);
      reachable.erase(it);
    }
    for(auto &a: add) {
      reachable.insert(a);
    }

#if 0
    cerr << pc.first << endl;
    for(auto &r: reachable) {
      cerr << r << " ";
    }
    cerr << endl;
#endif
  }

  WI(sz(reachable));

  return 0;
}


#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
// please, read the question correctly (do you need set or multiset)???
const int N=200010; //check the limits, dummy
vector<pair<int, int>> a[2*N];
vector<int> b[2*N];
int n, m;
int ans = 0;
bool vis[2*N];
void dfs(int x){
	// cout<<x<<endl;
	if(vis[x])return;
	vis[x]=1;
	if(x>m)++ans;
	for(auto it:b[x])dfs(it);
}
map<int, int> mp;
int main(){
	scanf("%d%d",&n,&m);
	int x, y;
	int cnt = 1;
	for(int i=1; i<=m; ++i){
		scanf("%d%d",&x,&y);
		if(mp.count(y)==0)
			mp[y]=cnt++;
		a[mp[y]].push_back(mk(x,i));
	}
	if(mp.count(n)==0)mp[n]=cnt++;
	a[mp[n]].push_back(mk(0,0));
	cnt = m+1;
	for(auto ii:mp){
		int i=ii.sc;
		if(a[i].size()==0)continue;
		a[i].push_back(mk(2*n+1, cnt++));
		sort(a[i].begin(), a[i].end());
	}
	for(auto ii:mp){
		int i=ii.sc;
		if(a[i].size()==0)continue;
		int p1=0, p2=0, k1 = mp[ii.fs+1], k2 = mp[ii.fs-1];
		for(int j=0; j+1<a[i].size(); ++j){
			auto it=a[i][j];
			for(p1;p1+1<a[k1].size();++p1){
				// cout<<ii.fs<<" "<<it.fs<<" "<<a[k1][p1].fs<<endl;
				if(a[k1][p1].fs>it.fs && a[k1][p1].fs<=a[i][j+1].fs)
					b[it.sc].push_back(a[k1][p1].sc);
				if(a[k1][p1].fs>a[i][j+1].fs)
					break;
			}
			for(p2;p2+1<a[k2].size();++p2){
				// cout<<ii.fs<<" "<<it.fs<<" %"<<a[k1][p1].fs<<endl;
				if(a[k2][p2].fs>it.fs && a[k2][p2].fs<=a[i][j+1].fs)
					b[it.sc].push_back(a[k2][p2].sc);
				if(a[k2][p2].fs>a[i][j+1].fs)
					break;
			}
		}
		int j=a[i].size();
		b[a[i][j-2].sc].push_back(a[i][j-1].sc);
	}
	ans = 0;
	dfs(0);
	cout<<ans<<endl;
}