Scanf.scanf "%d %d" (fun n m ->
    let ab = Array.init m (fun _ -> Scanf.scanf " %d %d" (fun a b -> b, a)) in
    Array.sort compare ab;

    let rec loop i l acc =
        if i = m then acc else
            let (b, a) = ab.(i) in
            if a <= l then loop (i + 1) l acc else
                loop (i + 1) (b - 1) (acc + 1)
    in
    loop 0 0 0 |> Printf.printf "%d\n"
)