#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=3e2+5;
int n,m,q,ans[N];vector<int>adj[N];vector<array<int,2>>ed;vector<vector<int>>seg,add(N),cut(N);
struct dsu
{
vector<int>parent,group;stack<array<int,4>>st;int res;
dsu(int n)
{
parent=vector<int>(n);
iota(parent.begin(),parent.end(),0);
group=vector<int>(n,1);
}
int find(int i)
{
if(parent[i]==i)
{
return i;
}
return find(parent[i]);
}
bool samegroup(int x,int y)
{
return find(x)==find(y);
}
void merge(int x,int y)
{
int leader1=find(x);
int leader2=find(y);
if(group[leader1]>group[leader2])
{
swap(leader1,leader2);
}
array<int,4>p={0,0,0,0};
p[0]=leader2,p[1]=group[leader2];
p[2]=leader1,p[3]=parent[leader1];
st.push(p);
if(leader1==leader2)
{
return;
}
res--;
group[leader2]+=group[leader1];
parent[leader1]=leader2;
}
void set(int n)
{
res=n;
}
int getsize(int x)
{
return group[find(x)];
}
void rollBack()
{
if(st.empty())return;
auto &[a,b,c,d]=st.top();
st.pop();
group[a]=b;
parent[c]=d;
res+=!samegroup(a,c);
}
int getAns()
{
return res;
}
}d(N);
void update(int l,int r,int node,int lx,int rx,int idx)
{
if(r<lx||l>rx) return;
if(l>=lx&&r<=rx)
{
seg[node].push_back(idx);
return;
}
int mid=l+r>>1;
update(l,mid,2*node+1,lx,rx,idx);
update(mid+1,r,2*node+2,lx,rx,idx);
}
void build(int l,int r,int node)
{
for(auto it:seg[node])
{
d.merge(ed[it][0],ed[it][1]);
}
if(l==r)
{
ans[l]=d.getAns();
for(auto it:seg[node])
{
d.rollBack();
}
return;
}
int mid=l+r>>1;
build(l,mid,2*node+1);
build(mid+1,r,2*node+2);
for(auto it:seg[node])
{
d.rollBack();
}
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
freopen("connect.in","r",stdin);
freopen("connect.out","w",stdout);
cin>>n>>q;
map<array<int,2>,int>mp;
ed.push_back({0,0});
vector<int>queries;
for(int i=1;i<=q;i++)
{
char op;cin>>op;
if(op=='?')
{
queries.push_back(i);
}
else
{
int a,b;cin>>a>>b;
if(b>a) swap(a,b);
if(op=='+')
{
if(!mp.count({a,b}))
{
ed.push_back({a,b});
mp[{a,b}]=ed.size()-1;
}
int idx=mp[{a,b}];
add[idx].push_back(i);
}
else
{
int idx=mp[{a,b}];
cut[idx].push_back(i);
}
}
}
for(int i=1;i<ed.size();i++)
{
cut[i].push_back(q+1);
}
int sz=1;
while(sz<=q+5) sz<<=1;
seg=vector<vector<int>>(sz<<1);
for(int i=1;i<ed.size();i++)
{
for(auto it:add[i])
{
update(0,sz-1,0,it,*lower_bound(cut[i].begin(),cut[i].end(),it)-1,i);
}
}
d.set(n);
build(0,sz-1,0);
for(auto it:queries)
{
cout<<ans[it]<<'\n';
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKY29uc3QgaW50IE49M2UyKzU7CmludCBuLG0scSxhbnNbTl07dmVjdG9yPGludD5hZGpbTl07dmVjdG9yPGFycmF5PGludCwyPj5lZDt2ZWN0b3I8dmVjdG9yPGludD4+c2VnLGFkZChOKSxjdXQoTik7CnN0cnVjdCBkc3UKewogICAgdmVjdG9yPGludD5wYXJlbnQsZ3JvdXA7c3RhY2s8YXJyYXk8aW50LDQ+PnN0O2ludCByZXM7CiAgICBkc3UoaW50IG4pCiAgICB7CiAgICAgICAgcGFyZW50PXZlY3RvcjxpbnQ+KG4pOwogICAgICAgIGlvdGEocGFyZW50LmJlZ2luKCkscGFyZW50LmVuZCgpLDApOwogICAgICAgIGdyb3VwPXZlY3RvcjxpbnQ+KG4sMSk7CiAgICB9CiAgICBpbnQgZmluZChpbnQgaSkKICAgIHsKICAgICAgICBpZihwYXJlbnRbaV09PWkpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gaTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGZpbmQocGFyZW50W2ldKTsKICAgIH0KICAgIGJvb2wgc2FtZWdyb3VwKGludCB4LGludCB5KQogICAgewogICAgICAgIHJldHVybiBmaW5kKHgpPT1maW5kKHkpOwogICAgfQogICAgdm9pZCBtZXJnZShpbnQgeCxpbnQgeSkKICAgIHsKICAgICAgICBpbnQgbGVhZGVyMT1maW5kKHgpOwogICAgICAgIGludCBsZWFkZXIyPWZpbmQoeSk7CiAgICAgICAgaWYoZ3JvdXBbbGVhZGVyMV0+Z3JvdXBbbGVhZGVyMl0pCiAgICAgICAgewogICAgICAgICAgICBzd2FwKGxlYWRlcjEsbGVhZGVyMik7CiAgICAgICAgfQogICAgICAgIGFycmF5PGludCw0PnA9ezAsMCwwLDB9OwogICAgICAgIHBbMF09bGVhZGVyMixwWzFdPWdyb3VwW2xlYWRlcjJdOwogICAgICAgIHBbMl09bGVhZGVyMSxwWzNdPXBhcmVudFtsZWFkZXIxXTsKICAgICAgICBzdC5wdXNoKHApOwogICAgICAgIGlmKGxlYWRlcjE9PWxlYWRlcjIpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIHJlcy0tOwogICAgICAgIGdyb3VwW2xlYWRlcjJdKz1ncm91cFtsZWFkZXIxXTsKICAgICAgICBwYXJlbnRbbGVhZGVyMV09bGVhZGVyMjsKICAgIH0KICAgIHZvaWQgc2V0KGludCBuKQogICAgewogICAgICAgIHJlcz1uOwogICAgfQogICAgaW50IGdldHNpemUoaW50IHgpCiAgICB7CiAgICAgICAgcmV0dXJuIGdyb3VwW2ZpbmQoeCldOwogICAgfQogICAgdm9pZCByb2xsQmFjaygpCiAgICB7CiAgICAgICAgaWYoc3QuZW1wdHkoKSlyZXR1cm47CiAgICAgICAgYXV0byAmW2EsYixjLGRdPXN0LnRvcCgpOwogICAgICAgIHN0LnBvcCgpOwogICAgICAgIGdyb3VwW2FdPWI7CiAgICAgICAgcGFyZW50W2NdPWQ7CiAgICAgICAgcmVzKz0hc2FtZWdyb3VwKGEsYyk7CiAgICB9CiAgICBpbnQgZ2V0QW5zKCkKICAgIHsKICAgICAgICByZXR1cm4gcmVzOwogICAgfQp9ZChOKTsKdm9pZCB1cGRhdGUoaW50IGwsaW50IHIsaW50IG5vZGUsaW50IGx4LGludCByeCxpbnQgaWR4KQp7CiAgICBpZihyPGx4fHxsPnJ4KSByZXR1cm47CiAgICBpZihsPj1seCYmcjw9cngpCiAgICB7CiAgICAgICAgc2VnW25vZGVdLnB1c2hfYmFjayhpZHgpOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGludCBtaWQ9bCtyPj4xOwogICAgdXBkYXRlKGwsbWlkLDIqbm9kZSsxLGx4LHJ4LGlkeCk7CiAgICB1cGRhdGUobWlkKzEsciwyKm5vZGUrMixseCxyeCxpZHgpOwp9CnZvaWQgYnVpbGQoaW50IGwsaW50IHIsaW50IG5vZGUpCnsKICAgIGZvcihhdXRvIGl0OnNlZ1tub2RlXSkKICAgIHsKICAgICAgICBkLm1lcmdlKGVkW2l0XVswXSxlZFtpdF1bMV0pOwogICAgfQogICAgaWYobD09cikKICAgIHsKICAgICAgICBhbnNbbF09ZC5nZXRBbnMoKTsKICAgICAgICBmb3IoYXV0byBpdDpzZWdbbm9kZV0pCiAgICAgICAgewogICAgICAgICAgICBkLnJvbGxCYWNrKCk7CiAgICAgICAgfQogICAgICAgIHJldHVybjsKICAgIH0KICAgIGludCBtaWQ9bCtyPj4xOwogICAgYnVpbGQobCxtaWQsMipub2RlKzEpOwogICAgYnVpbGQobWlkKzEsciwyKm5vZGUrMik7CiAgICBmb3IoYXV0byBpdDpzZWdbbm9kZV0pCiAgICB7CiAgICAgICAgZC5yb2xsQmFjaygpOwogICAgfQp9CnNpZ25lZCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTtjaW4udGllKDApO2NvdXQudGllKDApOwogICAgZnJlb3BlbigiY29ubmVjdC5pbiIsInIiLHN0ZGluKTsKICAgIGZyZW9wZW4oImNvbm5lY3Qub3V0IiwidyIsc3Rkb3V0KTsKICAgIGNpbj4+bj4+cTsKICAgIG1hcDxhcnJheTxpbnQsMj4saW50Pm1wOwogICAgZWQucHVzaF9iYWNrKHswLDB9KTsKICAgIHZlY3RvcjxpbnQ+cXVlcmllczsKICAgIGZvcihpbnQgaT0xO2k8PXE7aSsrKQogICAgewogICAgICAgIGNoYXIgb3A7Y2luPj5vcDsKICAgICAgICBpZihvcD09Jz8nKQogICAgICAgIHsKICAgICAgICAgICAgcXVlcmllcy5wdXNoX2JhY2soaSk7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGludCBhLGI7Y2luPj5hPj5iOwogICAgICAgICAgICBpZihiPmEpIHN3YXAoYSxiKTsKICAgICAgICAgICAgaWYob3A9PScrJykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYoIW1wLmNvdW50KHthLGJ9KSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBlZC5wdXNoX2JhY2soe2EsYn0pOwogICAgICAgICAgICAgICAgICAgIG1wW3thLGJ9XT1lZC5zaXplKCktMTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGludCBpZHg9bXBbe2EsYn1dOwogICAgICAgICAgICAgICAgYWRkW2lkeF0ucHVzaF9iYWNrKGkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaW50IGlkeD1tcFt7YSxifV07CiAgICAgICAgICAgICAgICBjdXRbaWR4XS5wdXNoX2JhY2soaSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBmb3IoaW50IGk9MTtpPGVkLnNpemUoKTtpKyspCiAgICB7CiAgICAgICAgY3V0W2ldLnB1c2hfYmFjayhxKzEpOwogICAgfQogICAgaW50IHN6PTE7CiAgICB3aGlsZShzejw9cSs1KSBzejw8PTE7CiAgICBzZWc9dmVjdG9yPHZlY3RvcjxpbnQ+Pihzejw8MSk7CiAgICBmb3IoaW50IGk9MTtpPGVkLnNpemUoKTtpKyspCiAgICB7CiAgICAgICAgZm9yKGF1dG8gaXQ6YWRkW2ldKQogICAgICAgIHsKICAgICAgICAgICAgdXBkYXRlKDAsc3otMSwwLGl0LCpsb3dlcl9ib3VuZChjdXRbaV0uYmVnaW4oKSxjdXRbaV0uZW5kKCksaXQpLTEsaSk7CiAgICAgICAgfQogICAgfQogICAgZC5zZXQobik7CiAgICBidWlsZCgwLHN6LTEsMCk7CiAgICBmb3IoYXV0byBpdDpxdWVyaWVzKQogICAgewogICAgICAgIGNvdXQ8PGFuc1tpdF08PCdcbic7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==