#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    int n = s.length()-1;
    int len = pi[n];
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }

    for (int i = 0; i < n-1; ++i)
    if (pi[i] == len)
    {
        cout << s.substr(0,len);
        return 0;
    }
    len = pi[len]-1;
    if (len == 0 || len == -1)
    {
        cout << "Just a legend";
        return 0;
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    int n = s.length()-1;
    int len = pi[n];
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }
    for (int i = 0; i < n-1; ++i)
        if (pi[i] == len)
    {
        cout << s.substr(0,len);
        return 0;
    }
    len = pi[len-1];
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    for (int i = 0; i < pi.size(); ++i)
        cout << pi[i] << " ";
    cout << endl;
    int n = s.length()-1;
    int len = pi[n];
    if (cnt[len] == 1)
    {
        len = pi[len]-1;
        while (!cnt[len])
        {
            if (len == 0)
            {
                cout << "Just a legend";
                return 0;
            }
            len = pi[len]-1;
        }
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    int n = s.length()-1;
    int len = pi[n];
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }

    for (int i = 0; i < n-1; ++i)
    if (pi[i] == len)
    {
        cout << s.substr(0,len);
        return 0;
    }
    len = pi[len]-1;
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    int n = s.length()-1;
    int len = pi[n];
    if (len == 0 || cnt[len] == 1)
    {
        cout << "Just a legend";
        return 0;
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    int n = s.length()-1;
    int len = pi[n];
    if (cnt[len] == 1)
    {
        len = pi[len]-1;
        while (!cnt[len])
        {
            if (len == 0)
            {
                cout << "Just a legend";
                return 0;
            }
            len = pi[len]-1;
        }
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    int n = s.length()-1;
    int len = pi[n];
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }

    for (int i = 0; i < n-1; ++i)
    if (pi[i] == len)
    {
        cout << s.substr(0,len);
        return 0;
    }
    len = pi[len]-1;
    cout << len << endl;
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    for (int i = 0; i < pi.size(); ++i)
        cout << pi[i] << " ";
    cout << endl;
    int n = s.length()-1;
    int len = pi[n];
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }

    for (int i = 0; i < n-1; ++i)
    if (pi[i] == len)
    {
        cout << s.substr(0,len);
        return 0;
    }
    len = pi[len]-1;
    cout << len << endl;
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }
    cout << s.substr(0,len);
    return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }

