#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<queue>
#include<deque>
#include<vector>
#include<map>
#include<iomanip>
#include<functional>
#include<cmath>
#include<time.h>
#include<list>
#include<set>
#include<sstream>
#include<stack>
using namespace std;
int n, q,ans,su,cur,rem,cnt,s1,s2;
int l[5005];
int r[5005];
int b[5005];
int a[5005];
int main()
{
	scanf("%d %d", &n, &q);
	for (int i = 0; i < q; i++)
	{
		scanf("%d %d", &l[i], &r[i]);
		b[l[i]]++;
		b[r[i] + 1]--;
	}
	su = 0;
	cur = 0;
	cnt = 0;
	for (int i = 1; i <= n; i++)
	{
		su += b[i];
		b[i] = su;
		if (b[i] == 0)
		{
			cur++;
		}
	}
	for (int i = 0; i < q - 1; i++)
	{
		rem = 0;
		cnt = 0;
		for (int j = l[i]; j <= r[i]; j++)
		{
			b[j]--;
			if (b[j] == 0)
			{
				rem++;
			}
		}
		for (int j = 1; j <= n; j++)
		{
			if (b[j] == 1)
			{
				a[cnt++] = j;
			}
		}
		for (int j = i + 1; j < q; j++)
		{
			s1 = lower_bound(a, a + cnt, l[j]) - a;
			s2 = lower_bound(a, a + cnt, r[j]) - a;
			if (b[r[j]] == 1)
				s2++;
			ans = max(ans, n - cur - rem - s2 + s1);
		}
		for (int j = l[i]; j <= r[i]; j++)
		{
			b[j]++;
		}
	}
	printf("%d\n", ans);
	return 0;
}


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

const int maxN = 5010;
int l[maxN], r[maxN], n, m;
int one[maxN], two[maxN], pre[maxN];

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		cin >> l[i] >> r[i];
		pre[l[i]]++, pre[r[i] + 1]--;
	}
	for (int i = 1; i <= n; i++)
		pre[i] = pre[i] + pre[i - 1];
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (pre[i] == 1) one[i] = 1;
		if (pre[i] == 2) two[i] = 1;
		if (pre[i] != 0) ans++;
	}
	for (int i = 1; i <= n; i++) {
		one[i] = one[i] + one[i - 1];
		two[i] = two[i] + two[i - 1];
	}
	int zero = INT_MAX;
	for (int i = 1; i <= m; i++)
	for (int j = 1; j <= m; j++) {
		if (i == j) continue;
		int cnt = one[r[i]] - one[l[i] - 1]
			+ one[r[j]] - one[l[j] - 1];
		int u = i, v = j;
		if (l[v] < l[u]) swap(u, v);
		if (r[u] < l[v]) {}
		else if (r[u] > r[v]) cnt += two[r[v]] - two[l[v] - 1];
		else cnt += two[r[u]] - two[l[v] - 1];
		zero = min(zero, cnt);
	}
	cout << ans - zero << endl;
 	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e4+5;
struct node{
	int l,r;
};
node a[N];
int vis[N];
int v1[N],v2[N];
int solve(int x,int y){
	if(a[x].l>a[y].l){
		swap(x,y);
	}
	if(a[x].r<a[y].l) return 0;
	if(a[x].r>a[y].r){
		return v2[a[y].r]-v2[a[y].l-1];
	}
	return v2[a[x].r]-v2[a[y].l-1];
}
int main(){
	int n,q;
	cin>>n>>q;
	for(int i=1;i<=q;i++){
		cin>>a[i].l>>a[i].r;
		for(int j=a[i].l;j<=a[i].r;j++){
			vis[j]++;
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(vis[i]>0) ans++;
		if(vis[i]==1) v1[i]=1;
		if(vis[i]==2) v2[i]=1;
		v1[i]+=v1[i-1];
		v2[i]+=v2[i-1];
	}
	int res=10000;
	for(int i=1;i<=q;i++){
		for(int j=1;j<=q;j++){
			if(i==j) continue;
			int tt=0;
			tt+=v1[a[i].r]-v1[a[i].l-1];
			tt+=v1[a[j].r]-v1[a[j].l-1];
			tt+=solve(i,j);
			res=min(res,tt);
		}
	}
	cout<<ans-res<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
struct pot{
    int l;
    int r;
}p[5005];
int c[5005],in[5005],sum[5005];
bool cmp(struct pot a,struct pot b){
    if(a.l!=b.l)return a.l<b.l;
    else return a.r>b.r;
}
int main(){
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=q;i++){
        scanf("%d%d",&p[i].l,&p[i].r);
        in[p[i].l]++;
        in[p[i].r+1]--;
    }
    for(int i=1;i<5005;i++)sum[i]=sum[i-1]+in[i];
    sort(p+1,p+1+q,cmp);
    int ans=0;
    c[1]=p[1].r-p[1].l+1;
    ans=c[1];

int maxx=p[1].r; for(int i=2;i<=q;i++){ c[i]=max(p[i].r-max(p[i].l,maxx+1)+1,0); ans+=c[i]; maxx=max(p[i].r,maxx); } sort(c+1,c+1+q); int tot=0; maxx=-1; for(int i=1;i<q;i++){ if(c[1]&&max(p[i].r-max(p[i].l,maxx+1)+1,0)==c[1]&&p[i+1].l<=p[i].r){ maxx=max(maxx,p[i].r); if(max(p[i+1].r-max(p[i+1].l,maxx+1)+1,0)!=c[2]||sum[p[i].r]>2) tot++; } else{ maxx=max(maxx,p[i].r); } } maxx=-1; tot=min(tot,2); int tot2=0; for(int i=1;i<q;i++){ if(c[2]&&max(p[i].r-max(p[i].l,maxx+1)+1,0)==c[2]&&p[i+1].l<=p[i].r){ maxx=max(maxx,p[i].r); if(max(p[i+1].r-max(p[i+1].l,maxx+1)+1,0)!=c[1]||sum[p[i].r]>2) tot2++; }

else{ maxx=max(maxx,p[i].r); } } tot2=min(1,tot2); //cout<<tot<<tot2<<endl; ans=ans+min(2,tot2+tot); cout<<ans-c[1]-c[2]<<endl; return 0; }


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define S second
#define F first
using namespace std;

int main(){
    int  n, q;
    cin >> n >> q;
    pair < int , int > painters[n];
    vector < vector < int > > fence(n + 1);
    for ( int i = 0 ; i < q ; ++i ) {
        cin >> painters[i].F >> painters[i].S ;
        for ( int j = painters[i].F ; j <= painters[i].S ; ++j ){
            fence[j].push_back(i);
        }
    }
    int cntr[q][q] = {};
    for ( int i = 1 ; i <= n ; ++i ){
        if ( fence[i].size() == 0 ) continue;
        else if ( fence[i].size() == 1 ){
            int x = fence[i][0];
            for ( int j = 0 ; j < q ; ++j )
                cntr[x][j]++, cntr[j][x]++;
        }else if ( fence[i].size() == 2 ){
            int x = fence[i][0], y = fence[i][1];
            cntr[x][y]++, cntr[y][x]++;
        }
    }
    int mn = INT_MAX;
    for ( int i = 0 ; i < q ; ++i )
        for( int j = i + 1; j < q ; ++j )
            mn = min(mn, cntr[i][j]);
    cout << n - mn << endl;

}


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define S second
#define F first
using namespace std;

int main(){
    int  n, q;
    cin >> n >> q;
    pair < int , int > painters[q];
    vector < vector < int > > fence(n + 1);
    for ( int i = 0 ; i < q ; ++i ) {
        cin >> painters[i].F >> painters[i].S ;
        for ( int j = painters[i].F ; j <= painters[i].S ; ++j ){
            fence[j].push_back(i);
        }
    }
//    for ( auto i : fence ){
//        for ( auto j : i )
//            cout << j << " ";
//        cout << endl;
//    }
    int cntr[q][q] = {};
    memset(cntr, 0, sizeof cntr);
    int cnt = 0;
    for ( int i = 1 ; i <= n ; ++i ){
        if ( fence[i].size() == 0 ) {cnt++;continue;}
        else if ( fence[i].size() == 1 ){
            int x = fence[i][0];
            for ( int j = 0 ; j < q ; ++j )
                cntr[x][j]++, cntr[j][x]++;
        }else if ( fence[i].size() == 2 ){
            int x = fence[i][0], y = fence[i][1];
            cntr[x][y]++, cntr[y][x]++;
        }
    }

    int mn = INT_MAX;
    for ( int i = 0 ; i < q ; ++i )
        for( int j = i + 1; j < q ; ++j )
            if ( i != j ) mn = min(mn, cntr[i][j]+cnt);
    cout << n - mn << endl;

}


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define S second
#define F first
using namespace std;

int main(){
    int  n, q;
    cin >> n >> q;
    pair < int , int > painters[q];
    vector < vector < int > > fence(n + 1);
    for ( int i = 0 ; i < q ; ++i ) {
        cin >> painters[i].F >> painters[i].S ;
        for ( int j = painters[i].F ; j <= painters[i].S ; ++j ){
            fence[j].push_back(i);
        }
    }
//    for ( auto i : fence ){
//        for ( auto j : i )
//            cout << j << " ";
//        cout << endl;
//    }
    int cntr[q][q] = {};
    for ( int i = 1 ; i <= n ; ++i ){
        if ( fence[i].size() == 0 ) {--n;continue;}
        else if ( fence[i].size() == 1 ){
            int x = fence[i][0];
            for ( int j = 0 ; j < q ; ++j )
                cntr[x][j]++, cntr[j][x]++;
        }else if ( fence[i].size() == 2 ){
            int x = fence[i][0], y = fence[i][1];
            cntr[x][y]++, cntr[y][x]++;
        }
    }
    int mn = INT_MAX;
    for ( int i = 0 ; i < q ; ++i )
        for( int j = i + 1; j < q ; ++j )
            mn = min(mn, cntr[i][j]);
    cout << n - mn << endl;

}


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define S second
#define F first
using namespace std;

int main(){
    int  n, q;
    cin >> n >> q;
    pair < int , int > painters[n];
    vector < vector < int > > fence(n + 1);
    for ( int i = 0 ; i < q ; ++i ) {
        cin >> painters[i].F >> painters[i].S ;
        for ( int j = painters[i].F ; j <= painters[i].S ; ++j ){
            fence[j].push_back(i);
        }
    }
//    for ( auto i : fence ){
//        for ( auto j : i )
//            cout << j << " ";
//        cout << endl;
//    }
    int cntr[q][q] = {};
    for ( int i = 1 ; i <= n ; ++i ){
        if ( fence[i].size() == 0 ) {--n;continue;}
        else if ( fence[i].size() == 1 ){
            int x = fence[i][0];
            for ( int j = 0 ; j < q ; ++j )
                cntr[x][j]++, cntr[j][x]++;
        }else if ( fence[i].size() == 2 ){
            int x = fence[i][0], y = fence[i][1];
            cntr[x][y]++, cntr[y][x]++;
        }
    }
    int mn = INT_MAX;
    for ( int i = 0 ; i < q ; ++i )
        for( int j = i + 1; j < q ; ++j )
            mn = min(mn, cntr[i][j]);
    cout << n - mn << endl;

}


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define S second
#define F first
using namespace std;

int main(){
    int  n, q;
    cin >> n >> q;
    pair < int , int > painters[q];
    vector < vector < int > > fence(n + 1);
    for ( int i = 0 ; i < q ; ++i ) {
        cin >> painters[i].F >> painters[i].S ;
        for ( int j = painters[i].F ; j <= painters[i].S ; ++j ){
            fence[j].push_back(i);
        }
    }
//    for ( auto i : fence ){
//        for ( auto j : i )
//            cout << j << " ";
//        cout << endl;
//    }
    int cntr[q][q] = {};
    for ( int i = 1 ; i <= n ; ++i ){
        if ( fence[i].size() == 0 ) {--n;continue;}
        else if ( fence[i].size() == 1 ){
            int x = fence[i][0];
            for ( int j = 0 ; j < q ; ++j )
                cntr[x][j]++, cntr[j][x]++;
        }else if ( fence[i].size() == 2 ){
            int x = fence[i][0], y = fence[i][1];
            cntr[x][y]++, cntr[y][x]++;
        }
    }
    int mn = INT_MAX;
    for ( int i = 0 ; i < q ; ++i )
        for( int j = i + 1; j < q ; ++j )
            if ( i != j ) mn = min(mn, cntr[i][j]);
    cout << n - mn << endl;

}


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define S second
#define F first
using namespace std;

int main(){
    int  n, q;
    cin >> n >> q;
    pair < int , int > painters[q];
    vector < vector < int > > fence(n + 1);
    for ( int i = 0 ; i < q ; ++i ) {
        cin >> painters[i].F >> painters[i].S ;
        for ( int j = painters[i].F ; j <= painters[i].S ; ++j ){
            fence[j].push_back(i);
        }
    }
//    for ( auto i : fence ){
//        for ( auto j : i )
//            cout << j << " ";
//        cout << endl;
//    }
    int cntr[q][q] = {};
    memset(cntr, 0, sizeof cntr);
    int cnt = 0;
    for ( int i = 1 ; i <= n ; ++i ){
        if ( fence[i].size() == 0 ) {cnt++;continue;}
        else if ( fence[i].size() == 1 ){
            int x = fence[i][0];
            for ( int j = 0 ; j < q ; ++j )
                cntr[x][j]++, cntr[j][x]++;
        }else if ( fence[i].size() == 2 ){
            int x = fence[i][0], y = fence[i][1];
            cntr[x][y]++, cntr[y][x]++;
        }
    }

    int mn = INT_MAX;
    for ( int i = 0 ; i < q ; ++i )
        for( int j = 0; j < q ; ++j )
            if ( i != j ) mn = min(mn, cntr[i][j]+cnt);
    cout << n - mn << endl;

}


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define S second
#define F first
using namespace std;

int main(){
    int  n, q;
    cin >> n >> q;
    pair < int , int > painters[q];
    vector < vector < int > > fence(n + 1);
    for ( int i = 0 ; i < q ; ++i ) {
        cin >> painters[i].F >> painters[i].S ;
        for ( int j = painters[i].F ; j <= painters[i].S ; ++j ){
            fence[j].push_back(i);
        }
    }
//    for ( auto i : fence ){
//        for ( auto j : i )
//            cout << j << " ";
//        cout << endl;
//    }
    int cntr[q][q] = {};
    memset(cntr, 0, sizeof cntr);
    for ( int i = 1 ; i <= n ; ++i ){
        if ( fence[i].size() == 0 ) {--n;continue;}
        else if ( fence[i].size() == 1 ){
            int x = fence[i][0];
            for ( int j = 0 ; j < q ; ++j )
                cntr[x][j]++, cntr[j][x]++;
        }else if ( fence[i].size() == 2 ){
            int x = fence[i][0], y = fence[i][1];
            cntr[x][y]++, cntr[y][x]++;
        }
    }
    int mn = INT_MAX;
    for ( int i = 0 ; i < q ; ++i )
        for( int j = i + 1; j < q ; ++j )
            if ( i != j ) mn = min(mn, cntr[i][j]);
    cout << n - mn << endl;

}


#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define S second
#define F first
using namespace std;

int main(){
    int  n, q;
    cin >> n >> q;
    pair < int , int > painters[n];
    vector < vector < int > > fence(n + 1);
    for ( int i = 0 ; i < q ; ++i ) {
        cin >> painters[i].F >> painters[i].S ;
        for ( int j = painters[i].F ; j <= painters[i].S ; ++j ){
            fence[j].push_back(i);
        }
    }
    int cntr[q][q] = {};
    for ( int i = 1 ; i <= n + 1 ; ++i ){
        if ( fence[i].size() == 0 ) continue;
        else if ( fence[i].size() == 1 ){
            int x = fence[i][0];
            for ( int j = 0 ; j < q ; ++j )
                cntr[x][j]++, cntr[j][x]++;
        }else if ( fence[i].size() == 2 ){
            int x = fence[i][0], y = fence[i][1];
            cntr[x][y]++, cntr[y][x]++;
        }
    }
    int mn = INT_MAX;
    for ( int i = 0 ; i < q ; ++i )
        for( int j = i + 1; j < q ; ++j )
            mn = min(mn, cntr[i][j]);
    cout << n - mn << endl;

}


#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define LL long long
//#define int ll//
#define pii pair<int,int>
using namespace std;
const int maxn = (ll) 5e3 + 5;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
int num[maxn];
pii a[maxn];
int cnt2[maxn], cnt1[maxn];

int query1(int l, int r) {
    return cnt1[r] - cnt1[l - 1];
}

int query2(int l, int r) {
    return cnt2[r] - cnt2[l - 1];
}

signed main() {
    start;
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; ++i) {
        cin >> a[i].first >> a[i].second;
        for (int j = a[i].first; j <= a[i].second; ++j)
            ++num[j];
    }
    sort(a + 1, a + 1 + q);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        cnt1[i] = cnt1[i - 1];
        cnt2[i] = cnt2[i - 1];
        if (num[i])
            ++ans;
        if (num[i] == 1)
            ++cnt1[i];
        if (num[i] == 2)
            ++cnt2[i];
    }
    int minn = inf;
    for (int i = 2; i <= q; ++i)
        for (int j = 1; j < i; ++j) {
            int tmp = 0;
            if (a[j].second >= a[i].first) {
                if (a[j].second <= a[i].second) {
                    tmp += query1(a[j].first, a[i].first - 1);
                    tmp += query2(a[i].first, a[j].second);
                    tmp += query1(a[j].second + 1, a[i].second);
                } else {
                    tmp += query1(a[j].first, a[i].first - 1);
                    tmp += query2(a[i].first, a[i].second);
                    tmp += query1(a[i].second + 1, a[j].second);
                }
            } else {
                tmp += query1(a[j].first, a[j].second);
                tmp += query1(a[i].first, a[i].second);
            }
            minn = min(minn, tmp);
        }
    cout << ans - minn;
    return 0;
}


