let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let es = Array.make n max_int in
  for i = 0 to m - 1 do
    Scanf.scanf "%d %d\n" @@ fun a b ->
      es.(a - 1) <- min (b - 1) es.(a - 1)
  done;
  let acc = ref 0 in
  for i = 0 to n - 2 do
    if es.(i) = i + 1 then incr acc
    else es.(i + 1) <- min es.(i) es.(i + 1)
  done;
  Printf.printf "%d\n" !acc
