var
fa,root,bottom,count:array[1..30000]of longint;
t,i,j:longint;
ch:char;
procedure find(x:longint);
begin
if fa[x]<>x then
begin
find(root[x]);
count[x]:=count[x]+count[root[x]]-1;
root[x]:=root[root[x]];
end;
end;
begin
assign(input,'galaxy.in');
reset(input);
assign(output,'galaxy.out');
rewrite(output);
for i:=1 to 30000 do
begin
fa[i]:=i;
root[i]:=i;
bottom[i]:=i;
count[i]:=1;
end;
readln(t);
for t:=1 to t do
begin
readln(ch,i,j);
if ch='M' then
begin
find(i);
find(j);
fa[root[i]]:=bottom[root[j]];
find(fa[root[i]]);
count[root[i]]:=count[fa[root[i]]]+1;
bottom[root[j]]:=bottom[root[i]];
root[root[i]]:=root[j];
end;
if ch='C' then
begin
find(i);
find(j);
if root[i]<>root[j] then writeln('-1')
else
begin
if count[i]>count[j] then writeln(count[i]-count[j]-1)
else writeln(count[j]-count[i]-1);
end;
end;
{for i:=1 to 20 do
writeln(i,' ',fa[i],' ',root[i],' ',count[i]);}
end;
close(input);
close(output);
end.