// Readiz lesson: complete local C++17 example.
#include <algorithm>
#include <deque>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#include <algorithm>
#include <vector>
using namespace std;

struct DSU {
    vector<int> parent;
    vector<int> size;

    DSU(int n) : parent(n), size(n, 1) {
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
    }

    int find(int x) {
        if (parent[x] == x) return x;
        return parent[x] = find(parent[x]);
    }

    bool same(int a, int b) {
        return find(a) == find(b);
    }

    bool unite(int a, int b) {
        int rootA = find(a);
        int rootB = find(b);
        if (rootA == rootB) return false;

        if (size[rootA] < size[rootB]) {
            swap(rootA, rootB);
        }
        parent[rootB] = rootA;
        size[rootA] += size[rootB];
        return true;
    }

    int componentSize(int x) {
        return size[find(x)];
    }
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n, q;
    if (!(cin >> n >> q) || n < 1 || n > 200000 || q < 0 || q > 200000) return 1;
    DSU dsu(n);
    int components = n;
    ostringstream output;
    for (int i = 0; i < q; ++i) {
        string op; int a, b;
        if (!(cin >> op)) return 1;
        if (op == "C") { output << components << '\n'; continue; }
        if (op != "U" && op != "Q" && op != "S") return 1;
        if (!(cin >> a) || a < 0 || a >= n) return 1;
        if (op == "S") { output << dsu.componentSize(a) << '\n'; continue; }
        if (!(cin >> b) || b < 0 || b >= n) return 1;
        if (op == "Q") output << (dsu.same(a, b) ? 1 : 0) << '\n';
        else {
            bool merged = dsu.unite(a, b);
            if (merged) --components;
            output << (merged ? 1 : 0) << '\n';
        }
    }
    cout << output.str();
    return 0;
}
