#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  bool flag = false;
  x = 0;
  char ch;
  ch = getchar();
  while (ch == ' ' || ch == '\n') ch = getchar();
  if (ch == '-') flag = true, ch = getchar();
  while (ch != ' ' && ch != '\n') {
    x = x * 10 + ch - '0';
    if (scanf("%c", &ch) == EOF) break;
  }
  if (flag) x = -x;
}
struct node {
  int grade, v;
} zy[200001];
bool cmp1(node a, node b) { return a.grade > b.grade; }
bool cmp2(node a, node b) { return a.v < b.v; }
int n, k;
bool use[200001];
long long check(int grade) {
  memset(use, 0, sizeof(use));
  int numtobeat = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (zy[i].grade >= grade - 1) {
      numtobeat++;
    }
  }
  numtobeat -= k - 1;
  int count = 0;
  for (int i = 1; i <= n && count < numtobeat; i++) {
    if (zy[i].grade == grade || zy[i].grade == grade - 1) {
      ans += zy[i].v;
      use[i] = true;
      count++;
    }
  }
  for (int i = 1; i <= n && count < grade; i++) {
    if (!use[i]) {
      ans += zy[i].v;
      count++;
    }
  }
  return ans;
}
int main() {
  read(n);
  read(k);
  for (int i = 1; i <= n; i++) {
    read(zy[i].grade);
    read(zy[i].v);
  }
  sort(zy + 1, zy + 1 + n, cmp1);
  int grade = zy[k].grade;
  long long ans = -1, fuckwmj;
  sort(zy + 1, zy + 1 + n, cmp2);
  for (int i = grade; i <= grade + 2; i++) {
    if (i > n) break;
    fuckwmj = check(i);
    if (fuckwmj < ans || ans == -1) ans = fuckwmj;
  }
  cout << ans;
  return 0;
}
