#include <bits/stdc++.h>
using namespace std;
#define rand() dis(gen)
mt19937 gen(time(0));
uniform_int_distribution<int> dis(0, 1e9);
const int INF = 1e9 + 69 * 69;
typedef struct treap {
int key, prior, dmin, submax, submin;
treap *left, *right;
treap(int _key = 0) { key = _key; submin = _key; dmin = INF; submax = _key; prior = rand(); left = right = 0; }
}*Treap;
int x;
string op;
Treap root;
void tupd(Treap &root) {
if(!root) return;
root->dmin = INF;
root->submin = root->submax = root->key;
if(root->left) {
root->dmin = min(root->dmin, root->key - root->left->submax);
root->dmin = min(root->dmin, root->left->dmin);
root->submin = min(root->left->submin, root->submin);
root->submax = max(root->left->submax, root->submax);
}
if(root->right) {
root->dmin = min(root->dmin, root->right->submin - root->key);
root->dmin = min(root->dmin, root->right->dmin);
root->submin = min(root->right->submin, root->submin);
root->submax = max(root->right->submax, root->submax);
}
}
void tsplit(Treap root, int key, Treap &l, Treap &r) {
if(!root) l = r = 0;
else if(key < root->key) tsplit(root->left, key, l, root->left), r = root;
else tsplit(root->right, key, root->right, r), l = root;
tupd(root);
}
void tmerge(Treap &root, Treap l, Treap r) {
if(!l || !r) root = (l ? l : r);
else if(l->prior > r->prior) tmerge(l->right, l->right, r), root = l;
else tmerge(r->left, l, r->left), root = r;
tupd(root);
}
void tinsert(Treap &root, Treap now) {
if(!root) root = now;
else if(root->prior < now->prior) tsplit(root, now->key, now->left, now->right), root = now;
else if(root->key < now->key) tinsert(root->right, now);
else tinsert(root->left, now);
tupd(root);
}
void terase(Treap &root, int key) {
if(root->key == key) tmerge(root, root->left, root->right);
else if(root->key < key) terase(root->right, key);
else terase(root->left, key);
tupd(root);
}
bool tfind(Treap &root, int key) {
if(!root) return 0;
if(root->key == key) return 1;
if(root->key < key) return tfind(root->right, key);
return tfind(root->left, key);
}
int main()
{
ifstream cin("zeap.in");
ofstream cout("zeap.out");
ios_base::sync_with_stdio(0);
while(cin >> op) {
if(op == "I") {
cin >> x;
if(!tfind(root, x)) tinsert(root, new treap(x));
continue;
}
if(op == "S") {
cin >> x;
if(!tfind(root, x)) cout << "-1\n";
else terase(root, x);
continue;
}
if(op == "C") cin >> x, cout << tfind(root, x) << '\n';
if(op == "MAX") {
if(!root || root->dmin == INF) {
cout << "-1\n";
continue;
}
cout << root->submax - root->submin << '\n';
continue;
}
if(op == "MIN") {
if(!root || root->dmin == INF) {
cout << "-1\n";
continue;
}
cout << root->dmin << '\n';
continue;
}
}
return 0;
}