#include <bits/stdc++.h>
int IntMaxVal = (int)1e20;
long long LongMaxVal = (long long)1e20;
template <typename T>
struct argument_type;
template <typename T, typename U>
struct argument_type<T(U)> {};
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& s) {
  is >> s.first >> s.second;
  return is;
}
int n;
vector<int> score;
vector<int> price;
vector<vector<pair<int, int> > > cheapestWithScore;
vector<pair<int, int> > allCheapest;
long long getCheapestWithScore(int score, int place, int guaranteed) {
  int needToWin = n + 1 - place;
  int toWinAsWell = needToWin - guaranteed;
  int canWinOther = cheapestWithScore[score].size();
  if (score) canWinOther += cheapestWithScore[score - 1].size();
  if (toWinAsWell > canWinOther) return -1;
  vector<pair<int, int> > targetSameScore(cheapestWithScore[score]);
  if (score > 0)
    for (int i = 0; i < cheapestWithScore[score - 1].size(); ++i)
      targetSameScore.push_back(cheapestWithScore[score - 1][i]);
  sort(targetSameScore.begin(), targetSameScore.end());
  long long p = 0;
  vector<bool> won(n);
  int rem = score;
  for (int i = 0; i < toWinAsWell; ++i) {
    int target = targetSameScore[i].second;
    p += price[target];
    won[target] = true;
    rem--;
  }
  for (int i = 0; i < allCheapest.size(); ++i) {
    if (rem == 0) break;
    int target = allCheapest[i].second;
    if (won[target]) continue;
    rem--;
    p += price[target];
  }
  return p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  int k;
  cin >> k;
  ;
  int maxScore = 200000;
  cheapestWithScore.resize(maxScore + 1);
  for (int i = 0; i < n; ++i) {
    int s;
    cin >> s;
    ;
    int e;
    cin >> e;
    ;
    score.push_back(s);
    price.push_back(e);
    allCheapest.push_back(make_pair(e, i));
    cheapestWithScore[s].push_back(make_pair(e, i));
  }
  long long eff = LongMaxVal;
  sort(allCheapest.begin(), allCheapest.end());
  int guaranteedToWin = 0;
  int enough = n + 1 - k;
  for (int score = 0; score < n + 1; ++score) {
    if (score >= 2) guaranteedToWin += cheapestWithScore[score - 2].size();
    long long c = getCheapestWithScore(score, k, guaranteedToWin);
    if (c != -1) {
      eff = min(eff, c);
    };
    if (guaranteedToWin >= enough) break;
  }
  if (eff == LongMaxVal)
    cout << -1;
  else
    cout << eff;
}
