#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 4557430888798830399;
int n, k;
int dp[2005][2005];
int dp2[2005];
int dppre[2005][2005];
int dpans[2005][2005];
int ans[2005];
char s[15][15] = {"1110111", "0010010", "1011101", "1011011", "0111010",
                  "1101011", "1101111", "1010010", "1111111", "1111011"};
int tch[15];
int ch[2005];
int ReadBinary() {
  int res = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) {
    res = res * 2 + ch - '0';
    ch = getchar();
  }
  return res;
}
int top;
char Getchar(int id) { return s[id][top++]; }
int ReadBinary2(int id) {
  top = 0;
  int res = 0;
  char ch = Getchar(id);
  while (!isdigit(ch)) ch = Getchar(id);
  while (isdigit(ch)) {
    res = res * 2 + ch - '0';
    ch = Getchar(id);
  }
  return res;
}
int dis[1 << 7][15];
void TestCase() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i <= 9; ++i) tch[i] = ReadBinary2(i);
  for (int i = 1; i <= n; ++i) ch[i] = ReadBinary();
  memset(dis, -INF, sizeof(dis));
  for (int i = 0; i < (1 << 7); ++i) {
    for (int ji = 0; ji <= 9; ++ji) {
      int j = tch[ji];
      int cnt = 0;
      for (int k = 0; k < 7; ++k) {
        if (((i >> k) & 1) == ((j >> k) & 1)) continue;
        if (((i >> k) & 1) == 1) {
          cnt = -INF;
          break;
        }
        ++cnt;
      }
      dis[i][ji] = cnt;
    }
  }
  for (int i = 0; i <= 2001; ++i)
    for (int j = 0; j <= 2001; ++j) dp[i][j] = -10000;
  memset(dppre, -INF, sizeof(dppre));
  dp[0][k] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int ji = 0; ji <= 9; ++ji) {
      int DIS = dis[ch[i]][ji];
      if (DIS == -INF) continue;
      for (int ki = DIS; ki <= k; ++ki) {
        if (dp[i - 1][ki] < 0) continue;
        if (dp[i - 1][ki] * 10 + ji > dp[i][ki - DIS]) {
          dp[i][ki - DIS] = max(dp[i - 1][ki] * 10 + ji, dp[i][ki - DIS]);
          dppre[i][ki - DIS] = ki;
          dpans[i][ki - DIS] = ji;
        }
      }
    }
    for (int ki = 0; ki <= k; ++ki) {
      dp2[ki] = dp[i][ki];
    }
    sort(dp2, dp2 + k + 1);
    int nk = unique(dp2, dp2 + k + 1) - dp2;
    for (int ki = 0; ki <= k; ++ki) {
      if (dp[i][ki] >= 0)
        dp[i][ki] = lower_bound(dp2, dp2 + nk, dp[i][ki]) - dp2;
      else
        dp[i][ki] = -10000;
    }
  }
  if (dp[n][0] < 0) {
    puts("-1");
    return;
  }
  int atop = 0;
  int curk = 0;
  for (int i = n; i >= 1; --i) {
    ans[++atop] = dpans[i][curk];
    curk = dppre[i][curk];
  }
  for (int i = n; i >= 1; --i) printf("%d", ans[i]);
  puts("");
  return;
}
int main() {
  int t = 1;
  for (int ti = 1; ti <= t; ++ti) TestCase();
  return 0;
}
