// Adam Campbell
// 3/11/05
// Solution to COP 3530 Recitation #6 Problem #2 Programming Problem

import java.io.*;
import java.util.*;

public class Rec6Prob2{

	public static void main(String[] args) throws Exception{
	
		BufferedReader fileReader = new BufferedReader(new FileReader("schedule.in"));
		StringTokenizer tokenizer;
		int n; // number of events
		Event[] events;
		int[] rooms; // the rooms that we can schedule in
		int numRoomsUsed; // the final answer
		int finishTimeLastEvent; 
		
		// read in the number of events
		n = Integer.parseInt(fileReader.readLine());
		
		events = new Event[n];
		rooms = new int[n];
		
		// read in the events
		for(int i = 0; i < n; i++){
			tokenizer = new StringTokenizer(fileReader.readLine());
			events[i] = new Event(Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()));
		}
		
		// sort the events by finish time
		Arrays.sort(events);
		
		numRoomsUsed = 0;
		
		// take the greedy approach to solving the problem
		for(int i = 0; i < n; i++){
		
			boolean canUseRoom = false;
			int roomToUse = -1;
			int finishTimeUsedRoom = Integer.MAX_VALUE;
			
			// see if one of the rooms can be used
			for(int j = 0; j < numRoomsUsed; j++){
				if(events[i].start >= rooms[j] && rooms[j] < finishTimeUsedRoom){
					canUseRoom = true;
					roomToUse = j;
					finishTimeUsedRoom = rooms[j];
				}
			}
			
			if(canUseRoom){
				rooms[roomToUse] = events[i].finish;
			}else{
				rooms[numRoomsUsed++] = events[i].finish;
			}
			
		}
		
		System.out.println("A minimum of " + numRoomsUsed + " rooms are necessary to schedule all events.");
	}
	
	private static class Event implements Comparable{
	
		int start, finish;
		
		public Event(int s, int f){
			start = s;
			finish = f;
		}
		
		public int compareTo(Object obj){
		
			Event that = (Event)obj;
			
			if(this.start < that.start) return -1;
			if(this.start > that.start) return 1;
			
			return 0;
			
		}
		
	}
	
}
