#include <bits/stdc++.h>
using namespace std;
const int maxn = 2340;
int T, n, k;
int cost[maxn];
int w[maxn], line[maxn], use[maxn], line2[maxn], que[maxn];
bool cmp(int a, int b) { return w[a] > w[b]; }
bool cmp2(int a, int b) { return cost[a] < cost[b]; }
long long type1() {
  long long ans1 = 0ll;
  int cnt = 0;
  memset(use, 0, sizeof(use));
  int e = w[line[k]], t = 0, fail = 0;
  for (int i = 1; i < k; i++)
    if (w[line[i]] == e) fail++;
  for (int i = 1; i <= n; i++)
    if (w[line[i]] == e || w[line[i]] == e - 1) que[++cnt] = line[i];
  sort(que + 1, que + cnt + 1, cmp2);
  for (int i = 1; i <= cnt - fail; i++) {
    ans1 += cost[que[i]];
    use[que[i]] = 1;
  }
  int need = max(e - (cnt - fail), 0);
  for (int i = 1; i <= n; i++)
    if (!use[line2[i]]) {
      if (need == 0) break;
      ans1 += cost[line2[i]];
      need--;
    }
  if (need) return 0x7ffffffffffffffll;
  return ans1;
}
long long type2() {
  long long ans1 = 0;
  int cnt = 0;
  memset(use, 0, sizeof(use));
  int e = w[line[k]] + 1, t = 0, fail = 0;
  for (int i = 1; i < k; i++)
    if (w[line[i]] == e) fail++;
  for (int i = 1; i <= n; i++)
    if (w[line[i]] == e || w[line[i]] == e - 1) que[++cnt] = line[i];
  sort(que + 1, que + cnt + 1, cmp2);
  for (int i = 1; i <= cnt - fail; i++) {
    ans1 += cost[que[i]];
    use[que[i]] = 1;
  }
  int need = max(e - (cnt - fail), 0);
  for (int i = 1; i <= n; i++)
    if (!use[line2[i]]) {
      if (need == 0) break;
      ans1 += cost[line2[i]];
      need--;
    }
  return ans1;
}
long long type3() {
  long long ans1 = 0;
  int need = w[line[k]] + 2;
  for (int i = 1; i <= n; i++) {
    if (need == 0) break;
    ans1 += cost[line2[i]];
    need--;
  }
  return ans1;
}
long long mincost() {
  long long ans = 0x7ffffffffffffffll;
  ans = min(ans, type1());
  ans = min(ans, type2());
  ans = min(ans, type3());
  return ans;
}
int main() {
  scanf("%d%d", &n, &k);
  if (k > n) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &w[i], &cost[i]);
    line[i] = i;
    line2[i] = i;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (w[i] > n) cnt++;
  if (cnt >= k) {
    printf("-1\n");
    return 0;
  }
  sort(line + 1, line + n + 1, cmp);
  sort(line2 + 1, line2 + n + 1, cmp2);
  long long ans = mincost();
  cout << ans << endl;
  return 0;
}
