const
maxn=50;
var
a:array[1..maxn,0..maxn]of longint;
g,b,d:array[1..maxn]of longint;
u:array[1..maxn*2]of boolean;
i,n,m,k,l,ans:longint;
f:boolean;
procedure sub(x:longint);
var i,j:longint;
begin
if f then exit;
for i:=1 to a[x,0] do if (not u[a[x,i]+n])and(b[a[x,i]]=0) then begin
inc(l);d[l]:=a[x,i];
for j:=1 to (l div 2) do begin
g[d[j*2-1]]:=d[j*2];
b[d[j*2]]:=d[j*2-1];
end;
inc(ans);
f:=true;
exit;
end;
for j:=1 to a[x,0] do begin
i:=a[x,j];
if (not u[i+n])and(b<>0)and(not u[b]) then begin
inc(l);
d[l]:=i;
inc(l);
d[l]:=b;
u[i+n]:=true;
u[b]:=true;
sub(b);
l:=l-2;
end;
end;
end;
begin
assign(input,'a.in');reset(input);
assign(output,'a.out');rewrite(output);
k:=1;
while k<>0 do begin
read(k,n,m);
fillchar(a,sizeof(a),0);
fillchar(g,sizeof(g),0);
fillchar(b,sizeof(b),0);
for i:=1 to k do begin
read(k,l);
inc(a[k,0]);
a[k,a[k,0]]:=l;
end;
ans:=0;
for i:=1 to n do if g=0 then begin
f:=false;
fillchar(u,sizeof(u),0);
u:=true;
l:=1;
d[l]:=i;
sub(i);
end;
writeln(ans);
read(k,n,m);
end;
close(input);
close(output);
end.