#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
pair<pair<int, int>, int> a[maxn], b[maxn];
int n, k;
long long ans = 1e18;
bool mark[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first.first >> a[i].first.second;
    a[i].second = i;
    b[i].first.first = a[i].first.second;
    b[i].first.second = a[i].first.first;
    b[i].second = a[i].second;
  }
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n);
  reverse(a + 1, a + 1 + n);
  int point = a[k].first.first;
  int p = k;
  long long cur = 0;
  int cnt = 0;
  if (point <= n) {
    while (p <= n && (a[p].first.first == a[k].first.first ||
                      a[p].first.first + 1 == a[k].first.first))
      p++;
    int num = p - k;
    int r = p - 1;
    if (num <= point) {
      vector<pair<int, int> > v;
      for (int i = r; i > 0; i--) {
        if (a[i].first.first > a[k].first.first) break;
        v.push_back(make_pair(a[i].first.second, a[i].second));
      }
      sort(v.begin(), v.end());
      for (int i = 0; i < num; i++) {
        cnt++;
        cur += v[i].first;
        mark[v[i].second] = true;
      }
      for (int i = 1; i <= n; i++) {
        if (cnt >= point) break;
        if (!mark[b[i].second]) {
          cur += b[i].first.first;
          cnt++;
          mark[b[i].second] = true;
        }
      }
      ans = min(ans, cur);
    }
  }
  if (point + 2 <= n) {
    cur = 0;
    for (int i = 1; i <= point + 2; i++) cur += b[i].first.first;
    ans = min(ans, cur);
  }
  if (point + 1 <= n) {
    memset(mark, 0, sizeof(mark));
    cur = 0;
    cnt = 0;
    p = k;
    while (p <= n && a[p].first.first == a[k].first.first) p++;
    int num = p - k;
    if (num <= point + 1) {
      vector<pair<int, int> > v;
      int r = p - 1;
      for (int i = r; i > 0; i--) {
        if (a[i].first.first > a[k].first.first + 1) break;
        v.push_back(make_pair(a[i].first.second, a[i].second));
      }
      sort(v.begin(), v.end());
      for (int i = 0; i < num; i++) {
        cnt++;
        cur += v[i].first;
        mark[v[i].second] = true;
      }
      for (int i = 1; i <= n; i++) {
        if (cnt >= point + 1) break;
        if (!mark[b[i].second]) {
          cur += b[i].first.first;
          cnt++;
          mark[b[i].second] = true;
        }
      }
      ans = min(ans, cur);
    }
  }
  if (ans >= 1e14)
    cout << "-1";
  else
    cout << ans;
  cout << endl;
  return 0;
}
