import java.io.*;
import java.util.*;

public class taxibus_sp implements Runnable {

	public static void main(String[] args) {
		new Thread(new taxibus_sp()).start();
	}

	public void run() {
		try {
			Locale.setDefault(Locale.US);
			br = new BufferedReader(new FileReader(FILENAME + ".in"));
			out = new PrintWriter(FILENAME + ".out");
			solve();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	BufferedReader br;

	PrintWriter out;

	StringTokenizer st;

	String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	void myAssert(boolean e, String s) {
		if (!e) {
			throw new Error("Assertion failure " + s);
		}
	}

	private static final String FILENAME = "taxibus";
	
	class Passenger implements Comparable<Passenger>{
		int l, r, seat;
		
		Passenger(int ll, int rr) {
			l = ll;
			r = rr;
		}

		@Override
		public int compareTo(Passenger o) {
			return r - o.r;
		}
	}
	
	class Event implements Comparable<Event> {
		int t, type;
		Passenger p;
		
		Event(int time, int typ, Passenger pp) {
			t = time;
			type = typ;
			p = pp;
		}

		@Override
		public int compareTo(Event o) {
			return t - o.t;
		}
	}

	public void solve() throws IOException {
		int n = nextInt();
		Passenger[] p = new Passenger[n];
		Event[] e = new Event[2 * n];
		for (int i = 0; i < n; i++) {
			p[i] = new Passenger(nextInt(), nextInt());
			e[2 * i] = new Event(p[i].l, -1, p[i]);
			e[2 * i + 1] = new Event(p[i].r, 1, p[i]);
		}
		Passenger[] ps = p.clone();
		Arrays.sort(ps);
		for (int i = 0; i < n; i++) {
			ps[i].seat = i;
		}
		Arrays.sort(e);
		long ans = 0;
		RSQ rsq = new RSQ(n);
		for (Event ee : e) {
			if (ee.type == -1) {
				ans += rsq.sum(0, ee.p.seat);
				rsq.add(ee.p.seat, 1);
			} else {
				rsq.add(ee.p.seat, -1);
			}
		}
		out.println(ans);
		for (int i = 0; i < n; i++) {
			out.print(p[i].seat + 1 + " ");
		}
	}
	
	class RSQ {
		int[] a;
		int n;
		
		RSQ(int m) {
			n = 1;
			while (n < m) {
				n *= 2;
			}
			a = new int[2 * n];
		}
		
		int sum(int l, int r, int ll, int rr, int x) {
			if (l > rr || r < ll) return 0;
			if (ll >= l && rr <= r) return a[x];
			return sum(l, r, ll, (ll + rr) / 2, 2 * x) + sum(l, r, (ll + rr) / 2 + 1, rr, 2 * x + 1); 
		}
		
		int sum(int l, int r) {
			return sum(l, r, 0, n - 1, 1);
		}
		
		void add(int x, int v) {
			x += n;
			while (x > 0) {
				a[x] += v;
				x /= 2;
			}
		}
	}
}
