#pragma comment(linker, "/STACK:33554432")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <iterator>
#include <stack>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <cstring>
#include <bitset>

using namespace std;
void dout() { cerr << endl; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
	cerr << H << ' ';
	dout(T...);
}

using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<long long, long long>;

const ll MAXN = 5010;
const ll MAXK = 26 * 2;
const ll MAXM = 50100;
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ld EPS = 1e-9;
const ll INF = numeric_limits<int> ::max() / 4;
const ll MSIZE = 20;
const ld pi = acos(-1.0);
const ll H = 8;
const int MAX_SIZE = 44;
const ll STO_PITSOT = 100500;

void precalc()
{
}

int n, q;

pair<int, int> mall[MAXN];

int pref[MAXN];

int colors[MAXN];
int less1[MAXN];
int less2[MAXN];
int sum = 0;

int solve_for(int i, int j)
{
	int res = sum;

	int a = mall[i].first;
	int b = mall[i].second;
	int c = mall[j].first;
	int d = mall[j].second;

	if (b < c)
	{
		return res - (less1[b] - less1[a - 1]) - (less1[d] - less1[c - 1]);
	}

	if (b == c)
	{
		return res - (less2[c] - less2[b - 1]) - (less1[d] - less1[c]) + (less1[b - 1] - less1[a - 1]);
	}

	int e = c;
	int f = b;

	int curr = less2[f] - less2[e - 1];

	curr += max(0, less1[c - 1] - less1[a - 1]);
	curr += max(0, less1[d] - less1[b]);

	return res;
}

void solve()
{
	cin >> n >> q;

	for (int i = 0; i < q; ++i)
	{
		cin >> mall[i].first >> mall[i].second;

		for (int j = mall[i].first; j <= mall[i].second; ++j)
			colors[j]++;
	}

	for (int i = 1; i <= n; ++i)
	{
		if (colors[i] >= 1)
			sum++;
	}

	for (int i = 1; i <= n; ++i)
	{
		less1[i] = less1[i - 1];
		if (colors[i] <= 1)
			less1[i]++;
	}

	for (int i = 1; i <= n; ++i)
	{
		less2[i] = less2[i - 1];
		if (colors[i] <= 2)
			less2[i]++;
	}

	int answer = 0;

	for (int i = 0; i < q; ++i)
	{
		for (int j = i + 1; j < q; ++j)
		{
			answer = max(answer, solve_for(i, j));
		}
	}

	cout << answer;

}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(20);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif

	solve();

#ifdef LOCAL
	//cout << '\n' << (double)clock() / CLOCKS_PER_SEC << '\n';
#endif
}


#pragma comment(linker, "/STACK:33554432")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <iterator>
#include <stack>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <cstring>
#include <bitset>

using namespace std;
void dout() { cerr << endl; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
	cerr << H << ' ';
	dout(T...);
}

using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<long long, long long>;

const ll MAXN = 5010;
const ll MAXK = 26 * 2;
const ll MAXM = 50100;
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ld EPS = 1e-9;
const ll INF = numeric_limits<int> ::max() / 4;
const ll MSIZE = 20;
const ld pi = acos(-1.0);
const ll H = 8;
const int MAX_SIZE = 44;
const ll STO_PITSOT = 100500;

void precalc()
{
}

int n, q;

vector<pair<int, int>> mall;

int pref[MAXN];

int colors[MAXN];
int less1[MAXN];
int less2[MAXN];
int sum = 0;

int solve_for(int i, int j)
{
	int res = sum;

	int a = mall[i].first;
	int b = mall[i].second;
	int c = mall[j].first;
	int d = mall[j].second;

	if (b < c)
	{
		return res - (less1[b] - less1[a - 1]) - (less1[d] - less1[c - 1]);
	}

	if (b == c)
	{
		return res - (less2[c] - less2[b - 1]) - (less1[d] - less1[c]) - (less1[b - 1] - less1[a - 1]);
	}

	if (a <= c && d <= b)
	{

		int curr = 0;

		curr += less2[d] - less2[c - 1];
		curr += less1[c - 1] - less1[a - 1];
		curr += less1[b] - less1[d];

		return res - curr;
	}

	int e = c;
	int f = b;

	int curr = less2[f] - less2[e - 1];

	curr += max(0, less1[c - 1] - less1[a - 1]);
	curr += max(0, less1[d] - less1[b]);

	return res - curr;
}

void solve()
{
	cin >> n >> q;

	for (int i = 0; i < q; ++i)
	{
		int a, b;
		cin >> a >> b;

		mall.push_back({ a, b });
		for (int j = mall[i].first; j <= mall[i].second; ++j)
			colors[j]++;
	}

	sort(mall.begin(), mall.end());

	for (int i = 1; i <= n; ++i)
	{
		if (colors[i] >= 1)
			sum++;
	}

	for (int i = 1; i <= n; ++i)
	{
		less1[i] = less1[i - 1];
		if (colors[i] <= 1)
			less1[i]++;
	}

	for (int i = 1; i <= n; ++i)
	{
		less2[i] = less2[i - 1];
		if (colors[i] <= 2)
			less2[i]++;
	}

	int answer = 0;

	for (int i = 0; i < q; ++i)
	{
		for (int j = i + 1; j < q; ++j)
		{
			answer = max(answer, solve_for(i, j));
		}
	}

	cout << answer;

}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(20);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif

	solve();

#ifdef LOCAL
	//cout << '\n' << (double)clock() / CLOCKS_PER_SEC << '\n';
#endif
}


#pragma comment(linker, "/STACK:33554432")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <iterator>
#include <stack>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <cstring>
#include <bitset>

using namespace std;
void dout() { cerr << endl; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
	cerr << H << ' ';
	dout(T...);
}

using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<long long, long long>;

const ll MAXN = 5010;
const ll MAXK = 26 * 2;
const ll MAXM = 50100;
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ld EPS = 1e-9;
const ll INF = numeric_limits<int> ::max() / 4;
const ll MSIZE = 20;
const ld pi = acos(-1.0);
const ll H = 8;
const int MAX_SIZE = 44;
const ll STO_PITSOT = 100500;

void precalc()
{
}

int n, q;

pair<int, int> mall[MAXN];

int pref[MAXN];

int colors[MAXN];
int less1[MAXN];
int less2[MAXN];
int sum = 0;

int solve_for(int i, int j)
{
	int res = sum;

	int a = mall[i].first;
	int b = mall[i].second;
	int c = mall[j].first;
	int d = mall[j].second;

	if (b < c)
	{
		return res - (less1[b] - less1[a - 1]) - (less1[d] - less1[c - 1]);
	}

	if (b == c)
	{
		return res - (less2[c] - less2[b - c]) + (less1[d] - less1[c]) + (less1[b - 1] - less1[a - 1]);
	}

	int e = c;
	int f = b;

	int curr = less2[f] - less2[e - 1];

	curr += max(0, less1[c - 1] - less1[a - 1]);
	curr += max(0, less1[d] - less1[b]);

	return res;
}

void solve()
{
	cin >> n >> q;

	for (int i = 0; i < q; ++i)
	{
		cin >> mall[i].first >> mall[i].second;

		for (int j = mall[i].first; j <= mall[i].second; ++j)
			colors[j]++;
	}

	for (int i = 1; i <= n; ++i)
	{
		if (colors[i] >= 1)
			sum++;
	}

	for (int i = 1; i <= n; ++i)
	{
		less1[i] = less1[i - 1];
		if (colors[i] <= 1)
			less1[i]++;
	}

	for (int i = 1; i <= n; ++i)
	{
		less2[i] = less2[i - 1];
		if (colors[i] <= 2)
			less2[i]++;
	}

	int answer = 0;

	for (int i = 0; i < q; ++i)
	{
		for (int j = i + 1; j < q; ++j)
		{
			answer = max(answer, solve_for(i, j));
		}
	}

	cout << answer;

}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(20);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif

	solve();

#ifdef LOCAL
	//cout << '\n' << (double)clock() / CLOCKS_PER_SEC << '\n';
#endif
}


#pragma comment(linker, "/STACK:33554432")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <unordered_map>
#include <cmath>
#include <iterator>
#include <stack>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <cstring>
#include <bitset>

using namespace std;
void dout() { cerr << endl; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
	cerr << H << ' ';
	dout(T...);
}

using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<long long, long long>;

const ll MAXN = 5010;
const ll MAXK = 26 * 2;
const ll MAXM = 50100;
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ld EPS = 1e-9;
const ll INF = numeric_limits<int> ::max() / 4;
const ll MSIZE = 20;
const ld pi = acos(-1.0);
const ll H = 8;
const int MAX_SIZE = 44;
const ll STO_PITSOT = 100500;

void precalc()
{
}

int n, q;

vector<pair<int, int>> mall;

int pref[MAXN];

int colors[MAXN];
int less1[MAXN];
int less2[MAXN];
int sum = 0;