int main()
{
    int l,r,m,n,i,f;
    string fx,sf,s;
    cin>>s;
    l=s.size();
    r=l/2;
    m=1;
    for(i=1;i<=l/2;i++)
    {
        fx=s.substr(0,i);
       //cout<<fx<<endl;
        m=s.find(fx,m);
        //cout<<m<<endl;
        if(m>=r&&m<n)
        r=m+1;
        else if(m<0||m>=l)
        {
            cout<<"Just a legend"<<endl;
            return 0;
        }
        //cout<<r<<endl;
        r=s.find(fx,r);
        //cout<<r<<endl;
        if(r>0&&r<l)
        {
           if(r+i==l)
           {
               cout<<fx<<endl;
               return 0;
           }
        }
        else if(r<0||r>=l)
        {
            cout<<"Just a legend"<<endl;
            return 0;
        }
        f=r;
        while(1)
        {
            f=s.find(fx,f+1);
            if(f>0&&f<l)
            {
                if(f+i==l)
               {
                 cout<<fx<<endl;
                 return 0;
               }
            }
            else
            break;
        }
    }
    return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
#define MaxNode 1000005
vector<int>v[2];
int ch[200];
int main()
{
    long l,i,j,cnt,ans=0,k=1,x;
    string str;
    //cout<<(k^1)<<endl;
   cin>>str;
   l=str.size();
   for(i=0;i<=l;i++)
   {
   v[0].pb(i);
   ch[str[i]]++;

   }
   if(ch[str[0]]==l&&l>=3)
   {
      for(i=0;i<l-2;i++)
      cout<<str[i];
      return 0;
   }
   for(i=1;i<l;i++)
   {
       cnt=0;
     for(x=0;x<v[k^1].size();x++)
     {
         j=v[k^1][x]+1;
       if(str[i-1]==str[j-1])
       {
           v[k].pb(j);
           if(j>=i)
           cnt++;
           if(j==l&&cnt>2)
           ans=i;
       }
     }
     v[k^1].clear();
     k=k^1;
     if(cnt<3)
     break;
     if(i>l/3&&ans==0)
     break;
   }
   for(i=0;i<ans;i++)
   cout<<str[i];
   if(ans==0)
   cout<<"Just a legend"<<endl;
    return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }

int main()
{
    long int l,r,m,i,f;
    string fx,sf,s,ans="";
    cin>>s;
    l=s.size();
    r=2;
    m=1;
    for(i=1;i<l;i++)
    {
        fx=s.substr(0,i);
       //cout<<fx<<endl;
        m=s.find(fx,m);
       // cout<<m<<endl;
        if(m>=r&&m<l)
        r=m+1;
        else if(m<0||m>=l)
        {
            break;
        }
        //cout<<r<<endl;
        r=s.find(fx,r);
       // cout<<r<<endl;
        if(r>0&&r<l)
        {
           if(r+i==l)
           {
               ans=fx;
           }
        }
        else if(r<0||r>=l)
        {
            break;
        }
        f=r;
        while(1)
        {
            f=s.find(fx,f+1);
            if(f>0&&f<l)
            {
                if(f+i==l)
               {
                 ans=fx;
                 break;
               }
               else if(f+i>l)
               break;
            }
            else
            break;
        }
    }
    if(ans.size()>0)
    {
        cout<<ans<<endl;
    }
    else
    cout<<"Just a legend"<<endl;
    return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }

int main()
{
    long int l,r,m,i,f;
    string fx,sf,s,ans="";
    cin>>s;
    l=s.size();
    r=l/2;
    m=1;
    for(i=1;i<l;i++)
    {
        fx=s.substr(0,i);
       //cout<<fx<<endl;
        m=s.find(fx,m);
        //cout<<m<<endl;
        if(m>=r&&m<l)
        r=m+1;
        else if(m<0||m>=l)
        {
            break;
        }
        //cout<<r<<endl;
        r=s.find(fx,r);
       // cout<<r<<endl;
        if(r>0&&r<l)
        {
           if(r+i==l)
           {
               ans=fx;
           }
        }
        else if(r<0||r>=l)
        {
            break;
        }
        f=r;
        while(1)
        {
            f=s.find(fx,f+1);
            if(f>0&&f<l)
            {
                if(f+i==l)
               {
                 ans=fx;
                 break;
               }
            }
            else
            break;
        }
    }
    if(ans.size()>0)
    {
        cout<<ans<<endl;
    }
    else
    cout<<"Just a legend"<<endl;
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<functional>
#include<math.h>
#include<assert.h>
#include<stdarg.h>
#include<time.h>
#include<limits.h>
#include<ctype.h>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<iostream>
#include<sstream>
using namespace std;

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define LPI(i,n) for(int i=0;i<(n);i++)
#define LPI1(i,a,b) for(int i=(a);i<=(b);i++)
#define LPIL(i,x) for(int i=0;x[i];i++)
#define LPD(i,n) for(int i=(n)-1;i>=0;i--)
#define LPD1(i,a,b) for(int i=(a);i>=(b);i--)
#define I(x) scanf("%d",&x)
#define RI(x) int x;I(x)
#define II(x,y) scanf("%d%d",&x,&y)
#define RII(x,y) int x,y;II(x,y)
#define III(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RIII(x,y,z) int x,y,z;III(x,y,z)
#define RS(x) scanf("%s",x)
#define PI(x) printf("%d\n",x)
#define PIS(x) printf("%d ",x)
#define CASET int ___T,cas=1;scanf("%d ",&___T);while(___T--)
#define CASEN0(n) int cas=1;while(scanf("%d",&n)!=EOF&&n)
#define CASEN(n) int cas=1;while(scanf("%d",&n)!=EOF)
#define MP make_pair
#define PB push_back
#define MS0(x) memset(x,0,sizeof(x))
#define MS1(x) memset(x,-1,sizeof(x))
#define SEP(x) ((x)?'\n':' ')
#define F first
#define S second
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
char s[1000005];
int z[1000005];
int main(){

    int L = 0, R = 0,n,max=0,p=0;
    scanf("%s",s);
    n=strlen(s);
for (int i = 1; i < n; i++)
{
  if (i > R)
  {
    L = R = i;
    while (R < n && s[R-L] == s[R])
    R++;
    z[i] = R-L; R--;
    if(i+z[i]==n&&z[i]<=max)
      {
         p=i;
         for( i=p;i<n;i++)
         cout<<s[i];
          cout<<endl;
          return 0;
      }
    if(z[i]>max)
    {
       // p=i;
        max=z[i];
    }

  }
  else
  {
    int k = i-L;
    if (z[k] < R-i+1)
    z[i] = z[k];
    else
    {
      L = i;
      while (R < n && s[R-L] == s[R])
       R++;
      z[i] = R-L; R--;
      if(i+z[i]==n&&z[i]<=max)
       {
         p=i;
         for( i=p;i<n;i++)
         cout<<s[i];
          cout<<endl;
          return 0;
      }
      if(z[i]>max)
      {
       // p=i;
        max=z[i];
      }

    }
  }
}
//cout<<max<<endl;

cout<<"Just a legend"<<endl;

return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }

int main()
{
    long int l,r,m,i,f;
    string fx,sf,s,ans="";
    cin>>s;
    l=s.size();
    r=2;
    m=1;
    for(i=1;i<l;i++)
    {
        fx=s.substr(0,i);
       //cout<<fx<<endl;
        m=s.find(fx,m);
       // cout<<m<<endl;
        if(m>=r&&m<l)
        r=m+1;
        else if(m<0||m>=l)
        {
            break;
        }
        //cout<<r<<endl;
        r=s.find(fx,r);
       // cout<<r<<endl;
        if(r>0&&r<l)
        {
           if(r+i==l)
           {
               ans=fx;
           }
        }
        else if(r<0||r>=l)
        {
            break;
        }
        f=r;
        while(1)
        {
            f=s.find(fx,f+1);
            if(f>0&&f<l)
            {
                if(f+i==l)
               {
                 ans=fx;
                 break;
               }
            }
            else
            break;
        }
    }
    if(ans.size()>0)
    {
        cout<<ans<<endl;
    }
    else
    cout<<"Just a legend"<<endl;
    return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
#define MaxNode 1000005
vector<int>v[2];
int ch[200];
int main()
{
    long l,i,j,cnt,ans=0,k=1,x;
    string str;
    //cout<<(k^1)<<endl;
   cin>>str;
   l=str.size();
   for(i=0;i<=l;i++)
   {
   v[0].pb(i);
   ch[str[i]]++;

   }
   if(ch[str[0]]==l&&l>=3)
   {
      for(i=0;i<l-2;i++)
      cout<<str[i];
      return 0;
   }
   for(i=1;i<l;i++)
   {
       cnt=0;
     for(x=0;x<v[k^1].size();x++)
     {
         j=v[k^1][x]+1;
       if(str[i-1]==str[j-1])
       {
           v[k].pb(j);
           if(j>=i)
           cnt++;
           if(j==l&&cnt>2)
           ans=i;
       }
     }
     v[k^1].clear();
     k=k^1;
     if(cnt<3)
     break;
   }
   for(i=0;i<ans;i++)
   cout<<str[i];
   if(ans==0)
   cout<<"Just a legend"<<endl;
    return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }

int main()
{
    long int l,r,m,i,f;
    string fx,sf,s,ans="";
    cin>>s;
    l=s.size();
    r=l/2;
    m=1;
    for(i=1;i<=l/2;i++)
    {
        fx=s.substr(0,i);
       //cout<<fx<<endl;
        m=s.find(fx,m);
        //cout<<m<<endl;
        if(m>=r&&m<l)
        r=m+1;
        else if(m<0||m>=l)
        {
            break;
        }
        //cout<<r<<endl;
        r=s.find(fx,r);
       // cout<<r<<endl;
        if(r>0&&r<l)
        {
           if(r+i==l)
           {
               ans=fx;
           }
        }
        else if(r<0||r>=l)
        {
            break;
        }
        f=r;
        while(1)
        {
            f=s.find(fx,f+1);
            if(f>0&&f<l)
            {
                if(f+i==l)
               {
                 ans=fx;
                 break;
               }
            }
            else
            break;
        }
    }
    if(ans.size()>0)
    {
        cout<<ans<<endl;
    }
    else
    cout<<"Just a legend"<<endl;
    return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }

int main()
{
    long int l,r,m,i,f;
    string fx,sf,s,ans="";
    cin>>s;
    l=s.size();
    r=2;
    m=1;
    for(i=1;i<l;i++)
    {
        fx=s.substr(0,i);
       //cout<<fx<<endl;
        m=s.find(fx,m);
       // cout<<m<<endl;
        if(m>=r&&m<l)
        r=m+1;
        else if(m<0||m>=l)
        {
            break;
        }
        //cout<<r<<endl;
        r=s.find(fx,r);
       // cout<<r<<endl;
        if(r>0&&r<l)
        {
           if(r+i==l)
           {
               ans=fx;
           }
        }
        else if(r<0||r>=l)
        {
            break;
        }
        f=r;
        while(l-f>ans.size())
        {
            f=s.find(fx,f+1);
            if(f>0&&f<l)
            {
                if(f+i==l)
               {
                 ans=fx;
                 break;
               }
               else if(f+i>l)
               break;
            }
            else
            break;
        }
    }
    if(ans.size()>0)
    {
        cout<<ans<<endl;
    }
    else
    cout<<"Just a legend"<<endl;
    return 0;
}


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

// #defines
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define sz(x) ((int)(x).size())
#define ms0(x) memset(x,0,sizeof(x))
#define ms1(x) memset(x,-1,sizeof(x))
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f first
#define s second
#define mp make_pair
#define pb push_back
#ifdef ONLINE_JUDGE
#define FILEIO(x) freopen(#x ".in","r",stdin);freopen(#x ".out","w",stdout);
#define FILEIOS freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#else
#define FILEIO(x) ;
#define FILEIOS ;
#endif
#define inf 1001001001
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
#define MaxNode 1000005
vector<int>v[2];
int ch[200];
int main()
{
    long l,i,j,cnt,ans=0,k=1,x;
    string str;
    //cout<<(k^1)<<endl;
   cin>>str;
   l=str.size();
   for(i=0;i<=l;i++)
   {
   v[0].pb(i);
   ch[str[i]]++;

   }
   if(ch[str[0]]==l&&l<=3)
   {
      for(i=0;i<l-2;i++)
      cout<<str[i];
      return 0;
   }
   for(i=1;i<l;i++)
   {
       cnt=0;
     for(x=0;x<v[k^1].size();x++)
     {
         j=v[k^1][x]+1;
       if(str[i-1]==str[j-1])
       {
           v[k].pb(j);
           if(j>=i)
           cnt++;
           if(j==l&&cnt>2)
           ans=i;
       }
     }
     v[k^1].clear();
     k=k^1;
     if(cnt<3)
     break;
   }
   for(i=0;i<ans;i++)
   cout<<str[i];
   if(ans==0)
   cout<<"Just a legend"<<endl;
    return 0;
}


/*Author - Dhananjay Singh (@Dhananjay_049)*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef vector<ll> vl;		typedef vector<int> vi;
typedef vector<vl> vvl;	  	typedef vector<vi> vvi;
typedef pair<ll, ll> pll;	typedef pair<int, int> pii;
#define forn(i,n) for(int i=0;i<n;i++)
#define rforn(i,n) for(int i=n-1;i>=0;i--)
#define forne(i,n) for(int i=1;i<=n;i++)
#define forse(i,s,e) for(int i=s;i<e;i++)
#define rforse(i,s,e) for(int i=e-1;i>=s;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define fill(a,x) memset(a,x,sizeof a);
#define trav(a,x) for(auto &a:x)
#define INF	ll(1e18)
#ifdef DHANANJAY_049
#include "../code-pieces/trace.h"
#else
#define trace(args...)
#endif
//ordered_set T; T.insert(val);*T.find_by_order(index);T.order_of_key(val);
//find_by_order(k): It returns to an iterator to the kth element (counting from zero)
//order_of_key(k) : It returns to the number of items that are strictly smaller than our item k
//Thus we can now solve the above problem easily i.e. count of elements between l and r can be found by:
//o_set.order_of_key(r+1) – o_set.order_of_key(l)
ll modpow(ll a, ll b, ll m = MOD) {
	a %= m; ll res = 1; while (b) {if (b & 1)res = res * a % m; a = a * a % m; b >>= 1;}
	return res;
}
ll bpow(ll a, ll b) {ll res = 1; while (b) {if (b & 1)res = res * a; a = a * a; b >>= 1;} return res;}
ll modinv(ll a,ll m = MOD) {return modpow(a, m - 2, m);}
void graph(vvi &adj, int m) {int x, y; forn(i, m) {cin >> x >> y; adj[x - 1].pb(y - 1); adj[y - 1].pb(x - 1);}}
 struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
const int mxN = 2e5 + 5;
int t[5005*4];

void update(int v, int tl, int tr, int l, int r, int add) {
    if (l > r)
        return;
    if (l == tl && r == tr) {
        t[v] += add;
    } else {
        int tm = (tl + tr) / 2;
        update(v*2, tl, tm, l, min(r, tm), add);
        update(v*2+1, tm+1, tr, max(l, tm+1), r, add);
    }
}

int get(int v, int tl, int tr, int pos) {
    if (tl == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        return t[v] + get(v*2, tl, tm, pos);
    else
        return t[v] + get(v*2+1, tm+1, tr, pos);
}

void solve(){
   int n,q;
   cin>>n>>q;
    vector<pair<int,int>> v;
    forn(i,q){
        //cout<<"HELLO\n";
        int x,y;
        cin>>x>>y;
        update(1,1,n+1,x,y,1);
        v.push_back({x,y});
    }
    int sum=0;
    int a[n+1];
    vector<int> store[2];
    forse(i,1,n+1){
        a[i]=get(1,1,n+1,i);
        if(a[i]){sum++;}
        if(a[i]==1||a[i]==2){
            store[a[i]-1].push_back(i);
        }
    }

    int ans=-1;
    forn(i,q){
        int l = v[i].first,r=v[i].second;
        forse(j,i+1,q){
            int temp = sum;
            int l1 = v[j].first,r1=v[j].second;
            if(r1<l||r<l1){
                int a = lower_bound(store[0].begin(),store[0].end(),l)-store[0].begin();
                int b = upper_bound(store[0].begin(),store[0].end(),r)-store[0].begin();
                temp-=(b-a);
                a = lower_bound(store[0].begin(),store[0].end(),l1)-store[0].begin();
                b = upper_bound(store[0].begin(),store[0].end(),r1)-store[0].begin();
                temp-=(b-a);
            } else {
                int x = max(l,l1);
                int y = min(r,r1);
                l = min(l,l1);
                r = max(r,r1);
                int a = lower_bound(store[0].begin(),store[0].end(),l)-store[0].begin();
                int b = upper_bound(store[0].begin(),store[0].end(),x-1)-store[0].begin();
                temp-=(b-a);
                a = lower_bound(store[0].begin(),store[0].end(),y+1)-store[0].begin();
                b = upper_bound(store[0].begin(),store[0].end(),r)-store[0].begin();
                temp-=(b-a);
                a = lower_bound(store[1].begin(),store[1].end(),x)-store[1].begin();
                b = upper_bound(store[1].begin(),store[1].end(),y)-store[1].begin();
                temp-=(b-a);
            }
            ans=max(temp,ans);
        }
    }
    cout<<ans<<'\n';
}
int main() {
	ios_base::sync_with_stdio(false);
	//cin.tie(NULL);
	//cout.tie(NULL);
#ifdef DHANANJAY_049
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
#endif
	int t = 1;
	//cin >> t;
	while (t--) {
        solve();
	}
}


/*Author - Dhananjay Singh (@Dhananjay_049)*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef vector<ll> vl;		typedef vector<int> vi;
typedef vector<vl> vvl;	  	typedef vector<vi> vvi;
typedef pair<ll, ll> pll;	typedef pair<int, int> pii;
#define forn(i,n) for(int i=0;i<n;i++)
#define rforn(i,n) for(int i=n-1;i>=0;i--)
#define forne(i,n) for(int i=1;i<=n;i++)
#define forse(i,s,e) for(int i=s;i<e;i++)
#define rforse(i,s,e) for(int i=e-1;i>=s;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define fill(a,x) memset(a,x,sizeof a);
#define trav(a,x) for(auto &a:x)
#define INF	ll(1e18)
#ifdef DHANANJAY_049
#include "../code-pieces/trace.h"
#else
#define trace(args...)
#endif
//ordered_set T; T.insert(val);*T.find_by_order(index);T.order_of_key(val);
//find_by_order(k): It returns to an iterator to the kth element (counting from zero)
//order_of_key(k) : It returns to the number of items that are strictly smaller than our item k
//Thus we can now solve the above problem easily i.e. count of elements between l and r can be found by:
//o_set.order_of_key(r+1) – o_set.order_of_key(l)
ll modpow(ll a, ll b, ll m = MOD) {
	a %= m; ll res = 1; while (b) {if (b & 1)res = res * a % m; a = a * a % m; b >>= 1;}
	return res;
}
ll bpow(ll a, ll b) {ll res = 1; while (b) {if (b & 1)res = res * a; a = a * a; b >>= 1;} return res;}
ll modinv(ll a,ll m = MOD) {return modpow(a, m - 2, m);}
void graph(vvi &adj, int m) {int x, y; forn(i, m) {cin >> x >> y; adj[x - 1].pb(y - 1); adj[y - 1].pb(x - 1);}}
 struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
const int mxN = 2e5 + 5;
int t[5005*4];

void update(int v, int tl, int tr, int l, int r, int add) {
    if (l > r)
        return;
    if (l == tl && r == tr) {
        t[v] += add;
    } else {
        int tm = (tl + tr) / 2;
        update(v*2, tl, tm, l, min(r, tm), add);
        update(v*2+1, tm+1, tr, max(l, tm+1), r, add);
    }
}

int get(int v, int tl, int tr, int pos) {
    if (tl == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        return t[v] + get(v*2, tl, tm, pos);
    else
        return t[v] + get(v*2+1, tm+1, tr, pos);
}

void solve(){
   int n,q;
   cin>>n>>q;
    vector<pair<int,int>> v;
    forn(i,q){
        //cout<<"HELLO\n";
        int x,y;
        cin>>x>>y;
        update(1,1,n+1,x,y,1);
        v.push_back({x,y});
    }
    int sum=0;
    int a[n+1];
    vector<int> store[2];
    forse(i,1,n+1){
        a[i]=get(1,1,n+1,i);
        if(a[i]){sum++;}
        if(a[i]==1||a[i]==2){
            store[a[i]-1].push_back(i);
        }
       // cout<<a[i]<<' ';
    }
    //cout<<'\n';

    int ans=0;
    forn(i,q){
        int l = v[i].first,r=v[i].second;
        forse(j,i+1,q){
            int temp = sum;
            int l1 = v[j].first,r1=v[j].second;
            if(r1<l||r<l1){
                int a = lower_bound(store[0].begin(),store[0].end(),l)-store[0].begin();
                int b = upper_bound(store[0].begin(),store[0].end(),r)-store[0].begin();
                temp-=(b-a);
                a = lower_bound(store[0].begin(),store[0].end(),l1)-store[0].begin();
                b = upper_bound(store[0].begin(),store[0].end(),r1)-store[0].begin();
                temp-=(b-a);
            } else {
                int x = max(l,l1);
                int y = min(r,r1);
                l = min(l,l1);
                r = max(r,r1);
                int a = lower_bound (store[0].begin(),store[0].end(),l)-store[0].begin();
                int b = upper_bound(store[0].begin(),store[0].end(),x-1)-store[0].begin();
                temp-=(b-a);
                a = lower_bound(store[0].begin(),store[0].end(),y+1)-store[0].begin();
                b = upper_bound(store[0].begin(),store[0].end(),r)-store[0].begin();
                temp-=(b-a);
                a = lower_bound(store[1].begin(),store[1].end(),x)-store[1].begin();
                b = upper_bound(store[1].begin(),store[1].end(),y)-store[1].begin();
                temp-=(b-a);
            }
           // cout<<i<<' '<<j<<' '<<temp<<'\n';
            ans=max(temp,ans);
        }
    }
    cout<<ans<<'\n';
}
int main() {
	ios_base::sync_with_stdio(false);
	//cin.tie(NULL);
	//cout.tie(NULL);
#ifdef DHANANJAY_049
	freopen("inp.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
#endif
	int t = 1;
	//cin >> t;
	while (t--) {
        solve();
	}
}


#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/detail/standard_policies.hpp>
#define sz(s) (int)(s.size())
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
#define fi first
#define se second
#define nxtl '\n'
#define rep(a,b,c) for(int a=b; a<=c;++a)
#define per(a,b,c) for(int a=b; a>=c;--a)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
const double pi = (double)(acos(-1.0)), eps = (double)(1e-7);
const int mod = 1e9+7, Pr = 15937, inf = 1<<30, MAXN = 5e3+17;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
template<typename T>inline bool updmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<typename T>inline bool updmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
template<typename T>inline T gcd(T a, T b) {while(a && b) a > b ? a %= b : b %= a; return a + b;}
int l[MAXN], r[MAXN], val[MAXN], cnt[3][MAXN];
inline int get(int i, int l, int r) {
	if(l > r)
		return 0;
	return cnt[i][r]-cnt[i][l-1];
}
int main() { _
	#ifdef local
		freopen (".in", "r", stdin);
		// freopen (".out", "w", stdout);
	#endif
	int n, q; cin >> n >> q;
	rep(i, 1, q) {
		cin >> l[i] >> r[i];
		rep(j, l[i], r[i])
			++val[j];
	}
	rep(i, 0, 2)
		rep(j, 1, n)
			cnt[i][j] = cnt[i][j-1]+(val[j] == i);
	int mx = 0;
	rep(i, 1, q) {
		rep(j, i+1, q) {
			bool swapped = 0;
			if(l[i] > l[j]) {
				swap(i, j);
				swapped = 1;
			}
			if(l[j] > r[i])
				updmax(mx, n-cnt[0][n]-get(1, l[i], r[i])-get(1, l[j], r[j]));
			else
				updmax(mx, n-cnt[0][n]-get(1, l[i], l[j]-1)-get(1, min(r[i], r[j])+1, max(r[i], r[j]))-get(2, l[j], min(r[i], r[j])));
			if(swapped)
				swap(i, j);
		}
	}
	cout << mx << nxtl;
	return 0;
}


//In the name of God
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include<bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>*/

#define sz(s) ((int)(s.size()))
#define all(s) s.begin(),s.end()
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<nxtl
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = (int)5e3+500;
const char nxtl = '\n';
const int inf = (int)2e9+228;
const double eps = (double)1e-7;
const int mod = (int)1e9+7;
template<typename T>inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T>inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
/*template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2>
using ordered_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;*/
//find_by_order() order_of_key()

int f[maxn];
vector<int>t[maxn*4];
void build(int v, int tl, int tr) {
	if(tl == tr) {
		t[v].push_back(f[tl]);
		return;
	}
	int tm = tl+tr>>1;
	build(v<<1, tl, tm);
	build(v<<1|1, tm+1, tr);
	merge(all(t[v<<1]), all(t[v<<1|1]), back_inserter(t[v]));
}
int get(int v, int tl, int tr, int l, int r, int x) {
	if(tl > r || l > tr || l > r) return 0;
	if(l <= tl && tr <= r) return upper_bound(all(t[v]), x)-t[v].begin();
	int tm = tl+tr>>1;
	return get(v<<1, tl, tm, l, r, x)+get(v<<1|1, tm+1, tr, l, r, x);
}
int L[maxn], R[maxn];

signed main() {
#ifdef LOCAL
	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);cin.tie(0);
	unsigned int FOR;asm("rdtsc":"=A"(FOR));srand(FOR);
	int n, q; cin >> n >> q;
	rep(i, 1, q) {
		cin >> L[i] >> R[i];
		++f[L[i]];
		--f[R[i]+1];
	}
	rep(i, 1, n) f[i] += f[i-1];
	int all = 0;
	rep(i, 1, n) all += (f[i] > 0);
	build(1, 1, n);
	int mx = 0;
	rep(i, 1, q) rep(j, i+1, q) {
		if(L[i] > R[j] || L[j] > R[i]) {
			int fi = get(1, 1, n, L[i], R[i], 1);
			int se = get(1, 1, n, L[j], R[j], 1);
			cerr << ' ';
			updmax(mx, all-fi-se);
		} else {
			int l1, r1, l2, r2, l3, r3;
			if(L[i] <= L[j] && L[j] <= R[i]) {
				l1 = L[i]; l1 = L[j]-1;
				l2 = R[i]+1; l2 = L[j];
				l3 = L[j]; l3 = R[i];
			} else {
				l1 = L[j]; l1 = L[i]-1;
				l2 = R[j]+1; r2 = L[i];
				l3 = L[i]; l3 = R[j];
			}
			int fi = get(1, 1, n, l1, r1, 1);
			int se = get(1, 1, n, l2, r2, 1);
			int th = get(1, 1, n, l3, r3, 2);
			cerr << ' ';
			updmax(mx, all-fi-se-th);
		}
	}
	cout << mx << nxtl;
	return 0;
}


//In the name of God
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include<bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>*/

#define sz(s) ((int)(s.size()))
#define all(s) s.begin(),s.end()
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<nxtl
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = (int)5e3+500;
const char nxtl = '\n';
const int inf = (int)2e9+228;
const double eps = (double)1e-7;
const int mod = (int)1e9+7;
template<typename T>inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T>inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
/*template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2>
using ordered_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;*/
//find_by_order() order_of_key()

int f[maxn];
vector<int>t[maxn*4];
void build(int v, int tl, int tr) {
	if(tl == tr) {
		t[v].push_back(f[tl]);
		return;
	}
	int tm = tl+tr>>1;
	build(v<<1, tl, tm);
	build(v<<1|1, tm+1, tr);
	merge(all(t[v<<1]), all(t[v<<1|1]), back_inserter(t[v]));
}
int get(int v, int tl, int tr, int l, int r, int x) {
	if(tl > r || l > tr || l > r) return 0;
	if(l <= tl && tr <= r) return upper_bound(all(t[v]), x)-t[v].begin();
	int tm = tl+tr>>1;
	return get(v<<1, tl, tm, l, r, x)+get(v<<1|1, tm+1, tr, l, r, x);
}
int L[maxn], R[maxn];

signed main() {
#ifdef LOCAL
	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);cin.tie(0);
	unsigned int FOR;asm("rdtsc":"=A"(FOR));srand(FOR);
	int n, q; cin >> n >> q;
	rep(i, 1, q) {
		cin >> L[i] >> R[i];
		++f[L[i]];
		--f[R[i]+1];
	}
	rep(i, 1, n) f[i] += f[i-1];
	int all = 0;
	rep(i, 1, n) all += (f[i] > 0);
	build(1, 1, n);
	int mx = 0;
	rep(i, 1, q) rep(j, i+1, q) {
		if(L[i] > R[j] || L[j] > R[i]) {
			int fi = get(1, 1, n, L[i], R[i], 1);
			int se = get(1, 1, n, L[j], R[j], 1);
			cerr << i << " " << j << " " << all-fi-se << nxtl;
			updmax(mx, all-fi-se);
		} else {
			int l1, r1, l2, r2, l3, r3;
			if(L[i] <= L[j] && L[j] <= R[i]) {
				l1 = L[i]; l1 = L[j]-1;
				l2 = R[i]+1; l2 = L[j];
				l3 = L[j]; l3 = R[i];
			} else {
				l1 = L[j]; l1 = L[i]-1;
				l2 = R[j]+1; r2 = L[i];
				l3 = L[i]; l3 = R[j];
			}
			int fi = get(1, 1, n, l1, r1, 1);
			int se = get(1, 1, n, l2, r2, 1);
			int th = get(1, 1, n, l3, r3, 2);
			cerr << i << " " << j << " " << all-fi-se-th << nxtl;
			updmax(mx, all-fi-se-th);
		}
	}
	cout << mx << nxtl;
	return 0;
}


//In the name of God
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include<bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>*/

#define sz(s) ((int)(s.size()))
#define all(s) s.begin(),s.end()
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<nxtl
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = (int)5e3+5;
const char nxtl = '\n';
const int inf = (int)2e9+228;
const double eps = (double)1e-7;
const int mod = (int)1e9+7;
template<typename T>inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T>inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
/*template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2>
using ordered_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;*/
//find_by_order() order_of_key()

int n, q, all, mx;
int f[maxn], t[2][maxn][maxn];
inline int get(int l, int r, int x) {
	return l > r ? 0 : t[x][l][r];
}
int L[maxn], R[maxn];

signed main() {
#ifdef LOCAL
	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);cin.tie(0);
	unsigned int FOR;asm("rdtsc":"=A"(FOR));srand(FOR);
	cin >> n >> q;
	rep(i, 1, q) {
		cin >> L[i] >> R[i];
		++f[L[i]];
		--f[R[i]+1];
	}
	rep(i, 1, n) {
		f[i] += f[i-1];
		all += (f[i] > 0);
	}
	rep(x, 0, 1) rep(l, 1, n) rep(r, l, n)
		t[x][l][r] = t[x][l][r-1]+(f[r]<=x+1);
	rep(i, 1, q) rep(j, i+1, q) {
		if(L[i] > R[j] || L[j] > R[i]) {
			int fi = get(L[i], R[i], 0);
			int se = get(L[j], R[j], 0);
			updmax(mx, all-fi-se);
		} else {
			int l1, r1, l2, r2, l3, r3;
			if(L[i] <= L[j] && L[j] <= R[i]) {
				l1 = L[i]; r1 = L[j]-1;
				l2 = R[i]+1; r2 = L[j];
				l3 = L[j]; r3 = R[i];
			} else {
				l1 = L[j]; r1 = L[i]-1;
				l2 = R[j]+1; r2 = L[i];
				l3 = L[i]; r3 = R[j];
			}
			int fi = get(l1, r1, 0);
			int se = get(l2, r2, 0);
			int th = get(l3, r3, 1);
			updmax(mx, all-fi-se-th);
		}
	}
	cout << mx << nxtl;
	return 0;
}


