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 Rust (1.15.1) (judge verdict: Accepted)


//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

// ここまで

struct SegmentTreeRUPQ<T, F> {
    n: usize,
    b: usize,
    a: Vec<T>,
    id: T,
    op: F,
}

impl <T: Copy, F: Fn (T, T) -> T> SegmentTreeRUPQ<T, F> {
    pub fn new (n: usize, id: T, op: F) -> SegmentTreeRUPQ<T, F> {
        let mut k = 0;
        while (1 << k) < n {
            k += 1;
        }
        SegmentTreeRUPQ {
            n: 1 << k,
            b: k,
            a: vec![id; 2 << k],
            id: id,
            op: op,
        }
    }
    fn down (&mut self, x: usize) {
        let k = x + self.n;
        let a = &mut self.a;
        for i in (1..(self.b + 1)).rev() {
            let y = k >> i;
            a[2 * y] = (self.op) (a[2 * y], a[y]);
            a[2 * y + 1] = (self.op) (a[2 * y + 1], a[y]);
            a[y] = self.id;
        }
    }
    pub fn update (&mut self, mut l: usize, mut r: usize, v: T) {
        self.down (l);
        self.down (r - 1);
        l += self.n;
        r += self.n;
        let a = &mut self.a;
        while l < r {
            if (l & 1) == 1 {
                a[l] = (self.op) (a[l], v);
                l += 1;
            }
            if (r & 1) == 1 {
                r -= 1;
                a[r] = (self.op) (a[r], v);
            }
            l >>= 1;
            r >>= 1;
        }
    }
    pub fn find (&mut self, x: usize) -> T {
        self.down (x);
        self.a[x + self.n]
    }
}

fn add (a: u32, b: u32) -> u32{
    a + b
}

fn run() {
    input! {
        n: usize,
        m: usize,
        p: [(usize, usize); m],
    }
    let mut s = SegmentTreeRUPQ::new (n, 0, add);
    for (l, r) in p {
        s.update (l - 1, r, 1);
    }
    let mut ans = 0;
    for i in 0..n {
        if s.find (i) == (m as u32) {
            ans += 1;
        }
    }
    println! ("{}", ans);
}

fn main() {
    run();
}