// 問題の URL を書いておく
// 

#include <bits/stdc++.h>

using namespace std;

//#define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v) \
do {\
    cout << #v << ": " << v << endl; \
}while(0)

#define PrintVec(v) \
do {\
    for(int __i = 0; __i < v.size(); ++__i) \
    { \
        cout << #v << "[" << __i << "]: " << v[__i] << endl; \
    }\
}while(0)

#define P(fmt, ...) printf(fmt, __VA_ARGS__)
#define L printf("L: %d\n", __LINE__)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)
#define P(fmt, ...) ((void)0)
#define L ((void)0)

#endif

#define rep(i, n) for(int i = 0; i < (int)(n); ++i)

using ll = long long;

map<int, map<int, int>> cache;
map<int, map<int, bool>> cached;

int test(int N, int y, int x, const map<int, set<int>>& g)
{
    if(cached[y][x]) return cache[y][x];
    cached[y][x] = true;

    int diff[] = {-1, 1};
    rep(i, 2)
    {
        auto ny = y + diff[i];
        if(ny < 0 || ny > 2 * N) continue;
        auto lfound = g.find(ny);

        if(lfound == g.end())
        {
            if(ny == N)
            {
                cache[y][x] = 1;
                return 1;
            }
            continue;
        }
        const auto& l = g.at(ny);
        auto found = upper_bound(l.begin(), l.end(), x - 1);
        if(found == l.begin())
        {
            // 一番上まで行く
            if(ny == N)
            {
                cache[y][x] = 1;
                return 1;
            }
        }
        else
        {
            found--;
            auto nx = *found;
            if(test(N, ny, nx, g) == 1)
            {
                cache[y][x] = 1;
                return 1;
            }
        }
    }

    cache[y][x] = 0;
    return 0;
}

int main(int, const char**)
{
    int N, M;
    cin >> N >> M;

    map<int, set<int>> blacks;
    rep(i, M)
    {
        int X, Y;
        cin >> X >> Y;
        blacks[Y].insert(X);
    }

    P("bsize: %zu\n", blacks.size());

    int ans = 0;
    for(auto itr = blacks.begin(); itr != blacks.end(); ++itr)
    {
L;
        P("y: %d\n", itr->first);
        ans += test(N, itr->first, *(itr->second.rbegin()), blacks);
L;
    }

    // Y == N にポーン置かれてない場合、まっすぐ下に降りるルートを足す
    if(blacks[N].size() == 0) ans++;

    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int N = 4e5 + 7;
int n, m;

map<int,vector<int> >  plcs;
map<int,vector<int> >  yplcs ; 
int xs[N], ys[N];
map<pair<int,int> ,int> gid; 

map<int,int> dp ; 

set<int> ans ; 

void solve(int i)
{
       if(dp[i])return ; 
       dp[i] =1 ; 
       if(xs[i] == yplcs[ys[i]].back())ans.insert(i) ; 

       for(int j = -1 ; j <= 1; j += 2){
              if(ys[i] + j >= 0 && ys[i] + j <= n){
                     int ny = ys[i] + j; 
                     auto it = lower_bound(yplcs[ny].begin() , yplcs[ny].end() , xs[i] + 1) ; 
                     while(it != yplcs[ny].end()){
                            int nx = *it ; 
                            auto it2 = lower_bound(yplcs[ys[i]].begin() , yplcs[ys[i]].end() , xs[i] + 1) ; 
                            if(it2!=yplcs[ys[i]].end()){
                                   int m_y = *it2 ; 
                                   if(m_y < nx){
                                          break; 
                                   }
                            }
                            solve(gid[{nx,ny}]) ; 
                            it ++ ; 
                     }
              }
       }


}

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
       freopen("in.in", "r", stdin);
#endif
       cin >> n >> m;
       plcs[0].push_back(n);
       yplcs[n].push_back(0) ; 
       xs[0] = 0 , ys[0] = n;
       n = n * 2 + 1;

       for (int i = 1; i <= m; ++i)
       {
              int x, y;
              cin >> x >> y;
              xs[i] = x, ys[i] = y;
              plcs[x].push_back(y);
              yplcs[y].push_back(x); 
              gid[{x,y}] = i ; 
       }
       for(auto &u : plcs){
              sort(u.second.begin() , u.second.end()) ; 
       }
       for(auto &u : yplcs){
              sort(u.second.begin() , u.second.end()) ; 
       }
       

       solve(0) ; 
       cout << ans.size() ; 

       return 0;
}
