#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double tau = 2 * pi;
const double epsilon = 1e-6;
const long long INF = (1LL) << 60;
const long long MAX_N = 200100;
long long N, K;
pair<long long, long long> opps[MAX_N];
struct segment {
  vector<long long> elements;
  vector<long long> sums;
  segment() {}
  long long nSmaller(long long q) {
    if (q >= elements[((int)elements.size()) - 1])
      return ((int)elements.size());
    return (upper_bound(elements.begin(), elements.end(), q) -
            elements.begin());
  }
};
segment segtree[3 * MAX_N];
void merge_segs(long long p, long long c1, long long c2) {
  long long idx1 = 0, idx2 = 0;
  while (idx1 < ((int)segtree[c1].elements.size()) ||
         idx2 < ((int)segtree[c2].elements.size())) {
    if (idx1 == ((int)segtree[c1].elements.size()))
      segtree[p].elements.push_back(segtree[c2].elements[idx2++]);
    else if (idx2 == ((int)segtree[c2].elements.size()))
      segtree[p].elements.push_back(segtree[c1].elements[idx1++]);
    else if (segtree[c1].elements[idx1] < segtree[c2].elements[idx2])
      segtree[p].elements.push_back(segtree[c1].elements[idx1++]);
    else
      segtree[p].elements.push_back(segtree[c2].elements[idx2++]);
  }
  segtree[p].sums.push_back(segtree[p].elements[0]);
  for (long long i = 1; i < ((int)segtree[p].elements.size()); i++)
    segtree[p].sums.push_back(segtree[p].sums[i - 1] + segtree[p].elements[i]);
}
void build(long long idx, long long b, long long e) {
  if (b > e) return;
  if (b == e) {
    segtree[idx].elements.push_back(opps[b].second);
    segtree[idx].sums.push_back(opps[b].second);
  } else {
    build(2 * idx, b, (b + e) / 2);
    build(2 * idx + 1, (b + e) / 2 + 1, e);
    merge_segs(idx, 2 * idx, 2 * idx + 1);
  }
}
long long q1(long long idx, long long b, long long e, long long l, long long r,
             long long q) {
  if (b > e || l >= N || l > r) return 0;
  if (b > r || e < l)
    return 0;
  else if (l <= b && e <= r)
    return segtree[idx].nSmaller(q);
  else
    return (q1(2 * idx, b, (b + e) / 2, l, r, q) +
            q1(2 * idx + 1, (b + e) / 2 + 1, e, l, r, q));
}
long long q2(long long idx, long long b, long long e, long long l, long long r,
             long long q) {
  if (b > e || l >= N || l > r) return 0;
  if (b > r || e < l)
    return 0;
  else if (l <= b && e <= r) {
    long long nS = segtree[idx].nSmaller(q);
    if (nS == 0)
      return 0;
    else
      return segtree[idx].sums[nS - 1];
  } else
    return (q2(2 * idx, b, (b + e) / 2, l, r, q) +
            q2(2 * idx + 1, (b + e) / 2 + 1, e, l, r, q));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> N >> K;
  for (long long i = 0; i < N; i++) cin >> opps[i].first >> opps[i].second;
  sort(opps, opps + N);
  build(1, 0, N - 1);
  long long mnEffort = INF;
  for (long long x = 0, l = 0, r = -1; x <= N; x++) {
    while (l < N && opps[l].first < x - 1) ++l;
    while (r < N - 1 && opps[r + 1].first <= x) ++r;
    long long h = N - 1 - r;
    if (h >= K) continue;
    long long nMid;
    if (l == N)
      nMid = 0;
    else if (l > r)
      nMid = 0;
    else
      nMid = r - l + 1;
    long long nMidWins = nMid - (K - h - 1);
    if (nMidWins < 0) nMidWins = 0;
    long long low = -1, high = MAX_N;
    while (low < high - 1) {
      long long mid = (low + high) / 2;
      long long nSmaller = q1(1, 0, N - 1, l, r, mid);
      if (nSmaller >= nMidWins)
        high = mid;
      else
        low = mid;
    }
    long long midQ = high;
    low = -1, high = MAX_N;
    while (low < high - 1) {
      long long mid = (low + high) / 2;
      long long nSmaller = q1(1, 0, N - 1, 0, N - 1, mid);
      if (mid < midQ)
        nSmaller -= q1(1, 0, N - 1, l, r, mid);
      else
        nSmaller -= nMidWins;
      if (nSmaller >= x - nMidWins)
        high = mid;
      else
        low = mid;
    }
    long long sideQ = high;
    long long nSmaller = q1(1, 0, N - 1, 0, N - 1, sideQ);
    if (sideQ < midQ)
      nSmaller -= q1(1, 0, N - 1, l, r, sideQ);
    else
      nSmaller -= nMidWins;
    long long midEffort = q2(1, 0, N - 1, l, r, midQ);
    midEffort -= midQ * (q1(1, 0, N - 1, l, r, midQ) - nMidWins);
    long long sideEffort = q2(1, 0, N - 1, 0, N - 1, sideQ);
    if (sideQ < midQ)
      sideEffort -= q2(1, 0, N - 1, l, r, sideQ);
    else
      sideEffort -= midEffort;
    sideEffort -= sideQ * (nSmaller - (x - nMidWins));
    mnEffort = min(midEffort + sideEffort, mnEffort);
  }
  if (mnEffort == INF) mnEffort = -1;
  cout << mnEffort << '\n';
  return 0;
}
