#include <iostream>
using namespace std;
const int N = 30010;
int m;
// s存储集合中元素的个数,p[x]表示x到p[x]的距离
int p[N], s[N], d[N];
int find(int x) {
if (p[x] != x) {
int root = find(p[x]);
d[x] += d[p[x]];
p[x] = root;
}
return p[x];
}
int main() {
scanf("%d", &m);
for (int i = 0; i < N; i++) {
p[i] = i;
s[i] = 1;
// d默认全为0,不需要初始化了
}
while (m--) {
char op[2];
int a, b;
scanf("%s%d%d", op, &a, &b);
if (op[0] == 'M') {
int pa = find(a), pb = find(b);
d[pa] = s[pb];
s[pb] += s[pa];
p[pa] = pb;
} else {
int pa = find(a), pb = find(b);
if (pa != pb) puts("-1");
else printf("%d\n", max(0, abs(d[a] - d[b]) - 1));
}
}
return 0;
}