int solve_for(int i, int j)
{
	int res = sum;

	int a = mall[i].first;
	int b = mall[i].second;
	int c = mall[j].first;
	int d = mall[j].second;

	if (b < c)
	{
		return res - (less1[b] - less1[a - 1]) - (less1[d] - less1[c - 1]);
	}

	if (b == c)
	{
		return res - (less2[c] - less2[b - 1]) - (less1[d] - less1[c]) - (less1[b - 1] - less1[a - 1]);
	}

	int e = c;
	int f = b;

	int curr = less2[f] - less2[e - 1];

	curr += max(0, less1[c - 1] - less1[a - 1]);
	curr += max(0, less1[d] - less1[b]);

	return res;
}

void solve()
{
	cin >> n >> q;

	for (int i = 0; i < q; ++i)
	{
		int a, b;
		cin >> a >> b;

		mall.push_back({ a, b });
		for (int j = mall[i].first; j <= mall[i].second; ++j)
			colors[j]++;
	}

	sort(mall.begin(), mall.end());

	for (int i = 1; i <= n; ++i)
	{
		if (colors[i] >= 1)
			sum++;
	}

	for (int i = 1; i <= n; ++i)
	{
		less1[i] = less1[i - 1];
		if (colors[i] <= 1)
			less1[i]++;
	}

	for (int i = 1; i <= n; ++i)
	{
		less2[i] = less2[i - 1];
		if (colors[i] <= 2)
			less2[i]++;
	}

	int answer = 0;

	for (int i = 0; i < q; ++i)
	{
		for (int j = i + 1; j < q; ++j)
		{
			answer = max(answer, solve_for(i, j));
		}
	}

	cout << answer;

}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cout << fixed << setprecision(20);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif

	solve();

#ifdef LOCAL
	//cout << '\n' << (double)clock() / CLOCKS_PER_SEC << '\n';
#endif
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>a[i].r;
        b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e8;i++){
        ans+=1;
    }
}


#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

template<typename T> T mo(T x, T y) { x %= y; return x <= 0 ? x + y : x; }

const int MX = 5007;

int l[MX], r[MX];
int a[MX];
int md[MX];
int f[MX];

int main() {
#ifdef FASTIO
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; i++) {
        cin >> l[i] >> r[i];
    }
    int ans = 0;
    for (int x = 1; x <= q; x++) {
        memset(a, 0, sizeof(a));
        memset(md, 0, sizeof(md));
        for (int i = 1; i <= q; i++) {
            if (i != x) {
                md[l[i]]++;
                md[r[i] + 1]--;
            }
        }
        int sm = 0;
        int tot = 0;
        for (int i = 1; i <= n; i++) {
            sm += md[i];
            if (sm > 0) {
                tot++;
            }
            f[i] = f[i - 1];
            if (sm == 1) {
                f[i]++;
            }
        }
        for (int y = 1; y <= q; y++) {
            if (x == y) {
                continue;
            }
            int cur = tot - (f[r[y]] - f[l[y] - 1]);
                                                }
    cout << ans << "\n";
    return 0;
}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e10;i++){
        ans+=1;
    }
    cout<<7<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=2e9;i++){
        ans+=1;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
       // b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e8;i++){
        ans+=1;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e9;i++){
        ans+=1;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e15;i++){
        ans+=1;
    }
    cout<<7<<endl;
}


#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

template<typename T> T mo(T x, T y) { x %= y; return x <= 0 ? x + y : x; }

const int MX = 5007;

int l[MX], r[MX];
int a[MX];
int md[MX];
int f[MX];

int main() {
#ifdef FASTIO
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; i++) {
        cin >> l[i] >> r[i];
    }
    int ans = 0;
    for (int x = 1; x <= q; x++) {
        memset(a, 0, sizeof(a));
        memset(md, 0, sizeof(md));
        for (int i = 1; i <= q; i++) {
            if (i != x) {
                md[l[i]]++;
                md[r[i] + 1]--;
            }
        }
        int sm = 0;
        int tot = 0;
        for (int i = 1; i <= n; i++) {
            sm += md[i];
            if (sm > 0) {
                tot++;
            }
            f[i] = f[i - 1];
            if (sm == 1) {
                f[i]++;
            }
        }
        for (int y = 1; y <= q; y++) {
            if (x == y) {
                continue;
            }
            int cur = tot - (f[r[y]] - f[l[y] - 1]);
                                                }
    cout << ans << "\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=5e3+9;
int f[maxn],sum[maxn];
struct node{
    int l,r;
}a[maxn];
int main(){
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=1;i<=m;i++){
        cin>>a[i].l>>a[i].r;
        sum[a[i].l]++;
        sum[a[i].r+1]--;
    }
    int ans=0,add=0;
    for(i=1;i<=m;i++){
        int tot=0;
        add=0;
        sum[a[i].l]--;
        sum[a[i].r+1]++;
        for(j=1;j<=n;j++){
                //cout<<sum[i]<<' ';
            add+=sum[j];
            if(add>0)tot++;
            f[j]=f[j-1];
            if(add==1){
                f[j]++;
            }
        }//cout<<endl;
        for(j=1;j<=m;j++){
            if(j==i)continue;
            //cout<<tot<<endl;
            ans=max(ans,tot-(f[a[j].r]-f[a[j].l-1]));
        }
        sum[a[i].l]++;
        sum[a[i].r+1]--;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e9;i++){
        ans+=1;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0)
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>aj[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e9;i++){
        ans+=1;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0)
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>aj[i].r;
        b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e9;i++){
        ans+=1;
    }
}


#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

template<typename T> T mo(T x, T y) { x %= y; return x <= 0 ? x + y : x; }

const int MX = 5007;

int l[MX], r[MX];
int a[MX];
int md[MX];
int f[MX];

int main() {
#ifdef FASTIO
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; i++) {
        cin >> l[i] >> r[i];
    }
    int ans = 0;
    for (int x = 1; x <= q; x++) {
        memset(a, 0, sizeof(a));
        memset(md, 0, sizeof(md));
        for (int i = 1; i <= q; i++) {
            if (i != x) {
                md[l[i]]++;
                md[r[i] + 1]--;
            }
        }
        int sm = 0;
        int tot = 0;
        for (int i = 1; i <= n; i++) {
            sm += md[i];
            if (sm > 0) {
                tot++;
            }
            f[i] = f[i - 1];
            if (sm == 1) {
                f[i]++;
            }
        }
        for (int y = 1; y <= q; y++) {
            if (x == y) {
                continue;
            }
            int cur = tot - (f[r[y]] - f[l[y] - 1]);
            ans=max(ans,cur);
     }
    }
    cout << ans << "\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e15;i++){
        ans+=1;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e9;i++){
        ans+=1;
    }
    cout<<7<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=2.5e9;i++){
        ans+=1;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=1e9;i++){
        ans+=1;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn=5e3+9;
struct node{
    int l,r;
}a[maxn];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int i,j,k,n,m;
    cin>>n>>m;
    for(i=0;i<m;i++){
        cin>>a[i].l>>a[i].r;
        //b[i]=a[i].r;
    }
    ll ans=0;
    for(i=1;i<=5e8;i++){
        ans+=1;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int l[5040],r[5040];
int c[5040],c1[5040],p[5040],n,q,mini;
map<int,int>res;
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                c1[l[i]]++;
               c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             if(c[i]==1)
             p[i+1]=p[i]+c[i];
             else
            p[i+1]=p[i];
            if(c[i]!=0 && i!=n)cnt++;
         }
         fo(i,0,q)
         {
             if(i!=k)
             {
                   res[i]=p[r[i]]-p[l[i]];
                 mini=min(mini,res[i]);
             }
         }
    return cnt-mini;
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
     cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int main()
{
    int n,q;
    cin>>n>>q;
    int l[5010],r[5010];
    int c[5010],c1[5010],p[5010],res[5040];
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int mini,ans=0;
    fo(k,0,q)
    {
        memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                  c1[l[i]]++;
                 c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
      // fo(i,0,n)cout<<c[i]<<" ";
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             if(c[i]==1)
             p[i+1]=p[i]+c[i];
             else
            p[i+1]=p[i];
         }
         //fo(i,0,n)cout<<p[i]<<" ";
         fo(i,0,q)
         {
             if(i!=k)
             {
                  res[i]=p[r[i]]-p[l[i]];
                 mini=min(mini,res[i]);
             }
         }
         fo(i,0,n)
         if(c[i]!=0)cnt++;
         //cout<<cnt-mini<<" ";
         ans=max(ans,cnt-mini);
    }
   cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int l[5040],r[5040];
int c[5040],c1[5040],p[5040],n,q,mini;
map<int,int>res;
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                c1[l[i]]++;
               c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             p[i+1]=p[i]+(c[i]==1? 1:0);
            //if(c[i]!=0 && i!=n)cnt++;
         }
        /* fo(i,0,q)
         {
             if(i!=k)
             {
                   res[i]=p[r[i]]-p[l[i]];
                   mini=min(mini,res[i]);
             }
         }*/
    int ans = -int(1e9);
	for(int i = 0; i < q; i++)
	{
		if(i == k) continue;
		ans = max(ans, p[l[i]] - p[r[i]]);
	}
	for(int i = 0; i < n; i++)
		if(c[i] > 0)
			ans++;
	return ans;
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
     cout<<ans;
 }


