#include <cstdio>
#include <cassert>
#include <cstring>
#include <map>
using namespace std;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
typedef pair <int, int> pii;
const int maxn = (int)3e5 + 10;
const int max_mem = maxn * 10;
int n, k, a[maxn], b[maxn], pa[maxn];
int p[maxn], size[maxn];
char type[maxn];
map <pii, int> ind;
int mpos, mem[maxn * 4];
int cc, u[maxn];
int sn, ss[max_mem];
inline int Set( int *x, int y )
{
assert(sn < max_mem);
ss[sn++] = (int)x, ss[sn++] = *x;
return *x = y;
}
inline void Restore( int old )
{
while (sn > old)
sn -= 2, *((int *)(ss[sn])) = ss[sn + 1];
}
int Get( int a )
{
return a == p[a] ? a : Set(&p[a], Get(p[a]));
}
void Join( int a, int b )
{
if (size[a] > size[b])
swap(a, b);
Set(&p[a], b);
Set(&size[b], size[b] + size[a]);
}
void Do( int L, int R, int pn, int *p, int ans )
{
int oldMPos = mpos, oldSN = sn;
int e, x, y, pn1 = 0, *p1 = mem + mpos;
cc++;
for (int i = L; i < R; i++)
if (type[i] == '-')
u[pa[i]] = cc;
forn(i, pn)
if (u[e = p[i]] == cc)
{
assert(mpos < maxn * 4);
p1[pn1++] = e, mpos++;
}
else if (pa[e] > L && (x = Get(a[e])) != (y = Get(b[e])))
Join(x, y), ans--;
if (R - L == 1)
{
if (type[L] == '?')
printf("%d\n", ans);
}
else
{
int M = (L + R) / 2;
Do(L, M, pn1, p1, ans);
for (int i = L; i < M; i++)
if (type[i] == '+')
{
assert(mpos < maxn * 4);
p1[pn1++] = i, mpos++;
}
Do(M, R, pn1, p1, ans);
}
mpos = oldMPos, Restore(oldSN);
}
int main()
{
freopen("connect.in", "r", stdin);
freopen("connect.out", "w", stdout);
scanf("%d%d", &n, &k);
forn(i, k)
{
pa[i] = k;
scanf(" %c", &type[i]);
if (type[i] != '?')
{
scanf("%d%d", &a[i], &b[i]), a[i]--, b[i]--;
if (a[i] > b[i])
swap(a[i], b[i]);
}
pii p = mp(a[i], b[i]);
if (ind.count(p))
pa[i] = ind[p], pa[pa[i]] = i, ind.erase(p);
else
ind[p] = i;
}
forn(i, n)
p[i] = i, size[i] = 1;
if (k)
Do(0, k, 0, 0, n);
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNhc3NlcnQ+CiNpbmNsdWRlIDxjc3RyaW5nPgojaW5jbHVkZSA8bWFwPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgZm9ybihpLCBuKSBmb3IgKGludCBpID0gMDsgaSA8IChpbnQpKG4pOyBpKyspCiNkZWZpbmUgbXAgbWFrZV9wYWlyCgp0eXBlZGVmIHBhaXIgPGludCwgaW50PiBwaWk7Cgpjb25zdCBpbnQgbWF4biA9IChpbnQpM2U1ICsgMTA7CmNvbnN0IGludCBtYXhfbWVtID0gbWF4biAqIDEwOwoKaW50IG4sIGssIGFbbWF4bl0sIGJbbWF4bl0sIHBhW21heG5dOwppbnQgcFttYXhuXSwgc2l6ZVttYXhuXTsKY2hhciB0eXBlW21heG5dOwptYXAgPHBpaSwgaW50PiBpbmQ7CmludCBtcG9zLCBtZW1bbWF4biAqIDRdOwppbnQgY2MsIHVbbWF4bl07CmludCBzbiwgc3NbbWF4X21lbV07CgppbmxpbmUgaW50IFNldCggaW50ICp4LCBpbnQgeSApCnsKICBhc3NlcnQoc24gPCBtYXhfbWVtKTsKICBzc1tzbisrXSA9IChpbnQpeCwgc3Nbc24rK10gPSAqeDsKICByZXR1cm4gKnggPSB5Owp9CgppbmxpbmUgdm9pZCBSZXN0b3JlKCBpbnQgb2xkICkKewogIHdoaWxlIChzbiA+IG9sZCkKICAgIHNuIC09IDIsICooKGludCAqKShzc1tzbl0pKSA9IHNzW3NuICsgMV07Cn0KCmludCBHZXQoIGludCBhICkKewogIHJldHVybiBhID09IHBbYV0gPyBhIDogU2V0KCZwW2FdLCBHZXQocFthXSkpOwp9Cgp2b2lkIEpvaW4oIGludCBhLCBpbnQgYiApCnsKICBpZiAo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