#include<bits/stdc++.h>
using namespace std;
map<int, int> a;
int num;
void insertValue(int x) {
//TODO 实现插入逻辑
a[x]++;
num++;
}
void eraseValue(int x) {
//TODO 实现删除逻辑
auto it = a.find(x);
if (it == a.end()) return;
it->second--;
num--;
if (it->second == 0) {
a.erase(it);
}
}
int xCount(int x) {
//TODO 求x在集合中的个数
auto it = a.find(x);
return it->second;
}
int sizeOfSet() {
//TODO 返回集合大小
return num;
}
int getPre(int x) {
//TODO 实现找前驱
auto it = a.lower_bound(x);
if (it != a.begin()) {
--it;
return it->first;
}
return -1;
}
int getBack(int x) {
//TODO 实现找后继
auto it = a.upper_bound(x);
if (it != a.end()) {
return it->first;
}
return -1;
}
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;
}