#include<bits/stdc++.h>

using namespace std;
multiset<int> s;
void insertValue(int x){
    //TODO 实现插入逻辑
    s.insert(x);
}
void eraseValue(int x){
    //TODO 实现删除逻辑
    auto it = s.find(x);//找不到就会赋值为s.end虚空区域
    if (it != s.end()) {
        s.erase(it);//这里多重集合的erase接受迭代器,那么只会删除迭代器指向的元素
        // s.erase(x); 如果让erase接受int,那么触发erase函数重载,删除多重集合里所有值等于x的元素
    }
}
int xCount(int x){
    //TODO 求x在集合中的个数
    return s.count(x);
}
int sizeOfSet(){
    //TODO 返回集合大小
    return s.size();
}
int getPre(int x){
    //TODO 实现找前驱
    auto pre = s.lower_bound(x);
    if (pre == s.begin()) {
        return -1;
    }
    pre--;
    return *pre;
}
int getBack(int x){
    //TODO 实现找后继
    auto back = s.upper_bound(x);
    if (back == s.end()) {
        return -1;
    }
    return *back;
}

int main(){
    int q,op,x;
    cin>>q;
    while(q--){
        cin>>op;
        if(op==1){
            cin>>x;
            insertValue(x);
        }
        if(op==2){
            cin>>x;
            eraseValue(x);
        }
        if(op==3){
            cin>>x;
            cout<<xCount(x)<<endl;
        }
        if(op==4){
            cout<<sizeOfSet()<<endl;
        }
        if(op==5){
            cin>>x;
            cout<<getPre(x)<<endl;
        }
        if(op==6){
            cin>>x;
            cout<<getBack(x)<<endl;
        }
    }
    return 0;
}