#include <bits/stdc++.h>
using namespace std;
const long long maxN = 200000 + 10;
const long long INF = maxN * 200000;
long long p[maxN], e[maxN];
long long n, k, ans = INF, target, rank, tn;
map<pair<int, int>, int> mp, mp2;
pair<int, int> a[maxN], b[maxN];
long long solve1() {
  cerr << "here!" << endl;
  for (int i = 0; i < n; i++) a[i] = make_pair(e[i], -p[i]);
  for (int i = 0; i < n; i++) b[i] = make_pair(p[i], e[i]);
  sort(a, a + n);
  sort(b, b + n);
  for (int i = 0; i < n; i++) a[i].second *= -1;
  long long kans = 0;
  tn = b[target].first;
  cerr << tn << "endl; " << endl;
  for (int i = 0; i < n; i++) {
    p[i] = b[i].first;
    e[i] = b[i].second;
  }
  if (tn + 2 > n) return INF;
  for (int i = 0; i < tn + 2; i++) kans += a[i].first;
  return kans;
}
long long solve2() {
  mp.clear();
  mp2.clear();
  int sz = 0, szz = 0;
  long long kans = 0;
  if (tn > n - 1) return INF;
  for (int i = 0; i < n; i++) {
    mp[make_pair(e[i], p[i])]++;
    b[i] = make_pair(e[i], -p[i]);
    if (p[i] == tn || p[i] == tn + 1) {
      a[sz++] = make_pair(e[i], -p[i]);
      szz += p[i] == tn;
    }
  }
  sort(a, a + sz);
  sort(b, b + n);
  for (int i = 0; i < sz; i++) a[i].second *= -1;
  for (int i = 0; i < n; i++) b[i].second *= -1;
  for (int i = 0; i < min(sz, szz); i++) {
    mp2[a[i]]++;
    kans += a[i].first;
  }
  int need = tn - szz + 1;
  for (int i = 0; i < n && need; i++)
    if (mp[b[i]] != mp2[b[i]]) {
      need--;
      kans += b[i].first;
      mp2[b[i]]++;
    }
  return kans;
}
long long solve3() {
  long long sz = 0, szz = 0, szzz = 0;
  long long kans = 0;
  mp.clear();
  mp2.clear();
  for (int i = 0; i < n; i++) {
    mp[make_pair(e[i], p[i])]++;
    b[i] = make_pair(e[i], -p[i]);
    if (p[i] == tn || p[i] == tn - 1) a[sz++] = make_pair(e[i], -p[i]);
    if (i <= target) szz = max(szz, sz);
  }
  sort(a, a + sz);
  sort(b, b + n);
  for (int i = 0; i < sz; i++) a[i].second *= -1;
  for (int i = 0; i < n; i++) b[i].second *= -1;
  for (int i = 0; i < min(szz, sz); i++) {
    mp2[a[i]]++;
    kans += a[i].first;
  }
  cerr << sz << " " << szz << endl;
  int need = tn - szz;
  for (int i = 0; need && i < n; i++)
    if (mp[b[i]] != mp2[b[i]]) {
      mp2[b[i]]++;
      need--;
      kans += b[i].first;
    }
  return kans;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> p[i] >> e[i];
  target = n - k;
  if (target < 0) {
    cout << -1 * (target != -1) << endl;
    return 0;
  }
  cerr << "k" << endl;
  ans = solve1();
  cerr << solve1() << ", " << solve2() << ", " << solve3() << endl;
  if (tn > n) {
    cout << -1 << endl;
    return 0;
  }
  cerr << "here!" << endl;
  ans = min(ans, solve2());
  ans = min(ans, solve3());
  if (ans == INF) ans = -1;
  cout << ans << endl;
  cin >> n;
}
