#include using namespace std; typedef long long ll; typedef vector vint; typedef pair pint; typedef vector vpint; #define rep(i,n) for(int i=0;i<(n);i++) #define reps(i,f,n) for(int i=(f);i<(n);i++) #define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++) #define all(v) (v).begin(),(v).end() #define pb push_back #define mp make_pair #define fi first #define se second #define chmax(a, b) a = (((a)<(b)) ? (b) : (a)) #define chmin(a, b) a = (((a)>(b)) ? (b) : (a)) const int MOD = 1e9 + 7; const int INF = 1e9; typedef unsigned long long ull; unordered_map mapp; ull B = 1e9 + 7; ull table[100010]; int n, q; int main(void){ cin >> n >> q; ull t = 1; rep(i, 100001){ t *= B; table[i] = t; } //区間(l~r)に k を加算するときの値を table[l] * table[r] * table[k] //現在のmapp値を Z として、 ull d = B; mapp[d] = 0; int cnt = 0; rep(i, q){ string c; cin >> c; if(c == "!"){ int l, r, k; cin >> l >> r >> k; cnt++; if(k < 0) k *= -1; ull h = table[l] * table[r] * table[k]; d ^= h; if(mapp.find(d) == mapp.end()){ mapp[d] = cnt; } }else{ printf("%d\n", mapp[d]); } } }