/*#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++;
    cout<<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<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define vii vector<int>
using namespace std;

int main()
{
    int n,q;
    cin>>n>>q;
    int l[5010],r[5010];
    int c[5010];
    fo(i,1,q+1)
    {
        cin>>l[i]>>r[i];
        fo(j,l[i],r[i]+1) c[j]++;
    }
    int mini,ans=0;
    map<int,int>res,p,d;
    fo(k,1,q+1)
    {
        mini=INT_MAX;
        int cnt=0;
        fo(i,l[k],r[k]+1)c[i]--;
      // fo(i,1,n+1)cout<<c[i]<<" ";
         fo(i,1,n+1)if(c[i]==1)d[i]=1;
          else d[i]=0;
         //fo(i,1,n+1)cout<<d[i]<<" ";
         p[0]=0;
         fo(i,1,n+1)
         {
             p[i]=p[i-1]+d[i];
         }
         //fo(i,1,n+1)cout<<p[i]<<" ";
         fo(i,1,q+1)
         {
             if(i!=k)
             {
                  res[i]=p[r[i]]-p[l[i]-1];
                 mini=min(mini,res[i]);
             }
         }
        // cout<<mini<<" ";
         fo(i,1,n+1)
         if(c[i]!=0)cnt++;
         //cout<<cnt-mini<<" ";
         ans=max(ans,cnt-mini);
         res.clear();
         p.clear();
         d.clear();
          fo(i,l[k],r[k]+1)c[i]++;
          //fo(i,1,n+1)cout<<c[i]<<" ";
         //cout<<"\n";
    }
   cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int l[5040],r[5040];
int c[5040],c1[5040],p[5040],n,q,mini;
map<int,int>res;
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                c1[l[i]]++;
               c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             p[i+1]=p[i]+(c[i]==1? 1:0);
            if(c[i]!=0 && i!=n)cnt++;
         }
         fo(i,0,q)
         {
             if(i!=k)
             {
                   res[i]=p[r[i]]-p[l[i]];
                 mini=min(mini,res[i]);
             }
         }
    return cnt-mini;
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
     cout<<ans;
 }


/*#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++;
    cout<<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<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define ii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define vii vector<int>
using namespace std;

int main()
{
    int n,q;
    cin>>n>>q;
    map<int,int>only;
    int l[n],r[n];
    vii c[5010];
    fo(i,1,q+1)
    {
        cin>>l[i]>>r[i];
        fo(j,l[i],r[i]+1) c[j].pb(i);
    }
    int ans=0;
    fo(k,1,q+1)
    {
       // fo(i,1,n+1)if(c[i].size()==1)cout<<c[i][0]<<" "<<i<<"\n";
        //fo(j,l[k],r[k]+1)c[j][0];
        fo(j,1,n+1)
        {
            //cout<<j<<" ";
            if(c[j].size()==1 && (j>r[k] || j<l[k]))
            {
                 only[c[j][0]]++;
                 //cout<<"hgf";
            }
            else if(j<=r[k]&&j>=l[k] )
            {
                if(c[j].size()==2)
               {
                   if(c[j][0]==k)
                  only[c[j][1]]++;
                  else only[c[j][0]]++;
                   //cout<<"gh";
                }
            }
        }
        //cout<<only[2]<<'\n';
        int mini=INT_MAX;
        fo(i,1,q+1)
        {
            //cout<<only[i];
            if(mini>only[i] && i!=k)mini=only[i];
        }
       // cout<<mini;
        int cnt=0;
        fo(i,1,n+1)
        {
           // cout<<c[i].size()<<' '<<i<<"\n";
               if(i>r[k]||i<l[k])
            {
                if(c[i].size()!=0)cnt++;
            }
            else
            {
              if(c[i].size()!=1 ) cnt++;
            }
        }
        //cout<<cnt;
        //cout<<mini;
       // cout<<cnt-mini<<" ";
          ans=max(cnt-mini,ans);
          only.clear();
        //cout<<ans;
        //cout<<"\n";
    }
    cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int l[5040],r[5040];
int c[5040],c1[5040],p[5040],n,q,mini;
map<int,int>res;
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                c1[l[i]]++;
               c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             p[i+1]=p[i]+(c[i]==1? 1:0);
             if(c[i]!=0 && i!=n)cnt++;
         }
        /* fo(i,0,q)
         {
             if(i!=k)
             {
                   res[i]=p[r[i]]-p[l[i]];
                   mini=min(mini,res[i]);
             }
         }*/
    int ans = -int(1e9);
	for(int i = 0; i < q; i++)
	{
		if(i == k) continue;
		ans = max(ans, p[l[i]] - p[r[i]]);
	}
	/*for(int i = 0; i < n; i++)
		if(c[i] > 0)
			ans++;*/
	return ans+cnt;
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
     cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define ii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define vii vector<int>
using namespace std;

int main()
{
    int n,q;
    cin>>n>>q;
    int l[5040],r[5040],only[5040];
    vii c[5040];
    fo(i,1,q+1)
    {
        cin>>l[i]>>r[i];
        fo(j,l[i],r[i]+1)
        /*if(c[j].size()<=5)*/c[j].pb(i);
    }
    int ans=0;
    fo(k,1,q+1)
    {
        memset(only,0,sizeof only);
        fo(j,1,n+1)
        {
            if(c[j].size()==1 && (j>r[k] || j<l[k]))
            {
                 only[c[j][0]]++;
                 //cout<<"hgf";
            }
            else if(j<=r[k]&&j>=l[k] )
            {
                if(c[j].size()==2)
               {
                   if(c[j][0]==k)
                  only[c[j][1]]++;
                  else only[c[j][0]]++;
                   //cout<<"gh";
                }
            }
        }
        //cout<<only[2]<<'\n';
        int mini=INT_MAX;
        fo(i,1,q+1)
        {
            //cout<<only[i];
            if(mini>only[i] && i!=k)mini=only[i];
        }
       // cout<<mini;
        int cnt=0;
        fo(i,1,n+1)
        {
           // cout<<c[i].size()<<' '<<i<<"\n";
               if(i>r[k]||i<l[k])
            {
                if(c[i].size()!=0)cnt++;
            }
            else
            {
              if(c[i].size()!=1 ) cnt++;
            }
        }
        //cout<<cnt;
        //cout<<mini;
       // cout<<cnt-mini<<" ";
          ans=max(cnt-mini,ans);
        //cout<<ans;
        //cout<<"\n";
    }
    cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int main()
{
    int n,q;
    cin>>n>>q;
    int l[5010],r[5010];
    int c[5010],c1[5010];
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int mini,ans=0;
    map<int,int>res,p,d;
    fo(k,0,q)
    {
        memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                  c1[l[i]]++;
                 c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       //fo(i,0,n)cout<<c[i]<<" ";
         fo(i,0,n)if(c[i]==1)d[i]=1;
          else d[i]=0;
         //fo(i,0,n)cout<<d[i]<<" ";
         fo(i,0,n+1)
         {
             p[i+1]=p[i]+d[i];
         }
         //fo(i,0,n)cout<<p[i]<<" ";
         fo(i,0,q)
         {
             if(i!=k)
             {
                  res[i]=p[r[i]]-p[l[i]];
                 mini=min(mini,res[i]);
             }
         }
         fo(i,0,n)
         if(c[i]!=0)cnt++;
         //cout<<cnt-mini<<" ";
         ans=max(ans,cnt-mini);
    }
   cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int main()
{
    int n,q;
    cin>>n>>q;
    int l[5010],r[5010];
    int c[5010],c1[5010],p[5010];
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int mini,ans=0;
    map<int,int>res,d;
    fo(k,0,q)
    {
        memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                  c1[l[i]]++;
                 c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
      // fo(i,0,n)cout<<c[i]<<" ";
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             if(c[i]==1)
             p[i+1]=p[i]+c[i];
             else
            p[i+1]=p[i];
         }
         //fo(i,0,n)cout<<p[i]<<" ";
         fo(i,0,q)
         {
             if(i!=k)
             {
                  res[i]=p[r[i]]-p[l[i]];
                 mini=min(mini,res[i]);
             }
         }
         fo(i,0,n)
         if(c[i]!=0)cnt++;
         //cout<<cnt-mini<<" ";
         ans=max(ans,cnt-mini);
    }
   cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int l[5040],r[5040];
int c[5040],c1[5040],p[5040],n,q,mini;
map<int,int>res;
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k==i)
                continue;
            c1[l[i]]++;
            c1[r[i]]--;
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             if(c[i]==1)
             p[i+1]=p[i]+c[i];
             else
            p[i+1]=p[i];
            if(c[i]!=0 && i!=n)cnt++;
         }
         fo(i,0,q)
         {
             if(i==k)continue;
                  res[i]=p[r[i]]-p[l[i]];
                 mini=min(mini,res[i]);
         }
    return cnt-mini;
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
     cout<<ans;
 }


