#include<iostream>
#include<cmath>
using namespace std;
#define N 30001
struct union_Find_Sets{
int fa[N],siz[N],dis[N];
int findFa(int v){
if(fa[v]!=v){
int f=fa[v];
fa[v]=findFa(fa[v]);
dis[v]+=dis[f];
siz[v]=siz[fa[v]];
}
return fa[v];
}
void unionn(int v,int u){
int fx=findFa(v),fy=findFa(u);
if(fx!=fy){
fa[fx]=fy;
dis[fx]=siz[fy];
siz[fy]+=siz[fx];
siz[fx]=siz[fy];
}
}
int query(int v,int u){
int fx=findFa(v),fy=findFa(u);
if(fx!=fy)return -1;
else return abs(dis[v]-dis[u])-1;
}
void init(int size){
for(int i=1;i<=size;i++){
fa[i]=i;
siz[i]=1;
}
}
}ufs;
int n,x,y;
char op;
int main(){
cin>>n;
ufs.init(N-1);
for(int i=1;i<=n;i++){
cin>>op>>x>>y;
if(op=='M'){
ufs.unionn(x,y);
}
else{
cout<<ufs.query(x,y)<<endl;
//ufs.query(x,y);
}
}
// for(int i=1;i<=4;i++){
// cout<<ufs.fa[i]<<' '<<ufs.dis[i]<<' '<<ufs.siz[i]<<endl;
// }
}