#include <bits/stdc++.h>
using namespace std;
bool debug = 1;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long ln, lk, lm;
pair<int, int> a[200105], b[200105];
int x;
long long ans = (1LL << 60);
void work(int p) {
  if (n < p) return;
  vector<int> v, v1;
  long long tmp = 0;
  int i = 0;
  while (a[i].first < p - 1) {
    v.push_back(a[i].second);
    i++;
  }
  int rm = (n - k + 1) - v.size();
  assert(rm > 0);
  while (i < n && a[i].first <= p) {
    v1.push_back(a[i].second);
    i++;
  }
  sort(v1.begin(), v1.end());
  assert(v1.size() >= rm);
  for (int(j) = 0; (j) < (int)(rm); (j)++) {
    tmp += v1[j];
  }
  for (int j = rm; j < v1.size(); j++) v.push_back(v1[j]);
  for (; i < n; i++) v.push_back(a[i].second);
  sort(v.begin(), v.end());
  for (int(j) = 0; (j) < (int)(p - rm); (j)++) {
    tmp += v[j];
  }
  ans = min(ans, tmp);
}
void cal() {
  sort(b, b + n);
  long long tmp = 0;
  if (x + 2 <= n) {
    for (int(i) = 0; (i) < (int)(x + 2); (i)++) tmp += b[i].first;
    ans = min(ans, tmp);
    tmp = 0;
  }
  work(x + 1);
  work(x);
  printf("%lld\n", ans);
}
int main() {
  scanf("%d%d", &n, &k);
  if (n < k) {
    puts("0");
    return 0;
  }
  for (int(i) = 0; (i) < (int)(n); (i)++) {
    scanf("%d%d", &a[i].first, &a[i].second);
    b[i].first = a[i].second;
    b[i].second = a[i].first;
  }
  sort(a, a + n);
  x = a[n - k].first;
  if (n < x)
    puts("-1");
  else
    cal();
  return 0;
}
