Problem:
Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

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

#define dbg(x) write(#x " = "), write(x), write("\n")
#define getch() (maxip == ip ? EOF : (*ip++))
#define putch(x) (*op++) = (x)
#define writeln(...) write(__VA_ARGS__), putch('\n')

char inp[1 << 24], *ip = inp, *maxip;
char outp[1 << 20], *op = outp;
int readtime, precision = 6;

inline void main_init();
inline char getchigws();
template <typename T, typename... Args> void read(T& x, Args&... args);
void read();
template <typename T> void read(T& x);
template <> void read<double>(double& x);
template <typename T, typename... Args>
void write(const T& x, const Args&... args);
void write();
template <typename T> void write(const T& x);
template <> void write<double>(const double& x);
template <> void write<char>(const char& ch);
void write(const char* s);
// define fast io

int main()
{
    main_init();
    int n, m;
    read(n, m);
    int L, R, Min = INT_MAX, Max = INT_MIN;
    for (int i = 1; i <= m; i++)
        read(L, R), Min = min(Min, R), Max = max(Max, L);
    writeln(max(Min - Max + 1, 0));
#ifdef LOCAL
    main_init();
    fprintf(stderr, "Time used = %.2f\n",
            ( double )(clock() - readtime) / CLOCKS_PER_SEC);
    while (1)
        ;
#else
    main_init();
#endif
    return 0;
}

inline void main_init()
{
    static bool inited = false;
    if (inited)
        fwrite(outp, 1, op - outp, stdout), op = outp;
    else
    {
        maxip = ip + fread(inp, 1, sizeof(inp), stdin) + 1;
        readtime = clock();
        inited = true;
    }
}

inline char getchigws()
{
    char ch;
    while (isspace(ch = getch()))
        ;
    return ch;
}

template <typename T, typename... Args> void read(T& x, Args&... args)
{
    read(x);
    read(args...);
}

void read() {}

template <typename T> void read(T& x)
{
    T f = 1;
    int ch;
    while (!isdigit(ch = getch()) && ch != EOF)
        if (ch == '-')
            f = -1;
    x = 0;
    while (isdigit(ch) && ch != EOF)
        x = (x << 1) + (x << 3) + (ch ^ 48), ch = getch();
    ip--;
    x *= f;
}

template <> void read<double>(double& x)
{
    double l = 0, r = 0, f = 1;
    int s = 0, ch;
    while (!isdigit(ch = getch()) && ch != EOF)
        if (ch == '-')
            f = -1;
    while (isdigit(ch) && ch != EOF)
    {
        l = l * 10 + (ch ^ 48);
        ch = getch();
    }
    if (ch == '.')
        while (isdigit(ch = getch()) && ch != EOF)
            r = r * 10 + (ch ^ 48), s++;
    ip--;
    x = (l + r / pow(10, s)) * f;
}

template <typename T, typename... Args>
void write(const T& x, const Args&... args)
{
    write(x);
    write(args...);
}

void write() {}
template <typename T> void write(const T& x)
{
    if (x < 0)
        putch('-'), write(-x);
    else
    {
        if (x >= 10)
            write(x / 10);
        putch(x % 10 ^ 48);
    }
}

template <> void write<double>(const double& x)
{
    if (x == 0)
    {
        write("0.");
        for (int i = 0; i < precision; i++)
            putch('0');
        return;
    }
    if (x < 0)
        putch('-'), write(-x);
    else
    {
        double r = x - floor(x), l = floor(x);
        if (l == 0)
            putch('0');
        char buf[310];
        int i = 0;
        while (floor(l) != 0)
        {
            buf[i++] = floor(l - floor(l / 10) * 10);
            l /= 10;
        }
        for (int j = i - 1; j >= 0; j--)
            putch(buf[j] ^ 48);
        putch('.');
        for (int i = 0; i < precision - 1; i++)
            putch(int(floor(r *= 10)) ^ 48), r -= floor(r);
        double tmp = r * 10;
        if (int(floor(tmp * 10 - floor(tmp) * 10)) >= 5)
            putch((int(floor(tmp)) + 1) ^ 48);
        else
            putch(int(floor(tmp)) ^ 48);
    }
}

template <> void write<char>(const char& ch) { putch(ch); }

void write(const char* s)
{
    while (*s)
        putch(*s++);
}