program cogs1682;
const
maxn=10000000;
var
l,r,i,j,n,m:longint;
min,max,t,total:longint;
a:array[1..maxn] of longint;
f:array[0..maxn] of boolean;
begin
assign(input,'ha14d.in'); reset(input);
assign(output,'ha14d.out'); rewrite(output);
max:=-maxlongint; min:=maxlongint; total:=0;
readln(n,m);
fillchar(a,sizeof(a),0);
for i:=1 to m do
begin
readln(l,r);
if r<l then begin t:=l; l:=r; r:=t end;
if l<min then min:=l;
if r>max then max:=r;
for j:=l to r do
a[j]:=i;
end;
for i:=min to max do
f[a[i]]:=true;
for i:=1 to m do
if f[i] then inc(total);
writeln(total);
close(input); close(output);
end.