#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int cnt;
int dp[510][510][11];
int f[11], g[11];
int trans[510][11];
int n, k;
int dep[510], siz[510];
char st[510];
void calc(int u) {
  for (int i = 0; i < 10; i++)
    if (trans[u][i]) calc(trans[u][i]);
  for (int i = 0; i <= dep[u]; i++) {
    for (int j = 0; j <= k; j++) f[j] = 0;
    for (int j = 0; j < 10; j++)
      if (trans[u][j]) {
        int v = trans[u][j];
        for (int w = 0; w <= k; w++) g[w] = inf;
        for (int i1 = 0; i1 <= k; i1++)
          for (int j1 = 0; i1 + j1 <= k; j1++)
            g[i1 + j1] = min(g[i1 + j1], f[i1] + dp[v][i][j1]);
        for (int w = 0; w <= k; w++) f[w] = g[w];
      }
    if (i != dep[u]) {
      for (int j = 0; j <= k; j++) dp[u][i][j] = f[j] + siz[u] * (dep[u] - i);
    } else {
      if (!i)
        printf("%d\n", f[k]);
      else
        for (int j = 0; j < dep[u]; j++)
          for (int w = 0; w < k; w++)
            dp[u][j][w + 1] = min(dp[u][j][w + 1], f[w]);
    }
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    int num;
    scanf("%s%d", st + 1, &num);
    int now = 0, len = strlen(st + 1);
    for (int j = 1; j <= len; j++) {
      int c = st[j] - '0';
      if (!trans[now][c]) trans[now][c] = ++cnt, dep[cnt] = dep[now] + 1;
      now = trans[now][c];
    }
    siz[now] += num;
  }
  calc(0);
}
