#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "URDL";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
int tr[535][12], cnt = 1, K;
int dp[535], f[535][535][12], val[535];
char ins[535];
void dfs(int x) {
  for (int(j) = 0; (j) < (int)(10); (j)++)
    if (tr[x][j]) {
      int v = tr[x][j];
      dfs(v);
      for (int d = 0; d <= dp[x]; d++) {
        for (int i = K; i >= 0; i--) {
          int tmp = (1 << 30);
          for (int j = 0; j <= i; j++) {
            mintt(tmp, f[x][d][j] + f[v][d + 1][i - j]);
          }
          f[x][d][i] = tmp;
        }
      }
    }
  for (int d = 0; d <= dp[x]; d++) {
    for (int i = K; i >= 0; i--) {
      f[x][d][i] += d * val[x];
    }
  }
  for (int d = 0; d <= dp[x]; d++) {
    for (int i = K; i >= 1; i--) {
      mintt(f[x][d][i], f[x][0][i - 1]);
    }
  }
}
void fmain(int tid) {
  scanf("%d%d", &n, &K);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    scanf("%s%d", ins + 1, &m);
    int rt = 1;
    int l = strlen(ins + 1);
    for (int(j) = 1; (j) <= (int)(l); (j)++) {
      int c = ins[j] - '0';
      if (tr[rt][c] == 0) {
        tr[rt][c] = ++cnt;
        dp[cnt] = dp[rt] + 1;
      }
      rt = tr[rt][c];
    }
    val[rt] += m;
  }
  dfs(1);
  printf("%d\n", f[1][0][K]);
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
