var
father:array[1..1000]of longint;
i,j,n,q,ans,x,y:longint;
function search(x:longint):longint;
begin
if father[x]<>x then father[x]:=search(father[x]);
exit(father[x]);
end;
procedure judge(x,y:longint);
var a,b:longint;
begin
a:=search(x); b:=search(y);
if a<>b then father[b]:=a;
end;
begin
read(n);
while n<>0 do begin
read(q);
for i:=1 to n do father[i]:=i;
for i:=1 to q do begin
read(x,y);
judge(x,y);
end;
ans:=0;
for i:=1 to n do
if father[i]=i then inc(ans);
writeln(ans-1);
read(n);
end;
end.
/**************************************************************
Problem: 2203
User: admin
Language: Pascal
Result: Wrong Answer
****************************************************************/