//In the name of God
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include<bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>*/

#define sz(s) ((int)(s.size()))
#define all(s) s.begin(),s.end()
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<nxtl
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = (int)5e3+500;
const char nxtl = '\n';
const int inf = (int)2e9+228;
const double eps = (double)1e-7;
const int mod = (int)1e9+7;
template<typename T>inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T>inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
/*template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2>
using ordered_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;*/
//find_by_order() order_of_key()

int f[maxn];
vector<int>t[maxn*4];
void build(int v, int tl, int tr) {
	if(tl == tr) {
		t[v].push_back(f[tl]);
		return;
	}
	int tm = tl+tr>>1;
	build(v<<1, tl, tm);
	build(v<<1|1, tm+1, tr);
	merge(all(t[v<<1]), all(t[v<<1|1]), back_inserter(t[v]));
}
int get(int v, int tl, int tr, int l, int r, int x) {
	if(tl > r || l > tr || l > r) return 0;
	if(l <= tl && tr <= r) return upper_bound(all(t[v]), x)-t[v].begin();
	int tm = tl+tr>>1;
	return get(v<<1, tl, tm, l, r, x)+get(v<<1|1, tm+1, tr, l, r, x);
}
int L[maxn], R[maxn];

signed main() {
#ifdef LOCAL
	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);cin.tie(0);
	unsigned int FOR;asm("rdtsc":"=A"(FOR));srand(FOR);
	int n, q; cin >> n >> q;
	rep(i, 1, q) {
		cin >> L[i] >> R[i];
		++f[L[i]];
		--f[R[i]+1];
	}
	rep(i, 1, n) f[i] += f[i-1];
	int all = 0;
	rep(i, 1, n) all += (f[i] > 0);
	build(1, 1, n);
	int mx = 0;
	rep(i, 1, q) rep(j, i+1, q) {
		if(L[i] > R[j] || L[j] > R[i]) {
			int fi = get(1, 1, n, L[i], R[i], 1);
			int se = get(1, 1, n, L[j], R[j], 1);
			cerr << i;
			updmax(mx, all-fi-se);
		} else {
			int l1, r1, l2, r2, l3, r3;
			if(L[i] <= L[j] && L[j] <= R[i]) {
				l1 = L[i]; l1 = L[j]-1;
				l2 = R[i]+1; l2 = L[j];
				l3 = L[j]; l3 = R[i];
			} else {
				l1 = L[j]; l1 = L[i]-1;
				l2 = R[j]+1; r2 = L[i];
				l3 = L[i]; l3 = R[j];
			}
			int fi = get(1, 1, n, l1, r1, 1);
			int se = get(1, 1, n, l2, r2, 1);
			int th = get(1, 1, n, l3, r3, 2);
			cerr << i;
			updmax(mx, all-fi-se-th);
		}
	}
	cout << mx << nxtl;
	return 0;
}


