#include<bits/stdc++.h>
using namespace std;
int t;
char opt;
int x,y;
int f[30005];
int siz[30005];
queue<int> l[30005];
int lie[30005];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
int main(){
for(int i=1;i<=30000;i++){
siz[i]=1;
l[i].push(i);
lie[i]=i;
}
cin>>t;
while(t--){
cin>>opt>>x>>y;
if(opt=='M'){
int li=lie[x];
while(!l[li].empty()){
int n=l[li].front();
l[li].pop();
lie[n]=lie[y];
siz[n]=siz[l[lie[y]].back()]+1;
l[lie[y]].push(n);
}
}
else{
if(lie[x]!=lie[y])cout<<-1<<endl;
else cout<<abs(siz[x]-siz[y])-1<<endl;
}
}
}