#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define LL long long
#define int ll
#define pii pair<int,int>
using namespace std;
const int maxn = (ll) 5e3 + 5;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
int num[maxn];
pii a[maxn];
int cnt2[maxn], cnt1[maxn];

int query1(int l, int r) {
    return cnt1[r] - cnt1[l - 1];
}

int query2(int l, int r) {
    return cnt2[r] - cnt2[l - 1];
}

signed main() {
    start;
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; ++i) {
        cin >> a[i].first >> a[i].second;
        for (int j = a[i].first; j <= a[i].second; ++j)
            ++num[j];
    }
    sort(a + 1, a + 1 + q);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        cnt1[i] = cnt1[i - 1];
        cnt2[i] = cnt2[i - 1];
        if (num[i])
            ++ans;
        if (num[i] == 1)
            ++cnt1[i];
        if (num[i] == 2)
            ++cnt2[i];
    }
    int minn = inf;
    for (int i = 2; i <= q; ++i)
        for (int j = 1; j < i; ++j) {
            int tmp = 0;
            if (a[j].second >= a[i].first) {
                if (a[j].second <= a[i].second) {
                    tmp += query1(a[j].first, a[i].first - 1);
                    tmp += query2(a[i].first, a[j].second);
                    tmp += query1(a[j].second + 1, a[i].second);
                } else {
                    tmp += query1(a[j].first, a[i].first - 1);
                    tmp += query2(a[i].first, a[i].second);
                    tmp += query1(a[i].second + 1, a[j].second);
                }
            } else {
                tmp += query1(a[j].first, a[j].second);
                tmp += query1(a[i].first, a[i].second);
            }
            minn = min(minn, tmp);
        }
    cout << ans - minn;
    return 0;
}


#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define LL long long
#define int ll
#define pii pair<int,int>
using namespace std;
const int maxn = (ll) 5e3 + 5;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
int num[maxn];
pii a[maxn];
int cnt2[maxn], cnt1[maxn];

int query1(int l, int r) {
    return cnt1[r] - cnt1[l - 1];
}

int query2(int l, int r) {
    return cnt2[r] - cnt2[l - 1];
}

signed main() {
    start;
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; ++i) {
        cin >> a[i].first >> a[i].second;
        for (int j = a[i].first; j <= a[i].second; ++j)
            ++num[j];
    }
    sort(a + 1, a + 1 + q);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        cnt1[i] = cnt1[i - 1];
        cnt2[i] = cnt2[i - 1];
        if (num[i])
            ++ans;
        if (num[i] == 1)
            ++cnt1[i];
        if (num[i] == 2)
            ++cnt2[i];
    }
    int minn = inf;
    for (int i = 2; i <= q; ++i)
        for (int j = 1; j < i; ++j) {
            int tmp = 0;
            if (a[j].second >= a[i].first) {
                tmp += query1(a[j].first, a[i].first - 1);
                tmp += query2(a[i].first, a[j].second);
                tmp += query1(a[j].second + 1, a[i].second);
            } else {
                tmp += query1(a[j].first, a[j].second);
                tmp += query1(a[i].first, a[i].second);
            }
            minn = min(minn, tmp);
        }
    cout << ans - minn;
    return 0;
}


#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define LL long long
#define int ll
#define pii pair<int,int>
using namespace std;
const int maxn = (ll) 5e3 + 5;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
int num[maxn];
pii a[maxn];

signed main() {
    start;
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; ++i) {
        cin >> a[i].first >> a[i].second;
        for (int j = a[i].first; j <= a[i].second; ++j)
            ++num[j];
    }
    sort(a + 1, a + 1 + q);
    int ans = 0;
    for (int i = 1; i <= n; ++i)
        if (num[i])
            ++ans;
    int minn = inf;
    for (int i = 2; i <= q; ++i)
        for (int j = 1; j < i; ++j) {
            int tmp = 0;
            if (a[j].second >= a[i].first) {
                for (int k = a[j].first; k < a[i].first; ++k)
                    if (num[k] == 1)
                        ++tmp;
                for (int k = a[i].first; k <= a[j].second; ++k)
                    if (num[k] == 2)
                        ++tmp;
                for (int k = a[j].second + 1; k <= a[i].second; ++k)
                    if (num[k] == 1)
                        ++tmp;
            } else {
                for (int k = a[j].first; k <= a[j].second; ++k)
                    if (num[k] == 1)
                        ++tmp;
                for (int k = a[i].first; k <= a[i].second; ++k)
                    if (num[k] == 1)
                        ++tmp;
            }
            minn = min(minn, tmp);
        }
    cout << ans - minn;
    return 0;
}


#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define LL long long
//#define int ll
#define pii pair<int,int>
using namespace std;
const int maxn = (ll) 5e3 + 5;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
int num[maxn];
pii a[maxn];
int cnt2[maxn], cnt1[maxn];

int query1(int l, int r) {
    return cnt1[r] - cnt1[l - 1];
}

int query2(int l, int r) {
    return cnt2[r] - cnt2[l - 1];
}

signed main() {
    start;
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; ++i) {
        cin >> a[i].first >> a[i].second;
        for (int j = a[i].first; j <= a[i].second; ++j)
            ++num[j];
    }
    sort(a + 1, a + 1 + q);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        cnt1[i] = cnt1[i - 1];
        cnt2[i] = cnt2[i - 1];
        if (num[i])
            ++ans;
        if (num[i] == 1)
            ++cnt1[i];
        if (num[i] == 2)
            ++cnt2[i];
    }
    int minn = inf;
    for (int i = 2; i <= q; ++i)
        for (int j = 1; j < i; ++j) {
            int tmp = 0;
            if (a[j].second >= a[i].first) {
                if (a[j].second <= a[i].second) {
                    tmp += query1(a[j].first, a[i].first - 1);
                    tmp += query2(a[i].first, a[j].second);
                    tmp += query1(a[j].second + 1, a[i].second);
                } else {
                    tmp += query1(a[j].first, a[i].first - 1);
                    tmp += query2(a[i].first, a[i].second);
                    tmp += query1(a[i].second + 1, a[j].second);
                }
            } else {
                tmp += query1(a[j].first, a[j].second);
                tmp += query1(a[i].first, a[i].second);
            }
            minn = min(minn, tmp);
        }
    cout << ans - minn;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include<limits>