/*#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++;
    cout<<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<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define ii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define vii vector<int>
using namespace std;

int main()
{
    int n,q;
    cin>>n>>q;
    map<int,int>only;
    int l[5010],r[5010];
    vii c[5010];
    fo(i,1,q+1)
    {
        cin>>l[i]>>r[i];
        fo(j,l[i],r[i]+1) c[j].pb(i);
    }
    int ans=0;
    fo(k,1,q+1)
    {
        fo(j,1,n+1)
        {
            if(c[j].size()==1 && (j>r[k] || j<l[k]))
            {
                 only[c[j][0]]++;
                 //cout<<"hgf";
            }
            else if(j<=r[k]&&j>=l[k] )
            {
                if(c[j].size()==2)
               {
                   if(c[j][0]==k)
                  only[c[j][1]]++;
                  else only[c[j][0]]++;
                   //cout<<"gh";
                }
            }
        }
        //cout<<only[2]<<'\n';
        int mini=INT_MAX;
        fo(i,1,q+1)
        {
            //cout<<only[i];
            if(mini>only[i] && i!=k)mini=only[i];
        }
       // cout<<mini;
        int cnt=0;
        fo(i,1,n+1)
        {
           // cout<<c[i].size()<<' '<<i<<"\n";
               if(i>r[k]||i<l[k])
            {
                if(c[i].size()!=0)cnt++;
            }
            else
            {
              if(c[i].size()!=1 ) cnt++;
            }
        }
        //cout<<cnt;
        //cout<<mini;
       // cout<<cnt-mini<<" ";
          ans=max(cnt-mini,ans);
          only.clear();
        //cout<<ans;
        //cout<<"\n";
    }
    cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int l[5040],r[5040];
int c[5040],c1[5040],p[5040],n,q,mini;
map<int,int>res;
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                  c1[l[i]]++;
                 c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             if(c[i]==1)
             p[i+1]=p[i]+c[i];
             else
            p[i+1]=p[i];
            if(c[i]!=0 && i!=n)cnt++;
         }
         fo(i,0,q)
         {
             if(i!=k)
             {
                  res[i]=p[r[i]]-p[l[i]];
                 mini=min(mini,res[i]);
             }
         }
    return cnt-mini;
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
     cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

 int l[5010],r[5010];
int c[5010],c1[5010],p[5010],n,q,mini;
map<int,int>res,d;
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                  c1[l[i]]++;
                 c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
      // fo(i,0,n)cout<<c[i]<<" ";
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             if(c[i]==1)
             p[i+1]=p[i]+c[i];
             else
            p[i+1]=p[i];
         }
         //fo(i,0,n)cout<<p[i]<<" ";
         fo(i,0,q)
         {
             if(i!=k)
             {
                  res[i]=p[r[i]]-p[l[i]];
                 mini=min(mini,res[i]);
             }
         }
         fo(i,0,n)
         if(c[i]!=0)cnt++;
         return cnt-mini;
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
   cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int l[5040],r[5040];
int c[5040],c1[5040],p[5040],n,q,mini,res[5040];
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                c1[l[i]]++;
               c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             p[i+1]=p[i]+(c[i]==1? 1:0);
             if(c[i]!=0 && i!=n)cnt++;
         }
         fo(i,0,q)
         {
             if(i!=k)
             {
                   res[i]=p[r[i]]-p[l[i]];
                   mini=min(mini,res[i]);
             }
         }
    /*int ans = -int(1e9);
	for(int i = 0; i < q; i++)
	{
		if(i == k) continue;
		ans = max(ans, p[l[i]] - p[r[i]]);
	}*/
	/*for(int i = 0; i < n; i++)
		if(c[i] > 0)
			ans++;*/
	return (cnt-mini);
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
     cout<<ans;
 }


#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<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define MEM(f,g) memset(f,g,sizeof f)
#define vii vector<int>
using namespace std;

int l[5040],r[5040];
int c[5040],c1[5040],p[5040],n,q,mini;
map<int,int>res;
int solve(int k)
{
     memset(c,0,sizeof c);
        memset(c1,0,sizeof c1);
        fo(i,0,q)
        {
            if(k!=i)
            {
                c1[l[i]]++;
               c1[r[i]]--;
            }
        }
        int temp=0;
        fo(i,0,n)
        {
            temp+=c1[i];
            c[i]=temp;
        }
        mini=INT_MAX;
        int cnt=0;
       memset(p,0,sizeof p);
         fo(i,0,n+1)
         {
             p[i+1]=p[i]+(c[i]==1? 1:0);
             if(c[i]!=0 && i!=n)cnt++;
         }
         fo(i,0,q)
         {
             if(i!=k)
             {
                   res[i]=p[r[i]]-p[l[i]];
                   mini=min(mini,res[i]);
             }
         }
    /*int ans = -int(1e9);
	for(int i = 0; i < q; i++)
	{
		if(i == k) continue;
		ans = max(ans, p[l[i]] - p[r[i]]);
	}*/
	/*for(int i = 0; i < n; i++)
		if(c[i] > 0)
			ans++;*/
	return (cnt-mini);
}

int main()
{
    cin>>n>>q;
    fo(i,0,q)
    {
        cin>>l[i]>>r[i];
        l[i]--;
    }
    int ans=0;
    fo(k,0,q)
    {
        ans=max(ans,solve(k));
    }
     //cout<<cnt-mini<<" ";
     cout<<ans;
 }


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fo(i,j,n) for(register int i=j;i<n;i++)
#define S size()
#define vii vector<int>
using namespace std;

