#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
using PII=pair<int,int>;
const int N=5e4+10;
int n,id[N];
pair<PII,int >cows[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=0;i<n;++i){
//get star and end time point
cin>>cows[i].first.first>>cows[i].first.second;
cows[i].second=i;
}
sort(cows,cows+n);
priority_queue<PII,vector<PII>,greater<PII>> heap;
for(int i=0;i<n;++i){
if(heap.empty()||heap.top().first>=cows[i].first.first){
PII stall={cows[i].first.second,heap.size()};
id[cows[i].second]=stall.second;
heap.push(stall);
}else{
auto stall=heap.top();
heap.pop();
stall.first=cows[i].first.second;
id[cows[i].second]=stall.second;
heap.push(stall);
}
}
cout<<heap.size()<<'\n';
for(int i=0;i<n;++i){
cout<<id[i]+1<<'\n';
}
return 0;
}