import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    private static int N;
    private static int M;
    private static ArrayList<Integer> Li = new ArrayList<>();
    private static ArrayList<Integer> Ri = new ArrayList<>();

    private static void init() {
        Scanner scanner = new Scanner(System.in);

        String header = scanner.nextLine();
        String[] headerItems = header.split(" ");
        N = Integer.parseInt(headerItems[0]);
        M = Integer.parseInt(headerItems[1]);

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        }

        scanner.close();
    }

    public static void main(String[] args) {
        init();
        // logic
        logic();
    }

    private static void logic() {
        int maxL = Integer.MIN_VALUE;
        int minR = Integer.MAX_VALUE;

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        }

        System.out.println((minR >= maxL) ? minR - maxL + 1 : 0);
    }
} import java.util.ArrayList; ArrayList java.util import java.util.Scanner; Scanner java.util public class Main {
    private static int N;
    private static int M;
    private static ArrayList<Integer> Li = new ArrayList<>();
    private static ArrayList<Integer> Ri = new ArrayList<>();

    private static void init() {
        Scanner scanner = new Scanner(System.in);

        String header = scanner.nextLine();
        String[] headerItems = header.split(" ");
        N = Integer.parseInt(headerItems[0]);
        M = Integer.parseInt(headerItems[1]);

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        }

        scanner.close();
    }

    public static void main(String[] args) {
        init();
        // logic
        logic();
    }

    private static void logic() {
        int maxL = Integer.MIN_VALUE;
        int minR = Integer.MAX_VALUE;

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        }

        System.out.println((minR >= maxL) ? minR - maxL + 1 : 0);
    }
} Main private static int N; N private static int M; M private static ArrayList<Integer> Li = new ArrayList<>(); Li new ArrayList<>() private static ArrayList<Integer> Ri = new ArrayList<>(); Ri new ArrayList<>() private static void init() {
        Scanner scanner = new Scanner(System.in);

        String header = scanner.nextLine();
        String[] headerItems = header.split(" ");
        N = Integer.parseInt(headerItems[0]);
        M = Integer.parseInt(headerItems[1]);

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        }

        scanner.close();
    } init {
        Scanner scanner = new Scanner(System.in);

        String header = scanner.nextLine();
        String[] headerItems = header.split(" ");
        N = Integer.parseInt(headerItems[0]);
        M = Integer.parseInt(headerItems[1]);

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        }

        scanner.close();
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) String header = scanner.nextLine(); header scanner.nextLine() scanner.nextLine scanner String[] headerItems = header.split(" "); headerItems header.split(" ") header.split header " " N = Integer.parseInt(headerItems[0]) N Integer.parseInt(headerItems[0]) Integer.parseInt Integer headerItems[0] headerItems 0 M = Integer.parseInt(headerItems[1]) M Integer.parseInt(headerItems[1]) Integer.parseInt Integer headerItems[1] headerItems 1 for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        } int gateIndex = 0; int gateIndex = 0; gateIndex 0 gateIndex < M gateIndex M gateIndex++ gateIndex++ gateIndex {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        } {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        } String card = scanner.nextLine(); card scanner.nextLine() scanner.nextLine scanner String[] cardItems = card.split(" "); cardItems card.split(" ") card.split card " " int L = Integer.parseInt(cardItems[0]); L Integer.parseInt(cardItems[0]) Integer.parseInt Integer cardItems[0] cardItems 0 int R = Integer.parseInt(cardItems[1]); R Integer.parseInt(cardItems[1]) Integer.parseInt Integer cardItems[1] cardItems 1 Li.add(L) Li.add Li L Ri.add(R) Ri.add Ri R scanner.close() scanner.close scanner public static void main(String[] args) {
        init();
        // logic
        logic();
    } main {
        init();
        // logic
        logic();
    } init() init logic() logic String[] args args private static void logic() {
        int maxL = Integer.MIN_VALUE;
        int minR = Integer.MAX_VALUE;

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        }

        System.out.println((minR >= maxL) ? minR - maxL + 1 : 0);
    } logic {
        int maxL = Integer.MIN_VALUE;
        int minR = Integer.MAX_VALUE;

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        }

        System.out.println((minR >= maxL) ? minR - maxL + 1 : 0);
    } int maxL = Integer.MIN_VALUE; maxL Integer.MIN_VALUE Integer Integer.MIN_VALUE int minR = Integer.MAX_VALUE; minR Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        } int gateIndex = 0; int gateIndex = 0; gateIndex 0 gateIndex < M gateIndex M gateIndex++ gateIndex++ gateIndex {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        } {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        } int L = Li.get(gateIndex); L Li.get(gateIndex) Li.get Li gateIndex int R = Ri.get(gateIndex); R Ri.get(gateIndex) Ri.get Ri gateIndex maxL = Math.max(maxL, L) maxL Math.max(maxL, L) Math.max Math maxL L minR = Math.min(minR, R) minR Math.min(minR, R) Math.min Math minR R System.out.println((minR >= maxL) ? minR - maxL + 1 : 0) System.out.println System.out System System.out (minR >= maxL) ? minR - maxL + 1 : 0 (minR >= maxL) minR maxL minR - maxL + 1 minR - maxL minR maxL 1 0 public class Main {
    private static int N;
    private static int M;
    private static ArrayList<Integer> Li = new ArrayList<>();
    private static ArrayList<Integer> Ri = new ArrayList<>();

    private static void init() {
        Scanner scanner = new Scanner(System.in);

        String header = scanner.nextLine();
        String[] headerItems = header.split(" ");
        N = Integer.parseInt(headerItems[0]);
        M = Integer.parseInt(headerItems[1]);

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        }

        scanner.close();
    }

    public static void main(String[] args) {
        init();
        // logic
        logic();
    }

    private static void logic() {
        int maxL = Integer.MIN_VALUE;
        int minR = Integer.MAX_VALUE;

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        }

        System.out.println((minR >= maxL) ? minR - maxL + 1 : 0);
    }
} public class Main {
    private static int N;
    private static int M;
    private static ArrayList<Integer> Li = new ArrayList<>();
    private static ArrayList<Integer> Ri = new ArrayList<>();

    private static void init() {
        Scanner scanner = new Scanner(System.in);

        String header = scanner.nextLine();
        String[] headerItems = header.split(" ");
        N = Integer.parseInt(headerItems[0]);
        M = Integer.parseInt(headerItems[1]);

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        }

        scanner.close();
    }

    public static void main(String[] args) {
        init();
        // logic
        logic();
    }

    private static void logic() {
        int maxL = Integer.MIN_VALUE;
        int minR = Integer.MAX_VALUE;

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        }

        System.out.println((minR >= maxL) ? minR - maxL + 1 : 0);
    }
} Main
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    private static int N;
    private static int M;
    private static ArrayList<Integer> Li = new ArrayList<>();
    private static ArrayList<Integer> Ri = new ArrayList<>();

    private static void init() {
        Scanner scanner = new Scanner(System.in);

        String header = scanner.nextLine();
        String[] headerItems = header.split(" ");
        N = Integer.parseInt(headerItems[0]);
        M = Integer.parseInt(headerItems[1]);

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            String card = scanner.nextLine();
            String[] cardItems = card.split(" ");
            int L = Integer.parseInt(cardItems[0]);
            int R = Integer.parseInt(cardItems[1]);

            Li.add(L);
            Ri.add(R);
        }

        scanner.close();
    }

    public static void main(String[] args) {
        init();
        // logic
        logic();
    }

    private static void logic() {
        int maxL = Integer.MIN_VALUE;
        int minR = Integer.MAX_VALUE;

        for (int gateIndex = 0; gateIndex < M; gateIndex++) {
            int L = Li.get(gateIndex);
            int R = Ri.get(gateIndex);
            maxL = Math.max(maxL, L);
            minR = Math.min(minR, R);
        }

        System.out.println((minR >= maxL) ? minR - maxL + 1 : 0);
    }
}