int main()
{
    int n,q;
    cin>>n>>q;
    int l[5040],r[5040];
    int c[5040],res[5040],p[5040];
    fo(i,1,q+1)
    {
        cin>>l[i]>>r[i];
        fo(j,l[i],r[i]+1) c[j]++;
    }
    int mini,ans=0;
    fo(k,1,q+1)
    {
        memset(p,0,sizeof p);
        memset(res,0,sizeof res);
        mini=INT_MAX;
        int cnt=0;
        fo(i,l[k],r[k]+1)c[i]--;
      // fo(i,1,n+1)cout<<c[i]<<" ";
        // fo(i,1,n+1)if(c[i]==1)d[i]=1;
          //else d[i]=0;
         //fo(i,1,n+1)cout<<d[i]<<" ";
         p[0]=0;
         fo(i,1,n+1)
         {
             if(c[i]==1)
             p[i]=p[i-1]+c[i];
             else p[i]=p[i-1];
         }
         //fo(i,1,n+1)cout<<p[i]<<" ";
         fo(i,1,q+1)
         {
             if(i!=k)
             {
                  res[i]=p[r[i]]-p[l[i]-1];
                 mini=min(mini,res[i]);
             }
         }
        // cout<<mini<<" ";
         fo(i,1,n+1)
         if(c[i]!=0)cnt++;
         //cout<<cnt-mini<<" ";
         ans=max(ans,cnt-mini);
          fo(i,l[k],r[k]+1)c[i]++;
          //fo(i,1,n+1)cout<<c[i]<<" ";
         //cout<<"\n";
    }
   cout<<ans;
 }


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    for(int f = 0; f < t; ++f){
        for(int s = f+1; s < t; ++s){
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(zap[s].fi <= zap[f].se)
                ZAB += pr_2[zap[f].se+1]-pr_2[zap[s].fi], ZAB -= pr_1[zap[f].se+1]-pr_1[zap[s].fi];
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    sort(all(zap));
    for(int f = 0; f < t; ++f){
        for(int s = 0; s < t; ++s){
            if(s == f) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            //if(min(zap[f].se, zap[s].se) >= max(zap[f].fi, zap[s].fi))
                ZAB += pr_2[min(zap[f].se, zap[s].se)+1]-pr_2[max(zap[f].fi, zap[s].fi)], ZAB -= pr_1[min(zap[f].se, zap[s].se)+1]-pr_1[max(zap[f].fi, zap[s].fi)];
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    sort(all(zap));
    for(int f = 0; f < t; ++f){
        for(int s = f+1; s < t; ++s){
            if(s == f) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(min(zap[f].se, zap[s].se) <= max(zap[f].fi, zap[s].fi))
                ZAB += pr_2[min(zap[f].se, zap[s].se)+1]-pr_2[max(zap[f].fi, zap[s].fi)];//, ZAB -= pr_1[min(zap[f].se, zap[s].se)+1]-pr_1[max(zap[f].fi, zap[s].fi)];
                //cout << f << " " << s << " " << ZAB << "::" << pr_1[zap[f].se+1] << "-" << pr_1[zap[f].fi] << "+" << pr_1[zap[s].se+1] << "-" << pr_1[zap[s].fi] << " and " << pr_2[min(zap[f].se, zap[s].se)+1] << "-" << pr_2[max(zap[f].fi, zap[s].fi)] << "\n";
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    for(int f = 0; f < t; ++f){
        for(int s = 0; s < t; ++s){
            if(s == t) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(zap[s].fi <= zap[f].se)
                ZAB += pr_2[zap[f].se+1]-pr_2[zap[s].fi], ZAB -= pr_1[zap[f].se+1]-pr_1[zap[s].fi];
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    sort(all(zap));
    for(int f = 0; f < t; ++f){
        for(int s = 0; s < t; ++s){
            if(s == f) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(min(zap[f].se, zap[s].se) >= max(zap[f].fi, zap[s].fi))
                ZAB += pr_2[min(zap[f].se, zap[s].se)+1]-pr_2[max(zap[f].fi, zap[s].fi)], ZAB -= pr_1[min(zap[f].se, zap[s].se)+1]-pr_1[max(zap[f].fi, zap[s].fi)];
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    sort(all(zap));
    for(int f = 0; f < t; ++f){
        for(int s = 0; s < t; ++s){
            if(s == f) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(min(zap[f].se, zap[s].se) <= max(zap[f].fi, zap[s].fi))
                ZAB += pr_2[min(zap[f].se, zap[s].se)+1]-pr_2[max(zap[f].fi, zap[s].fi)], ZAB -= pr_1[min(zap[f].se, zap[s].se)+1]-pr_1[max(zap[f].fi, zap[s].fi)];
                //cout << f << " " << s << " " << ZAB << "::" << pr_1[zap[f].se+1] << "-" << pr_1[zap[f].fi] << "+" << pr_1[zap[s].se+1] << "-" << pr_1[zap[s].fi] << " and " << pr_2[min(zap[f].se, zap[s].se)+1] << "-" << pr_2[max(zap[f].fi, zap[s].fi)] << "\n";
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    for(int f = 0; f < t; ++f){
        for(int s = 0; s < t; ++s){
            if(s == f) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(zap[s].fi <= zap[f].se)
                ZAB += pr_2[zap[f].se+1]-pr_2[zap[s].fi], ZAB -= pr_1[zap[f].se+1]-pr_1[zap[s].fi];
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    sort(all(zap));
    for(int f = 0; f < t; ++f){
        for(int s = f+1; s < t; ++s){
            if(s == f) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(min(zap[f].se, zap[s].se) <= max(zap[f].fi, zap[s].fi))
                ZAB += pr_2[min(zap[f].se, zap[s].se)+1]-pr_2[max(zap[f].fi, zap[s].fi)], ZAB -= pr_1[min(zap[f].se, zap[s].se)+1]-pr_1[max(zap[f].fi, zap[s].fi)];
                //cout << f << " " << s << " " << ZAB << "::" << pr_1[zap[f].se+1] << "-" << pr_1[zap[f].fi] << "+" << pr_1[zap[s].se+1] << "-" << pr_1[zap[s].fi] << " and " << pr_2[min(zap[f].se, zap[s].se)+1] << "-" << pr_2[max(zap[f].fi, zap[s].fi)] << "\n";
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < PII > tree(t);
    for(int i = 0; i < t; ++i){
        tree[i] = {0, i};
        for(int j = zap[i].fi; j <= zap[i].se; ++j){
            if(q[j] == 1)
                tree[i].fi++;
        }
    }
    sort(all(tree));
    for(int i = zap[tree[0].se].fi; i <= zap[tree[0].se].se; ++i)
        q[i]--;
    for(int i = zap[tree[1].se].fi; i <= zap[tree[1].se].se; ++i)
        q[i]--;
    int ans = 0;
    for(int i = 0; i < n; ++i)
        if(q[i])
            ans++;
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    for(int f = 0; f < t; ++f){
        for(int s = f+1; s < t; ++s){
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(zap[s].fi <= zap[f].se)
                ZAB += pr_2[zap[f].se+1]-pr_2[zap[s].fi];
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    int DANIK = 0;
    for(int i = 0; i < n; ++i)
        if(!q[i])
            DANIK++;
    sort(all(zap));
    for(int f = 0; f < t; ++f){
        for(int s = 0; s < t; ++s){
            if(s == f) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(min(zap[f].se, zap[s].se) >= max(zap[f].fi, zap[s].fi))
                ZAB += pr_2[min(zap[f].se, zap[s].se)+1]-pr_2[max(zap[f].fi, zap[s].fi)], ZAB -= pr_1[min(zap[f].se, zap[s].se)+1]-pr_1[max(zap[f].fi, zap[s].fi)];
            ans = max(ans, n-ZAB-DANIK);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef pair<long long, long long> PII;
signed main()
{
    int n, t;
    cin >> n >> t;
    vec < int > q(n);
    int a, b;
    int t1 = t;
    vec < PII > zap;
    while(t1--){
        cin >> a >> b;
        a--, b--;
        for(int i = a; i <= b; ++i)
            q[i]++;
        zap.pb({a, b});
    }
    vec < int > pr_1, pr_2;
    pr_1.pb(0), pr_2.pb(0);
    for(int i = 0; i < n; ++i){
        if(q[i] == 1)
            pr_1.pb(pr_1[i]+1), pr_2.pb(pr_2[i]);
        else if(q[i] == 2)
            pr_2.pb(pr_2[i]+1), pr_1.pb(pr_1[i]);
        else
            pr_1.pb(pr_1[i]), pr_2.pb(pr_2[i]);
    }
    int ans = -1;
    sort(all(zap));
    for(int f = 0; f < t; ++f){
        for(int s = f+1; s < t; ++s){
            if(s == f) continue;
            int ZAB = (pr_1[zap[f].se+1]-pr_1[zap[f].fi]+pr_1[zap[s].se+1]-pr_1[zap[s].fi]);
            if(zap[s].fi <= zap[f].se)
                ZAB += pr_2[zap[f].se+1]-pr_2[zap[s].fi], ZAB -= pr_1[zap[f].se+1]-pr_1[zap[s].fi];//, cout << zap[s].fi << " " << zap[f].se << "\t" << f <<  " " << s << "\n";
            ans = max(ans, n-ZAB);
        }
    }
    cout << ans;
    return 0;
}


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

typedef long long ll;

ll n,q;
vector<pair<ll,ll>> arr;

bool over(ll a,ll b,ll a1,ll b1) {
	ll s = max(a,a1);
	ll e = min(b,b1);
	if(e-s >= 0) return 1;
	return 0;
}

void solve()
{

	cin>>n>>q;
	// priority_queue<pair<ll,ll>> pq;
	arr.resize(q);
	ll mx = 0;
	for(int i=0;i<q;i++) {
		ll s,e;
		 cin>>s>>e;
		 arr[i]={s,e};
		 // pq.push({e-s,i});
		 if((e-s) > (arr[mx].second-arr[mx].first)) mx=i;
	 }
	 vector<bool> vis(n+1);
	 vector<bool> vis1(q+1);

	 ll tot=0;
	 while(tot != q-2) {
		 // cout<<mx<<" ";
		 ll s = arr[mx].first;
		 ll e = arr[mx].second;
		 for(int i=s;i<=e;i++) vis[i]=true;
		 mx=0;
		 for(int i=0;i<q;i++) {
			 if(vis1[i])continue;
			 ll s1=arr[i].first;
			 ll e1=arr[i].second;
			 if(over(s,e,s1,e1)){
				 if(e <= e1) s1=e+1;
				 else if(s >= s1) e1=s-1;
				 else e1=s1-1;
				 arr[i].first=s1;
				 arr[i].second=e1;
			 }
			 if(s1 > e1) {
				 vis1[i]=true;
				continue;}
			 if((e1-s1) > (arr[mx].second-arr[mx].first)) mx=i;
		 }
		 tot++;
	 }
	 // cout<<endl;
	ll ans=0;
	for(int i=1;i<=n;i++) if(vis[i])ans++;
	cout<<ans;

}

int main()
{
	// ios::sync_with_stdio(0);
	// cin.tie(0);

	int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
		cout<<"\n";
	}
	return 0;
}


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

typedef long long ll;

ll n,q;

void solve()
{

	cin>>n>>q;
	vector<pair<ll,ll>> arr(q);
	vector<ll> tot(n+7);
	for(int i=0;i<q;i++) {
		 cin>>arr[i].first>>arr[i].second;
		 tot[arr[i].first]++;
		 tot[arr[i].second+1]--;
	}
	ll mx=0;
	for(int id=0;id<q;id++) {
		tot[arr[id].first]--;
		tot[arr[id].second+1]++;
		vector<ll> pre(n+1);
		vector<ll> one(n+1);
		ll ans=0;
		for(int i=1;i<=n;i++) {
			pre[i]=pre[i-1] + tot[i];
			if(pre[i] == 1)one[i]=1;
			if(pre[i])ans++;
		}
		for(int i=1;i<=n;i++) {
			pre[i]=pre[i-1]+one[i];
		}
		ll mn=INT_MAX;
		for(int i=0;i<q;i++) {
			if(i==id) continue;
			mn = min(mn,pre[arr[i].second]-pre[arr[i].first-1]);
		}
		mx = max(mx,ans-mn);
		tot[arr[id].first]++;
		tot[arr[id].second+1]--;
		// cout<<ans<<","<<mn<<"  ";
	}
	cout<<endl;
	cout<<mx;

}

int main()
{
	// ios::sync_with_stdio(0);
	// cin.tie(0);

	int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
		cout<<"\n";
	}
	return 0;
}


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

typedef long long ll;

ll n,q;
vector<pair<ll,ll>> arr;

bool over(ll a,ll b,ll a1,ll b1) {
	ll s = max(a,a1);
	ll e = min(b,b1);
	if(e-s >= 0) return 1;
	return 0;
}

void solve()
{

	cin>>n>>q;
	// priority_queue<pair<ll,ll>> pq;
	arr.resize(q);
	ll mx = 0;
	for(int i=0;i<q;i++) {
		ll s,e;
		 cin>>s>>e;
		 arr[i]={s,e};
		 // pq.push({e-s,i});
		 if((e-s) > (arr[mx].second-arr[mx].first)) mx=i;
	 }
	 vector<bool> vis(n+1);

	 ll tot=0;
	 while(tot != q-2) {
		 // cout<<mx<<" ";
		 ll s = arr[mx].first;
		 ll e = arr[mx].second;
		 for(int i=s;i<=e;i++) vis[i]=true;
		 mx=0;
		 for(int i=0;i<q;i++) {
			 ll s1=arr[i].first;
			 ll e1=arr[i].second;
			 if(over(s,e,s1,e1)){
				 if(e <= e1) s1=e+1;
				 else if(s >= s1) e1=s-1;
				 else e1=s1-1;
				 arr[i].first=s1;
				 arr[i].second=e1;
			 }
			 if((e1-s1) > (arr[mx].second-arr[mx].first)) mx=i;
		 }
		 tot++;
	 }
	 // cout<<endl;
	ll ans=0;
	for(int i=1;i<=n;i++) if(vis[i])ans++;
	cout<<ans;

}

int main()
{
	// ios::sync_with_stdio(0);
	// cin.tie(0);

	int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
		cout<<"\n";
	}
	return 0;
}


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

typedef long long ll;

ll n,q;
vector<pair<ll,ll>> arr;

bool over(ll a,ll b,ll a1,ll b1) {
	ll s = max(a,a1);
	ll e = min(b,b1);
	if(e-s >= 0) return 1;
	return 0;
}

void solve()
{

	cin>>n>>q;
	// priority_queue<pair<ll,ll>> pq;
	arr.resize(q);
	ll mx = 0;
	for(int i=0;i<q;i++) {
		ll s,e;
		 cin>>s>>e;
		 arr[i]={s,e};
		 // pq.push({e-s,i});
		 if((e-s) > (arr[mx].second-arr[mx].first)) mx=i;
	 }
	 vector<bool> vis(n+1);
	 vector<bool> vis1(q);

	 ll tot=0;
	 while(tot != q-2) {
		 // cout<<mx<<" ";
		 ll s = arr[mx].first;
		 ll e = arr[mx].second;
		 arr[mx].second=s-1;
		 for(int i=s;i<=e;i++) vis[i]=true;
		 mx=0;
		 for(int i=0;i<q;i++) {
			 ll s1=arr[i].first;
			 ll e1=arr[i].second;
			 if(over(s,e,s1,e1)){
				 if(e <= e1) s1=e+1;
				 else if(s >= s1) e1=s-1;
				 else e1=s1-1;
				 arr[i].first=s1;
				 arr[i].second=e1;
			 }
			 if((e1-s1) > (arr[mx].second-arr[mx].first)) mx=i;
		 }
		 tot++;
	 }
	 // cout<<endl;
	ll ans=0;
	for(int i=1;i<=n;i++) if(vis[i])ans++;
	cout<<ans;

}

int main()
{
	// ios::sync_with_stdio(0);
	// cin.tie(0);

	int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
		cout<<"\n";
	}
	return 0;
}


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

typedef long long ll;

ll n,q;
vector<pair<ll,ll>> arr;

bool over(ll a,ll b,ll a1,ll b1) {
	ll s = max(a,a1);
	ll e = min(b,b1);
	if(e-s >= 0) return 1;
	return 0;
}

void solve()
{

	cin>>n>>q;
	// priority_queue<pair<ll,ll>> pq;
	arr.resize(n);
	ll mx = 0;
	for(int i=0;i<q;i++) {
		ll s,e;
		 cin>>s>>e;
		 arr[i]={s,e};
		 // pq.push({e-s,i});
		 if((e-s) > (arr[mx].second-arr[mx].first)) mx=i;
	 }
	 vector<bool> vis(n+1);
	 ll tot=0;
	 while(tot != q-2) {
		 // cout<<mx<<" ";
		 ll s = arr[mx].first;
		 ll e = arr[mx].second;
		 arr[mx].second=s-1;
		 for(int i=s;i<=e;i++) vis[i]=true;
		 mx=0;
		 for(int i=0;i<q;i++) {
			 ll s1=arr[i].first;
			 ll e1=arr[i].second;
			 if(over(s,e,s1,e1)){
				 if(e <= e1) s1=e+1;
				 else if(s >= s1) e1=s-1;
				 else e1=s1-1;
				 arr[i].first=s1;
				 arr[i].second=e1;
			 }
			 if((e1-s1) > (arr[mx].second-arr[mx].first)) mx=i;
		 }
		 tot++;
	 }
	 // cout<<endl;
	ll ans=0;
	for(int i=1;i<=n;i++) if(vis[i])ans++;
	cout<<ans;

}

int main()
{
	// ios::sync_with_stdio(0);
	// cin.tie(0);

	int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
		cout<<"\n";
	}
	return 0;
}


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

typedef long long ll;

ll n,q;
vector<pair<ll,ll>> arr;

bool over(ll a,ll b,ll a1,ll b1) {
	ll s = max(a,a1);
	ll e = min(b,b1);
	if(e-s >= 0) return 1;
	return 0;
}

void solve()
{

	cin>>n>>q;
	// priority_queue<pair<ll,ll>> pq;
	arr.resize(q);
	ll mx = 0;
	for(int i=0;i<q;i++) {
		ll s,e;
		 cin>>s>>e;
		 arr[i]={s,e};
		 // pq.push({e-s,i});
		 if((e-s) > (arr[mx].second-arr[mx].first)) mx=i;
	 }
	 vector<bool> vis(n+1);
	 vector<bool> vis1(n+1);

	 ll tot=0;
	 while(tot != q-2) {
		 // cout<<mx<<" ";
		 ll s = arr[mx].first;
		 ll e = arr[mx].second;
		 for(int i=s;i<=e;i++) vis[i]=true;
		 mx=0;
		 for(int i=0;i<q;i++) {
			 if(vis1[i])continue;
			 ll s1=arr[i].first;
			 ll e1=arr[i].second;
			 if(over(s,e,s1,e1)){
				 if(e <= e1) s1=e+1;
				 else if(s >= s1) e1=s-1;
				 else e1=s1-1;
				 arr[i].first=s1;
				 arr[i].second=e1;
			 }
			 if(s1 > e1) {
				 vis1[i]=true;
				continue;}
			 if((e1-s1) > (arr[mx].second-arr[mx].first)) mx=i;
		 }
		 tot++;
	 }
	 // cout<<endl;
	ll ans=0;
	for(int i=1;i<=n;i++) if(vis[i])ans++;
	cout<<ans;

}

int main()
{
	// ios::sync_with_stdio(0);
	// cin.tie(0);

	int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
		cout<<"\n";
	}
	return 0;
}


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

typedef long long ll;

ll n,q;
vector<pair<ll,ll>> arr;

bool over(ll a,ll b,ll a1,ll b1) {
	ll s = max(a,a1);
	ll e = min(b,b1);
	if(e-s >= 0) return 1;
	return 0;
}

void solve()
{

	cin>>n>>q;
	// priority_queue<pair<ll,ll>> pq;
	arr.resize(q);
	ll mx = 0;
	for(int i=0;i<q;i++) {
		ll s,e;
		 cin>>s>>e;
		 arr[i]={s,e};
		 // pq.push({e-s,i});
		 if((e-s) > (arr[mx].second-arr[mx].first)) mx=i;
	 }
	 vector<bool> vis(n+1);
	 vector<bool> vis1(n+1);

	 ll tot=0;
	 while(tot != q-2) {
		 // cout<<mx<<" ";
		 ll s = arr[mx].first;
		 ll e = arr[mx].second;
		 for(int i=s;i<=e;i++) vis[i]=true;
		 mx=0;
		 for(int i=0;i<q;i++) {
			 if(vis1[i])continue;
			 ll s1=arr[i].first;
			 ll e1=arr[i].second;
			 if(over(s,e,s1,e1)){
				 if(e <= e1) s1=e+1;
				 else if(s >= s1) e1=s-1;
				 else {
					 vis1[i]=true;
					 continue;
				 };
				 arr[i].first=s1;
				 arr[i].second=e1;
			 }
			 if((e1-s1) > (arr[mx].second-arr[mx].first)) mx=i;
		 }
		 tot++;
	 }
	 // cout<<endl;
	ll ans=0;
	for(int i=1;i<=n;i++) if(vis[i])ans++;
	cout<<ans;

}

int main()
{
	// ios::sync_with_stdio(0);
	// cin.tie(0);

	int t=1;
	// cin>>t;
	while(t--)
	{
		solve();
		cout<<"\n";
	}
	return 0;
}


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;

namespace CodeforcesTemplate
{
    public static class Permutation
    {
        private static bool Next(ref int[] arr)
        {
            int k, j, l;
            for (j = arr.Length - 2; (j >= 0) && (arr[j] >= arr[j + 1]); j--) { }
            if (j == -1)
            {
                arr = arr.OrderBy(c => c).ToArray();
                return false;
            }
            for (l = arr.Length - 1; (arr[j] >= arr[l]) && (l >= 0); l--) { }
            var tmp = arr[j];
            arr[j] = arr[l];
            arr[l] = tmp;
            for (k = j + 1, l = arr.Length - 1; k < l; k++, l--)
            {
                tmp = arr[k];
                arr[k] = arr[l];
                arr[l] = tmp;
            }
            return true;
        }
        public static IEnumerable<int[]> AllPermutations(int[] arr)
        {
            do yield return arr;
            while (Next(ref arr));
        }
    }

    class Program
    {
        private const string FILENAME = "distance4";

        private const string INPUT = FILENAME + ".in";

        private const string OUTPUT = FILENAME + ".out";

        private static Stopwatch _stopwatch = new Stopwatch();

        private static StreamReader _reader = null;
        private static StreamWriter _writer = null;

        private static string[] _curLine;
        private static int _curTokenIdx;

        private static char[] _whiteSpaces = new char[] { ' ', '\t', '\r', '\n' };

        private static string ReadNextToken()
        {
            if (_curTokenIdx >= _curLine.Length)
            {
                //Read next line
                string line = _reader.ReadLine();
                if (line != null)
                    _curLine = line.Split(_whiteSpaces, StringSplitOptions.RemoveEmptyEntries);
                else
                    _curLine = new string[] { };
                _curTokenIdx = 0;
            }

            if (_curTokenIdx >= _curLine.Length)
                return null;

            return _curLine[_curTokenIdx++];
        }

        private static int ReadNextInt()
        {
            return int.Parse(ReadNextToken());
        }

        private static void RunTimer()
        {
#if (DEBUG)

            _stopwatch.Start();

#endif
        }

        private static void Main(string[] args)
        {

#if (DEBUG)
            double before = GC.GetTotalMemory(false);
#endif

            Console.ReadLine();

            List<long> mas = Console.ReadLine().Split(' ').Select(long.Parse).ToList();

            long min = mas.Min();

            List<int> indexes = new List<int>();

            for (int i = 0; i < mas.Count; i++)
            {
                if (mas[i] == min)
                {
                    indexes.Add(i);
                }
            }

            int minWay = int.MaxValue;

            for (int i = 0; i < indexes.Count; i++)
            {
                for (int j = 0; j < indexes.Count; j++)
                {
                    int countWay = indexes[i] - indexes[j];

                    if (countWay < minWay && countWay > 0)
                    {
                        minWay = countWay;
                    }
                }
            }

            Console.WriteLine(minWay);

#if (DEBUG)
            _stopwatch.Stop();

            double after = GC.GetTotalMemory(false);

            double consumedInMegabytes = (after - before) / (1024 * 1024);

            Console.WriteLine($"Time elapsed: {_stopwatch.Elapsed}");

            Console.WriteLine($"Consumed memory (MB): {consumedInMegabytes}");

            Console.ReadKey();
#endif

        }

        private static int CountLeadZeros(string source)
        {
            int countZero = 0;

            for (int i = source.Length - 1; i >= 0; i--)
            {
                if (source[i] == '0')
                {
                    countZero++;
                }

                else
                {
                    break;
                }

            }

            return countZero;
        }

        private static string ReverseString(string source)
        {
            char[] reverseArray = source.ToCharArray();

            Array.Reverse(reverseArray);

            string reversedString = new string(reverseArray);

            return reversedString;
        }

        private static int[] CopyOfRange(int[] src, int start, int end)
        {
            int len = end - start;
            int[] dest = new int[len];
            Array.Copy(src, start, dest, 0, len);
            return dest;
        }

        private static string StreamReader()
        {

            if (_reader == null)
                _reader = new StreamReader(INPUT);
            string response = _reader.ReadLine();
            if (_reader.EndOfStream)
                _reader.Close();
            return response;

        }

        private static void StreamWriter(string text)
        {

            if (_writer == null)
                _writer = new StreamWriter(OUTPUT);
            _writer.WriteLine(text);

        }

        private static int BFS(int start, int end, int[,] arr)
        {
            Queue<KeyValuePair<int, int>> queue = new Queue<KeyValuePair<int, int>>();
            List<bool> visited = new List<bool>();
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                visited.Add(false);
            }

            queue.Enqueue(new KeyValuePair<int, int>(start, 0));
            KeyValuePair<int, int> node;
            int level = 0;
            bool flag = false;

            while (queue.Count != 0)
            {
                node = queue.Dequeue();
                if (node.Key == end)
                {
                    return node.Value;
                }
                flag = false;
                for (int i = 0; i < arr.GetLength(0); i++)
                {

                    if (arr[node.Key, i] == 1)
                    {
                        if (visited[i] == false)
                        {
                            if (!flag)
                            {
                                level = node.Value + 1;
                                flag = true;
                            }
                            queue.Enqueue(new KeyValuePair<int, int>(i, level));
                            visited[i] = true;
                        }
                    }
                }

            }
            return 0;

        }

        private static void Write(string source)
        {
            File.WriteAllText(OUTPUT, source);
        }

        private static string ReadString()
        {
            return File.ReadAllText(INPUT);
        }

        private static void WriteStringArray(string[] source)
        {
            File.WriteAllLines(OUTPUT, source);
        }

        private static string[] ReadStringArray()
        {
            return File.ReadAllLines(INPUT);
        }

        private static int[] GetIntArray()
        {
            return ReadString().Split(' ').Select(int.Parse).ToArray();
        }

        private static double[] GetDoubleArray()
        {
            return ReadString().Split(' ').Select(double.Parse).ToArray();
        }

        private static int[,] ReadINT2XArray(List<string> lines)
        {
            int[,] arr = new int[lines.Count, lines.Count];

            for (int i = 0; i < lines.Count; i++)
            {
                int[] row = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

                for (int j = 0; j < lines.Count; j++)
                {
                    arr[i, j] = row[j];
                }
            }

            return arr;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Problem911A
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            var arr = Console.ReadLine().Split(' ').Select(_ => int.Parse(_)).ToArray();
            var min = arr.Min();
            int li = -1;
            int md = int.MaxValue;
            for (int i = 0; i < arr.Length; i++)
            {
                if (min == arr[i])
                {
                    if (li != -1)
                        md = Math.Min(md, i - li);
                    li = i;
                }
            }
            Console.WriteLine(md);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> Mins = new List<int>();

            int n = int.Parse(Console.ReadLine());
            var inputs = Console.ReadLine().Split();
            int min = int.Parse(inputs[0]);
            for (int i = 0; i < n; ++i)
            {
                int x = int.Parse(inputs[i]);
                if (x == min) Mins.Add(i);
                else if (x < min)
                {
                    Mins.Clear();
                    min = x;
                    Mins.Add(i);
                }
            }
            int res = int.MaxValue;
            for (int i = 0; i < Mins.Count-1; ++i)
            {
                if (Mins[i + 1] - Mins[i] < res) res = Mins[i + 1] - Mins[i];
            }
            Console.Write(res);
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.NET
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            line = Console.ReadLine();
            int[] numbers = line.Split(' ').Select(x => int.Parse(x)).ToArray();
            int minim = numbers.Min();
            List<int> positions = new List<int>();
            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] == minim)
                {
                    positions.Add(i+1);
                }
            }

            int solution = int.MaxValue;
            for (int i = 0; i < positions.Count-1; i++)
            {
                solution = Math.Min(solution, positions[i + 1] - positions[i]);
            }
            Console.WriteLine(solution);
            Console.ReadLine();
        }
    }
}


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication41
    {
        class Program
        {
            static void Main(string[] args)
            {
                int n = int.Parse(Console.ReadLine());
                long[] a = Console.ReadLine().Split().Select(long.Parse).ToArray();
                long mi = a.Min();

                List<int> y = new List<int>();

                for (int i = 0; i < n; i++)
                {
                    if (a[i]==mi)
                    {
                        y.Add(i);
                    }

                }

                Console.WriteLine(y[y.Count-1]-y[y.Count-2]);

            }
        }
    }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication41
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            long[] a = Console.ReadLine().Split().Select(long.Parse).ToArray();
            long mi = a.Min();
            int m = Array.IndexOf(a,mi);

            List<int> y = new List<int>();
            y.Add(m);

            for (int i = m+1; i < n; i++)
            {
                if (a[i]==mi)
                {
                    y.Add(i);
                }

            }

            Console.WriteLine(y[y.Count-1]-y[y.Count-2]);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication41
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            long[] a = Console.ReadLine().Split().Select(long.Parse).ToArray();
            long mi = a.Min();
            int m = Array.IndexOf(a,mi);
            List<int> y = new List<int>();
            int c = 1;
            for (int i = m+1; i < n; i++)
            {
                if (a[i]!=mi)
                {
                    c++;
                }
                else
                {
                    y.Add(c);
                    c = 1;
                }

            }
            if (c!=1)
            {
                y.Add(c);
            }

            Console.WriteLine(y.Min());

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication43
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            long[] a = Console.ReadLine().Split().Select(long.Parse).ToArray();
            long mi = a.Min();

            List<int> y = new List<int>();

            for (int i = 0; i < n; i++)
            {
                if (a[i] == mi)
                {
                    y.Add(i);
                }

            }
            int q = y[1] - y[0];
            for (int i = 1; i < y.Count-1; i++)
            {
                q = Math.Min(q, y[i + 1] - y[i]);
            }
            Console.WriteLine(q);
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
public class CodeForces
{
#if TEST
    // To set this go to Project -> Properties -> Build -> General -> Conditional compilation symbols: -> enter 'TEST' into text box.
    const bool testing = true;
#else
const bool testing = false;
#endif

    static void program(TextReader input)
    {
        var n = int.Parse(input.ReadLine());
        var data = input.ReadLine().Split(' ').Select(long.Parse).ToList();
        var min = data.Min();

        var res = long.MaxValue;
        var prevMin = -1;
        for(var i = 0; i < data.Count; i++)
        {
            if(prevMin == -1)
            {
                if(data[i] == min)
                {
                    prevMin = i;
                    continue;
                }
            } else
            {
                if (data[i] == min)
                {
                    res = Math.Min(i - prevMin, res);
                    prevMin = i;
                    continue;
                }
            }
        }
        Console.WriteLine(res);
    }

    public static void Main(string[] args)
    {
        if (!testing)
        { // set testing to false when submiting to codeforces
            program(Console.In); // write your program in 'program' function (its your new main !)
            return;
        }

        Console.WriteLine("Test Case(1) => expected :");
        Console.WriteLine("1\n");
        Console.WriteLine("Test Case(1) => found    :");
        program(new StringReader("2\n3 3\n"));
        Console.WriteLine();

        Console.WriteLine("Test Case(2) => expected :");
        Console.WriteLine("2\n");
        Console.WriteLine("Test Case(2) => found    :");
        program(new StringReader("3\n5 6 5\n"));
        Console.WriteLine();

        Console.WriteLine("Test Case(3) => expected :");
        Console.WriteLine("3\n");
        Console.WriteLine("Test Case(3) => found    :");
        program(new StringReader("9\n2 1 3 5 4 1 2 3 1\n"));
        Console.WriteLine();

    }
}


using System;
class NearestMinimuns{
    static int closestMinimums(int[] ar, int n){
        int min = Int32.MaxValue;
        for(int i=0;i<n;i++){
            min = Math.Min(min, ar[i]);
        }
        int mindist = Int32.MaxValue, prev = -1;
        for(int i=0;i<n;i++){
            if(prev == -1){
                if(min==ar[i]){
                    prev = i;
                }
            }
            else{
                if(ar[i] == min){
                    mindist = Math.Min(i-prev, mindist);
                    prev = i;
                }
            }
        }
        return mindist;
    }
    static void Main(string[] args){
        int n = Convert.ToInt32(Console.ReadLine().Trim());
        string[] str = Console.ReadLine().Trim().Split(' ');
        int[] ar = Array.ConvertAll(str, Int32.Parse);
        Console.WriteLine(closestMinimums(ar, n));
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace mitscv
{
    class Program
    {
        static void Main()
        {
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            string[] ss = s2.Split(' ');
            int[] a = ss.Select(s => int.Parse(s)).ToArray();
            int dist = a.Length;
            int minval = a[0];
            int index = 0;
            for (int i = 1; i < a.Length; i++)
            {
                if (minval == a[i])
                {
                    if (i - index < dist)
                    {
                        dist = i - index;
                    }
                    index = i;
                }
                else if (minval > a[i])
                {
                    minval = a[i];
                    dist = a.Length;
                    index = i;
                }
            }
            Console.WriteLine(dist);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace mitscv
{
    class Program
    {
        static void Main()
        {
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            string[] ss = s2.Split(' ');
            int[] a = ss.Select(s => int.Parse(s));
            int dist = a.Length;
            int minval = a[i];
            int index = 0;
            for (int i = 1; i < a.Length; i++)
            {
                if (minval == a[i])
                {
                    if (i - index < dist)
                    {
                        dist = i - index;
                    }
                    index = i;
                }
                else if (minval > a[i])
                {
                    minval = a[i];
                    dist = a.Length;
                    index = i;
                }
            }
            Console.WriteLine(dist);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Codeforces
{
    class Program
    {
        static private int Compute(int[] a)
        {
            Dictionary<int, List<int>> d = new Dictionary<int, List<int>>();
            for (int i = 0; i < a.Length; i++)
            {
                if (d.ContainsKey(a[i]))
                {
                    d[a[i]].Add(i);
                }
                else
                {
                    d.Add(a[i], new List<int>(){i});
                }
            }
            int result = -1;
            foreach (var key in d.Keys)
            {
                int[] b = d[key].ToArray();
                for (int i = 1; i < b.Length; i++)
                {
                    if (result == -1 || b[i] - b[i - 1] < result)
                    {
                        result = b[i] - b[i -1];
                    }
                }
            }
            return result;
        }

        static public int Main()
        {
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            var ss = s2.Split(' ');
            int[] a = new int[ss.Length];
            if (a.Length != int.Parse(s1))
            {
                throw new Exception();
            }
            for (int i = 0; i < ss.Length; i++)
            {
                a[i] = int.Parse(ss[i]);
            }
            int result = Compute(a);
            Console.WriteLine(result);
            return 0;
        }

    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Codeforces
{
    class Program
    {
        static private int Compute(int[] a)
        {
            int result = 0;
            return result;
        }

        static public int Main()
        {
            StreamReader sr = new StreamReader("standard.input");
            string s1 = sr.ReadLine();
            string s2 = sr.ReadLine();
            sr.Close();
            var ss = s2.Split(" ");
            int[] a = new int[ss.Count];
            for (int i = 0; i < a.Count; i++)
            {
                a[i] = int.Parse(ss[i]);
            }
            int result = Compute(a);
            StreamWriter sw = new StreamWriter("standard.output");
            sw.WriteLine(result);
            sw.Close();
            return 0;
        }

    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Codeforces
{
    class Program
    {
        static private int Compute(int[] a)
        {
            Dictionary<int, List<int>> d = new Dictionary<int, List<int>>();
            for (int i = 0; i < a.Length; i++)
            {
                if (d.ContainsKey(a[i]))
                {
                    d[a[i]].Add(i);
                }
                else
                {
                    d.Add(a[i], new List<int>(){i});
                }
            }
            int result = -1;
            foreach (var key in d.Keys)
            {
                int[] b = d[key].ToArray();
                for (int i = 1; i < b.Length; i++)
                {
                    if (result == -1 || b[i] - b[i - 1] < result)
                    {
                        result = b[i] - b[i -1];
                    }
                }
                break;
            }
            return result;
        }

        static public int Main()
        {
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            var ss = s2.Split(' ');
            int[] a = new int[ss.Length];
            if (a.Length != int.Parse(s1))
            {
                throw new Exception();
            }
            for (int i = 0; i < ss.Length; i++)
            {
                a[i] = int.Parse(ss[i]);
            }
            int result = Compute(a);
            Console.WriteLine(result);
            return 0;
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace mitscv
{
    class Program
    {
        static void Main()
        {
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            string[] ss = s2.Split(' ').ToArray();
            int[] a = ss.Select(s => int.Parse(s)).ToArray();
            int dist = a.Length;
            int minval = a[i];
            int index = 0;
            for (int i = 1; i < a.Length; i++)
            {
                if (minval == a[i])
                {
                    if (i - index < dist)
                    {
                        dist = i - index;
                    }
                    index = i;
                }
                else if (minval > a[i])
                {
                    minval = a[i];
                    dist = a.Length;
                    index = i;
                }
            }
            Console.WriteLine(dist);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

int Main()
{
    StreamReader sr = new StreamReader("standard.input");
    string s1 = sr.ReadLine();
    string s2 = sr.ReadLine();
    sr.Close();

    int result = 0;

    StreamWriter sw = new StreamWriter("standard.output");
    sw.WriteLine(result);
    sw.Close();
    return 0;
}