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