#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n, m;
pair<int, int> a[300000];
vector<int> v[300000];
int s[300000];
int b[300000];
int bi;
int main() {
  int i, j, k;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++)
    scanf("%d%d", &a[i].first, &a[i].second),
        v[a[i].first].push_back(a[i].second);
  sort(a, a + n, greater<pair<int, int> >());
  int wtg = a[m - 1].first;
  for (i = 200000; i >= 0; i--) {
    s[i] = v[i].size() + s[i + 1];
  }
  long long minn = LLONG_MAX;
  for (i = 0; i < 3; wtg++, i++) {
    int x = v[wtg].size() + v[wtg - 1].size();
    int y = s[wtg + 1];
    int z = m - y;
    int w = max(x - z + 1, 0);
    if (w > wtg) continue;
    bi = 0;
    for (j = 0; j < v[wtg].size(); j++) b[bi++] = v[wtg][j];
    for (j = 0; j < v[wtg - 1].size(); j++) b[bi++] = v[wtg - 1][j];
    sort(b, b + bi);
    long long hab = 0;
    for (j = 0; j < w; j++) {
      hab += (long long)b[j];
      b[j] = INT_MAX;
    }
    for (j = 0; j < n; j++) {
      if (a[j].first != wtg && a[j].first != wtg - 1) b[bi++] = a[j].second;
    }
    sort(b, b + bi);
    if (bi < wtg) continue;
    for (j = 0; j < wtg - w; j++) hab += (long long)b[j];
    minn = min(minn, hab);
  }
  if (minn == LLONG_MAX)
    printf("-1");
  else
    printf("%I64d", minn);
  return 0;
}
