// Arup Guha
// 4/29/2023
// Solution to Kattis Problem: Classrooms
// https://open.kattis.com/problems/classrooms
// Originally written in contest for COP 4516, later used for COP 3503 RP3.

import java.util.*;
import java.io.*;

public class classrooms {
    
    public static void main(String[] args) throws Exception {

		// Get # of activities, rooms.
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(stdin.readLine());
        int n = Integer.parseInt(tok.nextToken());
        int k = Integer.parseInt(tok.nextToken());
        
		// Store each activity.
        item[] list = new item[n];
        for (int i=0; i<n; i++) {
            tok = new StringTokenizer(stdin.readLine());
            int s =Integer.parseInt(tok.nextToken());
            int e =Integer.parseInt(tok.nextToken());
            list[i] = new item(s,e,i);
        }
		
		// Sort by end time (then start, then id)
        Arrays.sort(list);
        
		// Let each room be ready at time 0 to accept a new activity.
        TreeSet<room> roomList = new TreeSet<room>();
        for (int i=0; i<k; i++)
            roomList.add(new room(0, i));
            
		// Will store scheduled activities.
        int res = 0;
        for (int i=0; i<n; i++) {
        
			// Find a room that i open early enough, but just barely!!!
            room best = roomList.lower(new room(list[i].s, n+1));
            
            // No way to schedule this event greedily.
            if (best == null) continue;
			
			// The id of the event we are scheduling.
            int id = best.id;
			
			// Add it since it got scheduled.
            res++;
            
			// Remove the old event that was happening and place this one in.
            roomList.remove(best);
            roomList.add(new room(list[i].e+1, id));
        }
        
		// Ta da!
        System.out.println(res);
    }
}

class item implements Comparable<item> {

    public int s;
    public int e;
    public int id;
    
    public item(int mys, int mye, int myi) {
        s = mys;
        e = mye;
        id = myi;
    }
    
	// Events sorted by end time, then start time, then event id.
    public int compareTo(item other) {
        if (this.e != other.e) return this.e-other.e;
        if (this.s != other.s) return this.s-other.s;
        return this.id - other.id;
    }
}

class room implements Comparable<room> {
    
    public int free;
    public int id;
    
    public room(int t, int i) {
        free = t;
        id = i;
    }
    
	// Sort by when a room is free smaller to bigger, then id.
    public int compareTo(room other) {
        if (this.free != other.free) return this.free - other.free;
        return this.id - other.id;
    }
}