#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 10;
struct node {
  node *nxt[10];
  int num, sum;
  node(int _num) {
    num = _num;
    for (int i = 0; i < 10; i++) nxt[i] = 0;
    sum = 0;
  }
} * root;
int cnt, deep[M], f[1000][1000][11], g[1000][1000][11], st[M], sz, K;
void insert(char *s, int sum) {
  node *p = root;
  int x;
  for (int i = 1; s[i]; i++) {
    x = s[i] - '0';
    if (!p->nxt[x]) p->nxt[x] = new node(++cnt);
    p = p->nxt[x];
  }
  p->sum += sum;
}
void dfs(node *p) {
  int u = p->num, v;
  st[++sz] = u;
  for (int i = 0; i < 10; i++)
    if (p->nxt[i]) {
      v = p->nxt[i]->num, deep[v] = deep[u] + 1;
      dfs(p->nxt[i]);
      for (int j = 1; j <= sz; j++) {
        for (int k = K; k >= 0; k--) {
          f[u][st[j]][k] += f[v][st[j]][0], g[u][st[j]][k] += f[v][u][0];
          for (int x = k; x >= 0; x--) {
            f[u][st[j]][k] =
                min(f[u][st[j]][k], f[u][st[j]][k - x] + f[v][st[j]][x]);
            g[u][st[j]][k] =
                min(g[u][st[j]][k], g[u][st[j]][k - x] + f[v][u][x]);
          }
        }
      }
    }
  for (int j = 1; j <= sz; j++) {
    for (int k = K; k >= 0; k--)
      if (k >= 1)
        f[u][st[j]][k] = min(f[u][st[j]][k] + p->sum * (deep[u] - deep[st[j]]),
                             g[u][st[j]][k - 1]);
      else
        f[u][st[j]][k] = f[u][st[j]][k] + p->sum * (deep[u] - deep[st[j]]);
  }
  sz--;
}
char s[10000];
int main() {
  root = new node(cnt);
  int n, a;
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%s%d", s + 1, &a);
    insert(s, a);
  }
  dfs(root);
  printf("%d\n", f[0][0][K]);
}
