#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
//抄博友好程序 优先队列没掌握
int jg[50008];
struct nod{
int l,r,id;
};
bool cmp(nod x,nod y)
{
return x.l<y.l;
}
struct qme{
int r,stall;
bool operator<(const qme& a)const {//抄博友
return r > a.r;
}
};
/*
bool bj(qme x, qme y)
{
return x.r<y.r;
}*/
int main()
{
int n;
cin>>n;
vector<nod> ve;
for(int i=1;i<=n;i++)
{
nod t;
scanf("%d %d",&t.l,&t.r);
//cin>>t.l>>t.r;
t.id=i;
ve.push_back(t);
}
sort(ve.begin(),ve.end(),cmp);
int js=1;//stall数量
priority_queue<qme> pq;
jg[ve[0].id]=js;
qme t;
t.r=ve[0].r;
t.stall=js;
pq.push(t);
for(int i=1;i<n;i++)
{
if(ve[i].l>pq.top().r)
{
//cout<<"hello"<<" "<<i<<endl;
//cout<<pq.top().r<<endl;
qme t;
t.r=ve[i].r;
t.stall=pq.top().stall;
jg[ve[i].id]=pq.top().stall;
pq.pop();
pq.push(t);
}else
{
//cout<<"hi"<<" "<<i<<endl;
qme t;
t.r=ve[i].r;
js++;
t.stall=js;
jg[ve[i].id]=js;
pq.push(t);
}
}
printf("%d\n",js);
//cout<<js<<endl;
for(int i=1;i<=n;i++)
{
printf("%d\n",jg[i]);
//cout<<jg[i]<<endl;
}
return 0;
}