#include <bits/stdc++.h>
long long p[201000], e[201000];
long long tempa[201000], tempb[201000];
long long tempans;
long long ans;
long long as[201000], bs[201000];
int z[201000];
long long inf;
void mergesort(long long *a, long long *b, int n1, int n2);
long long go(int m, int k) {
  int i, j;
  long long temp;
  if (m < k) {
    temp = inf;
  } else {
    mergesort(as, bs, 1, m);
    temp = 0;
    for (i = m, j = 1; j <= k; i--, j++) {
      temp += as[i];
      z[bs[i]] = 0;
    }
  }
  return temp;
}
int main(void) {
  int n, k, i, tk, kk2;
  int m;
  long long pp;
  while (scanf("%d %d", &n, &k) != EOF) {
    for (i = 1; i <= n; i++) {
      scanf("%I64d %I64d", &p[i], &e[i]);
      p[i] += 2;
    }
    if (k > n) {
      printf("0\n");
      continue;
    }
    mergesort(p, e, 1, n);
    pp = p[k];
    inf = 2000000000;
    inf *= 1000000000;
    ans = inf;
    p[0] = inf;
    tempans = 0;
    m = 0;
    for (i = 1; i <= n; i++) {
      as[++m] = e[i];
    }
    tempans += go(m, pp);
    if (tempans < ans) {
      ans = tempans;
    }
    tempans = 0;
    m = 0;
    tk = 0;
    for (i = 1; i <= n; i++) {
      z[i] = 1;
      if (p[i] == pp || p[i] == pp + 1) {
        as[++m] = e[i];
        bs[m] = i;
      }
      if (p[i] >= pp) {
        tk++;
      }
    }
    kk2 = tk - k + 1;
    tempans += go(m, kk2);
    kk2 = (pp - 1) - kk2;
    m = 0;
    for (i = 1; i <= n; i++) {
      if (z[i]) {
        as[++m] = e[i];
      }
    }
    tempans += go(m, kk2);
    if (tempans < ans) {
      ans = tempans;
    }
    tempans = 0;
    m = 0;
    tk = 0;
    for (i = 1; i <= n; i++) {
      z[i] = 1;
      if (p[i] == pp || p[i] == pp - 1) {
        as[++m] = e[i];
        bs[m] = i;
      }
      if (p[i] >= pp - 1) {
        tk++;
      }
    }
    kk2 = tk - k + 1;
    tempans += go(m, kk2);
    kk2 = (pp - 2) - kk2;
    m = 0;
    for (i = 1; i <= n; i++) {
      if (z[i]) {
        as[++m] = e[i];
      }
    }
    tempans += go(m, kk2);
    if (tempans < ans) {
      ans = tempans;
    }
    if (ans >= inf) {
      ans = -1;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
void mergesort(long long *a, long long *b, int n1, int n2) {
  int a1, a2, n12;
  int i, j;
  if (n1 < n2) {
    n12 = (n1 + n2) / 2;
    mergesort(a, b, n1, n12);
    mergesort(a, b, n12 + 1, n2);
    for (i = n1; i <= n2; i++) {
      tempa[i] = a[i];
      tempb[i] = b[i];
    }
    a1 = n1;
    a2 = n12 + 1;
    for (i = n1; (a1 <= n12 && a2 <= n2); i++) {
      if (tempa[a1] >= tempa[a2]) {
        a[i] = tempa[a1];
        b[i] = tempb[a1];
        a1++;
      } else {
        a[i] = tempa[a2];
        b[i] = tempb[a2];
        a2++;
      }
    }
    if (a1 > n12) {
      for (j = a2; j <= n2; j++, i++) {
        a[i] = tempa[j];
        b[i] = tempb[j];
      }
    } else {
      for (j = a1; j <= n12; j++, i++) {
        a[i] = tempa[j];
        b[i] = tempb[j];
      }
    }
  }
}
