#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a,x) a.begin()+x, a.end()
#define pii pair<int, int>
using ll = long long;
const long long INF = 1e18;
const int inf = 1e9;
void read(){
   #define TASK "DULIEU"
      if(fopen(TASK ".INP", "r")){
         freopen(TASK ".INP", "r", stdin);
         //freopen(TASK ".OUT", "w", stdout);
      }
}
int n, q;
vector<int> st;
void update(int id, int l, int r, int pos, int val){
    if(l == r){
        st[id] = val;
        return;
    }
    int m = (l+r)/2;
    if(pos <= m) update(2*id, l, m, pos, val);
    else update(2*id+1, m+1, r, pos, val);
    st[id] = max(st[2*id], st[2*id+1]);
}
int walk(int id, int l, int r, int u, int v, int val){
    if(u > r || v < l || st[id] <= val) return -1;
    if(l == r) return l;
    int m = (l+r)/2;
    int res = walk(2*id+1, m+1, r, u, v, val);
    if(res != -1) return res;
    return walk(2*id, l, m, u, v, val);
}
void solve(){
    cin >> n >> q;
    vector<multiset<int>> s(n+2);
    st.assign(4*n+1, -inf);
    while(q--){
        int type; cin >> type;
        if(type == 1){
            int pos, val; cin >> pos >> val;
            s[pos].insert(val);
            int cur = *s[pos].rbegin();
            update(1,1,n,pos,cur);
        }
        else if(type == 2){
            int pos, val; cin>> pos >> val;
            auto it = s[pos].find(val);
            if(it != s[pos].end()){
                s[pos].erase(it);
                int cur = s[pos].empty() ? -inf : *s[pos].rbegin();
                update(1,1,n,pos,cur);
            }
        }
        else{
            int l, r, val; cin >> l >> r >> val;
            int ans = walk(1,1,n,l,r,val);
            if(ans == -1) cout << "NONE" << '\n';
            else cout << ans << '\n';
        }
    }
}
signed main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);
    read();
    int t=1; //cin >> t;
    while(t--) solve();
    return 0;
}
