#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define pre(i, a, b) for(int i = (a); i >= (b); i--)
#define Ede(i, u) for(int i = h[u]; i; i = ne[i])
#define go(i, a) for(auto i : a)
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define PIL pair<int, long long>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sf scanf
#define prf printf
#define el putchar('\n')
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define mmc(arr1, arr2) memcpy(arr1, arr2, sizeof(arr2))
const int inf = 0x3f3f3f3f;
template <typename T> inline void rd(T &x){
x = 0; bool f = true; char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') f = false; ch = getchar();}
while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar();}
if(!f) x = -x;
}
template <typename T, typename ...Args> inline void rd(T &x, Args &...args){ rd(x); rd(args...);}
using namespace std;
const int N = 5e4 + 10;
int n, cre[N], ans[N];
struct Cow{
int l, r, id;
}cow[N];
bool cmp(Cow a, Cow b){
return a.l < b.l;
}
priority_queue<PII, vector<PII>, greater<PII>> heap;
int main(){
rd(n);
rep(i, 1, n) rd(cow[i].l, cow[i].r), cow[i].id = i;
sort(cow + 1, cow + n + 1, cmp);
int cnt = 0;
rep(i, 1, n){
auto t = heap.top();
if(cow[i].l <= t.fi) cnt++;
}
rep(i, 1, n) ans[cow[i].id] = cre[i];
prf("%d\n", cnt);
rep(i, 1, n) prf("%d\n", ans[i]);
return 0;
}