#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};


int main() {
    ll N,M;
    cin >>N>>M;
    vector<ll>m(N+5);

    rep(i,M){
        ll l,r;
        cin>>l>>r;
        m[l]++;
        m[r+1]--;
    }
    ll ans =0;
    rep(i,N+4){
        m[i+1]+=m[i];
        if(m[i+1]==M){
            ans++;
        }
    }
    cout << ans << endl;


    return 0;
    

}
#include <bits/stdc++.h>
#define DAU  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define PLEC exit(0);
using namespace std;
#define int long long
int dp[6006], n, m, x, y, res;
pair<int, int> p[3003];
int32_t main() {
    DAU
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> p[i].first >> p[i].second;
    sort(p + 1, p + n + 1);
    dp[0] = 1;
    for (int i = 1; i <= n; ++i) {
        tie(x, y) = p[i];
        for (int j = m - 1; j >= 0; --j)
            if (dp[j])
                dp[j + x] = max(dp[j + x], dp[j] + y);
    }
    for (int i = 1; i <= m + 3000; ++i)
        res = max(res, dp[i]);
    cout << res - 1;
    PLEC
}
