#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

constexpr ll MOD1 = (ll)1e9 + 7LL;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, Q;
    cin >> N >> Q;
    mt19937 mt{random_device{}()};
    uniform_int_distribution<ll> dist1{2, MOD1 - 1};
    const ll base1 = dist1(mt);
    vector<ll> value1(N, 1);
    for (int i = 1; i < N; i++) {
        value1[i] = value1[i - 1] * base1;
        value1[i] %= MOD1;
    }
    ll x1 = 0;
    map<ll, int> hash;
    hash[0] = 0;
    for (int q = 0; q < Q; q++) {
        char c;
        cin >> c;
        if (c == '!') {
            int l, r;
            ll v;
            cin >> l >> r >> v;
            const ll add1 = ((r == N ? 0LL : value1[r] * ((MOD1 - v) % MOD1)) + value1[l] * ((MOD1 + v) % MOD1)) % MOD1;
            x1 = (x1 + add1) % MOD1;
            if (hash.find(x1) == hash.end()) {
                hash[x1] = q + 1;
            }
        } else {
            cout << hash[x1] << endl;
        }
    }
    return 0;
}