#include <bits/stdc++.h>
#define ll long long
#define ii pair<int, int>
#define st first
#define nd second
#define endl "\n"
#define all(v) v.begin(), v.end()

using namespace std;

const int MAXN = 1e6 + 5;

int n, k;
vector<int> s[MAXN], t[MAXN];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    multiset<ii> s1;
    priority_queue<ii> s2;

    vector<ii> g;
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        cin >> x >> y;
        s[y].push_back(x);
        t[x].push_back(y);
    }


    for (int x = 1e6; x >= 1; x--){
        for (auto y : t[x]){
            if (s1.size() < k) s1.insert({x, y});
            else s2.push({x, y});
        }

    }


    ll ans = 0;

    for (int y = 1; y <= 1e6; y++){
        while (s1.size() < k && s2.size()){
            if (s2.top().nd >= y) s1.insert(s2.top());
            s2.pop();
        }
        while (s2.size() && s2.top().nd < y) s2.pop();
        if (s1.size() < k) break;
        if (s2.size())
            ans += s1.begin()->st - s2.top().st;
        else ans += s1.begin()->st;
        for (auto x : s[y]){
            if (s1.size() && s1.find({x, y}) != s1.end()) s1.erase({x, y});
        }
    }
    cout << ans << endl;
    
}
