import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static int MAX = 2000000;
    public static int tree[] = new int[MAX];
    public static void update(int qs,int qe,int cs,int ce,int ci){
        if(cs>ce||cs>qe||qs>ce){
            return;
        }
        if(cs>=qs&&ce<=qe){
            //System.out.println(ci);
            tree[ci]++;
            return;
        }
        int mid = (cs+ce)/2;
        update(qs,qe,cs,mid,2*ci+1);
        update(qs,qe,mid+1,ce,2*ci+2);
    }
    public static void lazyPropagation(){
        for(int i=0;i<=MAX/2;i++){
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        }
    }
    public static int get(int qs,int qe,int cs,int ce,int ci,int limit){
        if(cs>ce||cs>qe||qs>ce){
            return 0;
        }
        if(cs>=qs&&ce<=qe){
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        }
        int mid = (cs+ce)/2;
        int left = get(qs,qe,cs,mid,2*ci+1,limit);
        int right = get(qs,qe,mid+1,ce,2*ci+2,limit);
        return left+right;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        //int num[] = new int[100001];
        for(int i=0;i<M;i++){
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        }
        lazyPropagation();
        int ans = get(1,100000,1,100000,0,M );
        System.out.println(ans);
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {
    public static int MAX = 2000000;
    public static int tree[] = new int[MAX];
    public static void update(int qs,int qe,int cs,int ce,int ci){
        if(cs>ce||cs>qe||qs>ce){
            return;
        }
        if(cs>=qs&&ce<=qe){
            //System.out.println(ci);
            tree[ci]++;
            return;
        }
        int mid = (cs+ce)/2;
        update(qs,qe,cs,mid,2*ci+1);
        update(qs,qe,mid+1,ce,2*ci+2);
    }
    public static void lazyPropagation(){
        for(int i=0;i<=MAX/2;i++){
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        }
    }
    public static int get(int qs,int qe,int cs,int ce,int ci,int limit){
        if(cs>ce||cs>qe||qs>ce){
            return 0;
        }
        if(cs>=qs&&ce<=qe){
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        }
        int mid = (cs+ce)/2;
        int left = get(qs,qe,cs,mid,2*ci+1,limit);
        int right = get(qs,qe,mid+1,ce,2*ci+2,limit);
        return left+right;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        //int num[] = new int[100001];
        for(int i=0;i<M;i++){
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        }
        lazyPropagation();
        int ans = get(1,100000,1,100000,0,M );
        System.out.println(ans);
    }
} Main public static int MAX = 2000000; MAX 2000000 public static int tree[] = new int[MAX]; tree new int[MAX] MAX public static void update(int qs,int qe,int cs,int ce,int ci){
        if(cs>ce||cs>qe||qs>ce){
            return;
        }
        if(cs>=qs&&ce<=qe){
            //System.out.println(ci);
            tree[ci]++;
            return;
        }
        int mid = (cs+ce)/2;
        update(qs,qe,cs,mid,2*ci+1);
        update(qs,qe,mid+1,ce,2*ci+2);
    } update {
        if(cs>ce||cs>qe||qs>ce){
            return;
        }
        if(cs>=qs&&ce<=qe){
            //System.out.println(ci);
            tree[ci]++;
            return;
        }
        int mid = (cs+ce)/2;
        update(qs,qe,cs,mid,2*ci+1);
        update(qs,qe,mid+1,ce,2*ci+2);
    } if(cs>ce||cs>qe||qs>ce){
            return;
        } cs>ce||cs>qe||qs>ce cs>ce||cs>qe||qs>ce cs>ce cs ce cs>qe cs qe qs>ce qs ce {
            return;
        } return; if(cs>=qs&&ce<=qe){
            //System.out.println(ci);
            tree[ci]++;
            return;
        } cs>=qs&&ce<=qe cs>=qs cs qs ce<=qe ce qe {
            //System.out.println(ci);
            tree[ci]++;
            return;
        } tree[ci]++ tree[ci] tree ci return; int mid = (cs+ce)/2; mid (cs+ce)/2 (cs+ce) cs ce 2 update(qs,qe,cs,mid,2*ci+1) update qs qe cs mid 2*ci+1 2*ci 2 ci 1 update(qs,qe,mid+1,ce,2*ci+2) update qs qe mid+1 mid 1 ce 2*ci+2 2*ci 2 ci 2 int qs qs int qe qe int cs cs int ce ce int ci ci public static void lazyPropagation(){
        for(int i=0;i<=MAX/2;i++){
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        }
    } lazyPropagation {
        for(int i=0;i<=MAX/2;i++){
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        }
    } for(int i=0;i<=MAX/2;i++){
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        } int i=0; int i=0; i 0 i<=MAX/2 i MAX/2 MAX 2 i++ i++ i {
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        } {
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        } int v1 = 2*i+1; v1 2*i+1 2*i 2 i 1 int v2 = 2*i+2; v2 2*i+2 2*i 2 i 2 if(v1<MAX){
                tree[v1] += tree[i];
            } v1<MAX v1 MAX {
                tree[v1] += tree[i];
            } tree[v1] += tree[i] tree[v1] tree v1 tree[i] tree i if(v2<MAX){
                tree[v2] += tree[i];
            } v2<MAX v2 MAX {
                tree[v2] += tree[i];
            } tree[v2] += tree[i] tree[v2] tree v2 tree[i] tree i public static int get(int qs,int qe,int cs,int ce,int ci,int limit){
        if(cs>ce||cs>qe||qs>ce){
            return 0;
        }
        if(cs>=qs&&ce<=qe){
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        }
        int mid = (cs+ce)/2;
        int left = get(qs,qe,cs,mid,2*ci+1,limit);
        int right = get(qs,qe,mid+1,ce,2*ci+2,limit);
        return left+right;
    } get {
        if(cs>ce||cs>qe||qs>ce){
            return 0;
        }
        if(cs>=qs&&ce<=qe){
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        }
        int mid = (cs+ce)/2;
        int left = get(qs,qe,cs,mid,2*ci+1,limit);
        int right = get(qs,qe,mid+1,ce,2*ci+2,limit);
        return left+right;
    } if(cs>ce||cs>qe||qs>ce){
            return 0;
        } cs>ce||cs>qe||qs>ce cs>ce||cs>qe||qs>ce cs>ce cs ce cs>qe cs qe qs>ce qs ce {
            return 0;
        } return 0; 0 if(cs>=qs&&ce<=qe){
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        } cs>=qs&&ce<=qe cs>=qs cs qs ce<=qe ce qe {
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        } if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            } tree[ci]==limit tree[ci] tree ci limit {
                return ce-cs+1;
            } return ce-cs+1; ce-cs+1 ce-cs ce cs 1 if(cs==ce){
                return 0;
            } cs==ce cs ce {
                return 0;
            } return 0; 0 int mid = (cs+ce)/2; mid (cs+ce)/2 (cs+ce) cs ce 2 int left = get(qs,qe,cs,mid,2*ci+1,limit); left get(qs,qe,cs,mid,2*ci+1,limit) get qs qe cs mid 2*ci+1 2*ci 2 ci 1 limit int right = get(qs,qe,mid+1,ce,2*ci+2,limit); right get(qs,qe,mid+1,ce,2*ci+2,limit) get qs qe mid+1 mid 1 ce 2*ci+2 2*ci 2 ci 2 limit return left+right; left+right left right int qs qs int qe qe int cs cs int ce ce int ci ci int limit limit public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        //int num[] = new int[100001];
        for(int i=0;i<M;i++){
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        }
        lazyPropagation();
        int ans = get(1,100000,1,100000,0,M );
        System.out.println(ans);
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        //int num[] = new int[100001];
        for(int i=0;i<M;i++){
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        }
        lazyPropagation();
        int ans = get(1,100000,1,100000,0,M );
        System.out.println(ans);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int N = Integer.parseInt(st.nextToken()); N Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int M = Integer.parseInt(st.nextToken()); M Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st for(int i=0;i<M;i++){
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        } int i=0; int i=0; i 0 i<M i M i++ i++ i {
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        } {
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) int L = Integer.parseInt(st.nextToken()); L Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int R = Integer.parseInt(st.nextToken()); R Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st update(L,R,1,100000,0) update L R 1 100000 0 lazyPropagation() lazyPropagation int ans = get(1,100000,1,100000,0,M ); ans get(1,100000,1,100000,0,M ) get 1 100000 1 100000 0 M System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    public static int MAX = 2000000;
    public static int tree[] = new int[MAX];
    public static void update(int qs,int qe,int cs,int ce,int ci){
        if(cs>ce||cs>qe||qs>ce){
            return;
        }
        if(cs>=qs&&ce<=qe){
            //System.out.println(ci);
            tree[ci]++;
            return;
        }
        int mid = (cs+ce)/2;
        update(qs,qe,cs,mid,2*ci+1);
        update(qs,qe,mid+1,ce,2*ci+2);
    }
    public static void lazyPropagation(){
        for(int i=0;i<=MAX/2;i++){
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        }
    }
    public static int get(int qs,int qe,int cs,int ce,int ci,int limit){
        if(cs>ce||cs>qe||qs>ce){
            return 0;
        }
        if(cs>=qs&&ce<=qe){
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        }
        int mid = (cs+ce)/2;
        int left = get(qs,qe,cs,mid,2*ci+1,limit);
        int right = get(qs,qe,mid+1,ce,2*ci+2,limit);
        return left+right;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        //int num[] = new int[100001];
        for(int i=0;i<M;i++){
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        }
        lazyPropagation();
        int ans = get(1,100000,1,100000,0,M );
        System.out.println(ans);
    }
} public class Main {
    public static int MAX = 2000000;
    public static int tree[] = new int[MAX];
    public static void update(int qs,int qe,int cs,int ce,int ci){
        if(cs>ce||cs>qe||qs>ce){
            return;
        }
        if(cs>=qs&&ce<=qe){
            //System.out.println(ci);
            tree[ci]++;
            return;
        }
        int mid = (cs+ce)/2;
        update(qs,qe,cs,mid,2*ci+1);
        update(qs,qe,mid+1,ce,2*ci+2);
    }
    public static void lazyPropagation(){
        for(int i=0;i<=MAX/2;i++){
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        }
    }
    public static int get(int qs,int qe,int cs,int ce,int ci,int limit){
        if(cs>ce||cs>qe||qs>ce){
            return 0;
        }
        if(cs>=qs&&ce<=qe){
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        }
        int mid = (cs+ce)/2;
        int left = get(qs,qe,cs,mid,2*ci+1,limit);
        int right = get(qs,qe,mid+1,ce,2*ci+2,limit);
        return left+right;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        //int num[] = new int[100001];
        for(int i=0;i<M;i++){
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        }
        lazyPropagation();
        int ans = get(1,100000,1,100000,0,M );
        System.out.println(ans);
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static int MAX = 2000000;
    public static int tree[] = new int[MAX];
    public static void update(int qs,int qe,int cs,int ce,int ci){
        if(cs>ce||cs>qe||qs>ce){
            return;
        }
        if(cs>=qs&&ce<=qe){
            //System.out.println(ci);
            tree[ci]++;
            return;
        }
        int mid = (cs+ce)/2;
        update(qs,qe,cs,mid,2*ci+1);
        update(qs,qe,mid+1,ce,2*ci+2);
    }
    public static void lazyPropagation(){
        for(int i=0;i<=MAX/2;i++){
            int v1 = 2*i+1;
            int v2 = 2*i+2;
            if(v1<MAX){
                tree[v1] += tree[i];
            }
            if(v2<MAX){
                tree[v2] += tree[i];
            }
        }
    }
    public static int get(int qs,int qe,int cs,int ce,int ci,int limit){
        if(cs>ce||cs>qe||qs>ce){
            return 0;
        }
        if(cs>=qs&&ce<=qe){
            if(tree[ci]==limit){
                return ce-cs+1;
            }else if(cs==ce){
                return 0;
            }
        }
        int mid = (cs+ce)/2;
        int left = get(qs,qe,cs,mid,2*ci+1,limit);
        int right = get(qs,qe,mid+1,ce,2*ci+2,limit);
        return left+right;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        //int num[] = new int[100001];
        for(int i=0;i<M;i++){
            st = new StringTokenizer(br.readLine());
            int L = Integer.parseInt(st.nextToken());
            int R = Integer.parseInt(st.nextToken());
            //for(int j=L;j<=R;j++){
            //    num[j]++;
            //}
            update(L,R,1,100000,0);
        }
        lazyPropagation();
        int ans = get(1,100000,1,100000,0,M );
        System.out.println(ans);
    }
}
