import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.StringTokenizer;

public class taxibus_sm_int {
	class Passenger implements Comparable<Passenger> {
		int from, to;
		public int place;
		int id;

		public Passenger(int from, int to, int id) {
			this.from = from;
			this.to = to;
			this.id = id;
		}

		@Override
		public int compareTo(Passenger p) {
			return to - p.to;
		}
	}

	class Event implements Comparable<Event> {
		int type;
		Passenger passenger;
		int stop;

		public Event(int type, Passenger passenger, int stop) {
			super();
			this.type = type;
			this.passenger = passenger;
			this.stop = stop;
		}

		@Override
		public int compareTo(Event e) {
			return stop - e.stop;
		}
	}

	class SegmentTree {
		int[] a;

		public SegmentTree() {
			a = new int[1 << 18];
		}

		int get(int l, int r) {
			l += 1 << 17;
			r += 1 << 17;
			int ans = 0;
			while (l < r) {
				if (l % 2 == 0) {
					ans += a[l];
				}
				l = l / 2;
				if (r % 2 == 1) {
					ans += a[r];
				}
				r = (r - 2) / 2;
			}
			if (l == r) {
				ans += a[l];
			}
			return ans;
		}

		void add(int x, int d) {
			x += 1 << 17;
			while (x > 0) {
				a[x] += d;
				x = (x - 1) / 2;
			}
			a[0] += d;
		}
	}

	void solve() throws IOException {
		int n = nextInt();
		assert 1 <= n && n <= 100000;
		HashSet<Integer> stop = new HashSet<Integer>();
		Passenger[] p = new Passenger[n];
		ArrayList<Event> es = new ArrayList<Event>();
		for (int i = 0; i < n; i++) {
			int from = nextInt();
			int to = nextInt();
			assert 1 <= from && from < to && to <= 200000;
			assert !stop.contains(from) && !stop.contains(to);
			stop.add(from);
			stop.add(to);
			p[i] = new Passenger(from, to, i);
			es.add(new Event(1, p[i], from));
			es.add(new Event(-1, p[i], to));
		}
		Arrays.sort(p);
		int[] ans = new int[n];
		for (int i = 0; i < p.length; i++) {
			p[i].place = i;
			ans[p[i].id] = i;
		}
		Collections.sort(es);
		SegmentTree tree = new SegmentTree();
		int sum = 0;
		for (Event e : es) {
			if (e.type == 1) {
				sum += tree.get(0, e.passenger.place);
				tree.add(e.passenger.place, 1);
			} else {
				tree.add(e.passenger.place, -1);
				assert tree.get(0, e.passenger.place) == 0;
			}
		}
		out.println(sum);
		for (int i = 0; i < ans.length; i++) {
			out.print((ans[i] + 1) + " ");
		}
		out.println();
	}

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;

	public void run() {
		try {
			String filename = this.getClass().getName()
					.substring(0, this.getClass().getName().indexOf("_"));
			br = new BufferedReader(new FileReader(filename + ".in"));
			out = new PrintWriter(filename + ".out");
			solve();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	String nextToken() {
		try {
			while (st == null || !st.hasMoreTokens()) {
				String s = br.readLine();
				if (s == null) {
					return null;
				}
				st = new StringTokenizer(s);
			}
			return st.nextToken();
		} catch (IOException e) {
			throw new RuntimeException();
		}
	}

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

	long nextLong() {
		return Integer.parseInt(nextToken());
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.US);
		new taxibus_sm_int().run();
	}
}
