#include<bits/stdc++.h>
using namespace std;
int t,b[1000000],s[1000000];
int fsb(int x){
if(b[x]==x) return x;
return fsb(b[x]);
}
int fss(int x){
if(s[x]==x) return x;
return fss(s[x]);
}
int gs(int x,int y){
int xx=x,yy=y,s=-1;
while(b[yy]!=yy){
if(yy==x) return s;
s++;
yy=b[yy];
}
s=-1;
while(b[xx]!=xx){
if(xx==y) return s;
s++;
xx=b[xx];
}
}
int main(){
cin>>t;
for(int i=1;i<=30000;i++) b[i]=i,s[i]=i;
while(t--){
char p;
int x,y;
cin>>p>>x>>y;
if(p=='M'){
int xx=fsb(x),yy=fss(y);
b[xx]=yy,s[yy]=xx;
}
else{
if(fsb(x)==fsb(y) && fss(x)==fss(y)) cout<<gs(x,y)<<endl;
else cout<<-1<<endl;
}
}
return 0;
}