var
 father,before,long:array[0..30005] of longint;
 i,j,t,x,y,sx,sy:longint;
 s:char;
function get(x:longint):longint;
var
 f:longint;
begin
 if father[x]=x then exit(x);
  f:=get(father[x]);
  before[x]:=before[x]+before[father[x]];
  father[x]:=f;
  get:=f;
end;
procedure init;
begin
 readln(t);
 for i:=1 to 30000 do begin father[i]:=i; before[i]:=0; long[i]:=1;end;
end;
procedure work;
begin
 for i:=1 to t do
  begin
   readln(s,x,y);
   if s='M' then
    begin
     sx:=get(x);
     sy:=get(y);
     father[sx]:=sy;
     before[sx]:=long[sy];
     long[sy]:=long[sy]+long[sx];
    end;
  if s='C' then
   begin
    sx:=get(x);
    sy:=get(y);
    if sx<>sy then j:=-1
     else j:=abs(before[x]-before[y])-1;
    writeln(j);
   end;
   end;
 end;
begin
 init;
 work;
 
end.

/**************************************************************
	Problem: 2240
	User: admin
	Language: Pascal
	Result: Wrong Answer
****************************************************************/