#include<bits/stdc++.h>
#define RG register
#define il inline
#define N 1100000
#define inf (1<<30)
using namespace std;
int ch[N][2],fa[N],st[N],sub[N],rt,cnt,c[N][2],f[N],sz[N];bool rev[N];
bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
void up(int x){sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;return;}
void down(int x){if(rev[x])rev[x]^=1,rev[ch[x][0]]^=1,rev[ch[x][1]]^=1,swap(ch[x][1],ch[x][0]);}
void Rotate(int x){
int y=fa[x],z=fa[y];
int l=ch[y][1]==x,r=l^1;
if(!isroot(y))ch[z][ch[z][1]==y]=x;
fa[x]=z,fa[y]=x,fa[ch[x][r]]=y;ch[y][l]=ch[x][r];
ch[x][r]=y;up(y),up(x);
}
void Splay(int x){int top(0),y=x;st[++top]=x;
while(!isroot(y))st[++top]=fa[y],y=fa[y];
for(int i=top;i;i--)down(st[i]);
while(!isroot(x)){
y=fa[x];int z=fa[y];
if(!isroot(y)){
if(ch[z][0]==y^ch[y][0]==x)Rotate(x);
else Rotate(y);
}Rotate(x);
}
}
void access(int x){int t(0);while(x)Splay(x),ch[x][1]=t,t=x,up(x),x=fa[x];}
void makeroot(int x){access(x),Splay(x),rev[x]^=1;}
void link(int x,int y){
if(!x||!y)return;
makeroot(x);
fa[x]=y;
}
void cut(int x,int y){
if(!x||!y)return;
makeroot(x),access(y),Splay(y);
ch[y][0]=fa[x]=0,up(x),up(y);
}
int Query(int x){makeroot(rt),access(x),Splay(x);return sz[x];}
set<int>s;
set<int>::iterator it;
struct ask{int flag,kk;}a[N];
int main(){
freopen("spaly.in","r",stdin);
freopen("spaly.out","w",stdout);
int T;scanf("%d",&T);
s.insert(-inf),s.insert(inf);
for(int i=1;i<=T;++i){
scanf("%d",&a[i].flag);
if(a[i].flag==1)scanf("%d",&a[i].kk),sub[++sub[0]]=a[i].kk;
}
sort(sub+1,sub+sub[0]+1);int szz=unique(sub+1,sub+sub[0]+1)-sub-1;
for(int i=1;i<=T;++i){
if(a[i].flag==1){
a[i].kk=lower_bound(sub+1,sub+szz+1,a[i].kk)-sub;
if(!cnt){cnt++,rt=a[i].kk,s.insert(rt),printf("1\n");continue;}
else{
it=s.upper_bound(a[i].kk);int nxt=*it,deep(0),x;--it;int pre=*it;
if(pre!=-inf){int now=Query(pre);if(now>deep)deep=now,x=pre;}
if(nxt!=inf){int now=Query(nxt);if(now>deep)deep=now,x=nxt;}
printf("%d\n",deep+1);c[x][a[i].kk>x]=a[i].kk,f[a[i].kk]=x;cnt++;link(x,a[i].kk);
s.insert(a[i].kk);
}
}
if(a[i].flag==2){
if(cnt==1){printf("1\n");continue;}
it=s.begin(),++it;int x=*it,y=c[x][1],z=f[x],k=Query(x);
if(rt!=x){
cut(x,z),cut(x,y),link(x,rt),link(z,y);
f[x]=0,c[x][1]=rt,f[rt]=x,rt=x,c[z][0]=y,f[y]=z;
}
printf("%d\n",k);
}
if(a[i].flag==3){
if(cnt==1){printf("1\n");continue;}
it=s.end(),--it,--it;int x=*it,y=c[x][0],z=f[x],k=Query(x);
if(rt!=x){
cut(x,z),cut(x,y),link(x,rt);link(z,y);
f[x]=0,c[x][0]=rt,f[rt]=x,rt=x,c[z][1]=y;
f[y]=z;
}printf("%d\n",k);
}
if(a[i].flag==4){
if(cnt==1){cnt--;printf("1\n"),s.erase(s.find(rt)),rt=0;continue;}
it=s.begin(),++it;int x=*it,y=c[x][1],z=f[x],k=Query(x);
cut(x,z),cut(x,y);
link(y,z);
cnt--;s.erase(s.find(x));
if(x==rt)rt=y;
c[x][0]=c[x][1]=f[x]=0,c[z][0]=y,f[y]=z;
printf("%d\n",k);
}
if(a[i].flag==5){
if(cnt==1){cnt--;printf("1\n"),s.erase(s.find(rt)),rt=0;continue;}
it=s.end(),--it,--it;int x=*it,y=c[x][0],z=f[x],k=Query(x);
cut(x,z),cut(x,y);link(y,z);cnt--;s.erase(s.find(x));
printf("%d\n",k);if(x==rt)rt=y;
c[x][0]=c[x][1]=f[x]=0,c[z][1]=y,f[y]=z;
}
}
return 0;
}