#include <bits/stdc++.h>
using namespace std;
const long long inf = 2e18 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = 2 * acos(0.0);
const double E = 2.71828;
int n, m;
vector<int> g[200005];
pair<int, int> a[200005];
long long b[200005], sz;
int Left[200005], Right[200005];
vector<int> now, mid;
int main(void) {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", &a[i].first, &a[i].second);
    g[a[i].first].push_back(a[i].second);
    b[i] = a[i].second;
  }
  for (int(i) = 0; (i) < (200005); ++(i)) {
    sort((g[i]).begin(), (g[i]).end());
  }
  Left[0] = g[0].size();
  Right[200005] = 0;
  for (int i = 1; i < 200005; ++i) {
    Left[i] = Left[i - 1] + g[i].size();
  }
  for (int i = 200004; i >= 0; --i) {
    Right[i] = Right[i + 1] + g[i].size();
  }
  sort(b + 1, b + n + 1);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    b[i] += b[i - 1];
  }
  long long res = inf;
  for (int i = 0; i <= n; ++i) {
    int higher = Right[i + 1];
    int lower = (i > 1 ? Left[i - 2] : 0);
    int prob = n - higher - lower;
    if (higher > m) {
      continue;
    }
    if (higher + prob < m) {
      res = min(res, b[i]);
    } else {
      int winner = prob - (m - higher - 1);
      long long score = 0;
      now.clear();
      mid.clear();
      for (int(i) = 0; (i) < (lower); ++(i)) {
        now.push_back(a[i + 1].second);
      }
      for (int i = lower + 1; i <= lower + prob; ++i) {
        mid.push_back(a[i].second);
      }
      for (int i = lower + prob + 1; i <= n; ++i) {
        now.push_back(a[i].second);
      }
      if (winner > mid.size() || winner < 0) {
        continue;
      }
      sort((mid).begin(), (mid).end());
      reverse((mid).begin(), (mid).end());
      for (int(i) = 0; (i) < (winner); ++(i)) {
        score += mid.back();
        mid.pop_back();
      }
      for (int(i) = 0; (i) < (mid.size()); ++(i)) {
        now.push_back(mid[i]);
      }
      sort((now).begin(), (now).end());
      for (int(j) = 0; (j) < (i - winner); ++(j)) {
        score += now[j];
      }
      res = min(res, score);
    }
  }
  if (res == inf) {
    res = -1;
  }
  cout << res << "\n";
  return 0;
}
