program zht;
var
n,m,i,t,p,ans:longint;
a,b:array[0..1000] of longint;
bh:array[0..10000000] of longint;
begin
assign(input,'ha14d.in');
assign(output,'ha14d.out');
reset(input);
rewrite(output);
readln(n,m);
for i:=1 to m do
readln(a[i],b[i]);
for i:=m downto 1 do
begin
t:=a[i];
p:=bh[0];
while t<=b[i] do
begin
if t>b[i] then break;
if bh[t]=0 then begin inc(bh[0]); bh[t]:=b[i]; inc(t);continue; end;
if bh[t]>0 then t:=bh[t]+1;
end;
if p<>bh[0] then inc(ans);
end;
writeln(ans);
close(input);
close(output);
end.