#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  register long long x = 0, f = 1;
  register char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  return x * f;
}
void write(long long x) {
  if (x < 0) x = -x, putchar('-');
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(long long x) {
  write(x);
  puts("");
}
const int maxn = 200005;
const long long inf = 1ll << 60;
pair<int, int> a[maxn];
int n, k;
vector<int> w;
long long calc(int v) {
  int l = lower_bound(a + 1, a + 1 + n, pair<int, int>(v - 1, 0)) - a;
  int r = lower_bound(a + 1, a + 1 + n, pair<int, int>(v + 1, 0)) - a - 1;
  int must = k - l + 1;
  if (must > r - l + 1 || v > n || v < must) return inf;
  long long ans = 0;
  w.clear();
  for (register int i = (l); i <= int(r); ++i) w.push_back(a[i].second);
  sort(w.begin(), w.end());
  reverse(w.begin(), w.end());
  for (register int i = (1); i <= int(must); ++i) {
    ans += w.back();
    w.pop_back();
    --v;
  }
  for (register int i = (1); i <= int(l - 1); ++i) w.push_back(a[i].second);
  for (register int i = (r + 1); i <= int(n); ++i) w.push_back(a[i].second);
  sort(w.begin(), w.end());
  reverse(w.begin(), w.end());
  for (register int i = (1); i <= int(v); ++i) {
    ans += w.back();
    w.pop_back();
  }
  return ans;
}
int main() {
  n = read(), k = read(), k = n - k + 1;
  for (register int i = (1); i <= int(n); ++i) {
    a[i].first = read();
    a[i].second = read();
  }
  sort(a + 1, a + 1 + n);
  if (!k) {
    puts("0");
    return 0;
  }
  int s = a[k].first;
  long long ans = inf;
  for (register int i = (s); i <= int(s + 2); ++i) ans = min(ans, calc(i));
  writeln(ans == inf ? -1 : ans);
  return 0;
}