//In the name of God
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include<bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>*/

#define sz(s) ((int)(s.size()))
#define all(s) s.begin(),s.end()
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<nxtl
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = (int)5e3+5;
const char nxtl = '\n';
const int inf = (int)2e9+228;
const double eps = (double)1e-7;
const int mod = (int)1e9+7;
template<typename T>inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T>inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
/*template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2>
using ordered_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;*/
//find_by_order() order_of_key()

int n, q, all, mx;
int f[maxn], t[3][maxn][maxn];
inline int get(int l, int r, int x) {
	return l > r ? 0 : t[x][l][r];
}
int L[maxn], R[maxn];

signed main() {
#ifdef LOCAL
	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);cin.tie(0);
	unsigned int FOR;asm("rdtsc":"=A"(FOR));srand(FOR);
	cin >> n >> q;
	rep(i, 1, q) {
		cin >> L[i] >> R[i];
		++f[L[i]];
		--f[R[i]+1];
	}
	rep(i, 1, n) {
		f[i] += f[i-1];
		all += (f[i] > 0);
	}
	rep(x, 1, 2) rep(l, 1, n) rep(r, l, n)
		t[x][l][r] = t[x][l][r-1]+(f[r]<=x);
	rep(i, 1, q) rep(j, i+1, q) {
		if(L[i] > R[j] || L[j] > R[i]) {
			int fi = get(L[i], R[i], 1);
			int se = get(L[j], R[j], 1);
			updmax(mx, all-fi-se);
		} else {
			int l1, r1, l2, r2, l3, r3;
			if(L[i] <= L[j] && L[j] <= R[i]) {
				l1 = L[i]; r1 = L[j]-1;
				l2 = R[i]+1; r2 = L[j];
				l3 = L[j]; r3 = R[i];
			} else {
				l1 = L[j]; r1 = L[i]-1;
				l2 = R[j]+1; r2 = L[i];
				l3 = L[i]; r3 = R[j];
			}
			int fi = get(l1, r1, 1);
			int se = get(l2, r2, 1);
			int th = get(l3, r3, 2);
			updmax(mx, all-fi-se-th);
		}
	}
	cout << mx << nxtl;
	return 0;
}


