program major2;
const max=1000;
var a:array[1..max,1..max] of byte;
    h:array[1..max] of integer;
    i,j,k,count,x,y,n,hor,ver:longint;
begin
{assign(input,'input.txt');
reset(input); }
fillchar(a,sizeof(a),0);
read(n,k);
hor:=0;
ver:=0;
count:=0;
for i:=1 to k do
    begin
    read(x,y); {coordinates}
    a[x,y]:=1;
    if ver<x
       then ver:=x;
    if hor<y
       then hor:=y;
    end;
fillchar(h,sizeof(h),0);
for i:=1 to ver do
    begin
    x:=0; {}
    for j:=1 to hor do
        begin

        if h[j]>0
           then x:=j;
        if a[i,j]=1
           then begin
 {               writeln(i,'-',j);}
                if x>0
                   then begin
                        dec(h[x]);
                        inc(h[j]);
                        end
                   else begin
                        inc(count);
                        inc(h[j]);
                        end;
                x:=j;
                end;
        end;
    end;
writeln(count);
end.
