/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/3/practice/contest/289392/problem/C */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;

#define V vector

#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)

#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)

#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;

using ll = long long;

using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;

template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
    os << vec[0];
    for (size_t i = 1; i < vec.size(); i++) {
        os << ' ' << vec[i];
    }
    os << '\n';
    return os;
}

template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
    for (auto &i : vec) {
        is >> i;
    }
    return is;
}

template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
    for (auto &i : vec) {
        os << i[0];
        for (size_t j = 1; j < i.size(); j++) {
            os << ' ' << i[j];
        }
        os << '\n';
    }
    return os;
}

template<class v>
auto operator>>(istream  &is, vector<vector<v>> &vec)->istream& {
    for (auto &i : vec) {
        for (auto &j : i) {
            is >> j;
        }
    }
    return is;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    struct query {
        int type;
        int l;
        int r;
        int u;
        int v;
        int i;
    };

    V<query> queries;
    vi ans;

    map<pair<int, int>, int> prev;

    int time = 0;

    rep(i, m) {
        char op;
        cin >> op;

        if (op == '?') {
            queries.emplace_back(1, time, time, 0, 0, ans.size());
            ans.emplace_back();
            continue;
        }

        int a, b;
        cin >> a >> b;
        a--, b--;

        if (a > b) {
            swap(a, b);
        }

        if (op == '-') {
            queries[prev[{a, b}]].r = time;
            time++;
            continue;
        }

        time++;
        prev[{a, b}] = queries.size();
        queries.emplace_back(0, time, INT32_MAX >> 1, a, b, -1);
    }

    vi dsu(n);
    vi size(n);
    stack<pair<int, int>> rev;

    int c;
    
    auto init = [&]() {
        c = n;
        rep(i, n) {
            dsu[i] = i;
            size[i] = 1;
        }
    };

    init();

    function<int(int)> find_p = [&](int i) {
        if (dsu[i] == i) {
            return i;
        }
        return find_p(dsu[i]);
    };

    auto join = [&](int a, int b) {
        a = find_p(a);
        b = find_p(b);

        if (a == b) {
            return;
        }

        c--;

        if (size[a] < size[b]) {
            swap(a, b);
        }

        if (size[a] == size[b]) {
            size[a]++;
        }

        dsu[b] = a;

        rev.emplace(a, b);
    };

    auto save = [&]() {
        rev.emplace(-1, -1);
    };

    auto rollback = [&]() {
        while (rev.top().first != -1) {
            auto [a, b] = rev.top();
            rev.pop();

            if (size[b] == size[a] - 1) {
                size[a]--;
            }

            dsu[b] = b;
            c++;
        }
        rev.pop();
    };

    function<void(int, int, V<query>&)> func = [&](int l, int r, V<query> &queries) {
        if (l == r) {
            save();
            repv(i, queries) {
                if (i.type == 0 && l >= i.l && r <= i.r) {
                    join(i.u, i.v);
                }
            }

            repv(i, queries) {
                if (i.type == 1 && i.l == l) {
                    ans[i.i] = c;
                }
            }
            rollback();

            return;
        }

        V<query> queries2;
        save();

        repv(i, queries) {
            if (l >= i.l && r <= i.r) {
                join(i.u, i.v);
                continue;
            }

            if (l <= i.r && i.l <= r) {
                queries2.emplace_back(i);
            }
        }

        int mid = (l + r) >> 1;

        func(l, mid, queries2);
        func(mid + 1, r, queries2);

        rollback();
    };

    func(0, time, queries);

    repv(i, ans) {
        cout << i << '\n';
    }
}