//In the name of God
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include<bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>*/

#define sz(s) ((int)(s.size()))
#define all(s) s.begin(),s.end()
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<nxtl
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = (int)5e3+500;
const char nxtl = '\n';
const int inf = (int)2e9+228;
const double eps = (double)1e-7;
const int mod = (int)1e9+7;
template<typename T>inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T>inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
/*template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2>
using ordered_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;*/
//find_by_order() order_of_key()

int f[maxn];
vector<int>t[maxn*4];
void build(int v, int tl, int tr) {
	if(tl == tr) {
		t[v].push_back(f[tl]);
		return;
	}
	int tm = tl+tr>>1;
	build(v<<1, tl, tm);
	build(v<<1|1, tm+1, tr);
	merge(all(t[v<<1]), all(t[v<<1|1]), back_inserter(t[v]));
}
int get(int v, int tl, int tr, int l, int r, int x) {
	if(tl > r || l > tr) return 0;
	if(l <= tl && tr <= r) return upper_bound(all(t[v]), x)-t[v].begin();
	int tm = tl+tr>>1;
	return get(v<<1, tl, tm, l, r, x)+get(v<<1|1, tm+1, tr, l, r, x);
}
int L[maxn], R[maxn], pref[maxn];

signed main() {
#ifdef LOCAL
	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);cin.tie(0);
	unsigned int FOR;asm("rdtsc":"=A"(FOR));srand(FOR);
	int n, q; cin >> n >> q;
	rep(i, 1, q) {
		cin >> L[i] >> R[i];
		++f[L[i]];
		--f[R[i]+1];
	}
	rep(i, 1, n) f[i] += f[i-1];
	int all = 0;
	rep(i, 1, n) all += (f[i] > 0);
	rep(i, 1, n) pref[i] = pref[i-1]+f[i];
	build(1, 1, n);
	int mx = 0;
	rep(i, 1, q) rep(j, i+1, q) {
		if(L[i] > R[j] || L[j] > R[i]) {
			int fi = get(1, 1, n, L[i], R[i], 1);
			int se = get(1, 1, n, L[j], R[j], 1);
			updmax(mx, all-fi-se);
		} else {
			int l1, r1, l2, r2, l3, r3;
			if(L[i] <= L[j] && L[j] <= R[i]) {
				l1 = L[i]; l1 = L[j]-1;
				l2 = R[i]+1; l2 = L[j];
				l3 = L[j]; l3 = R[i];
			} else {
				l1 = L[j]; l1 = L[i]-1;
				l2 = R[j]+1; r2 = L[i];
				l3 = L[i]; l3 = R[j];
			}
			int fi = get(1, 1, n, l1, r1, 1);
			int se = get(1, 1, n, l2, r2, 1);
			int th = get(1, 1, n, l3, r3, 2);
			updmax(mx, all-fi-se-th);
		}
	}
	cout << mx << nxtl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define INF 1000000000
#define EPS 1e-15
#define pb push_back
constexpr double pi = 3.14159265358979323846;

bool cmp(const pair< pair<int,int>, int>& l, const pair< pair<int,int>, int>& r) {
    return l.second > r.second;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,q; cin >> n >> q;
    vector<pair< pair<ll,ll>, ll> > f(q);
    for (int i(0); i < q; ++i) {
        ll l,r; cin >> l >> r;
        f[i] = {{l,r}, (abs(r-l)+1)};
    }
    sort(f.begin(),f.end());
    for (int i(1); i < q; ++i) {
        if (f[i-1].first.second > f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.first - f[i-1].first.second));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.first - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }
        if (f[i].first.second < f[i-1].first.second) {
            f[i].first.second = f[i-1].first.second;
        }
    }
    sort(f.begin(),f.end(),cmp);
    ll sum(0);
    for (int i(0); i < q-2; ++i) {
        sum += f[i].second;
    }
    cout << min(sum, n);
    cout << "\n";   // new line for vs-code
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define INF 1000000000
#define EPS 1e-15
#define pb push_back
constexpr double pi = 3.14159265358979323846;

bool cmp(const pair< pair<int,int>, int>& l, const pair< pair<int,int>, int>& r) {
    return l.second > r.second;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,q; cin >> n >> q;
    vector<pair< pair<ll,ll>, ll> > f(q);
    for (int i(0); i < q; ++i) {
        ll l,r; cin >> l >> r;
        f[i] = {{l,r}, (abs(r-l)+1)};
    }
    sort(f.begin(),f.end());
    for (int i(1); i < q; ++i) {
        if (f[i-1].first.second > f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.first - f[i-1].first.second));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.first - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }

        if (f[i-1].first.second > f[i].first.second) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.second - f[i-1].first.second));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.second - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.second) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }

        if (f[i].first.second < f[i-1].first.second) {
            f[i].first.second = f[i-1].first.second;
        }
    }
    sort(f.begin(),f.end(),cmp);
    ll sum(0);
    for (int i(0); i < q-2; ++i) {
        sum += f[i].second;
    }
    cout << sum;
    cout << "\n";   // new line for vs-code
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define INF 1000000000
#define EPS 1e-15
#define pb push_back
constexpr double pi = 3.14159265358979323846;

bool cmp(const pair< pair<int,int>, int>& l, const pair< pair<int,int>, int>& r) {
    return l.second > r.second;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,q; cin >> n >> q;
    vector<pair< pair<ll,ll>, ll> > f(q);
    for (int i(0); i < q; ++i) {
        ll l,r; cin >> l >> r;
        f[i] = {{l,r}, (abs(r-l)+1)};
    }
    sort(f.begin(),f.end());
    for (int i(1); i < q; ++i) {
        if (f[i-1].second >= f[i].second) {
                f[i].second = max((ll)0, f[i].second
                - max((ll)0, f[i].first.first - f[i-1].first.first)
                - max((ll)0, f[i-1].first.first - f[i].first.first)
                 - max((ll)0, f[i-1].first.second - f[i].first.second)
                 - max((ll)0, f[i].first.second - f[i-1].first.second));

            }
            else {
                f[i-1].second = max((ll)0, f[i-1].second
                - max((ll)0, f[i-1].first.first - f[i].first.first-1)
                - max((ll)0, f[i].first.first - f[i-1].first.first-1)
                 - max((ll)0, f[i].first.second - f[i-1].first.second-1)
                 - max((ll)0, f[i-1].first.second - f[i].first.second-1));
            }

        if (f[i].first.second < f[i-1].first.second) {
            f[i].first.second = f[i-1].first.second;
        }
        if (f[i].first.first > f[i-1].first.first) {
            f[i].first.first = f[i-1].first.first;
        }
    }
    sort(f.begin(),f.end(),cmp);
    ll sum(0);
    for (int i(0); i < q-2; ++i) {
        sum += f[i].second;
    }
    cout << min(n,sum);
    cout << "\n";   // new line for vs-code
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define INF 1000000000
#define EPS 1e-15
#define pb push_back
constexpr double pi = 3.14159265358979323846;

bool cmp(const pair< pair<int,int>, int>& l, const pair< pair<int,int>, int>& r) {
    return l.second > r.second;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,q; cin >> n >> q;
    vector<pair< pair<ll,ll>, ll> > f(q);
    for (int i(0); i < q; ++i) {
        ll l,r; cin >> l >> r;
        f[i] = {{l,r}, (abs(r-l)+1)};
    }
    sort(f.begin(),f.end());
    for (int i(1); i < q; ++i) {
        if (f[i-1].first.second > f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.first - f[i-1].first.second));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.first - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }

        if (f[i-1].first.first == f[i-1].first.second) {
            ;
        }
        else if (f[i-1].first.second > f[i].first.second) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.second - f[i-1].first.second));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.second - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.second) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }

        if (f[i].first.second < f[i-1].first.second) {
            f[i].first.second = f[i-1].first.second;
        }
    }
    sort(f.begin(),f.end(),cmp);
    ll sum(0);
    for (int i(0); i < q-2; ++i) {
        sum += f[i].second;
    }
    cout << min(n,sum);
    cout << "\n";   // new line for vs-code
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define INF 1000000000
#define EPS 1e-15
#define pb push_back
constexpr double pi = 3.14159265358979323846;

