#include <bits/stdc++.h>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
mt19937 mt(time(0));
const int inf=2e9;
struct nod{
int key;
int pr;
int cnt;
int mx;
int mn;
int dif;
nod *l, *r;
nod(int x):key(x),pr(mt()),cnt(1),mx(x),mn(x),dif(inf),l(NULL),r(NULL){}
};
typedef nod* treap;
treap root;
int cnt(treap t)
{
if(t)return t->cnt;
return 0;
}
int mx(treap t)
{
if(t)return t->mx;
return 0;
}
int mn(treap t)
{
if(t)return t->mn;
return inf;
}
int dif(treap t)
{
if(t)return t->dif;
return inf;
}
void upd(treap &t)
{
if(!t)return;
t->cnt = cnt(t->l)+cnt(t->r)+1;
t->mx = max(t->key,mx(t->r));
t->mn = min(t->key,mn(t->l));
t->dif = min(dif(t->l),dif(t->r));
if(t->l)t->dif = min(t->dif,t->key-mx(t->l));
if(t->r)t->dif = min(t->dif,mn(t->r)-t->key);
}
void split(treap t, treap &st, treap &dr, int x)
{
if(!t)
{
st=dr=NULL;
return;
}
if(t->key<=x)
split(t->r,t->r,dr,x),st=t;
else split(t->l,st,t->l,x),dr=t;
}
void mer(treap &t, treap st, treap dr)
{
if(!st)t=dr;
else if(!dr)t=st;
else if(st->pr>=dr->pr)
mer(st->r,st->r,dr),t=st;
else mer(dr->l,st,dr->l),t=dr;
upd(t);
}
bool cauta(treap t, int x)
{
if(!t)return 0;
if(t->key==x)return 1;
if(t->key>x)return cauta(t->l,x);
return cauta(t->r,x);
}
void ins(int x)
{
if(cauta(root,x))return;
treap maimic;
treap maimare;
treap egal = new nod(x);
split(root,maimic,maimare,x-1);
mer(maimic,maimic,egal);
mer(maimic,maimic,maimare);
root = maimic;
}
void sterge(int x)
{
if(!cauta(root,x)){fout<<-1<<'\n';return;}
treap maimic;
treap maimare;
treap trash;
split(root,maimic,maimare,x-1);
split(maimare,trash,maimare,x);
mer(root,maimic,maimare);
}
void maxdif()
{
if(cnt(root)<2){fout<<-1<<'\n';return;}
fout<<mx(root)-mn(root)<<'\n';
}
void mindif()
{
if(cnt(root)<2){fout<<-1<<'\n';return;}
fout<<dif(root)<<'\n';
}
string s;
int nr;
signed main()
{
while(fin>>s)
{
if(s=="MAX")maxdif();
else if(s=="MIN")mindif();
else {
fin>>nr;
if(s=="I")ins(nr);
else if(s=="S")sterge(nr);
else fout<<cauta(root,nr)<<'\n';
}
}
return 0;
}