#include <bits/stdc++.h>
using namespace std;
struct node {
  long long x;
  long long y;
  bool operator<(node a) const { return x > a.x; }
} date[200010];
long long a[200010], ai;
long long b[200010], bi;
long long min(long long a, long long b) {
  if (a == -1)
    return b;
  else if (b == -1)
    return a;
  return a < b ? a : b;
}
int main() {
  long long n, k, i, t, j, p, ans, mk, tp;
  cin >> n >> k;
  for (i = 1; i <= n; i++) cin >> date[i].x >> date[i].y;
  sort(date + 1, date + n + 1);
  t = date[k].x;
  if (t > n) {
    cout << -1 << endl;
    return 0;
  }
  ans = -1;
  for (i = 1; i <= n; i++)
    if (i < k && date[i].x == t) p++;
  for (i = t; i <= t + 2; i++) {
    ai = 0;
    bi = 0;
    p = 0;
    memset(b, 0x3f3f3f3f, sizeof(b));
    memset(a, 0x3f3f3f3f, sizeof(a));
    for (j = 1; j <= n; j++) {
      if (date[j].x >= i - 1 && date[j].x <= i) {
        if (j >= k) p++;
        a[ai++] = date[j].y;
      } else
        b[bi++] = date[j].y;
    }
    sort(a, a + ai);
    mk = 0;
    tp = p;
    for (j = 0; j <= ai - 1; j++) {
      if (tp > 0) {
        mk += a[j];
        --tp;
      } else
        b[bi++] = a[j];
    }
    sort(b, b + bi);
    for (j = 0; j <= i - p - 1; j++) mk += b[j];
    ans = min(ans, mk);
  }
  cout << ans << endl;
  return 0;
}