bool used[10000] = {false};

bool cmp(const pair< pair<int,int>, int>& l, const pair< pair<int,int>, int>& r) {
    return l.second > r.second;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    int n,q; cin >> n >> q;
    vector<pair< pair<int,int>, int> > f(q);
    for (int i(0); i < q; ++i) {
        int l,r; cin >> l >> r;
        f[i] = {{l,r}, (abs(r-l)+1)};
    }
    sort(f.begin(),f.end());
    for (int i(1); i < q; ++i) {
        if (f[i-1].first.second > f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max(0, f[i].second - abs(f[i].first.first - f[i-1].first.second));
            } else {
                f[i-1].second = max(0, f[i-1].second - abs(f[i-1].first.first - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max(0, f[i].second - 1);
            } else {
                f[i-1].second = max(0, f[i-1].second - 1);
            }
        }
    }
    sort(f.begin(),f.end(),cmp);
    ll sum(0);
    for (int i(0); i < q-2; ++i) {
        sum += f[i].second;
    }
    cout << sum;
    cout << "\n";   // new line for vs-code
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define INF 1000000000
#define EPS 1e-15
#define pb push_back
constexpr double pi = 3.14159265358979323846;

bool cmp(const pair< pair<int,int>, int>& l, const pair< pair<int,int>, int>& r) {
    return l.second > r.second;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,q; cin >> n >> q;
    vector<pair< pair<ll,ll>, ll> > f(q);
    for (int i(0); i < q; ++i) {
        ll l,r; cin >> l >> r;
        f[i] = {{l,r}, (abs(r-l)+1)};
    }
    sort(f.begin(),f.end());
    for (int i(1); i < q; ++i) {
        if (f[i-1].first.second > f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.first - f[i-1].first.second));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.first - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }
        if (f[i].first.second < f[i-1].first.second) f[i].first.second = f[i-1].first.second;
    }
    sort(f.begin(),f.end(),cmp);
    ll sum(0);
    for (int i(0); i < q-2; ++i) {
        sum += f[i].second;
    }
    cout << sum;
    cout << "\n";   // new line for vs-code
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define INF 1000000000
#define EPS 1e-15
#define pb push_back
constexpr double pi = 3.14159265358979323846;

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

int solve(int id) {
    fill(p1,p1+sizeof(p1)/sizeof(int),0);
    for (int i(0); i < q; ++i) {
        if (i==id) continue;
        ++p1[l[i]];
        --p1[r[i]];
    }
    fill(p2,p2+sizeof(p2)/sizeof(int),0);
    int c(0);
    for (int i(0); i < n; ++i) {
        c += p1[i];
        p2[i] = c;
    }
    fill(p3,p3+sizeof(p3)/sizeof(int),0);
    for (int i(0); i < n; ++i) {
        p3[i+1] = p3[i] + (p2[i] == 1 ? 1:0);
    }
    int ans = -numeric_limits<int>::max();
    for (int i(0); i < q; ++i) {
        if (i==id) 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(){
    ios::sync_with_stdio(false); cin.tie(0);
    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;
    cout << "\n";   // new line for vs-code
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define INF 1000000000
#define EPS 1e-15
#define pb push_back
constexpr double pi = 3.14159265358979323846;

bool cmp(const pair< pair<int,int>, int>& l, const pair< pair<int,int>, int>& r) {
    return l.second > r.second;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,q; cin >> n >> q;
    vector<pair< pair<ll,ll>, ll> > f(q);
    for (int i(0); i < q; ++i) {
        ll l,r; cin >> l >> r;
        f[i] = {{l,r}, (abs(r-l)+1)};
    }
    sort(f.begin(),f.end());
    for (int i(1); i < q; ++i) {
        if (f[i-1].first.second > f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.first - f[i-1].first.second));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.first - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }

        if (f[i-1].first.first == f[i-1].first.second) {
            ;
        }
        else if (f[i-1].first.second > f[i].first.second) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.second - f[i-1].first.second));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.second - f[i].first.second));
            }
        } else if (f[i-1].first.second == f[i].first.second) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }

        if (f[i-1].first.first > f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - abs(f[i].first.first - f[i-1].first.first));
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - abs(f[i-1].first.first - f[i].first.first));
            }
        } else if (f[i-1].first.first == f[i].first.first) {
            if (f[i-1].second > f[i].second) {
                f[i].second = max((ll)0, f[i].second - 1);
            } else {
                f[i-1].second = max((ll)0, f[i-1].second - 1);
            }
        }

        if (f[i].first.second < f[i-1].first.second) {
            f[i].first.second = f[i-1].first.second;
        }
    }
    sort(f.begin(),f.end(),cmp);
    ll sum(0);
    for (int i(0); i < q-2; ++i) {
        sum += f[i].second;
    }
    cout << min(n,sum);
    cout << "\n";   // new line for vs-code
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5005;
int n,m;
struct node
{
    int s;
    int x,y;
    node operator = (const node &a)
    {
        s=a.s;
        x=a.x;
        y=a.y;
        return *this;
    }

}d[maxn];
bool cmp(node a,node b)
{
    if(a.x!=b.x) return a.x<b.x;
    return a.y>b.y;
}
int run(int x,int y)
{
    int l=0,r=0,ans=0;
   // cout<<"ad  "<<x<<' '<<y<<endl;
    for(int i=1;i<=m;i++)
    {

        if(i==x||y==i) continue ;
        if(d[i].x<=r&&d[i].y>r &&l&&r)
        {
            ans+= d[i].y-r;
            r = d[i].y;
        }
        else if(d[i].y>y)
        {
            ans+=d[i].y-d[i].x+1;
            l = d[i].x;
            r = d[i].y;
        }
      //  cout<<l<<' '<<r<<' '<<ans<<' '<<"asd  "<<endl;
    }
    return ans;
}

int tag[maxn];
int main()
{
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        {
            cin>>d[i].x>>d[i].y;
        }

        sort(d+1,d+m+1,cmp);
        int ans=0;
        int maxs=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<i;j++)
            {
                maxs=max(maxs,run(i,j));
            }
        }
        cout<<maxs<<endl;
    }

}


#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>
using namespace std;
const int maxn=5005;
int n,m;
struct node
{
    int s;
    int x,y;
    node operator = (const node &a)
    {
        s=a.s;
        x=a.x;
        y=a.y;
        return *this;
    }

}d[maxn];
bool cmp(node a,node b)
{
    if(a.x!=b.x) return a.x<b.x;
    return a.y>b.y;
}
int run(int x,int y)
{
    int l=0,r=0,ans=0;
  //  cout<<"ad  "<<x<<' '<<y<<endl;
    for(int i=1;i<=m;i++)
    {

        if(i==x||y==i) continue ;
        if(d[i].x<=r&&d[i].y>r &&l&&r)
        {
            ans+= d[i].y-r;
            r = d[i].y;
        }
        else if(d[i].y>y)
        {
            ans+=d[i].y-d[i].x+1;
            l = d[i].x;
            r = d[i].y;
        }
       // cout<<l<<' '<<r<<' '<<ans<<' '<<"asd  "<<endl;
    }
    return ans;
}

int tag[maxn];
int main()
{
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        {
            cin>>d[i].x>>d[i].y;
        }

        sort(d+1,d+m+1,cmp);
        int ans=0;
        int maxs=0;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<i;j++)
            {
                maxs=max(maxs,run(i,j));
            }
        }
        cout<<maxs<<endl;
    }

}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5005;
struct node
{
    int x,y;
}d[maxn];
bool cmp(node &a,node &b)
{
   if(a.x!=b.x) return a.x<b.x;
    return a.y>b.y;
}

int dp[maxn][maxn];
int tag[maxn];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        {
            cin>>d[i].x>>d[i].y;
        }
        sort(d+1,d+m+1,cmp);
      //  cout<<endl;
        for(int i=1;i<=m;i++)
        {
          //  cout<<d[i].x<<' '<<d[i].y<<endl;
        }
      //  cout<<endl;
        int maxs=0;
        for(int i=1;i<=m;i++)
        {
            for(int j=0;j<i;j++)
            {
                int len;
                if(d[i].x<=d[j].y) len=d[i].y-d[j].y;
                else len=d[i].y-d[i].x+1;
                for(int k=1;k<=m-2;k++)
                {
                   // if(dp[j][k-1]+len < dp[i][k] ) break;
                    dp[i][k]=max(dp[i][k],dp[j][k-1]+ len );
                    maxs=max(maxs,dp[i][k]);
                   // cout<<"asd  "<<dp[i][k]<<' '<<i<<' '<<k<<' '<<j<<' '<<len<<endl;
                }
            }
        }
        cout<<maxs<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5e3+5;
