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 <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <numeric>

#include <array>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <ostream>
#include <type_traits>
#include <utility>
#include <chrono>

#ifdef ONLINE_JUDGE
#pragma GCC optimize ("O3")
#pragma GCC target ("avx,avx2")
#endif

using namespace std;

// Find value on pos
// Add val to range [l,r)
// Set val on pos
template <typename T>
class Fenwick
{
   public:
    Fenwick(int n)
    {
        len = 1;
        while (len < n) len <<= 1;
        data.resize(n + len);
    }

    void add(int l, int r, T val)
    {
        for (l += len, r += len; l < r; l >>= 1, r >>= 1) {
            if ((l % 2) == 1) data[l++] += val;
            if ((r % 2) == 1) data[--r] += val;
        }
    }

    T get(int pos)
    {
        T summa = 0;
        for (pos += len; pos > 0; pos >>= 1) {
            summa += data[pos];
        }
        return summa;
    }

    void set(int pos, T value) { data[pos + len] += value - get(pos); }

   private:
    int len;
    vector<T> data;
};

using u64 = uint64_t;
using i64 = int64_t;
using u32 = uint32_t;
using vi = vector<int>;
using vi64 = vector<i64>;

#define seq(i,a,b) for (int i = (a); i < (b); ++i)
#define rev(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define sz(x) (x.size())
#define all(x) x.begin(), x.end()
#define dbg(x) {cerr << #x << ": " << x << endl;}

int main(int argc, char *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n,m; cin >> n >> m;

    Fenwick<int> F(n);

    seq(_,0,m) {
        int l,r; cin >> l >> r;
        --l;
        F.add(l, r, 1);
    }

    // I could push everything down, but meh
    int res = 0;
    seq(i,0,n) {
        res += m == F.get(i);
    }
    cout << res << endl;
}