#include<bits/stdc++.h>
using namespace std;
const int N=3e4+10;
int t,x,y,fa[N],dis[N],siz[N];//dis->结点到根距离 siz->所在集合有几个元素
char c;
int get(int x){
if(fa[x]==x) return x;
dis[x]+=dis[fa[x]];
fa[x]=get(fa[x]);
siz[x]=siz[fa[x]];
return fa[x];
}
void merge(int x,int y){
int fx=get(x),fy=get(y);
if(fx!=fy) {
fa[fx]=fy;
dis[fx]+=siz[fy];
siz[fx]+=siz[fy];
siz[fy]=siz[fx];
}
}
bool ck(int x,int y){
int fx=get(x),fy=get(y);
return fx==fy;
}
int main() {
scanf("%d",&t);
for(int i=0;i<=3e4;i++) fa[i]=i,dis[i]=0,siz[i]=1;
for(int i=1;i<=t;i++){
cin>>c>>x>>y;
if(c=='M') merge(y,x);
else{
if(x==y) cout<<0<<endl;
else if(ck(x,y)==false) cout<<-1<<endl;
else cout<<abs(dis[x]-dis[y])-1<<endl;
}
}
return 0;
}
orz