#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define repr(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define rep3r(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;

int ans = 0;
using ll = long long;
set<ll> visits;
map<int, vector<int>> lines;
ll xy2id(int x, int y) {return (ll)y*1001001001+x; }

void dfs(int sx, int sy) {
    ll id = xy2id(sx,sy);
    if (visits.find(id) != visits.end()) return;
    visits.insert(id);
    vector<int> &line = lines[sy];
    int wall = 2001001001;
    if (line[line.size()-1] == sx) ++ans;
    else {
        auto pos = upper_bound(ALL(line), sx);
        // cerr << "[lines]" << endl;
        // for (int v: line) { cerr << v << endl;}
        int idx = distance(line.begin(), pos);
        if (idx < line.size()) {
            wall = line[idx];
        }
        // cerr << "[w]" << sx << " " << sy << " " << wall << endl;
    }
    for (int y : {sy-1, sy+1}) {
        vector<int> &nline = lines[y];
        if (nline.size() == 0)  continue;
        auto pos = upper_bound(ALL(nline), sx);
        int idx = distance(nline.begin(), pos);
        if (idx >= nline.size()) continue;
        rep3(i, idx, nline.size()) {
            int nx = nline[i];
            // cerr << sx << " " << sy << " " << nx << " " << y << " " << i << endl;
            if (nx > wall) break;
            dfs(nx, y);
        }
    }

}

int main() {
    int n, m; cin >> n >> m;
    lines[n].push_back(0);
    // char grid[2*n+1][2*n+1];
    // rep(i,2*n+1) {
    //     rep(j,2*n+1) {
    //         grid[j][i] = '.';
    //     }
    // }
    // grid[n][0] = 'o';
    rep(i,m) {
        int x, y; cin >> x >> y;
        lines[y].push_back(x);
        // grid[y][x] = 'x';
    }
    // rep(i,2*n+1) {
    //     rep(j,2*n+1) {
    //         cerr << grid[i][j];
    //     }
    //     cerr << endl;
    // }
    for (auto &it: lines) {
        sort(ALL(it.second));
    }
    // for (auto it: lines) {
    //     cerr << it.first << ":";
    //     for (int v: it.second) cerr << v << " ";
    //     cerr << endl;
    // }
    dfs(0, n);
    cout << ans << endl;

    return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
set<string> Set;
LL getMin(string s){
    sort(s.begin(), s.end());
    LL ans = 0;
    for(int i = 0;i<s.size();++i)
    {
        ans = ans *10+s[i]-'0';

    }
    return ans;
}
LL getMax(string s){
    sort(s.begin(), s.end(), greater<LL>());
    LL ans = 0;
    for(int i = 0;i<s.size();++i)
    {
        ans = ans *10+s[i]-'0';

    }
    return ans;
}
string getString(LL d){
    string s="";
    if(d==0)
    {
        return "0";
    }
    while(d){
        s += d%10+'0';
        d/=10;
    }
    reverse(s.begin(),s.end());
    return s;
}
int main() {
    string s;
    LL d;
    cin >> d;
    s = getString(d);
    int n;
    cin >> n;

    LL b=d, a=0;
    while(n--){
         a = getMin(s);
         b = getMax(s);
        s = getString(b-a);
        //cout << a << " " << b << " " << s  << "\n";

    }
    cout <<s << "\n";
    return 0;
}