#include<set>
using namespace std;
int main()
{
	int n, q;
	cin >> n >> q;
	vector<pair<int, int>> Mal;
	vector<pair<int, int>> Max;
	set<int> Ans;
	int Loss[5000] = {0};
	int Ent[5001] = {0};
	int ans = 0;
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		for(int j = a; j <= b; j++)
		{
			if(Ent[j] == 0)
			{
				Loss[i]++;
				ans++;
				Ent[j] = 1;
			}
		}
	}
	sort(Loss, Loss + q);
	cout << ans - Loss[0] - Loss[1];
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 5043;

int p1[N];
int p2[N];
int p3[N];
int n, q;
int l[N], r[N];

int solve(int idx)
{
	memset(p1, 0, sizeof p1);
	for(int i = 0; i < q; i++)
	{
		if(i == idx) continue;
		p1[l[i]]++;
		p1[r[i]]--;
	}
	memset(p2, 0, sizeof p2);
	int c = 0;
	for(int i = 0; i < n; i++)
	{
		c += p1[i];
		p2[i] = c;
	}
	memset(p3, 0, sizeof p3);
	for(int i = 0; i < n; i++)
		p3[i + 1] = p3[i] + (p2[i] == 1 ? 1 : 0);
	int ans = -int(1e9);
	for(int i = 0; i < q; i++)
	{
		if(i == idx) continue;
		ans = max(ans, p3[l[i]] - p3[r[i]]);
	}
	for(int i = 0; i < n; i++)
		if(p2[i] > 0)
			ans++;
	return ans;
}

int main()
{
	cin >> n >> q;
	for(int i = 0; i < q; i++)
	{
		cin >> l[i] >> r[i];
		l[i]--;
	}
	int ans = 0;
	for(int i = 0; i < q; i++)
		ans = max(ans, solve(i));
	cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include<limits>
#include<set>
using namespace std;
int main()
{
	int n, q;
	cin >> n >> q;
	int Loss[5000] = {0};
	int Ent[5001] = {0};
	int ans = 0;
	vector<pair<int, int>> Mal;
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		Mal.push_back({a, b});
		for(int j = a; j <= b; j++)
		{
			if(Ent[j] == 0)
			{
				//Loss[i]++;
				ans++;
			}
			Ent[j]++;
		}
	}
	n = ans;
	ans = 0;
	for(int i = 0; i < q; i++)
	{
		int ans1 = n;
		for(int j = 0; j < q; j++)
		{
			if(j != i)
			{
				int begin = j;
				int end = i;
				if(Mal[i].first < Mal[j].first)
				{
					begin = i;
					end = j;
				}
				for(int k = Mal[begin].first; k <= Mal[end].second; k++)
				{
					if(k > Mal[begin].second && k < Mal[end].first)
						continue;
					if(Ent[k] == 1)
						ans1--;
					else if(Ent[k] == 2 && max(Mal[begin].first, Mal[end].first) <= k && k <= min(Mal[end].second, Mal[begin].second))
						ans1--;
				}
				ans = max(ans, ans1);
			}
		}
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include<limits>
#include<set>
using namespace std;
int main()
{
	int n, q;
	cin >> n >> q;
	vector<pair<int, int>> Mal;
	vector<pair<int, int>> Max;
	set<int> Ans;
	int Loss[5000] = {0};
	int Ent[5001] = {0};
	int ans = 0;
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		for(int j = a; j <= b; j++)
		{
			if(Ent[j] == 0)
			{
				Loss[i]++;
				ans++;
				Ent[j] = 1;
			}
		}
	}
	sort(Loss, Loss + n);
	cout << ans - Loss[0] - Loss[1];
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include<limits>
#include<set>
using namespace std;
int main()
{
	int n, q;
	cin >> n >> q;
	int Loss[5000] = {0};
	int Ent[5001] = {0};
	int num = 0;
	vector<pair<int, int>> Mal;
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		Mal.push_back({a, b});
		for(int j = a; j <= b; j++)
		{
			if(Ent[j] == 0)
				num++;
			Ent[j]++;
		}
	}
	int ans2 = 0;
	int ans = 0;
	for(int i = 0; i < q; i++)
	{
		int ans1 = num;
		for(int j = i + 1; j < q; j++)
		{
			if(j != i)
			{
				int flag = 0;
				int begin = j;
				int end = i;
				if(Mal[i].first < Mal[j].first)
				{
					begin = i;
					end = j;
				}
				if(Mal[begin].second < Mal[end].first)
					flag = 1;
				if(Mal[i].first <= Mal[j].first && Mal[i].second >= Mal[j].second)
				{
					begin = i;
					end = i;
					flag = 0;
				}
				else if(Mal[j].first <= Mal[i].first && Mal[j].second >= Mal[i].second)
				{
					begin = j;
					end = j;
					flag = 0;
				}
				for(int k = Mal[begin].first; k <= Mal[end].second; k++)
				{
					if(k > Mal[begin].second && k < Mal[end].first && flag)
						continue;
					if(Ent[k] == 1)
						ans1--;
					else if(Ent[k] == 2 && max(Mal[begin].first, Mal[end].first) <= k && k <= min(Mal[end].second, Mal[begin].second))
						ans1--;
				}
				//cout << Mal[begin].first <<"-"<<Mal[end].second << "-" << ans1 << endl;
				ans = max(ans, ans1);
			}
		}
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include<limits>
#include<set>
using namespace std;
int main()
{
	int n, q;
	cin >> n >> q;
	int Loss[5043] = {0};
	int Ent[5043] = {0};
	int num = 0;
	vector<pair<int, int>> Mal;
	for(int i = 0; i < q; i++)
	{
		int a, b;
		cin >> a >> b;
		Mal.push_back({a, b});
		for(int j = a; j <= b; j++)
		{
			if(Ent[j] == 0)
				num++;
			Ent[j]++;
		}
	}
	int ans2 = 0;
	int ans = 0;
	for(int i = 0; i < q; i++)
	{
		int ans1 = num;
		for(int j = i + 1; j < q; j++)
		{
			if(j != i)
			{
				int flag = 0;
				int begin = j;
				int end = i;
				if(Mal[i].first < Mal[j].first)
				{
					begin = i;
					end = j;
				}
				if(Mal[begin].second < Mal[end].first)
					flag = 1;
				if(Mal[i].first <= Mal[j].first && Mal[i].second >= Mal[j].second)
				{
					begin = i;
					end = i;
					flag = 0;
				}
				else if(Mal[j].first <= Mal[i].first && Mal[j].second >= Mal[i].second)
				{
					begin = j;
					end = j;
					flag = 0;
				}
				for(int k = Mal[begin].first; k <= Mal[end].second; k++)
				{
					if(k > Mal[begin].second && k < Mal[end].first && flag)
						continue;
					if(Ent[k] == 1)
						ans1--;
					else if(Ent[k] == 2 && max(Mal[begin].first, Mal[end].first) <= k && k <= min(Mal[end].second, Mal[begin].second))
						ans1--;
				}
				//cout << Mal[begin].first <<"-"<<Mal[end].second << "-" << ans1 << endl;
				ans = max(ans, ans1);
			}
		}
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int,int> a[N] ;
int c[N] ;
int main() {
	Yahia74();
	int n , q ;
	cin >> n>> q ;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd ;
		c[a[i].ft]++ , c[a[i].sd+1]-- ;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i-1] ;
	}
	int cnt = 0 , mn = OO , l = 0 , r = 0 , idx = 0 ,dis = 0 , mnDis = OO ;
	for (int i = 0; i < q; ++i) {
		cnt = 0 , dis = 0;
		for (int j = a[i].ft; j <= a[i].sd; ++j) {
			if (c[j] == 1) {
				++cnt ;
			}
		}
		dis = a[i].sd - a[i].ft + 1 ;
		if (mn > cnt ){
			mn = cnt ;
			l = a[i].ft ;
			r = a[i].sd ;
			mnDis = r - l + 1 ;
			idx = i ;
		}else if (mn == cnt  &&  dis < mnDis){
			mn = cnt ;
			l = a[i].ft ;
			r = a[i].sd ;
			mnDis = r - l + 1 ;
			idx = i ;
		}
	}
	for (int j = l; j <= r; ++j)
		c[j]-- ;
	cnt = 0 , mn = OO , l = 0 , r = 0 , dis = 0 , mnDis = OO ;
	for (int i = 0; i < q; ++i) {
		cnt = 0 ;
		if (i != idx){
			for (int j = a[i].ft; j <= a[i].sd; ++j) {
				if (c[j] == 1) {
					++cnt ;
				}
			}

			if (mn > cnt){
				mn = cnt ;
				l = a[i].ft ;
				r = a[i].sd ;
			}
		}
	}
	for (int j = l; j <= r; ++j)
		c[j]-- ;
	int ans = 0 ;
	for (int i = 1; i <= n; ++i) {
		if (!c[i]) ++ans ;
	}
	cout << n - ans ;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

int main() {
	Yahia74();
	pair<int, int> a[N];
	int c[N], nc[N];
	int n, q;
	cin >> n >> q;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd;
		c[a[i].ft]++, c[a[i].sd + 1]--;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i - 1];
	}
	int sum, mx = -1;
	for (int i = 0; i < q; i++) {
		clr(nc,0) ;
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]--;
		}
		sum = 0;
		for (int j = 1; j <= n; j++) {
			if (c[j] != 0) {
				sum++;
			}
			if (c[j] == 1) {
				nc[j] = 1 ;
			}
		}
		for (int j = 1; j <= n; ++j) {
			nc[j] += nc[j - 1];
		}
		for (int j = 0; j < q; j++) {
			if (j != i) {
				mx = max(mx, sum - nc[a[j].sd] + nc[a[j].ft - 1]);
			}
		}
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]++;
		}
	}
	cout << mx;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int, int> a[N];
int c[N], nc[N];
int main() {
	Yahia74();
	int n, q;
	cin >> n >> q;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd;
		c[a[i].ft]++, c[a[i].sd + 1]--;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i - 1];
	}
	int sum, mx = -1;
	for (int i = 0; i < q; i++) {
		clr(nc,0) ;
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]--;
		}
		sum = 0;
		for (int j = 1; j <= n; j++) {
			if (c[j] != 0) {
				sum++;
			}
		}
		for (int j = 0; j <= n; ++j) {
			nc[j] = nc[j - 1];
			if (c[j] == 1) {
				nc[j] = 1 ;
			}
		}
		for (int j = 0; j < q; j++) {
			if (j != i) {
				mx = max(mx, sum - nc[a[j].sd] + nc[a[j].ft - 1]);
			}
		}
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]++;
		}
	}
	cout << mx;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int, int> a[N];
int c[N], nc[N];
int main() {
	Yahia74();
	int n, q;
	cin >> n >> q;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd;
		c[a[i].ft]++, c[a[i].sd + 1]--;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i - 1];
	}
	int sum, mx = -1;
	for (int i = 0; i < q; i++) {
		clr(nc,0) ;
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]--;
		}
		sum = 0;
		for (int j = 1; j <= n; j++) {
			if (c[j] != 0) {
				sum++;
			}
		}
		for (int j = 1; j <= n; ++j) {
			nc[j] = nc[j - 1];
			if (c[j] == 1) {
							nc[j]++ ;
						}
		}
		for (int j = 0; j < q; j++) {
			if (j != i) {
				mx = max(mx, sum - nc[a[j].sd] + nc[a[j].ft - 1]);
			}
		}
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]++;
		}
	}
	cout << mx;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int,int> a[N] ;
