{
haoi2009 moni2 t3
rp++
time:2009.4.21
}
program cch(input,output);
var
hang,lie:array[1..200000] of longint;
x,y:array[1..600000] of longint;
n,m:longint;
procedure init;
var
i:longint;
begin
assign(input,'board.in');
assign(output,'board.out');
reset(input);
rewrite(output);
readln(n,m);
for i:=1 to m do
readln(x[i],y[i]);
end;
function min(x,y:longint):longint;
begin
if x<y then exit(x)
else exit(y);
end;
procedure main;
var
i,h,l,ans1,ans,tmp1,tmp2:longint;
begin
for i:=1 to n do
hang[i]:=0;
for i:=1 to n do
lie[i]:=0;
for i:=1 to m do
begin
inc(hang[x[i]]); inc(lie[y[i]]);
end;
h:=0; l:=0;
for i:=1 to n do
if hang[i]>0 then inc(h);
for i:=1 to n do
if lie[i]>0 then inc(l);
ans1:=min(h,l);
ans:=0;
for i:=1 to m do
begin
dec(hang[x[i]]); dec(lie[y[i]]);
tmp1:=h; tmp2:=l;
if hang[x[i]]=0 then dec(h);
if hang[y[i]]=0 then dec(l);
if ans1=min(h,l) then inc(ans);
inc(hang[x[i]]); inc(lie[y[i]]);
h:=tmp1; l:=tmp2;
end;
writeln(ans);
close(input);
close(output);
end;
begin
init;
main;
end.