int l[maxn],r[maxn];
int n,qq;
int tag[maxn],p[maxn],q[maxn];
int run(int v)
{
    memset(tag,0,sizeof(tag));
    memset(p,0,sizeof(p));
    memset(q,0,sizeof(q));
    for(int i=0;i<qq;i++)
    {
        if(i==v) continue ;
        tag[l[i]]++;
        tag[r[i]]--;
    }
    int sum=0,ans=0;
    for(int i=0;i<=n;i++)
    {
        sum+=tag[i];
        p[i]=sum;
        if(p[i]>0) ans++;
       // cout<<p[i]<<' ';
    }
   // cout<<endl;

    for(int i=1;i<=n;i++)
    {
        q[i]+=q[i-1]+( p[i-1]==1? 1:0 ) ;
    }
    int maxs=-1e9;
    for(int i=0;i<qq;i++)
    {
        if(i==v) continue ;
        maxs=max(maxs,q[l[i]] - q[r[i]] );
    }
   // cout<<"asd "<<ans<<' '<<maxs<<endl;
    return ans+maxs;

}
int main()
{

    while(cin>>n>>qq)
    {
        for(int i=0;i<qq;i++)
        {
            cin>>l[i]>>r[i];
            l[i]--;
        }
        int maxs=0;
        for(int i=0;i<qq;i++)
        {
            maxs=max(maxs,run(i));
        }
        cout<<maxs<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5005;
struct node
{
    int x,y;
}d[maxn];
bool cmp(node &a,node &b)
{
   if(a.x!=b.x) return a.x<b.x;
    return a.y<b.y;
}

int dp[maxn][maxn];
int tag[maxn];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        {
            cin>>d[i].x>>d[i].y;
        }
        sort(d+1,d+m+1,cmp);
      //  cout<<endl;
        for(int i=1;i<=m;i++)
        {
          //  cout<<d[i].x<<' '<<d[i].y<<endl;
        }
      //  cout<<endl;
        int maxs=0;
        for(int i=1;i<=m;i++)
        {
            for(int j=0;j<i;j++)
            {
                int len;
                if(d[i].x<=d[j].y) len=d[i].y-d[j].y;
                else len=d[i].y-d[i].x+1;
                for(int k=1;k<=m-2;k++)
                {
                    if(dp[j][k-1]+len < dp[i][k] ) break;
                    dp[i][k]=max(dp[i][k],dp[j][k-1]+ len );
                    maxs=max(maxs,dp[i][k]);
                   // cout<<"asd  "<<dp[i][k]<<' '<<i<<' '<<k<<' '<<j<<' '<<len<<endl;
                }
            }
        }
        cout<<maxs<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=3e3+5;
int l[maxn],r[maxn];
int n,qq;
int tag[maxn],p[maxn],q[maxn];
int run(int v)
{
    memset(tag,0,sizeof(tag));
    memset(p,0,sizeof(p));
    memset(q,0,sizeof(q));
    for(int i=0;i<qq;i++)
    {
        if(i==v) continue ;
        tag[l[i]]++;
        tag[r[i]]--;
    }
    int sum=0,ans=0;
    for(int i=0;i<=n;i++)
    {
        sum+=tag[i];
        p[i]=sum;
        if(p[i]>0) ans++;
       // cout<<p[i]<<' ';
    }
   // cout<<endl;

    for(int i=1;i<=n;i++)
    {
        q[i]+=q[i-1]+( p[i-1]==1? 1:0 ) ;
    }
    int maxs=-1e9;
    for(int i=0;i<qq;i++)
    {
        if(i==v) continue ;
        maxs=max(maxs,q[l[i]] - q[r[i]] );
    }
   // cout<<"asd "<<ans<<' '<<maxs<<endl;
    return ans+maxs;

}
int main()
{

    while(cin>>n>>qq)
    {
        for(int i=0;i<qq;i++)
        {
            cin>>l[i]>>r[i];
            l[i]--;
        }
        int maxs=0;
        for(int i=0;i<qq;i++)
        {
            maxs=max(maxs,run(i));
        }
        cout<<maxs<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5005;
struct node
{
    int s;
    int x,y;
    node operator = (const node &a)
    {
        s=a.s;
        x=a.x;
        y=a.y;
        return *this;
    }

}d[maxn],dp[maxn],p[maxn];
bool cmp(node &a,node &b)
{
   if(a.x!=b.x) return a.x<b.x;
    return a.y>b.y;
}

int tag[maxn];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        {
            cin>>d[i].x>>d[i].y;
        }
        sort(d+1,d+m+1,cmp);
//        cout<<endl;
//        for(int i=1;i<=m;i++)
//            cout<<d[i].x<<' '<<d[i].y<<endl;
//        cout<<endl;
        int maxs=0;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=m-2;j++)
            {
                p[j]=dp[j];
                int len;
                if(d[i].x<=p[j-1].y ) len=d[i].y-p[j-1].y;
                else len=d[i].y-d[i].x+1;
                if(p[j-1].s+len>dp[j].s)
                {
                    dp[j].s=p[j-1].s+len;
                    dp[j].x=d[i].x;
                    dp[j].y=d[i].y;
                }
               // cout<<dp[j].s<<' '<<' '<<len<<' '<<"a  "<<p[j-1].x<<' '<<p[j-1].y<<"   "<<d[i].x<<' '<<d[i].y<<endl;
            }
           // cout<<endl;
        }
        cout<<dp[m-2].s<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5005;
struct node
{
    int x,y;
}d[maxn];
bool cmp(node &a,node &b)
{
   if(a.x!=b.x) return a.x<b.x;
    return a.y<b.y;
}

int dp[maxn][maxn];
int tag[maxn];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        {
            cin>>d[i].x>>d[i].y;
        }
        sort(d+1,d+m+1,cmp);
    //    cout<<endl;
        for(int i=1;i<=m;i++)
        {
         //   cout<<d[i].x<<' '<<d[i].y<<endl;
        }
      //  cout<<endl;
        int maxs=0;
        for(int i=1;i<=m;i++)
        {
            for(int j=0;j<i;j++)
            {
                int len;
                if(d[i].x<=d[j].y) len=d[i].y-d[j].y;
                else len=d[i].y-d[i].x+1;
                for(int k=1;k<=m-2;k++)
                {
                    dp[i][k]=max(dp[i][k],dp[j][k-1]+ len );
                    maxs=max(maxs,dp[i][k]);
                   // cout<<"asd  "<<dp[i][k]<<' '<<i<<' '<<k<<' '<<j<<' '<<len<<endl;
                }
            }
        }
        cout<<maxs<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=3e3+5;
int l[maxn],r[maxn];
int n,qq;
int tag[maxn],p[maxn],q[maxn];
int run(int v)
{
    memset(tag,0,sizeof(tag));
    memset(p,0,sizeof(p));
    memset(q,0,sizeof(q));
    for(int i=0;i<qq;i++)
    {
        if(i==v) continue ;
        tag[l[i]]++;
        tag[r[i]]--;
    }
    int sum=0,ans=0;
    for(int i=0;i<n;i++)
    {
        sum+=tag[i];
        p[i]=sum;
        if(p[i]>0) ans++;
       // cout<<p[i]<<' ';
    }
   // cout<<endl;

    for(int i=1;i<=n;i++)
    {
        q[i]+=q[i-1]+( p[i-1]==1? 1:0 ) ;
    }
    int maxs=-1e9;
    for(int i=0;i<qq;i++)
    {
        if(i==v) continue ;
        maxs=max(maxs,q[l[i]] - q[r[i]] );
    }
   // cout<<"asd "<<ans<<' '<<maxs<<endl;
    return ans+maxs;

}
int main()
{

    while(cin>>n>>qq)
    {
        for(int i=0;i<qq;i++)
        {
            cin>>l[i]>>r[i];
            l[i]--;
        }
        int maxs=0;
        for(int i=0;i<qq;i++)
        {
            maxs=max(maxs,run(i));
        }
        cout<<maxs<<endl;
    }
}


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

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);  cout.tie(NULL);

	long n, q;  cin>>n>>q;

	pair<long, long> p[q+1];
	long mp[n+2]={0}, c1[n+1]={0}, c2[n+1]={0}, c=0;

	for(long i=1; i<=q; i++)
	{
		long l, r;  cin>>l>>r;
		p[i]=make_pair(l, r);

		mp[l]++;
		mp[r+1]--;
	}

	sort(p+1, p+q+1);

	for(long i=1; i<=n; i++)
	{
		mp[i]+=mp[i-1];
		c1[i]=c1[i-1];
		c2[i]=c2[i-1];

		if(mp[i])
		c++;

		if(mp[i]==1)
		c1[i]++;

		if(mp[i]==2)
		c2[i]++;
	}

	long max_c=0;

	for(long i=1; i<=q; i++)
	{
		long cnt=c;
		long li=p[i].first, ri=p[i].second;

//		for(long j=li; j<=ri; j++)
//		mp[j]--;

        for(long j=i+1; j<=q; j++)
        {
        	long lj=p[j].first, rj=p[j].second;

			if(lj<=ri)
			{
			    cnt-=(c2[min(ri, rj)]-c2[lj-1]);
			    cnt-=(c1[max(ri, rj)]-c1[li-1]);
			}

			else
			{
				cnt-=(c1[rj]-c1[lj-1]);
				cnt-=(c1[ri]-c1[li-1]);
			}

			max_c=max(max_c, cnt);
		}

	}

	cout<<max_c;
}


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

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);  cout.tie(NULL);

	long n, q;  cin>>n>>q;

	pair<long, long> p[q+1];
	long mp[n+2]={0}, c1[n+1]={0}, c2[n+1]={0}, c=0;

	for(long i=1; i<=q; i++)
	{
		long l, r;  cin>>l>>r;
		p[i]=make_pair(l, r);

		mp[l]++;
		mp[r+1]--;
	}

	for(long i=1; i<=n; i++)
	{
		mp[i]+=mp[i-1];
		c1[i]=c1[i-1];
		c2[i]=c2[i-1];

		if(mp[i])
		c++;

		if(mp[i]==1)
		c1[i]++;

		if(mp[i]==2)
		c2[i]++;
	}

	long max_c=0;

	for(long i=1; i<=q; i++)
	{
		long cnt=c;
		long li=p[i].first, ri=p[i].second;

//		for(long j=li; j<=ri; j++)
//		mp[j]--;

        for(long j=i+1; j<=q; j++)
        {
        	long lj=p[j].first, rj=p[j].second;

			if(lj>ri || li>rj)
			{
			    cnt-=(c1[rj]-c1[lj-1]);
				cnt-=(c1[ri]-c1[li-1]);
			}

			else
			{
				cnt-=(c2[min(ri, rj)]-c2[max(li, lj)-1]);
			    cnt-=(c1[max(ri, rj)]-c1[min(li, lj)-1]);
			}

			max_c=max(max_c, cnt);
		}

	}

	cout<<max_c;
}


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

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);  cout.tie(NULL);

	long n, q;  cin>>n>>q;

	pair<long, long> p[q+1];
	long mp[n+2]={0}, c1[n+1]={0}, c2[n+1]={0}, c=0;

	for(long i=1; i<=q; i++)
	{
		long l, r;  cin>>l>>r;
		p[i]=make_pair(l, r);

		mp[l]++;
		mp[r+1]--;
	}

	for(long i=1; i<=n; i++)
	{
		mp[i]+=mp[i-1];
		c1[i]=c1[i-1];
		c2[i]=c2[i-1];

		if(mp[i])
		c++;

		if(mp[i]==1)
		c1[i]++;

		if(mp[i]==2)
		c2[i]++;
	}

	long max_c=0;

	for(long i=1; i<=q; i++)
	{
		long li=p[i].first, ri=p[i].second;

//		for(long j=li; j<=ri; j++)
//		mp[j]--;

        for(long j=i+1; j<=q; j++)
        {
        	long cnt=c;

        	long lj=p[j].first, rj=p[j].second;

			if(lj>ri || li>rj)
			{
			    cnt-=(c1[rj]-c1[lj-1]);
				cnt-=(c1[ri]-c1[li-1]);
			}

			else
			{
				cnt-=(c2[min(ri, rj)]-c2[max(li, lj)-1]);
			    cnt-=(c1[max(ri, rj)]-c1[min(li, lj)-1]);
			}

			max_c=max(max_c, cnt);
		}

	}

	cout<<max_c;
}


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

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);  cout.tie(NULL);

	long n, q;  cin>>n>>q;

	pair<long, long> p[q+1];
	long mp[n+1]={0}, c1[n+1]={0}, c2[n+1]={0}, c=0;

	for(long i=1; i<=q; i++)
	{
		long l, r;  cin>>l>>r;
		p[i]=make_pair(l, r);

		for(long j=l; j<=r; j++)
		{
			if(mp[j]==0)
			c++;

			mp[j]++;
		}
	}

	sort(p+1, p+q+1);

	for(long i=1; i<=n; i++)
	{
		c1[i]=c1[i-1];
		c2[i]=c2[i-1];

		if(mp[i]==1)
		c1[i]++;

		if(mp[i]==2)
		c2[i]++;
	}

	long max_c=0;

	for(long i=1; i<=q; i++)
	{
		long cnt=c;

//		for(long j=1; j<=n; j++)
//		mp1[j]=mp[j];
//
//		for(long j=i+1; j<=q; j++)
//		{
//			for(long k=lj; k<=rj; k++)
//		    {
//			    mp1[k]--;
//			    if(mp1[k]==0)
//			    cnt--;
//	     	}
//
//	     	for(long k=li; k<=ri; k++)
//		    {
//			    mp1[k]--;
//			    if(mp1[k]==0)
//			    cnt--;
//	     	}
//
//	     	max_c=max(max_c, cnt);
//		}

        for(long j=i+1; j<=q; j++)
        {
        	long li=p[i].first, ri=p[i].second;
        	long lj=p[j].first, rj=p[j].second;

        	if(li==lj)
        	{
        		cnt-=(c2[ri]-c2[li-1]);
        		cnt-=(c1[rj]-c1[ri]);
			}

			else
			{
				cnt-=(c1[lj-1]-c1[li-1]);
				cnt-=(c2[min(ri, rj)]-c2[lj-1]);
				cnt-=(c1[max(ri, rj)]-c1[min(ri, rj)]);
			}

			max_c=max(max_c, cnt);
		}

	}

	cout<<max_c;
}


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

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);  cout.tie(NULL);

	long n, q;  cin>>n>>q;

	pair<long, long> p[q+1];
	long mp[n+1]={0}, c1[n+1]={0}, c2[n+1]={0}, c=0;

	for(long i=1; i<=q; i++)
	{
		long l, r;  cin>>l>>r;
		p[i]=make_pair(l, r);

		for(long j=l; j<=r; j++)
		{
			if(mp[j]==0)
			c++;

			mp[j]++;
		}
	}

	sort(p+1, p+q+1);

	for(long i=1; i<=n; i++)
	{
		c1[i]=c1[i-1];
		c2[i]=c2[i-1];

		if(mp[i]==1)
		c1[i]++;

		if(mp[i]==2)
		c2[i]++;
	}

	long max_c=0;

	for(long i=1; i<=q; i++)
	{
		long cnt=c;
		long li=p[i].first, ri=p[i].second;

//		for(long j=li; j<=ri; j++)
//		mp[j]--;

        for(long j=i+1; j<=q; j++)
        {
        	long lj=p[j].first, rj=p[j].second;

			if(lj<=ri)
			{
			    cnt-=(c2[min(ri, rj)]-c2[lj-1]);
			    cnt-=(c1[max(ri, rj)]-c1[li-1]);
			}

			else
			{
				cnt-=(c1[rj]-c1[lj-1]);
				cnt-=(c1[ri]-c1[li-1]);
			}

			max_c=max(max_c, cnt);
		}

	}

	cout<<max_c;
}


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

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);  cout.tie(NULL);

	long n, q;  cin>>n>>q;

	pair<long, long> p[q+1];
	long mp[n+1]={0}, c1[n+1]={0}, c2[n+1]={0}, c=0;

	for(long i=1; i<=q; i++)
	{
		long l, r;  cin>>l>>r;
		p[i]=make_pair(l, r);

		for(long j=l; j<=r; j++)
		{
			if(mp[j]==0)
			c++;

			mp[j]++;
		}
	}

	sort(p+1, p+q+1);

	for(long i=1; i<=n; i++)
	{
		c1[i]=c1[i-1];
		c2[i]=c2[i-1];

		if(mp[i]==1)
		c1[i]++;

		if(mp[i]==2)
		c2[i]++;
	}

	long max_c=0;

	for(long i=1; i<=q; i++)
	{
		long cnt=c;

        for(long j=i+1; j<=q; j++)
        {
        	long li=p[i].first, ri=p[i].second;
        	long lj=p[j].first, rj=p[j].second;

			if(lj<=ri)
			{
				cnt-=(c1[lj-1]-c1[li-1]);
			    cnt-=(c2[min(ri, rj)]-c2[lj-1]);
			    cnt-=(c1[max(ri, rj)]-c1[min(ri, rj)]);
			}

			else
			{
				cnt-=(c1[rj]-c1[lj-1]);
				cnt-=(c1[ri]-c1[li-1]);
			}

			max_c=max(max_c, cnt);
		}

	}

	cout<<max_c;
}


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

int main()
{
	long n, q;  cin>>n>>q;

	long l[q+1], r[q+1];
	long mp[n+1]={0}, c=0;

	for(long i=1; i<=q; i++)
	{
		cin>>l[i]>>r[i];

		for(long j=l[i]; j<=r[i]; j++)
		{
			if(mp[j]==0)
			c++;

			mp[j]++;
		}
	}

	long max_c=0;

	for(long i=1; i<=q; i++)
	{
		long cnt=c, mp1[n+1];

		for(long j=1; j<=n; j++)
		mp1[j]=mp[j];

		for(long j=i+1; j<=q; j++)
		{
			for(long k=l[j]; k<=r[j]; k++)
		    {
			    mp1[k]--;
			    if(mp1[k]==0)
			    cnt--;
	     	}

	     	for(long k=l[i]; k<=r[i]; k++)
		    {
			    mp1[k]--;
			    if(mp1[k]==0)
			    cnt--;
	     	}
	     	max_c=max(max_c, cnt);
		}
	}

	cout<<max_c;
}


#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 <= b && c <= d)
	{

		int curr = 0;

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

		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 - 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]);
	}

	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;

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 - 1] - less1[d - 1];

		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
}