int c[N] ;
int main() {
	Yahia74();
	int n , q ;
	cin >> n>> q ;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd ;
		c[a[i].ft]++ , c[a[i].sd+1]-- ;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i-1] ;
	}
	int cnt = 0 , mn = OO , l = 0 , r = 0 , idx = 0 , dis = OO;
	for (int i = 0; i < q; ++i) {
		cnt = 0 ;
		for (int j = a[i].ft; j <= a[i].sd; ++j) {
			if (c[j] == 1) {
				++cnt ;
			}
		}
		if (mn >= cnt && dis < a[i].sd - a[i].ft + 1){
			mn = cnt ;
			l = a[i].ft ;
			r = a[i].sd ;
			dis = r - l + 1 ;
			idx = i ;
		}
	}
	for (int j = l; j <= r; ++j)
		c[j]-- ;
	cnt = 0 , mn = OO , l = 0 , r = 0 ;
	for (int i = 0; i < q; ++i) {
		cnt = 0 ;
		if (i != idx){
			for (int j = a[i].ft; j <= a[i].sd; ++j) {
				if (c[j] == 1) {
					++cnt ;
				}
			}
			if (mn > cnt){
				mn = cnt ;
				l = a[i].ft ;
				r = a[i].sd ;
			}
		}
	}
	for (int j = l; j <= r; ++j)
		c[j]-- ;
	int ans = 0 ;
	for (int i = 1; i <= n; ++i) {
		if (!c[i]) ++ans ;
	}
	cout << n - ans ;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int,int> a[N] ;
int c[N] ;
int main() {
	Yahia74();
	int n , q ;
	cin >> n>> q ;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd ;
		c[a[i].ft]++ , c[a[i].sd+1]-- ;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i-1] ;
	}
	int cnt = 0 , mn = OO , l = 0 , r = 0 , idx = 0;
	for (int i = 0; i < q; ++i) {
		cnt = 0 ;
		for (int j = a[i].ft; j <= a[i].sd; ++j) {
			if (c[j] == 1) {
				++cnt ;
			}
		}
		if (mn > cnt){
			mn = cnt ;
			l = a[i].ft ;
			r = a[i].sd ;
			idx = i ;
		}
	}
	for (int j = l; j <= r; ++j)
		c[j]-- ;
	cnt = 0 , mn = OO , l = 0 , r = 0 ;
	for (int i = 0; i < q; ++i) {
		cnt = 0 ;
		if (i != idx){
			for (int j = a[i].ft; j <= a[i].sd; ++j) {
				if (c[j] == 1) {
					++cnt ;
				}
			}
			if (mn > cnt){
				mn = cnt ;
				l = a[i].ft ;
				r = a[i].sd ;
			}
		}
	}
	for (int j = l; j <= r; ++j)
		c[j]-- ;
	int ans = 0 ;
	for (int i = 1; i <= n; ++i) {
		if (!c[i]) ++ans ;
	}
	cout << n - ans ;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int, int> a[N];
int c[N], nc[N];
int main() {
	Yahia74();
	int n, q;
	cin >> n >> q;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd;
		c[a[i].ft]++, c[a[i].sd + 1]--;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i - 1];
	}
	int sum, mx = -1;
	for (int i = 0; i < q; i++) {
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]--;
		}
		sum = 0;
		for (int j = 1; j <= n; j++) {
			if (c[j] != 0) {
				sum++;
			}
			nc[j] = nc[j - 1];
			if (c[j] == 1) {
				nc[j]++;
			}
		}
		for (int j = 0; j < q; j++) {
			if (j != i) {
				mx = max(mx, sum - nc[a[j].sd] + nc[a[j].ft - 1]);
			}
		}
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]++;
		}
	}
	cout << mx;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int, int> a[N];
int c[N], nc[N];
int main() {
	Yahia74();
	int n, q;
	cin >> n >> q;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd;
		c[a[i].ft]++, c[a[i].sd + 1]--;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i - 1];
	}
	int sum, mx = -1;
	for (int i = 0; i < q; i++) {
		clr(nc,0) ;
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]--;
		}
		sum = 0;
		for (int j = 1; j <= n; j++) {
			if (c[j] != 0) {
				sum++;
			}else if (c[j] == 1) {
				nc[j] = 1 ;
			}
		}
		for (int j = 0; j <= n; ++j) {
			nc[j] = nc[j - 1];
		}
		for (int j = 0; j < q; j++) {
			if (j != i) {
				mx = max(mx, sum - nc[a[j].sd] + nc[a[j].ft - 1]);
			}
		}
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]++;
		}
	}
	cout << mx;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int, int> a[N];
int c[N], nc[N];
int main() {
	Yahia74();
	int n, q;
	cin >> n >> q;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd;
		c[a[i].ft]++, c[a[i].sd + 1]--;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i - 1];
	}
	int sum, mx = -1;
	for (int i = 0; i < q; i++) {
		clr(nc,0) ;
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]--;
		}
		sum = 0;
		for (int j = 1; j <= n; j++) {
			if (c[j] != 0) {
				sum++;
			}else if (c[j] == 1) {
				nc[j]++ ;
			}
		}
		for (int j = 1; j <= n; ++j) {
			nc[j] = nc[j - 1];
		}
		for (int j = 0; j < q; j++) {
			if (j != i) {
				mx = max(mx, sum - nc[a[j].sd] + nc[a[j].ft - 1]);
			}
		}
		for (int j = a[i].ft; j <= a[i].sd; j++) {
			c[j]++;
		}
	}
	cout << mx;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define el      	'\n'
#define ft 			first
#define sd 			second
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define clr(a,b)	memset(a,b,sizeof(a))
typedef long long ll;
void Yahia74() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 5e3 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f;

pair<int,int> a[N] ;
int c[N] ;
int main() {
	Yahia74();
	int n , q ;
	cin >> n>> q ;
	for (int i = 0; i < q; ++i) {
		cin >> a[i].ft >> a[i].sd ;
		c[a[i].ft]++ , c[a[i].sd+1]-- ;
	}
	for (int i = 1; i <= n; ++i) {
		c[i] += c[i-1] ;
	}
	int cnt = 0 , mn = OO , l = 0 , r = 0 , idx = 0 ,dis = 0 , mnDis = OO ;
	for (int i = 0; i < q; ++i) {
		cnt = 0 , dis = 0;
		for (int j = a[i].ft; j <= a[i].sd; ++j) {
			if (c[j] == 1) {
				++cnt ;
			}
		}
		dis = a[i].sd - a[i].ft + 1 ;
		if (mn >= cnt &&  dis < mnDis ){
			mn = cnt ;
			l = a[i].ft ;
			r = a[i].sd ;
			mnDis = r - l + 1 ;
			idx = i ;
		}
	}
	for (int j = l; j <= r; ++j)
		c[j]-- ;
	cnt = 0 , mn = OO , l = 0 , r = 0 ;
	for (int i = 0; i < q; ++i) {
		cnt = 0 ;
		if (i != idx){
			for (int j = a[i].ft; j <= a[i].sd; ++j) {
				if (c[j] == 1) {
					++cnt ;
				}
			}
			if (mn > cnt){
				mn = cnt ;
				l = a[i].ft ;
				r = a[i].sd ;
			}
		}
	}
	for (int j = l; j <= r; ++j)
		c[j]-- ;
	int ans = 0 ;
	for (int i = 1; i <= n; ++i) {
		if (!c[i]) ++ans ;
	}
	cout << n - ans ;
	return 0;
}


