(*
ocamlfind ocamlopt -package batteries -linkpkg main.ml -o a.out
*)
open Batteries

let n, m = Scanf.sscanf (read_line ()) "%d %d" ( fun n m -> n, m)

let plst = List.init m (
        fun _ -> 
                Scanf.sscanf (read_line ()) "%d %d" (
                        fun l r -> 
                                (l, r)
                )
)

let rec f lst =
        match lst with
                  [] -> (1, n)
                | (lf, rf) :: rest -> 
                        let (lr, rr) = f rest in
                                if lf < lr && rf < rr then
                                        (lr, rf)
                                else
                                if lf < lr && rf > rr then
                                        (lr, rr)
                                else
                                if lf > lr && rf < rr then
                                        (lf, rf)
                                else
                                        (lf, rr)

let () =
        (
                let (l, r) = f plst in
                        if r < l then
                                0
                        else
                                List.range l `To r
                                |> List.length
        ) |> Printf.printf "%d\n"
(*

let slst = List.init m (
        fun _ -> 
                Scanf.sscanf (read_line ()) "%d %d" (
                        fun l r -> 
                                (l, r)
                )
) |> List.map (fun (l, r) -> Set.of_list (List.range l `To r))

let rec f lst =
        match lst with
                  [] -> Set.of_list []
                | first :: [] -> 
                        first
                | first :: rest -> 
                        Set.intersect first (f rest)

let () =
        f slst
        |> Set.cardinal
        |> Printf.printf "%d\n"

*)
