#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1000 * 1000 * 1000 * 1000 * 1000;
const int N = 200 * 1000 + 5;
pair<int, int> p[N], se[N];
int n, k;
short mrk[N];
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  return a.second < b.second;
}
long long find(int x) {
  if (x > n) return INF;
  fill(mrk, mrk + N, 0);
  vector<pair<int, int> > vl;
  long long tmp = 0;
  int l = -k + 1;
  for (int i = 0; i < n; ++i) {
    if (se[i].first == x || se[i].first + 1 == x)
      vl.push_back(make_pair(se[i].second, i));
    if (se[i].first + 1 >= x) ++l;
  }
  sort(vl.begin(), vl.end());
  for (int i = 0; i < l; ++i) tmp += vl[i].first, mrk[vl[i].second] = 1;
  x -= l;
  for (int i = 0; i < n && x; ++i)
    if (!mrk[i]) {
      --x;
      tmp += se[i].second;
    }
  return tmp;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) cin >> p[i].first >> p[i].second, se[i] = p[i];
  sort(p, p + n);
  sort(se, se + n, cmp);
  cerr << "se: " << endl;
  for (int i = 0; i < n; ++i)
    cerr << se[i].first << " " << se[i].second << endl;
  int s = p[n - k].first;
  long long ans = min(find(s), find(s + 1)), tmp = 0;
  if (s + 2 <= n)
    for (int i = 0; i < s + 2; ++i) tmp += se[i].second;
  else
    tmp = INF;
  cout << (min(ans, tmp) < INF ? min(ans, tmp) : -1) << endl;
  return 0;
}
