#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;
int n, m, k, x, y, z;
vector<int> v[N];
long long get(int x, int y) {
  int sum = 0;
  vector<int> temp, get;
  for (int i = 0; i <= x - 1; i++) sum += v[i].size();
  for (int i = 0; i <= N - 1; i++) {
    if (i == x || i == y) {
      for (__typeof(v[i].begin()) it = v[i].begin(); it != v[i].end(); it++)
        get.push_back(*it);
    } else {
      for (__typeof(v[i].begin()) it = v[i].begin(); it != v[i].end(); it++)
        temp.push_back(*it);
    }
  }
  sort(get.begin(), get.end());
  int need = max(k - sum, 0);
  long long cur = 0;
  if (need > get.size()) return linf;
  for (int i = 0; i < get.size(); i++) {
    if (i < need) {
      cur += get[i];
    } else
      temp.push_back(get[i]);
  }
  sort(temp.begin(), temp.end());
  need = y - need;
  if (temp.size() < need) return linf;
  for (int i = 0; i < need; i++) cur += temp[i];
  return cur;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
  }
  int sum = 0, T = 0;
  k = n - k + 1;
  for (int i = 0; i <= N - 1; i++) {
    sum += v[i].size();
    if (sum >= k) {
      T = i;
      break;
    }
  }
  long long ans = min(get(T - 1, T), min(get(T, T + 1), get(T + 1, T + 2)));
  if (ans > 1e12) ans = -1;
  printf("%lld\n", ans);
  return 0;
}
