#include <bits/stdc++.h>
using namespace std;
pair<int, int> in[200005];
pair<int, int> E[200005];
pair<int, int> P[200005];
int N, K;
long long ans;
bool beat[200005];
void solve(int manao_point) {
  memset(beat, 0, sizeof beat);
  if (manao_point > N) return;
  int losses = 0;
  vector<int> tobeat;
  for (int i = 0; i < N; i++) {
    int ind = E[i].second;
    if (in[ind].first == manao_point || in[ind].first + 1 == manao_point) {
      tobeat.push_back(ind);
    } else {
      if (in[ind].first > manao_point) losses++;
    }
  }
  int canLoseMore = K - losses - 1;
  if (canLoseMore < 0) return;
  long long useE = 0;
  int curScore = 0;
  for (int i = 0; i < (int)tobeat.size() - canLoseMore; i++) {
    useE += in[tobeat[i]].second;
    curScore++;
    beat[tobeat[i]] = true;
  }
  int j = 0;
  for (int i = 0; i < manao_point - curScore; i++) {
    while (beat[E[j].second]) j++;
    useE += E[j].first;
    j++;
  }
  if (ans == -1 || (ans > useE)) ans = useE;
}
int main() {
  scanf("%d %d", &N, &K);
  if (K >= N + 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &in[i].first, &in[i].second);
    P[i] = pair<int, int>(in[i].first, i);
    E[i] = pair<int, int>(in[i].second, i);
  }
  sort(P, P + N);
  sort(E, E + N);
  int manao = P[N - K].first;
  ans = -1;
  for (int i = 0; i < 3; i++) solve(manao + i);
  cout << ans << endl;
  return 0;
}
