#include <bits/stdc++.h>
using namespace std;
#define ms(s,n) memset(s,n,sizeof(s))
#define all(a) a.begin(),a.end()
#define present(t, x) (t.find(x) != t.end())
#define sz(a) int((a).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9+7;
const int INF = (int) 1e9+1;
inline ll gcd(ll a,ll b){ll r;while(b){r=a%b;a=b;b=r;}return a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
bool cmp(pair<pi,int> a, pair<pi, int > b){
if(a.fi.fi != b.fi.fi)
return a.fi.fi < b.fi.fi;
return a.fi.se < b.fi.se;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n; cin >>n;
vector<pair<pi, int >> a(n);
for(int i = 0; i < n; i++){
cin >> a[i].fi.fi >> a[i].fi.se;
a[i].se = i;
}
sort(all(a), cmp);
// for(auto it : a){
// cout << it.fi.fi <<" " << it.fi.se << endl;
// }
int cnt = 1;
vi res(n, 0);
multimap<int,int> mp;
mp.insert({a[0].fi.se, 1});
res[a[0].se] = 1;
for(int i = 1; i < n; i++){
int tmp = a[i].fi.fi;
auto it = mp.lower_bound(tmp);
if(it == mp.end()){
res[a[i].se] = mp.rbegin()->se;
auto it2 = mp.end();
mp.erase(--it2);
mp.insert({a[i].fi.se, res[a[i].se]});
}
else if(it == mp.begin()){
res[a[i].se] = ++cnt;
mp.insert({a[i].fi.se, cnt});
}
else{
--it;
res[a[i].se] = it->se;
mp.erase(it);
mp.insert({a[i].fi.se, res[a[i].se]});
}
}
cout << cnt << endl;
for(int x : res){
cout << x << " ";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cn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