#include <fstream>
#include <sstream>
using namespace std;
const int Inf=2000000000;
struct T{
int key,prt;
int Min,Max,mindif;
T *left,*right;
T() {}
T(int K,int P,T *L,T *R){
this->key=K;
this->prt=P;
this->left=L;
this->right=R;
}
} *R,*nil;
void Init(){
srand(time(0));
R=nil=new T(0,0,NULL,NULL);
R->mindif=nil->mindif=Inf;
R->Min=nil->Min=Inf;
R->Max=nil->Max=-Inf;
}
int Search(T *R,int x){
if (R==nil) return 0;
if (R->key==x) return 1;
if (R->key>x) return Search(R->left,x);
return Search(R->right,x);
}
void Update(T* &R){
R->Max=max(R->key,R->right->Max);
R->Min=min(R->key,R->left->Min);
R->mindif=min(min(R->left->mindif,R->right->mindif),
min(R->key - R->left->Max, R->right->Min - R->key));
}
void Rotleft(T* &R){
T *P=R->left;
R->left=P->right;
P->right=R;
Update(R);
R=P;
}
void Rotright(T* &R){
T *P=R->right;
R->right=P->left;
P->left=R;
Update(R);
R=P;
}
void Balance(T* &R){
if (R->prt < R->left->prt)
Rotleft(R);
else
if (R->prt < R->right->prt)
Rotright(R);
Update(R);
}
void Insert(T* &R,int x,int p){
if (R==nil) {R=new T(x,p,nil,nil);
Update(R);
return;}
if (R->key>x) Insert(R->left,x,p);
else Insert(R->right,x,p);
Balance(R);
}
void Erase(T* &R,int x){
if (R==nil) return;
if (R->key>x) Erase(R->left,x);
else if (R->key<x) Erase(R->right,x);
else
if (R->left==nil && R->right==nil)
{delete R,R=nil;return;}
else
{
if (R->right->prt > R->left->prt)
Rotright(R);
else
Rotleft(R);
Erase(R,x);
}
Update(R);
}
int main(){
ifstream f("zeap.in");
ofstream g("zeap.out");
string s,op;
int N=0,x;
Init();
while (getline(f,s)){
istringstream is(s);
is>>op;
if (op=="I") {is>>x;if (!Search(R,x)) Insert(R,x,1+rand()),++N;}
else
if (op=="S") {is>>x;if (Search(R,x)) Erase(R,x),--N;
else g<<"-1\n";}
else
if (op=="C") {is>>x;g<<Search(R,x)<<'\n';}
else
if (op=="MAX") {if (N>=2) g<<R->Max - R->Min<<'\n';
else g<<"-1\n";}
else
if (op=="MIN") {if (N>=2) g<<R->mindif<<'\n';
else g<<"-1\n";}
}
return 0;
}