#include<bits/stdc++.h>
#define x1 sfdga1
#define x2 sfdga2
#define y1 sfdga3
#define y2 sfdga4
#define x0 sfdga5
#define y0 sfdga6
using namespace std;
template<typename base_type, base_type _MOD>
struct IntMod {
	static const int INVERSE_CACHE_SIZE = (1 << 20);
	static base_type MOD;
	static void set_mod(base_type new_mod) {
		MOD = new_mod;
	}
	base_type n;
	IntMod(long long d = 0) {
		n = (d >= 0 ? d % MOD : (d % MOD + MOD) % MOD);
	}
	virtual ~IntMod() = default;
	IntMod operator-() const {
		return build(n == 0 ? 0 : MOD - n);
	}
	IntMod& operator+=(IntMod a) {
		n = (n >= MOD - a.n ? n - MOD + a.n : n + a.n);
		return *this;
	}
	IntMod& operator-=(IntMod a) {
		n = (n >= a.n) ? n - a.n : n - a.n + MOD;
		return *this;
	}
	IntMod& operator*=(IntMod a) {
		*this = *this * a;
		return *this;
	}
	IntMod& operator/=(IntMod a) {
		*this = *this / a;
		return *this;
	}
	static IntMod build(base_type n) {
		IntMod L;
		L.n = n;
		return L;
	}
	static base_type inverse_cache[INVERSE_CACHE_SIZE];
	static bool inverse_cache_ready;
	friend IntMod inverse(IntMod n) {
		return build(inverse_internal(n.n));
	}
	static base_type inverse_internal(base_type n) {
		if (!inverse_cache_ready) {
			inverse_cache_ready=true;
			inverse_cache[0] = 0;
			inverse_cache[1] = 1;
			for (int n = 2; n < INVERSE_CACHE_SIZE; ++n) inverse_cache[n] = (MOD - (base_type)((long long)inverse_cache[MOD % n] * (MOD / n) % MOD));
		}
		return n < INVERSE_CACHE_SIZE ? inverse_cache[n] : MOD - (base_type)((long long)inverse_internal(MOD % n) * (MOD / n) % MOD);
	}
	friend bool operator==(IntMod a, IntMod b) {
		return a.n == b.n;
	}
	friend bool operator!=(IntMod a, IntMod b) {
		return a.n != b.n;
	}
	friend IntMod operator+(IntMod a, IntMod b) {
		return build(a.n >= MOD - b.n ? a.n - MOD + b.n : a.n + b.n);
	}
	friend IntMod operator-(IntMod a, IntMod b) {
		return build(a.n >= b.n ? a.n - b.n : a.n - b.n + MOD);
	}
	friend IntMod operator*(IntMod a, IntMod b) {
		return build(static_cast<base_type>(static_cast<long long>(a.n) * b.n % MOD));
	}
	friend IntMod operator/(IntMod a, IntMod b) {
		return a * inverse(b);
	}
	friend IntMod pow(IntMod p, long long e) {
		if (e <= 0) return IntMod(1);
		IntMod L = IntMod(1);
		while(e) {
			if(e&1)L*=p;
			p*=p;
			e>>=1;
		}
		return L;
	}
	friend ostream&operator<<(ostream&os,const IntMod&p) {
		os<<p.n;
		return os;
	}
	friend istream&operator>>(istream&is,IntMod&p) {
		is>>p.n;
		return is;
	}
};
template<typename base_type, base_type _MOD> base_type IntMod<base_type, _MOD>::inverse_cache[INVERSE_CACHE_SIZE];
template<typename base_type, base_type _MOD> bool IntMod<base_type, _MOD>::inverse_cache_ready;
template<typename base_type, base_type _MOD> base_type IntMod<base_type, _MOD>::MOD = _MOD;
template<typename T,typename U>ostream&operator<<(ostream&os,const pair<T,U>&p) {
	os<<p.first<<" "<<p.second<<endl;
	return os;
}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&vec) {
	for(const auto &v:vec)os<<v<<' ';
	return os;
}
template<typename T>ostream&operator<<(ostream&os,const set<T>&vec) {
	for(const auto &v:vec)os<<v<<' ';
	return os;
}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&vec) {
	for(const auto &v:vec)os<<v<<' ';
	return os;
}
long long SQRT(long long x) {
	long long ret=sqrt(x)+5;
	while(1) {
		if(ret*ret<=x)break;
		ret--;
	}
	return ret;
}
#ifdef RINNE
const int maxn=123+555;
#else
const int maxn=3e5+555;
#endif
const long long MOD=1e9+7;
const int seed=233;
using Int=IntMod<int,MOD>;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<int>uni(1,maxn);
int len;
char x[666];
int dp[666][666];
int dfs(int l, int r) {
	if (dp[l][r] != -1) return dp[l][r];
	if (l == r) return 1;
	if (l > r) return 0;
	int ret = 1 + dfs(l + 1, r);
	int ll=-1,rr=-1;
	vector<pair<int,int> >V;
	for (int i = l + 1; i <= r; i++) {
		if (x[l] != x[i]) {
			if(ll==-1)ll=i;
			rr=i;
		} else if(ll!=-1)	{
			V.push_back(make_pair(ll,rr));
			ll=rr=-1;
			i--;
		}
	}
	if(ll!=-1)V.push_back(make_pair(ll,rr));
	int sum=0;
	for(auto i:V)sum+=dfs(i.first,i.second);
	ret=min(ret,sum+1);
	return dp[l][r] = ret;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>len>>(x+1);
	memset(dp,-1,sizeof(dp));
	cout<<dfs(1,len)<<endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <bitset>

using namespace std;

#define FSIO  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define DEBUG(a)   cout<<"DEBUG: "<<(a)<<endl;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define MP make_pair
#define X  first
#define Y  second
#define REP(i,st,ed)    for(int i=st;i<=ed;++i)
#define IREP(i,st,ed)   for(int i=st;i>=ed;--i)
#define TCASE(T)    cin>>T;while(T--)

const int MAXN = 5005;
const ll MOD = 1e9+7 ;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 1e9+7;
const double PI = acos(-1.0);
const double EPS = 1e-8;

int _;

using namespace std;

int tree[20][MAXN],sorted[MAXN],toleft[20][MAXN];
void Build(int l,int r,int dep)
{
    if(l==r) return;
    int mid=(l+r)>>1;
    int same=mid-l+1;
    for(int i=l;i<=r;i++)
        if(tree[dep][i]<sorted[mid]) same--;
    int lpos=l,rpos=mid+1;
    for(int i=l;i<=r;i++)
    {
        if(tree[dep][i]<sorted[mid])
            tree[dep+1][lpos++]=tree[dep][i];
        else if(tree[dep][i]==sorted[mid]&&same>0)
        {
            tree[dep+1][lpos++]=tree[dep][i];
            same--;
        }
        else tree[dep+1][rpos++]=tree[dep][i];
        toleft[dep][i]=toleft[dep][l-1]+lpos-l;
    }
    Build(l,mid,dep+1);
    Build(mid+1,r,dep+1);
}
int query(int L,int R,int l,int r,int dep,int k)
{
    if(l==r)
    {
        if(tree[dep][l]<=k) return 1;
        else return 0;
    }
    int mid=(L+R)>>1;
    int cnt=toleft[dep][r]-toleft[dep][l-1];
    if(sorted[mid]>k)
    {
        int newl=L+toleft[dep][l-1]-toleft[dep][L-1];
        int newr=newl+cnt-1;
        if(newr>=newl) query(L,mid,newl,newr,dep+1,k);
        else return 0;
    }
    else
    {
        int newr=r+toleft[dep][R]-toleft[dep][r];
        int newl=newr-(r-l-cnt);
        return cnt+query(mid+1,R,newl,newr,dep+1,k);
    }
}

pair<int,int> section[MAXN];

int main()
{
    //freopen("test.in","r+",stdin);
    //freopen("test.out","w+",stdout);
    FSIO;
    int N, M;
    while(cin>>N>>M)
    {
        memset(tree,0,sizeof(tree));
        memset(toleft,0,sizeof(toleft));
        int tot = 0;
        REP(i,1,M)
        {
            cin>>section[i].X>>section[i].Y;
            REP(j,section[i].X,section[i].Y)
            {
                if(tree[0][j]==0)   tot++;
                tree[0][j]++;
            }
        }
        REP(i,1,N)  sorted[i] = tree[0][i];

        sort(sorted+1,sorted+1+N);
        Build(1,N,0);
        int maxn = 0;
        sort(section+1,section+1+M);
        for(int i=1;i<=M;++i)
        {
            for(int j=i+1;j<=M;++j)
            {
                int to_decrease = 0;

                if(section[j].X>section[i].Y)
                {
                    to_decrease = query(1,N,section[i].X,section[i].Y,0,1)+
                                    query(1,N,section[j].X,section[j].Y,0,1);
                }
                else
                {
                    if(section[j].X-1>=section[i].X)    to_decrease+=query(1,N,section[i].X,section[j].X-1,0,1);
                    to_decrease+=query(1,N,section[j].X,min(section[i].Y,section[j].Y),0,2);
                    if(min(section[i].Y,section[j].Y)+1<=max(section[i].Y,section[j].Y))
                        to_decrease += query(1,N,min(section[i].Y,section[j].Y)+1,max(section[i].Y,section[j].Y),0,1);
                }

                maxn = max(maxn,tot-to_decrease);
            }
        }
        cout<<maxn<<endl;
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <bitset>

using namespace std;

#define FSIO  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define DEBUG(a)   cout<<"DEBUG: "<<(a)<<endl;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define MP make_pair
#define X  first
#define Y  second
#define REP(i,st,ed)    for(int i=st;i<=ed;++i)
#define IREP(i,st,ed)   for(int i=st;i>=ed;--i)
#define TCASE(T)    cin>>T;while(T--)

const int MAXN = 5005;
const ll MOD = 1e9+7 ;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 1e9+7;
const double PI = acos(-1.0);
const double EPS = 1e-8;

int _;

using namespace std;

int tree[20][MAXN],sorted[MAXN],toleft[20][MAXN];
void Build(int l,int r,int dep)
{
    if(l==r) return;
    int mid=(l+r)>>1;
    int same=mid-l+1;
    for(int i=l;i<=r;i++)
        if(tree[dep][i]<sorted[mid]) same--;
    int lpos=l,rpos=mid+1;
    for(int i=l;i<=r;i++)
    {
        if(tree[dep][i]<sorted[mid])
            tree[dep+1][lpos++]=tree[dep][i];
        else if(tree[dep][i]==sorted[mid]&&same>0)
        {
            tree[dep+1][lpos++]=tree[dep][i];
            same--;
        }
        else tree[dep+1][rpos++]=tree[dep][i];
        toleft[dep][i]=toleft[dep][l-1]+lpos-l;
    }
    Build(l,mid,dep+1);
    Build(mid+1,r,dep+1);
}
int query(int L,int R,int l,int r,int dep,int k)
{
    if(l==r)
    {
        if(tree[dep][l]<=k) return 1;
        else return 0;
    }
    int mid=(L+R)>>1;
    int cnt=toleft[dep][r]-toleft[dep][l-1];
    if(sorted[mid]>k)
    {
        int newl=L+toleft[dep][l-1]-toleft[dep][L-1];
        int newr=newl+cnt-1;
        if(newr>=newl) query(L,mid,newl,newr,dep+1,k);
        else return 0;
    }
    else
    {
        int newr=r+toleft[dep][R]-toleft[dep][r];
        int newl=newr-(r-l-cnt);
        return cnt+query(mid+1,R,newl,newr,dep+1,k);
    }
}

pair<int,int> section[MAXN];

int main()
{
    //freopen("test.in","r+",stdin);
    //freopen("test.out","w+",stdout);
    FSIO;
    int N, M;
    while(cin>>N>>M)
    {
        memset(tree,0,sizeof(tree));
        memset(toleft,0,sizeof(toleft));
        int tot = 0;
        REP(i,1,M)
        {
            cin>>section[i].X>>section[i].Y;
            REP(j,section[i].X,section[i].Y)
            {
                if(tree[0][j]==0)   tot++;
                tree[0][j]++;
            }
        }
        REP(i,1,N)  sorted[i] = tree[0][i];
        sort(sorted+1,sorted+1+N);
        Build(1,N,0);
        int maxn = 0;
        sort(section+1,section+1+N);
        for(int i=1;i<=M;++i)
        {
            for(int j=i+1;j<=M;++j)
            {
                int to_decrease = query(1,N,section[i].X,min(section[i].Y,section[j].X),0,1);
                if(section[i].Y>=section[j].X)  to_decrease += query(1,N,section[j].X,section[i].Y,0,2);
                to_decrease += query(1,N,max(section[i].Y,section[j].X),section[j].Y,0,1);
                maxn = max(maxn,tot-to_decrease);
            }
        }
        cout<<maxn<<endl;
    }
    return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <bitset>

using namespace std;

#define FSIO  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define DEBUG(a)   cout<<"DEBUG: "<<(a)<<endl;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define MP make_pair
#define X  first
#define Y  second
#define REP(i,st,ed)    for(int i=st;i<=ed;++i)
#define IREP(i,st,ed)   for(int i=st;i>=ed;--i)
#define TCASE(T)    cin>>T;while(T--)

const int MAXN = 50005;
const ll MOD = 1e9+7 ;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 1e9+7;
const double PI = acos(-1.0);
const double EPS = 1e-8;

int _;

using namespace std;

int tree[20][MAXN],sorted[MAXN],toleft[20][MAXN];
void Build(int l,int r,int dep)
{
    if(l==r) return;
    int mid=(l+r)>>1;
    int same=mid-l+1;
    for(int i=l;i<=r;i++)
        if(tree[dep][i]<sorted[mid]) same--;
    int lpos=l,rpos=mid+1;
    for(int i=l;i<=r;i++)
    {
        if(tree[dep][i]<sorted[mid])
            tree[dep+1][lpos++]=tree[dep][i];
        else if(tree[dep][i]==sorted[mid]&&same>0)
        {
            tree[dep+1][lpos++]=tree[dep][i];
            same--;
        }
        else tree[dep+1][rpos++]=tree[dep][i];
        toleft[dep][i]=toleft[dep][l-1]+lpos-l;
    }
    Build(l,mid,dep+1);
    Build(mid+1,r,dep+1);
}
int query(int L,int R,int l,int r,int dep,int k)
{
    if(l>r) return 0;
    if(l==r)
    {
        if(tree[dep][l]<=k) return 1;
        else return 0;
    }
    int mid=(L+R)>>1;
    int cnt=toleft[dep][r]-toleft[dep][l-1];
    if(sorted[mid]>k)
    {
        int newl=L+toleft[dep][l-1]-toleft[dep][L-1];
        int newr=newl+cnt-1;
        if(newr>=newl) query(L,mid,newl,newr,dep+1,k);
        else return 0;
    }
    else
    {
        int newr=r+toleft[dep][R]-toleft[dep][r];
        int newl=newr-(r-l-cnt);
        return cnt+query(mid+1,R,newl,newr,dep+1,k);
    }
}

pair<int,int> section[MAXN];

int main()
{
    //freopen("test.in","r+",stdin);
    //freopen("test.out","w+",stdout);
    FSIO;
    int N, M;
    while(cin>>N>>M)
    {
        memset(tree,0,sizeof(tree));
        memset(toleft,0,sizeof(toleft));
        int tot = 0;
        REP(i,1,M)
        {
            cin>>section[i].X>>section[i].Y;
            REP(j,section[i].X,section[i].Y)
            {
                if(tree[0][j]==0)   tot++;
                tree[0][j]++;
            }
        }
        REP(i,1,N)  sorted[i] = tree[0][i];

        sort(sorted+1,sorted+1+N);
        Build(1,N,0);
        int maxn = 0;
        sort(section+1,section+1+M);
        for(int i=1;i<=M;++i)
        {
            for(int j=i+1;j<=M;++j)
            {
                int to_decrease = 0;

                if(section[j].X>section[i].Y)
                {
                    to_decrease = query(1,N,section[i].X,section[i].Y,0,1)+
                                    query(1,N,section[j].X,section[j].Y,0,1);
                }
                else
                {
                    if(section[j].X-1>=section[i].X)    to_decrease+=query(1,N,section[i].X,section[j].X-1,0,1);
                    to_decrease+=query(1,N,section[j].X,min(section[i].Y,section[j].Y),0,2);
                    if(min(section[i].Y,section[j].Y)+1<=max(section[i].Y,section[j].Y))
                        to_decrease += query(1,N,min(section[i].Y,section[j].Y)+1,max(section[i].Y,section[j].Y),0,1);
                }

                maxn = max(maxn,tot-to_decrease);
            }
        }
        cout<<maxn<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define x1 sfdga1
#define x2 sfdga2
#define y1 sfdga3
#define y2 sfdga4
#define x0 sfdga5
#define y0 sfdga6
using namespace std;
template<typename base_type, base_type _MOD>
struct IntMod {
	static const int INVERSE_CACHE_SIZE = (1 << 20);
	static base_type MOD;
	static void set_mod(base_type new_mod) {
		MOD = new_mod;
	}
	base_type n;
	IntMod(long long d = 0) {
		n = (d >= 0 ? d % MOD : (d % MOD + MOD) % MOD);
	}
	virtual ~IntMod() = default;
	IntMod operator-() const {
		return build(n == 0 ? 0 : MOD - n);
	}
	IntMod& operator+=(IntMod a) {
		n = (n >= MOD - a.n ? n - MOD + a.n : n + a.n);
		return *this;
	}
	IntMod& operator-=(IntMod a) {
		n = (n >= a.n) ? n - a.n : n - a.n + MOD;
		return *this;
	}
	IntMod& operator*=(IntMod a) {
		*this = *this * a;
		return *this;
	}
	IntMod& operator/=(IntMod a) {
		*this = *this / a;
		return *this;
	}
	static IntMod build(base_type n) {
		IntMod L;
		L.n = n;
		return L;
	}
	static base_type inverse_cache[INVERSE_CACHE_SIZE];
	static bool inverse_cache_ready;
	friend IntMod inverse(IntMod n) {
		return build(inverse_internal(n.n));
	}
	static base_type inverse_internal(base_type n) {
		if (!inverse_cache_ready) {
			inverse_cache_ready=true;
			inverse_cache[0] = 0;
			inverse_cache[1] = 1;
			for (int n = 2; n < INVERSE_CACHE_SIZE; ++n) inverse_cache[n] = (MOD - (base_type)((long long)inverse_cache[MOD % n] * (MOD / n) % MOD));
		}
		return n < INVERSE_CACHE_SIZE ? inverse_cache[n] : MOD - (base_type)((long long)inverse_internal(MOD % n) * (MOD / n) % MOD);
	}
	friend bool operator==(IntMod a, IntMod b) {
		return a.n == b.n;
	}
	friend bool operator!=(IntMod a, IntMod b) {
		return a.n != b.n;
	}
	friend IntMod operator+(IntMod a, IntMod b) {
		return build(a.n >= MOD - b.n ? a.n - MOD + b.n : a.n + b.n);
	}
	friend IntMod operator-(IntMod a, IntMod b) {
		return build(a.n >= b.n ? a.n - b.n : a.n - b.n + MOD);
	}
	friend IntMod operator*(IntMod a, IntMod b) {
		return build(static_cast<base_type>(static_cast<long long>(a.n) * b.n % MOD));
	}
	friend IntMod operator/(IntMod a, IntMod b) {
		return a * inverse(b);
	}
	friend IntMod pow(IntMod p, long long e) {
		if (e <= 0) return IntMod(1);
		IntMod L = IntMod(1);
		while(e) {
			if(e&1)L*=p;
			p*=p;
			e>>=1;
		}
		return L;
	}
	friend ostream&operator<<(ostream&os,const IntMod&p) {
		os<<p.n;
		return os;
	}
	friend istream&operator>>(istream&is,IntMod&p) {
		is>>p.n;
		return is;
	}
};
template<typename base_type, base_type _MOD> base_type IntMod<base_type, _MOD>::inverse_cache[INVERSE_CACHE_SIZE];
template<typename base_type, base_type _MOD> bool IntMod<base_type, _MOD>::inverse_cache_ready;
template<typename base_type, base_type _MOD> base_type IntMod<base_type, _MOD>::MOD = _MOD;
template<typename T,typename U>ostream&operator<<(ostream&os,const pair<T,U>&p) {
	os<<p.first<<" "<<p.second<<endl;
	return os;
}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&vec) {
	for(const auto &v:vec)os<<v<<' ';
	return os;
}
template<typename T>ostream&operator<<(ostream&os,const set<T>&vec) {
	for(const auto &v:vec)os<<v<<' ';
	return os;
}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&vec) {
	for(const auto &v:vec)os<<v<<' ';
	return os;
}
long long SQRT(long long x) {
	long long ret=sqrt(x)+5;
	while(1) {
		if(ret*ret<=x)break;
		ret--;
	}
	return ret;
}
#ifdef RINNE
const int maxn=123+555;
#else
const int maxn=3e5+555;
#endif
const long long MOD=1e9+7;
const int seed=233;
using Int=IntMod<int,MOD>;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<int>uni(1,maxn);
int n,q;
vector<pair<int,int> >V;
int a,b;
int dp[6666];
int sum1[6666];
int sum2[6666];
int sum3[6666];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>q;
	for(int i=0; i<q; i++) {
		cin>>a>>b;
		dp[a]++;
		dp[b+1]--;
		V.push_back(make_pair(a,b));
	}
	int c=0;
	for(int i=1; i<=5000; i++) {
		c+=dp[i];
		sum1[i]=sum1[i-1]+(c>=1);
		sum2[i]=sum2[i-1]+(c>=2);
		sum3[i]=sum3[i-1]+(c>=3);
	}
	sort(V.begin(),V.end());
	int ans=0;
	for(int i=0; i<(int)V.size(); i++) {
		for(int j=i+1; j<(int)V.size(); j++) {
			int res=0;
			if(V[i].second<V[j].first)
				res=sum1[V[i].first-1]+
				    sum2[V[i].second]-sum2[V[i].first-1]+
				    sum1[V[j].first-1]-sum1[V[i].second]+
				    sum2[V[j].second]-sum2[V[j].first-1]+
				    sum1[5000]-sum1[V[j].second];
			else {
				int ll=V[i].first;
				int rr=V[j].first-1;
				int ll2=V[j].first;
				int rr2=V[i].second;
				int ll3=V[i].second+1;
				int rr3=V[j].second;
				res=
				sum1[ll-1]+
				sum2[rr]-sum2[ll-1]+
				sum3[rr2]-sum3[ll2-1]+
				sum2[rr3]-sum2[ll3-1]+
				sum1[5000]-sum1[rr3];
			}
			ans=max(ans,res);
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <bitset>

using namespace std;

#define FSIO  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define DEBUG(a)   cout<<"DEBUG: "<<(a)<<endl;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define MP make_pair
#define X  first
#define Y  second
#define REP(i,st,ed)    for(int i=st;i<=ed;++i)
#define IREP(i,st,ed)   for(int i=st;i>=ed;--i)
#define TCASE(T)    cin>>T;while(T--)

const int MAXN = 50005;
const ll MOD = 1e9+7 ;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 1e9+7;
const double PI = acos(-1.0);
const double EPS = 1e-8;

int _;

using namespace std;

int tree[30][MAXN],sorted[MAXN],toleft[30][MAXN];
void Build(int l,int r,int dep)
{
    if(l==r) return;
    int mid=(l+r)>>1;
    int same=mid-l+1;
    for(int i=l;i<=r;i++)
        if(tree[dep][i]<sorted[mid]) same--;
    int lpos=l,rpos=mid+1;
    for(int i=l;i<=r;i++)
    {
        if(tree[dep][i]<sorted[mid])
            tree[dep+1][lpos++]=tree[dep][i];
        else if(tree[dep][i]==sorted[mid]&&same>0)
        {
            tree[dep+1][lpos++]=tree[dep][i];
            same--;
        }
        else tree[dep+1][rpos++]=tree[dep][i];
        toleft[dep][i]=toleft[dep][l-1]+lpos-l;
    }
    Build(l,mid,dep+1);
    Build(mid+1,r,dep+1);
}
int query(int L,int R,int l,int r,int dep,int k)
{
    if(l==r)
    {
        if(tree[dep][l]<=k) return 1;
        else return 0;
    }
    int mid=(L+R)>>1;
    int cnt=toleft[dep][r]-toleft[dep][l-1];
    if(sorted[mid]>k)
    {
        int newl=L+toleft[dep][l-1]-toleft[dep][L-1];
        int newr=newl+cnt-1;
        if(newr>=newl) query(L,mid,newl,newr,dep+1,k);
        else return 0;
    }
    else
    {
        int newr=r+toleft[dep][R]-toleft[dep][r];
        int newl=newr-(r-l-cnt);
        return cnt+query(mid+1,R,newl,newr,dep+1,k);
    }
}

pair<int,int> section[MAXN];

int main()
{
    //freopen("test.in","r+",stdin);
    //freopen("test.out","w+",stdout);
    FSIO;
    int N, M;
    while(cin>>N>>M)
    {
        memset(tree,0,sizeof(tree));
        memset(toleft,0,sizeof(toleft));
        int tot = 0;
        REP(i,1,M)
        {
            cin>>section[i].X>>section[i].Y;
            REP(j,section[i].X,section[i].Y)
            {
                if(tree[0][j]==0)   tot++;
                tree[0][j]++;
            }
        }
        REP(i,1,N)  sorted[i] = tree[0][i];

        sort(sorted+1,sorted+1+N);
        Build(1,N,0);
        int maxn = 0;
        sort(section+1,section+1+M);
        for(int i=1;i<=M;++i)
        {
            for(int j=i+1;j<=M;++j)
            {
                int to_decrease = 0;

                if(section[j].X>section[i].Y)
                {
                    to_decrease = query(1,N,section[i].X,section[i].Y,0,1)+
                                    query(1,N,section[j].X,section[j].Y,0,1);
                }
                else
                {
                    if(section[j].X-1>=section[i].X)    to_decrease+=query(1,N,section[i].X,section[j].X-1,0,1);
                    to_decrease+=query(1,N,section[j].X,min(section[i].Y,section[j].Y),0,2);
                    if(min(section[i].Y,section[j].Y)+1<=max(section[i].Y,section[j].Y))
                        to_decrease += query(1,N,min(section[i].Y,section[j].Y)+1,max(section[i].Y,section[j].Y),0,1);
                }

                maxn = max(maxn,tot-to_decrease);
            }
        }
        cout<<maxn<<endl;
    }
    return 0;
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};
	int best = INT_MAX;

	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];
		if(y1>y2)swap(x1,x2),swap(y1,y2);

		int x3=0,y3=0;
		if(x2<=y1)x3=x2,y3=y1;
		int curr = getOne(x1,y1)+getOne(x2,y2)+getTwo(x3,y3)-getOne(x3,y3)*2;
		if(curr<best){
			best=curr;
		}
	}
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = INT_MAX;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score=getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3)-getOne(x3,y3);
		best=min(best,score);
	}
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};
	int best = INT_MAX;

	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=0,y3=0;
		// x1,y1 is inside x2,y2:
		if(x1>=x2 and y1<=y2)x3=x1,y3=y1;
		// x2,y2 is inside x1,y1:
		else if(x2>=x1 and y2<=y1)x3=x2,y3=y2;
		//x1 starts before y2:
		else if(x1>=x2 and x1<=y2)x3=x1,y3=min(y1,y2);
		//x2 starts before y1:
		else if(x2>=x1 and x2<=y1)x3=x2,y3=min(y1,y2);

		int curr = getOne(x1,y1)+getOne(x2,y2)+getTwo(x3,y3)-getOne(x3,y3)*2;
		if(curr<best){
			best=curr;
		}
	}
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		assert(pq.top()<=i);
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = INT_MAX;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score = 0;
		for(int k=min(x1,x2); k<=max(y1,y2); ++k){
			bool p1 = false;
			bool p2 = false;
			if(k>=x1 && k<=y1)p1=true;
			if(k>=x2 && k<=y2)p2=true;
			if(b[k]==1 && (p1^p2))++score;
			else if(b[k]==2 && p1 && p2)++score;
		}
		best=min(best,score);
	}
	assert(best>=0 && best<=n);
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = INT_MAX;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score = 0;
		for(int k=min(x1,x2); k<=max(y1,y2); ++k){
			bool p1 = false;
			bool p2 = false;
			if(k>=x1 && k<=y1)p1=true;
			if(k>=x2 && k<=y2)p2=true;
			if(b[k]==1 && (p1||p2))++score;
			else if(b[k]==2 && p1 && p2)++score;
		}
		best=min(best,score);
	}
	assert(best>=0 && best<=n);
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int getOne(int x,int y){return c[y]-c[x-1];}
int getTwo(int x,int y){return d[y]-d[x-1];}

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	int total = 0;
	for(int i=1;i<=n;++i)if(b[i])++total;

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+c[i]+(b[i]==2);

	int best = 0;

	auto getScore = [&](int s1,int s2){
		int x1=qx[s1],y1=qy[s1];
		int x2=qx[s2],y2=qy[s2];
		int x3=max(x1,x2);
		int y3=min(y1,y2);
		int score = getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3)-getOne(x3,y3)*2;
		return total-score;
	};

	vector<int> segments(q);
	iota(begin(segments),end(segments),0);

	auto it1 = min_element(begin(segments),end(segments),[&](int a,int b){
		return getOne(qx[a],qy[a]) < getOne(qx[b],qy[b]);
	});

	int s1 = *it1;
	segments.erase(it1);

	int s2 = *max_element(begin(segments),end(segments),[&](int a,int b){
		return getScore(s1,a) < getScore(s1,b);
	});

	printf("%d\n",getScore(s1,s2));
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};
	int best = INT_MAX;

	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=0,y3=0;
		// x1,y1 is inside x2,y2:
		if(x1>=x2 and y1<=y2)x3=x1,y3=y1;
		// x2,y2 is inside x1,y1:
		else if(x2>=x1 and y2<=y1)x3=x2,y3=y2;
		//x1 starts before y2:
		else if(x1>=x2 and x1<=y2)x3=x1,y3=min(y1,y2);
		//x2 starts before y1:
		else if(x2>=x1 and x2<=y1)x3=x2,y3=min(y1,y2);

		int curr = getOne(x1,y1)+getOne(x2,y2)+getTwo(x3,y3)-getOne(x3,y3);
		if(curr<best){
			best=curr;
		}
	}
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = INT_MAX;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score=getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3);
		best=min(best,score);
	}
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = INT_MAX;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score = 0;
		for(int k=min(x1,x2); k<=max(y1,y2); ++k){
			bool p1 = false;
			bool p2 = false;
			if(k>=x1 && k<=y1)p1=true;
			if(k>=x2 && k<=y2)p2=true;
			if(b[k] == 1 && (p1^p2))++score;
			else if(b[k]==2 && p1 && p2)++score;
		}
		best=min(best,score);
	}
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2)+(b[i]==1);

	int total = 0;
	for(int i=1;i<=n;++i)if(b[i])++total;

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = 0;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score=getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3)-getOne(x3,y3)*2;
		best=max(best,total-score);
		if(best==total)break;
	}
	printf("%d\n",best);
}


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

