#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2005;
const int dig[10] = {119, 36, 93, 109, 46, 107, 123, 37, 127, 111};
char s[10];
int n, k, a[MAXN];
int g[128][10];
bool h[128][8];
bool f[MAXN][MAXN];
int todig(char* s) {
  int x = 0;
  for (int i = 0; s[i]; i++) x += (s[i] - '0') << i;
  return x;
}
int main() {
  for (int i = 0; i < 128; i++)
    for (int j = 0; j < 10; j++) {
      if ((i & dig[j]) == i) {
        g[i][j] = __builtin_popcount(i ^ dig[j]);
        h[i][g[i][j]] = 1;
      } else
        g[i][j] = -1;
    }
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%s", s), a[i] = todig(s);
  f[n + 1][0] = 1;
  for (int i = n; i >= 1; i--)
    for (int j = 0; j <= k; j++)
      for (int l = 0; l <= j && l < 8; l++)
        if (h[a[i]][l] && f[i + 1][j - l]) {
          f[i][j] = 1;
          break;
        }
  if (!f[1][k]) return puts("-1"), 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 9; j >= 0; j--)
      if (g[a[i]][j] != -1 && g[a[i]][j] <= k)
        if (f[i + 1][k - g[a[i]][j]]) {
          putchar(j + '0');
          k -= g[a[i]][j];
          break;
        }
  }
  putchar('\n');
  return 0;
}
