#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 13;
const long long INF = 1e17;
const long double eps = 1e-9;
int n, k;
int p[N], e[N];
int asz = 0;
long long a[N];
int bsz = 0;
long long b[N];
long long ans;
bool ok(int cur) {
  asz = bsz = 0;
  int place = 0;
  for (int i = 0; i < n; ++i) {
    if (cur == p[i] || cur - 1 == p[i]) {
      a[asz++] = e[i];
    } else {
      if (cur < p[i]) place++;
      b[bsz++] = e[i];
    }
  }
  sort(a, a + asz);
  sort(b, b + bsz);
  for (int i = 0; i < asz; ++i)
    if (i) a[i] += a[i - 1];
  for (int i = 0; i < bsz; ++i)
    if (i) b[i] += b[i - 1];
  ans = INF;
  for (int i = 0; i <= min(asz, cur); i++) {
    int np = place + asz - i;
    if (np <= k) {
      int j = cur - i;
      if (j <= bsz) {
        ans = min(ans, (i ? a[i - 1] : 0) + (j ? b[j - 1] : 0));
      }
    }
  }
  return ans < INF;
}
bool solve() {
  if (scanf("%d%d", &n, &k) != 2) return false;
  k--;
  for (int i = 0; i < n; ++i) scanf("%d%d", &p[i], &e[i]);
  int lf = 0, rg = n;
  while (lf != rg) {
    int mid = (lf + rg) >> 1;
    if (ok(mid))
      rg = mid;
    else
      lf = mid + 1;
  }
  long long ans = INF;
  for (; lf <= min(rg + 100, n); lf++) {
    ok(lf);
    ans = min(ans, ::ans);
  }
  if (ans == INF) ans = -1;
  cout << ans;
  return true;
}
int main() {
  while (solve()) puts("");
}
