#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  if (k == n + 1) {
    cout << 0;
    return 0;
  }
  vector<pair<int, int> > byRank(n), byCost(n);
  for (int i = (0); i <= (n - 1); ++i) {
    int p, e;
    cin >> p >> e;
    byRank[i] = make_pair(p, i);
    byCost[i] = make_pair(e, i);
  }
  sort(byRank.begin(), byRank.end());
  sort(byCost.begin(), byCost.end());
  vector<int> Rank(n);
  for (int i = (0); i <= ((int)(byRank).size() - 1); ++i)
    Rank[byRank[i].second] = i;
  int pts = byRank[n - k].first;
  long long int cost = 1000000000000000001;
  if (pts + 2 <= n) {
    long long int sum = 0;
    for (int i = (0); i <= (pts + 1); ++i) sum += byCost[i].first;
    cost = (((cost) < (sum)) ? (cost) : (sum));
  }
  if (pts + 1 <= n) {
    int cnt = 0;
    for (int i = (0); i <= (n - k); ++i)
      if (byRank[i].first == pts || byRank[i].first == pts + 1) cnt++;
    vector<bool> taken(n, 0);
    long long int sum = 0;
    int pts_already = 0;
    for (int i = (0); i <= ((int)(byCost).size() - 1); ++i)
      if (cnt) {
        int tmp = byRank[Rank[byCost[i].second]].first;
        if (tmp == pts || tmp == pts + 1) {
          sum += byCost[i].first;
          taken[byCost[i].second] = 1;
          cnt--;
          pts_already++;
        }
      }
    for (int i = (0); i <= ((int)(byCost).size() - 1); ++i)
      if (pts_already < pts + 1 && !taken[byCost[i].second]) {
        sum += byCost[i].first;
        pts_already++;
      }
    if (pts_already >= pts + 1) cost = (((cost) < (sum)) ? (cost) : (sum));
  }
  if (pts <= n) {
    int cnt = 0;
    for (int i = (0); i <= (n - k); ++i)
      if (byRank[i].first == pts || byRank[i].first == pts - 1) cnt++;
    vector<bool> taken(n, 0);
    long long int sum = 0;
    int pts_already = 0;
    for (int i = (0); i <= ((int)(byCost).size() - 1); ++i)
      if (cnt) {
        int tmp = byRank[Rank[byCost[i].second]].first;
        if (tmp == pts || tmp == pts - 1) {
          sum += byCost[i].first;
          taken[byCost[i].second] = 1;
          cnt--;
          pts_already++;
        }
      }
    for (int i = (0); i <= ((int)(byCost).size() - 1); ++i)
      if (pts_already < pts && !taken[byCost[i].second]) {
        sum += byCost[i].first;
        pts_already++;
      }
    if (pts_already >= pts) cost = (((cost) < (sum)) ? (cost) : (sum));
  }
  if (cost < 1000000000000000001)
    cout << cost;
  else
    cout << -1;
  return 0;
}
