let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  Array.init m (fun _ ->
    Scanf.scanf "%d %d\n" @@ fun a b -> a, b)
  |> Array.to_list
  |> List.sort (fun (a, b) (a', b') -> compare (a, b') (a', b))
  |> List.fold_left (fun (acc, b') (a, b) ->
      if a + 1 = min b b' then (1 + acc, max_int)
      else (acc, min b b')) (0, max_int)
  |> (fun (acc, b') ->
      if b' = max_int then acc
      else 1 + acc)
  |> Printf.printf "%d\n"
