#include<bits/stdc++.h>
using namespace std;
set<int> st; 
void instrtValue(int x){
    //TODO 实现插入逻辑
    st.insert(x);
    return;
}
void erastValue(int x){
    //TODO 实现删除逻辑
    st.erase(x);
    return;
}
int xInstt(int x){
    //TODO 实现存在性检查
    if(st.find(x)!=st.end()){
    	return 1;
	}else{
		return 0;
	}
}
int sizeOfstt(){
    //TODO 返回集合大小
    return st.size();
}
int getPre(int x){
    //TODO 实现找前驱
    bool f=false;
    if(st.find(x)==st.end()){
    	st.insert(x);
    	f=true;
	}else{
	    st.insert(x);	
	}
	set<int>::iterator it=st.find(x);
    if(it==st.begin()){
    	if(f) st.erase(x);
    	return -1;
	}else{
		it--;
		if(f) st.erase(x);
		return *(it);
	}
}
int getBack(int x){
    //TODO 实现找后继
    pair<set<int>::const_iterator,set<int>::const_iterator> pr;
    pr=st.equal_range(x);
    if(pr.second!=st.end()){
    	return *pr.second;
	}else{
		return -1;
	}
}

int main(){
    int q,op,x;
    cin>>q;
    while(q--){
        cin>>op;
        if(op==1){
            cin>>x;
            instrtValue(x);
        }
        if(op==2){
            cin>>x;
            erastValue(x);
        }
        if(op==3){
            cin>>x;
            if(xInstt(x)){
                cout<<"YES\n";
            }else{
                cout<<"NO\n";
            }
        }
        if(op==4){
            cout<<sizeOfstt()<<endl;
        }
        if(op==5){
            cin>>x;
            cout<<getPre(x)<<endl;
        }
        if(op==6){
            cin>>x;
            cout<<getBack(x)<<endl;
        }
    }
    return 0;
}