#define pb push_back
#define vi vector<int>
#define ll long long

const int N=5001;
int n,q;
map<ll,int> a;
map<ll,int> b;
map<ll,int> c;
int l[N],r[N];

int getOne(int x,int y){
	return b[y]-b[x-1];
}

int getTwo(int x,int y){
	return c[y]-c[y-1];
}

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	#endif

	scanf("%d%d",&n,&q);
	for(int i=0,x,y;i<q;++i){
		scanf("%d%d",&x,&y);
		l[i]=x;
		r[i]=y;
		a[x]++;
		a[y+1]--;
	}

	for(auto it=begin(a);it!=end(a);++it){
		if(next(it)!=end(a))
			next(it)->second+=it->second;
	}

	int total=0;
	b[0]=0;
	auto it2=begin(b);
	for(auto it1=begin(a);it1!=end(a);++it1){
		pair<int,int> p={it1->first,it1->second==1};
		if(it2->first==0)p.second*=(it1->first-it2->first);

		int d=it1->first-it2->first;
		if(it2->first!=0)total+=d;

		b.insert(it2,p);
		it2++;
	}

	c[0]=0;
	it2=begin(c);
	for(auto it1=begin(a);it1!=end(a);++it1){
		pair<int,int> p={it1->first,it2->second==1};
		if(it2->first==0)p.second*=(it2->first-it1->first);

		c.insert(it2,p);
		it2++;
	}

	int best=0;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=l[i],y1=r[i];
		int x2=l[j],y2=r[j];
		int x3=max(x1,x2);
		int y3=min(y1,y2);
		int score=getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score+=getTwo(x3,y3);
		best=max(best,total-score);
	}
	printf("%d\n",best);

}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	int total = 0;
	for(int i=1;i<=n;++i)if(b[i])++total;

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = 0;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score=getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3);	// non ci possono essere 1 in (x3,y3)
		best=max(best,total-score);
		if(best==total)break;
	}
	printf("%d\n",best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int getOne(int x,int y){return c[y]-c[x-1];}
int getTwo(int x,int y){return d[y]-d[x-1];}

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	int total = 0;
	for(int i=1;i<=n;++i)if(b[i])++total;

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+c[i]+(b[i]==2);

	int best = 0;

	struct cmp1{
		bool operator()(int a,int b)const{
			int x1=qx[a],y1=qy[a];
			int x2=qx[b],y2=qy[b];
			return getOne(x1,y1) > getOne(x2,y2);
		}
	};

	struct cmp2{
		bool operator()(int a,int b){
			int x1=qx[a],y1=qy[a];
			int x2=qx[b],y2=qy[b];
			return getTwo(x1,y1) > getTwo(x2,y2);
		}
	};

	priority_queue<int,vector<int>,cmp1> segmentsOne;
	priority_queue<int,vector<int>,cmp2> segmentsTwo;

	segmentsOne.push(0);
	segmentsTwo.push(0);

	auto getScore = [&](int s1,int s2){
		int x1=qx[s1],y1=qy[s1];
		int x2=qx[s2],y2=qy[s2];
		int x3=max(x1,x2);
		int y3=min(y1,y2);
		int score = getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3)-getOne(x3,y3)*2;
		return total-score;
	};

	for(int i=1;i<q;++i){
		int p1 = segmentsOne.top();
		int p2 = segmentsTwo.top();
		int score = getScore(p1,i);
		score=max(score,getScore(p2,i));
		segmentsOne.push(i);
		segmentsTwo.push(i);
		best=max(best,score);
	}
	printf("%d\n",best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};
	int best = INT_MAX;

	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=0,y3=0;
		if(y1>y2)swap(x1,x2),swap(y1,y2);
		// y1 <= y2

		if(x1>=x2)x3=max(x1,x2),y3=y1;
		int curr = getOne(x1,y1)+getOne(x2,y2)+getTwo(x3,y3)-getOne(x3,y3)*2;
		if(curr<best){
			best=curr;
		}
	}
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	int total = 0;
	for(int i=1;i<=n;++i)if(b[i])++total;

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = 0;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score=getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3);
		best=max(best,total-score);
		if(best==total)break;
	}
	printf("%d\n",best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int getOne(int x,int y){return c[y]-c[x-1];}
int getTwo(int x,int y){return d[y]-d[x-1];}

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	int total = 0;
	for(int i=1;i<=n;++i)if(b[i])++total;

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+c[i]+(b[i]==2);

	int best = 0;

	auto getScore = [&](int s1,int s2){
		int x1=qx[s1],y1=qy[s1];
		int x2=qx[s2],y2=qy[s2];
		int x3=max(x1,x2);
		int y3=min(y1,y2);
		int score = getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3)-getOne(x3,y3)*2;
		return total-score;
	};

	vector<int> segments(n);
	iota(begin(segments),end(segments),0);

	auto it1 = min_element(begin(segments),end(segments),[&](int a,int b){
		return getOne(qx[a],qy[a]) < getOne(qx[b],qy[b]);
	});
	int s1 = *it1;
	segments.erase(it1);

	int s2 = *max_element(begin(segments),end(segments),[&](int a,int b){
		return getScore(s1,a) < getScore(s1,b);
	});

	printf("%d\n",getScore(s1,s2));
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2);

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = INT_MAX;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score=getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3)-getOne(x3,y3)-getOne(x3,x3)-getOne(y3,y3);
		best=min(best,score);
	}
	printf("%d\n",n-best);
}


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

