program explosion;
type
integer=longint;
const
maxn=110000;
var
father:array[0..maxn] of integer;
procedure Fopen;
begin
assign(input,'explosion.in'); reset(input);
assign(output,'explosion.out'); rewrite(output);
end;
procedure Fclose;
begin
close(input); close(output);
end;
function getfather(x:integer):integer;
begin
if father[x]=x then exit(x);
father[x]:=getfather(father[x]);
exit(father[x]);
end;
procedure Solve;
var
i,p,q:integer;
fx,fy:integer;
ans:integer;
begin
for i:=0 to maxn do father[i]:=i;
ans:=0;
repeat
read(p); if p=-1 then break;
read(q);
fx:=getfather(p); fy:=getfather(q);
if fx=fy then inc(ans) else father[fx]:=fy;
until false;
writeln(ans);
end;
begin
Fopen;
Solve;
Fclose;
end.