#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define ld long double
#define rep(i,n) for (ll i = 0;i<(n);++i)
#define all(v) v.begin(),v.end()

template <typename T>bool chmax(T &a, const T& b) {if (a < b) {a = b;return true;}return false;}
template <typename T>bool chmin(T &a, const T& b) {if (a > b) {a = b;return true;}return false;}
vector<long long> divisor(long long n){vector<long long> res;long long i = 1;while (i*i<=n){if(n%i==0){res.push_back(i);}i++;}if(res.size()==0)return res;for(long long i = res.size()-1-(res.back()*res.back()==n);i>=0;--i){res.push_back(n/res[i]);}return res;}
long long safe_mod(long long x,long long m){x%=m;if(x<0){x+=m;}return x;}
long long modpow(long long x,long long n,long long mod){long long ans=1;while(n>0){if(n&1){ans*=x;ans%=mod;}n>>=1;x*=x;x%=mod;}return ans;}
long long intpow(long long x,long long n){long long ans=1;while(n>0){if(n&1){ans*=x;}n>>=1;x*=x;}return ans;}
//template<typename T>T intpow(T x,T n){T ans=1;while(n>0){if(n&1){ans*=x;}n>>=1;x*=x;}return ans;}
vector<pair<long long,long long>> factor_lst(long long n){vector<pair<long long,long long>> factor_lst;long long d=2;while(d*d<=n){if(n%d==0){long long num=0;while(n%d==0){num+=1;n/=d;}factor_lst.push_back({d,num});}d+=1;}if(n!=1){factor_lst.push_back({n,1});}return factor_lst;}
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))


int msb(int x){return x==0 ? -1:32-__builtin_clz(x);}//1-indexed
int lsb(int x){return x==0 ? 32:__builtin_ctz(x)+1;}//1-indexed
int popcnt(int x){return __builtin_popcount(x);}
int popcnt(long long x){return __builtin_popcount(x);}

bool ingrid(int i,int j,int H,int W){
    return 0<=i&&i<H&&0<=j&&j<W;
}

const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
template<typename T> void print(vector<T> &v){for(int i=0;i<v.size();++i){if(i)cout<<" ";cout<<v[i];}cout<<endl;} 
template<typename T> void print(T* v,int size){for(int i=0;i<size;++i){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
template<typename T,typename S>void print(pair<T,S>&p){cout<<p.first<<" "<<p.second<<endl;}
const ll LINF=4*1e18;
const ll MINF=5*1e15;
const int INF=1e9+5;
const ld PI=acosl(-1);
const ld DINF=INF;

void Main();
int main(){cout<<fixed<<setprecision(15);Main();}

void Main(){

int N,M;cin>>N>>M;
vector<pair<int,int>> P(M);
vector<int> lstx,lsty;
lstx.push_back(0);
lsty.push_back(N);
lsty.push_back(N+1);
rep(i,M){
    cin>>P[i].first>>P[i].second;
    lstx.push_back(P[i].first);
    lsty.push_back(P[i].second);
    lsty.push_back(P[i].second+1);
}
Uniq(lstx);Uniq(lsty);
map<int,int> mx,my;
rep(i,lstx.size())mx[lstx[i]]=i;
rep(i,lsty.size())my[lsty[i]]=i;
vector<vector<int>> query(lstx.size());
rep(i,M)query[mx[P[i].first]].push_back(my[P[i].second]);
vector<bool> dp(lsty.size(),false);
dp[my[N]]=true;
rep(i,lstx.size()){
    vector<bool> lst(query[i].size());
    rep(j,query[i].size()){
        lst[j]=(query[i][j]>0&&dp[query[i][j]-1])||((query[i][j]<lsty.size()-1)&&dp[query[i][j]+1]);
    }
    rep(j,query[i].size()){
        dp[query[i][j]]=lst[j];
    }
}
int ans=0;
rep(i,lsty.size()-1)if(dp[i])ans+=lsty[i+1]-lsty[i];
cout<<ans<<endl;
}



/*
AtCoder Regular Contest 111 - A - Simple Math 2
https://atcoder.jp/contests/arc111/tasks/arc111_a
*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;

double EPS=1e-9;
int INF=1000000005;
long long INFF=1000000000000000005ll;
double PI=acos(-1);
int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 };
ll MOD = 1000000007;

// ll sum() { return 0; }
// template<typename T, typename... Args>
// T sum(T a, Args... args) { return a + sum(args...); }

#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << endl
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl

void solve() {
    // [10 ^ N / M] % M
    // = [10 ^ N - k * M ^ 2] % M
    // = [10 ^ N / M] % M
    ll n, m;
    cin >> n >> m;
    ll sq = m * m, ans = 1, pow = 10;
    while (n > 0) {
        if (n & 1) ans = (ans * pow) % sq;
        pow = (pow * pow) % sq;
        n >>= 1;
    }
    OUT((ans / m) % m);
}


int main()
{
    FAST_INP;
//    #ifndef ONLINE_JUDGE
//    freopen("input.txt","r", stdin);
//    freopen("output.txt","w", stdout);
//    #endif

    // int tc; cin >> tc;
    // TC(tc) solve();
    solve();
    return 0;
}