#define pb push_back
#define vi vector<int>

const int N=5001;
int n,q,qx[N],qy[N],b[N],c[N],d[N];
vi a[N];

int main(){
	#ifdef LOCAL
	freopen("input.txt","r",stdin);
	# endif

	scanf("%d%d",&n,&q);
	for(int i=0;i<q;++i)scanf("%d%d",&qx[i],&qy[i]);
	for(int i=0;i<q;++i)a[qy[i]].pb(qx[i]);

	priority_queue<int> pq;
	pq.push(0);

	int p=0;
	for(int i=n;i>0;--i){
		for(int x:a[i])pq.push(x),++p;
		b[i]=p;
		while(pq.top()==i)--p,pq.pop();
	}

	for(int i=1;i<=n;++i)c[i]=c[i-1]+(b[i]==1);
	for(int i=1;i<=n;++i)d[i]=d[i-1]+(b[i]==2)+(b[i]==1);

	int total = 0;
	for(int i=1;i<=n;++i)if(b[i])++total;

	auto getOne = [&](int x,int y){return c[y]-c[x-1];};
	auto getTwo = [&](int x,int y){return d[y]-d[x-1];};

	int best = 0;
	for(int i=0;i<q;++i)for(int j=i+1;j<q;++j){
		int x1=qx[i],y1=qy[i];
		int x2=qx[j],y2=qy[j];

		int x3=max(x1,x2);
		int y3=min(y1,y2);

		int score=getOne(x1,y1)+getOne(x2,y2);
		if(x3<=y3)score = score+getTwo(x3,y3)-getOne(x3,y3)*2;
		best=max(best,total-score);
	}
	printf("%d\n",best);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<int,int> pi[5010];
int arr[5010];
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.first!=b.first)
		return a.first<b.first;
	else
		return a.second<b.second;
}
int main()
{
	int n,q;
	cin>>n>>q;
	for(int i=0;i<q;i++){
		scanf("%d%d",&pi[i].first,&pi[i].second);
	}
	sort(pi,pi+q,cmp);
	int num=0;
	int ans=0;
	ans+=pi[0].second-pi[0].first+1;
	int mn=1000000000;
	int mx=pi[0].second;
	for(int i=1;i<q;i++){
		ans+=max(0,pi[i].second-max((pi[i].first-1),mx));
		mx=max(mx,pi[i].second);
	}
//	cout<<ans<<endl;
	arr[0]=min(pi[1].first,pi[0].second+1)-pi[0].first;
	for(int i=1;i<q-1;i++){
		arr[i]=min(pi[i+1].first,pi[i].second+1)-max(pi[i].first,pi[i-1].second+1);
	}
	arr[q-1]=pi[q-1].second-max(pi[q-1].first-1,pi[q-2].second);
//	for(int i=0;i<q;i++)
//		cout<<arr[i]<<endl;
	for(int i=0;i<q;i++){
		for(int j=i+1;j<q;j++){
			mn=max(0,min(mn,arr[i]+arr[j]+max(0,pi[i].second-pi[j].first+1)));
//			if(mn==1)
//				cout<<i<<" "<<j<<endl;
		}
	}
//	sort(arr,arr+q-1);
//	ans-=max(0,arr[0]);
//	ans-=max(0,arr[1]);
//	for(int i=0;i<q;i++)
//		cout<<arr[i]<<" ";
	cout<<ans-mn<<endl;
//	for(int i=0;i<q;i++)
//		cout<<pi[i].first<<" "<<pi[i].second<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<int,int> pi[5010];
int arr[5010];
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.first!=b.first)
		return a.first<b.first;
	else
		return a.second<b.second;
}
int main()
{
	int n,q;
	cin>>n>>q;
	for(int i=0;i<q;i++){
		scanf("%d%d",&pi[i].first,&pi[i].second);
	}
	sort(pi,pi+q,cmp);
	int num=0;
	int ans=0;
	ans+=pi[0].second-pi[0].first+1;
	int mn=1000000000;
	int mx=pi[0].second;
	for(int i=1;i<q;i++){
		ans+=max(0,pi[i].second-max((pi[i].first-1),mx));
		mx=max(mx,pi[i].second);
	}
//	cout<<ans<<endl;
	arr[0]=max(0,min(pi[1].first,pi[0].second+1)-pi[0].first);
	for(int i=1;i<q-1;i++){
		arr[i]=max(0,min(pi[i+1].first,pi[i].second+1)-max(pi[i].first,pi[i-1].second+1));
	}
	arr[q-1]=max(0,pi[q-1].second-max(pi[q-1].first-1,pi[q-2].second));
//	for(int i=0;i<q;i++)
//		cout<<arr[i]<<endl;
	for(int i=0;i<q;i++){
		for(int j=i+1;j<q;j++){
			if(pi[i].second<pi[j].first)
				mn=max(0,min(mn,arr[i]+arr[j]));
			else{
				int temp=max(0,min(mn,arr[i]+arr[j]+max(0,pi[i].second-pi[j].first+1)));
				int mx=pi[j].first-1;
				for(int k=i+1;k<j;k++){
					temp-=max(0,min(pi[i].second,pi[k].second)-mx);
					mx=max(mx,pi[k].second);
				}
				mn=max(0,min(mn,temp));
			}
//			if(mn==4)
//				cout<<i<<" "<<j<<endl;
		}
	}
//	sort(arr,arr+q-1);
//	ans-=max(0,arr[0]);
//	ans-=max(0,arr[1]);
//	for(int i=0;i<q;i++)
//		cout<<arr[i]<<" ";
	cout<<ans-mn<<endl;
//	for(int i=0;i<q;i++)
//		cout<<pi[i].first<<" "<<pi[i].second<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<int,int> pi[5010];
int arr[5010];
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.first!=b.first)
		return a.first<b.first;
	else
		return a.second<b.second;
}
int main()
{
	int n,q;
	cin>>n>>q;
	for(int i=0;i<q;i++){
		scanf("%d%d",&pi[i].first,&pi[i].second);
	}
	sort(pi,pi+q,cmp);
	int num=0;
	int ans=0;
	ans+=pi[0].second-pi[0].first+1;
	int mn=1000000000;
	for(int i=1;i<n;i++){
		ans+=max(0,pi[i].second-max((pi[i].first-1),pi[i-1].second));
	}
	if(n==100)
		cout<<ans<<endl;
	arr[0]=min(pi[1].first,pi[0].second+1)-pi[0].first;
	for(int i=1;i<q-1;i++){
		arr[i]=min(pi[i+1].first,pi[i].second+1)-max(pi[i].first,pi[i-1].second+1);
	}
	arr[q-1]=pi[q-1].second-max(pi[q-1].first-1,pi[q-2].second);
	for(int i=0;i<q;i++){
		for(int j=0;j<q;j++){
//			if(j-i!=1)
//				mn=min(mn,arr[i]+arr[j]);
//			else{
				mn=max(0,min(mn,arr[i]+arr[j]+max(0,pi[i].second-pi[j].first+1)));
//			}
		}
	}
//	sort(arr,arr+q-1);
//	ans-=max(0,arr[0]);
//	ans-=max(0,arr[1]);
//	for(int i=0;i<q;i++)
//		cout<<arr[i]<<" ";
	cout<<ans-mn<<endl;
//	for(int i=0;i<q;i++)
//		cout<<pi[i].first<<" "<<pi[i].second<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<int,int> pi[5010];
int arr[5010];
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.first!=b.first)
		return a.first<b.first;
	else
		return a.second<b.second;
}
int main()
{
	int n,q;
	cin>>n>>q;
	for(int i=0;i<q;i++){
		scanf("%d%d",&pi[i].first,&pi[i].second);
	}
	sort(pi,pi+q,cmp);
	int num=0;
	int ans=0;
	ans+=pi[0].second-pi[0].first+1;
	int mn=1000000000;
	int mx=pi[0].second;
	for(int i=1;i<q;i++){
		ans+=max(0,pi[i].second-max((pi[i].first-1),mx));
		mx=max(mx,pi[i].second);
	}
//	cout<<ans<<endl;
	arr[0]=max(0,min(pi[1].first,pi[0].second+1)-pi[0].first);
	for(int i=1;i<q-1;i++){
		arr[i]=max(0,min(pi[i+1].first,pi[i].second+1)-max(pi[i].first,pi[i-1].second+1));
	}
	arr[q-1]=max(0,pi[q-1].second-max(pi[q-1].first-1,pi[q-2].second));
//	for(int i=0;i<q;i++)
//		cout<<arr[i]<<endl;
	for(int i=0;i<q;i++){
		for(int j=i+1;j<q;j++){
			if(pi[i].second<pi[j].first)
				mn=max(0,min(mn,arr[i]+arr[j]));
			else{
				int temp=max(0,min(mn,arr[i]+arr[j]+max(0,pi[i].second-pi[j].first+1)));
				int mx=pi[j].first-1;
				for(int k=i+1;k<j;k++){
					temp-=max(0,min(pi[i].second,pi[k].second)-mx);
					mx=max(mx,pi[k].second);
				}
				mn=max(0,min(mn,temp));
			}
			if(mn==4)
				cout<<i<<" "<<j<<endl;
		}
	}
//	sort(arr,arr+q-1);
//	ans-=max(0,arr[0]);
//	ans-=max(0,arr[1]);
//	for(int i=0;i<q;i++)
//		cout<<arr[i]<<" ";
	cout<<ans-mn<<endl;
//	for(int i=0;i<q;i++)
//		cout<<pi[i].first<<" "<<pi[i].second<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.first!=b.first)
		return a.first<b.first;
	else
		return a.second<b.second;
}
/*int arr[100100];
int num1l[100100];
int num1r[100100];
int num2l[100100];
int num2r[100100];
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		scanf("%d",&arr[i]);
		if(arr[i]==1){
			num1l[i]=num1r[i]=1;
		}
		else if(arr[i]==2){
			num2l[i]=num2r[i]=1;
		}
	}
	for(int i=1;i<n;i++){
		if(num1l[i]!=0)
			num1l[i]+=num1l[i-1];
		if(num21[i]!=0)
			num2l[i]+=num2l[i-1];
	}
	for(int i=n-1;i>=0;i--){
		if(num1[i]!=0)
			num1l[i]+=num1l[i-1];
		if(num21[i]!=0)
			num2l[i]+=num2l[i-1];
	}
}*/
pair<int,int> pi[5010];
int arr[5010],tot[5010];
int num1[5010],num2[5010];
priority_queue<int,vector<int>,greater<int> >qq;
int main()
{
	int n,q;
	cin>>n>>q;
	for(int i=0;i<q;i++){
		scanf("%d%d",&pi[i].first,&pi[i].second);
	}
	sort(pi,pi+q,cmp);
	for(int i=0,j=0;i<=5000;i++){
		while(pi[j].first==i&&j<q){
			qq.push(pi[j].second);
			j++;
		}
		while(!qq.empty()&&qq.top()<i){
			qq.pop();
		}
		tot[i]=qq.size();
	}
	if(tot[0]==1)
		num1[0]=1;
	else if(tot[0]==2)
		num2[0]=1;
	for(int i=1;i<=5000;i++){
		if(tot[i]==1){
			num1[i]=num1[i-1]+1;
			num2[i]=num2[i-1];
		}
		else if(tot[i]==2){
			num1[i]=num1[i-1];
			num2[i]=num2[i-1]+1;
		}
		else{
			num1[i]=num1[i-1];
			num2[i]=num2[i-1];
		}
	}
	int ans=0;
	ans+=pi[0].second-pi[0].first+1;
	int mn=1000000000;
	int mx=pi[0].second;
	for(int i=1;i<q;i++){
		ans+=max(0,pi[i].second-max((pi[i].first-1),mx));
		mx=max(mx,pi[i].second);
	}
	for(int i=0;i<q;i++){
		for(int j=i+1;j<q;j++){
			if(pi[i].second<pi[j].first)
				mn=min(mn,num1[pi[i].second]-num1[pi[i].first-1]+num1[pi[j].second]-num1[pi[j].first-1]);
			else{
				if(pi[i].second<=pi[j].second)
					mn=min(mn,num1[pi[j].first-1]-num1[pi[i].first-1]+num1[pi[j].second]-num1[pi[i].second]+num2[pi[i].second]-num2[pi[j].first-1]);
				else
					mn=min(mn,num1[pi[j].first-1]-num1[pi[i].first-1]+num1[pi[i].second]-num1[pi[j].second]+num2[pi[j].second]-num2[pi[j].first-1]);
			}
		}
	}
	cout<<ans-mn<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.first!=b.first)
		return a.first<b.first;
	else
		return a.second<b.second;
}
/*int arr[100100];
int num1l[100100];
int num1r[100100];
int num2l[100100];
int num2r[100100];
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		scanf("%d",&arr[i]);
		if(arr[i]==1){
			num1l[i]=num1r[i]=1;
		}
		else if(arr[i]==2){
			num2l[i]=num2r[i]=1;
		}
	}
	for(int i=1;i<n;i++){
		if(num1l[i]!=0)
			num1l[i]+=num1l[i-1];
		if(num21[i]!=0)
			num2l[i]+=num2l[i-1];
	}
	for(int i=n-1;i>=0;i--){
		if(num1[i]!=0)
			num1l[i]+=num1l[i-1];
		if(num21[i]!=0)
			num2l[i]+=num2l[i-1];
	}
}*/
pair<int,int> pi[5010];
int arr[5010],tot[5010];
int num1[5010],num2[5010];
priority_queue<int,vector<int>,greater<int> >qq;
int main()
{
	int n,q;
	cin>>n>>q;
	for(int i=0;i<q;i++){
		scanf("%d%d",&pi[i].first,&pi[i].second);
	}
	sort(pi,pi+q,cmp);
//	cout<<"123";
	for(int i=0,j=0;i<=5000;i++){
		while(pi[j].first==i&&j<q){
			qq.push(pi[j].second);
			j++;
		}
		while(!qq.empty()&&qq.top()<i){
			qq.pop();
		}
		tot[i]=qq.size();
	}
	if(tot[0]==1)
		num1[0]=1;
	else if(tot[0]==2)
		num2[0]=1;
	for(int i=1;i<=5000;i++){
		if(tot[i]==1){
			num1[i]=num1[i-1]+1;
			num2[i]=num2[i-1];
		}
		else if(tot[i]==2){
			num1[i]=num1[i-1];
			num2[i]=num2[i-1]+1;
		}
		else{
			num1[i]=num1[i-1];
			num2[i]=num2[i-1];
		}
	}
	int ans=0;
	ans+=pi[0].second-pi[0].first+1;
	int mn=1000000000;
	int mx=pi[0].second;
	for(int i=1;i<q;i++){
		ans+=max(0,pi[i].second-max((pi[i].first-1),mx));
		mx=max(mx,pi[i].second);
	}
//	arr[0]=max(0,min(pi[1].first,pi[0].second+1)-pi[0].first);
//	for(int i=1;i<q-1;i++){
//		arr[i]=max(0,min(pi[i+1].first,pi[i].second+1)-max(pi[i].first,pi[i-1].second+1));
//	}
//	arr[q-1]=max(0,pi[q-1].second-max(pi[q-1].first-1,pi[q-2].second));
	for(int i=0;i<q;i++){
		for(int j=i+1;j<q;j++){
			if(pi[i].second<pi[j].first)
//				mn=max(0,min(mn,getans(pi[i].first,pi[i].second,0,1)+getans(pi[j].first,pi[j].second,0,1)));
				mn=min(mn,num1[pi[i].second]-num1[pi[i].first-1]+num1[pi[j].second]-num1[pi[j].first-1]);
			else{
//				mn=max(0,min(mn,getans(pi[i].first,pi[j].first-1,0,1)+getans(pi[i].second+1,pi[j].second,0,1)+getans(pi[j].first,pi[i].second,0,2)));
				mn=min(mn,num1[pi[j].first-1]-num1[pi[i].first-1]+num1[pi[j].second]-num1[pi[i].second+1]+num2[pi[i].second]-num2[pi[j].first-1]);

			}
		}
	}
	cout<<ans-mn<<endl;
}