#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * asin(1.0);
const int INF = 1000000000;
const double EPS = 1e-10;
inline int cmp(double x, double y = 0, double tol = EPS) {
  return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}
const int MAXN = 200005;
int n, k;
pair<int, int> fights[MAXN];
long long vec1[MAXN], vec2[MAXN];
long long calc(int w) {
  if (w < 0) return -1;
  int t1 = 0, t2 = 0;
  int rank = 1;
  for (int i = 0; i < n; ++i) {
    if (fights[i].first == w - 1 || fights[i].first == w) {
      vec1[t1++] = fights[i].second;
    } else {
      vec2[t2++] = fights[i].second;
      if (fights[i].first > w) rank++;
    }
  }
  if (rank > k) return -1;
  sort(vec1, vec1 + t1);
  rank += t1;
  int i;
  long long effort = 0;
  for (i = 0; i < t1; i++) {
    if (rank <= k) break;
    rank--;
    effort += vec1[i];
    w--;
  }
  for (; i < t1; i++) vec2[t2++] = vec1[i];
  sort(vec2, vec2 + t2);
  for (int i = 0; i < t2; ++i) {
    if (w <= 0) break;
    effort += vec2[i];
    w--;
  }
  if (w != 0) return -1;
  return effort;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  while (cin >> n >> k) {
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      fights[i] = pair<int, int>(a, b);
    }
    sort(fights, fights + n);
    reverse(fights, fights + n);
    int j = 0;
    int left = 0, right = 0;
    for (int i = MAXN; i >= 0; i--) {
      while (j < n && fights[j].first > i) j++;
      if (j >= k) {
        left = i;
        break;
      }
    }
    j = 0;
    for (int i = MAXN; i >= 0; i--) {
      while (j < n && fights[j].first >= i - 1) j++;
      if (j >= k) {
        right = i;
        break;
      }
    }
    long long res = -1;
    for (int i = left - 10; i < right + 10; ++i) {
      long long aux = calc(i);
      if (aux == -1) continue;
      if (aux < res || res == -1) res = aux;
    }
    cout << res << endl;
  }
}
