#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, a[200000];
void solve(){
      cin >> n;
      for(int i=0;i<n;i++) cin >> a[i];
      int move = 0, sum1 = 0,sum2 = 0;
      int i=0,j = n-1;
      ll x = 0,y=0;
      if(n == 1) cout << 1 << " " << a[0] << " " << 0 << endl;
      else{
            while(i < j){
            x = 0;
            if(i<=j) move++;
            while(x <= y and i<=j){
                  x += a[i];
                  i++;
            }
            sum1 += x;
            y = 0;
            if(i<=j) move++;
            while(y <= x and i<=j){
                  y += a[j];
                  j--;
            }
            sum2 += y;
      }
      cout << move << " " << sum1 << " " << sum2 << endl;
      }
}

int main()
{
      int t; cin >> t;
      while(t--)
            solve();
}


#include "bits/stdc++.h"
using namespace std;

typedef long long ll;

int main(int argc, char const* argv[]) {
	ll t;
  scanf("%lld\n", &t);
	while (t--) {
		ll n;
		cin >> n;
		vector<ll> a(n);
		for (auto &it : a) cin >> it;
		ll l = 0, r = n - 1;
		ll suml = 0, sumr = 0;
		ll cnt = 0, ansl = 0, ansr = 0;
		while (l <= r) {
			if (cnt % 2 == 0) {
				ll nsuml = 0;
				while (l <= r && nsuml <= sumr) {
					nsuml += a[l++];
				}
				ansl += nsuml;
				suml = nsuml;
			} else {
				ll nsumr = 0;
				while (l <= r && nsumr <= suml) {
					nsumr += a[r--];
				}
				ansr += nsumr;
				sumr = nsumr;
			}
			++cnt;
		}
		printf("%lld %lld %lld\n", cnt, ansl, ansr);
	}
	/*
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll t;
  scanf("%lld\n", &t);
  while (t--) {
    ll n;
    scanf("%lld", &n);
    vector<ll> A(n);
    for (ll i = 0; i < n; i++) {
      scanf("%lld", &A[i]);
    }
    ll lo = 0;
    ll hi = n - 1;
    ll moves = 0, bound = 0, asum = 0, bsum = 0;
    while (lo <= hi) {
      ll sum = 0;
      if (moves % 2 == 0) {
        while (lo <= hi && sum <= bound) {
          sum += A[lo];
          lo++;
        }
        asum += sum;
      } else {
        while (lo <= hi && sum <= bound) {
          sum += A[hi];
          hi--;
        }
        bsum += sum;
      }
      moves++;
      bound = sum;
    }
    print("%lld %lld %lld\n", moves, asum, bsum);
  }
  return 0;
  */
}


#include "bits/stdc++.h"
using namespace std;

typedef long long ll;

int main(int argc, char const* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll t;
  scanf("%lld\n", &t);
  while (t--) {
    ll n;
    scanf("%lld", &n);
    vector<ll> A(n);
    for (ll i = 0; i < n; i++) {
      scanf("%lld", &A[i]);
    }
    ll lo = 0;
    ll hi = n - 1;
    ll moves = 0, bound = 0, asum = 0, bsum = 0;
    while (lo <= hi) {
      ll sum = 0;
      if (moves % 2 == 0) {
        while (lo <= hi && sum <= bound) {
          sum += A[lo];
          lo++;
        }
        asum += sum;
      } else {
        while (lo <= hi && sum <= bound) {
          sum += A[hi];
          hi--;
        }
        bsum += sum;
      }
      moves++;
      bound = sum;
    }
    printf("%lld %lld %lld\n", moves, asum, bsum);
  }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;

typedef long long ll;

int main(int argc, char const* argv[]) {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		vector<int> a(n);
		for (auto &it : a) cin >> it;
		int l = 0, r = n - 1;
		int suml = 0, sumr = 0;
		int cnt = 0, ansl = 0, ansr = 0;
		while (l <= r) {
			if (cnt % 2 == 0) {
				int nsuml = 0;
				while (l <= r && nsuml <= sumr) {
					nsuml += a[l++];
				}
				ansl += nsuml;
				suml = nsuml;
			} else {
				int nsumr = 0;
				while (l <= r && nsumr <= suml) {
					nsumr += a[r--];
				}
				ansr += nsumr;
				sumr = nsumr;
			}
			++cnt;
		}
		cout << cnt << " " << ansl << " " << ansr << endl;
	}
	/*
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll t;
  scanf("%Ld\n", &t);
  while (t--) {
    ll n;
    scanf("%Ld", &n);
    vector<ll> A(n);
    for (ll i = 0; i < n; i++) {
      scanf("%Ld", &A[i]);
    }
    ll lo = 0;
    ll hi = n - 1;
    ll moves = 0, bound = 0, asum = 0, bsum = 0;
    while (lo <= hi) {
      ll sum = 0;
      if (moves % 2 == 0) {
        while (lo <= hi && sum <= bound) {
          sum += A[lo];
          lo++;
        }
        asum += sum;
      } else {
        while (lo <= hi && sum <= bound) {
          sum += A[hi];
          hi--;
        }
        bsum += sum;
      }
      moves++;
      bound = sum;
    }
    printf("%Ld %Ld %Ld\n", moves, asum, bsum);
  }
  return 0;
  */
}


#include "bits/stdc++.h"
using namespace std;

typedef long long ll;

int main(int argc, char const* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t;
  scanf("%Ld\n", &t);
  while (t--) {
    ll n;
    scanf("%Ld", &n);
    vector<ll> A(n);
    for (auto& elem : A) {
      scanf("%Ld", &elem);
    }
    ll lo = 0;
    ll hi = n - 1;
    ll moves = 0, bound = 0, asum = 0, bsum = 0;
    while (true) {
      ll sum = 0;
      if (moves % 2 == 0) {
        while (sum <= bound) {
          sum += A[lo];
          lo++;
          if (lo > hi) {
            break;
          }
        }
        asum += sum;
      } else {
        while (sum <= bound) {
          sum += A[hi];
          hi--;
          if (lo > hi) {
            break;
          }
        }
        bsum += sum;
      }
      moves++;
      if (lo > hi) {
        break;
      }
      bound = sum;
    }
    printf("%Ld %Ld %Ld\n", moves, asum, bsum);
  }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;

typedef long long ll;

int main(int argc, char const* argv[]) {
	ll t;
  scanf("%Ld\n", &t);
	while (t--) {
		ll n;
		cin >> n;
		vector<ll> a(n);
		for (auto &it : a) cin >> it;
		ll l = 0, r = n - 1;
		ll suml = 0, sumr = 0;
		ll cnt = 0, ansl = 0, ansr = 0;
		while (l <= r) {
			if (cnt % 2 == 0) {
				ll nsuml = 0;
				while (l <= r && nsuml <= sumr) {
					nsuml += a[l++];
				}
				ansl += nsuml;
				suml = nsuml;
			} else {
				ll nsumr = 0;
				while (l <= r && nsumr <= suml) {
					nsumr += a[r--];
				}
				ansr += nsumr;
				sumr = nsumr;
			}
			++cnt;
		}
		printf("%Ld %Ld %Ld\n", cnt, ansl, ansr);
	}
	/*
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll t;
  scanf("%Ld\n", &t);
  while (t--) {
    ll n;
    scanf("%Ld", &n);
    vector<ll> A(n);
    for (ll i = 0; i < n; i++) {
      scanf("%Ld", &A[i]);
    }
    ll lo = 0;
    ll hi = n - 1;
    ll moves = 0, bound = 0, asum = 0, bsum = 0;
    while (lo <= hi) {
      ll sum = 0;
      if (moves % 2 == 0) {
        while (lo <= hi && sum <= bound) {
          sum += A[lo];
          lo++;
        }
        asum += sum;
      } else {
        while (lo <= hi && sum <= bound) {
          sum += A[hi];
          hi--;
        }
        bsum += sum;
      }
      moves++;
      bound = sum;
    }
    print("%Ld %Ld %Ld\n", moves, asum, bsum);
  }
  return 0;
  */
}


#include "bits/stdc++.h"
using namespace std;

typedef long long ll;

int main(int argc, char const* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll t;
  scanf("%Ld\n", &t);
  while (t--) {
    ll n;
    scanf("%Ld", &n);
    vector<ll> A(n);
    for (ll i = 0; i < n; i++) {
      scanf("%Ld", &A[i]);
    }
    ll lo = 0;
    ll hi = n - 1;
    ll moves = 0, bound = 0, asum = 0, bsum = 0;
    while (true) {
      ll sum = 0;
      if (moves % 2 == 0) {
        while (sum <= bound) {
          sum += A[lo];
          lo++;
          if (lo > hi) {
            break;
          }
        }
        asum += sum;
      } else {
        while (sum <= bound) {
          sum += A[hi];
          hi--;
          if (lo > hi) {
            break;
          }
        }
        bsum += sum;
      }
      moves++;
      if (lo > hi) {
        break;
      }
      bound = sum;
    }
    printf("%Ld %Ld %Ld\n", moves, asum, bsum);
  }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;

typedef long long ll;

int main(int argc, char const* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll t;
  scanf("%Ld\n", &t);
  while (t--) {
    ll n;
    scanf("%Ld", &n);
    vector<ll> A(n);
    for (auto& elem : A) {
      scanf("%Ld", &elem);
    }
    ll lo = 0;
    ll hi = n - 1;
    ll moves = 0, bound = 0, asum = 0, bsum = 0;
    while (true) {
      ll sum = 0;
      if (moves % 2 == 0) {
        while (sum <= bound) {
          sum += A[lo];
          lo++;
          if (lo > hi) {
            break;
          }
        }
        asum += sum;
      } else {
        while (sum <= bound) {
          sum += A[hi];
          hi--;
          if (lo > hi) {
            break;
          }
        }
        bsum += sum;
      }
      moves++;
      if (lo > hi) {
        break;
      }
      bound = sum;
    }
    printf("%Ld %Ld %Ld\n", moves, asum, bsum);
  }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;

typedef long long ll;

int main(int argc, char const* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll t;
  scanf("%Ld\n", &t);
  while (t--) {
    ll n;
    scanf("%Ld", &n);
    vector<ll> A(n);
    for (ll i = 0; i < n; i++) {
      scanf("%Ld", &A[i]);
    }
    ll lo = 0;
    ll hi = n - 1;
    ll moves = 0, bound = 0, asum = 0, bsum = 0;
    while (lo <= hi) {
      ll sum = 0;
      if (moves % 2 == 0) {
        while (lo <= hi && sum <= bound) {
          sum += A[lo];
          lo++;
        }
        asum += sum;
      } else {
        while (lo <= hi && sum <= bound) {
          sum += A[hi];
          hi--;
        }
        bsum += sum;
      }
      moves++;
      bound = sum;
    }
    printf("%Ld %Ld %Ld\n", moves, asum, bsum);
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <deque>
#define int long long
#define fast_io ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,start,end) for(int i=start;i<end;i++)
#define per(i,start,end) for (int i=end-1;i>=start;i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define vp vector<pair<int,int>>
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
const int N = 1e5;
using namespace std;
void solve()
{
	int n;
	cin >> n;
	vector<int> a(n);
	rep(i, 0, n)
		cin >> a[i];
	if (n == 1)
	{
		cout << "1\n";
		cout << a[0] << " " << 0 << "\n";
		return;
	}
	int alice = a[0], bob = 0;
	a.erase(a.begin());
	int c = 1;
	int al = a[0];
	while (a.size() > 0)
	{
		int b = 0;
		al++;
		while (a.size()>0 && b<al)
		{
			b += a[a.size() - 1];
			bob += a[a.size() - 1];
			a.erase(a.begin() + a.size() - 1);
		}
		c++;
		if (a.size() == 0)
			break;
		al = 0;
		b++;
		while (a.size() > 0 && al<b)
		{
			if (a.size() == 0)
				break;
			alice += a[0];
			al += a[0];
			a.erase(a.begin());
		}
		c++;
		if (a.size() == 0)
			break;
	}
	cout << c << " ";
	cout << alice << " " << bob << "\n";

}
int32_t main()
{
	fast_io;
	int t;
	cin >> t;
	//t = 1;
	while (t--)
		solve();
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <deque>
#define int long long
#define fast_io ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,start,end) for(int i=start;i<end;i++)
#define per(i,start,end) for (int i=end-1;i>=start;i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define vp vector<pair<int,int>>
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
const int N = 1e5;
using namespace std;
void solve()
{
	int n;
	cin >> n;
	vector<int> a(n);
	rep(i, 0, n)
		cin >> a[i];
	if (n == 1)
	{
		cout << "1\n";
		cout << a[0] << " " << 0 << "\n";
		return;
	}
	int alice = a[0], bob = 0;
	a.erase(a.begin());
	int i = 1, j = n - 1;
	int c = 1;
	int al = a[0];
	while (a.size() > 0)
	{
		int b = 0;
		while (a.size()>0 && al+1 > b)
		{
			b += a[a.size() - 1];
			bob += a[a.size() - 1];
			a.erase(a.begin() + a.size() - 1);
		}
		c++;
		if (a.size() == 0)
			break;
		al = 0;
		while (a.size() > 0 && b+1 > al)
		{
			if (a.size() == 0)
				break;
			alice += a[0];
			al += a[0];
			a.erase(a.begin());
		}
		c++;
		if (a.size() == 0)
			break;
	}
	cout << c << " ";
	cout << alice << " " << bob << "\n";

}
int32_t main()
{
	fast_io;
	int t;
	cin >> t;
	//t = 1;
	while (t--)
		solve();
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <deque>
#define int long long
#define fast_io ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,start,end) for(int i=start;i<end;i++)
#define per(i,start,end) for (int i=end-1;i>=start;i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define vp vector<pair<int,int>>
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
const int N = 1e5;
using namespace std;
void solve()
{
	int n;
	cin >> n;
	vector<int> a(n);
	rep(i, 0, n)
		cin >> a[i];
	if (n == 1)
	{
		cout << "1\n";
		cout << a[0] << " " << 0 << "\n";
		return;
	}
	int alice = a[0], bob = 0;
	int al = a[0];

	a.erase(a.begin());
	int c = 1;
	while (a.size() > 0)
	{
		int b = 0;
		al++;
		bool b1 = false;
		while (a.size()>0 && b<al)
		{
			b += a[a.size() - 1];
			bob += a[a.size() - 1];
			a.erase(a.begin() + a.size() - 1);
			b1 = true;
		}
		if (b1)
			c++;

		if (a.size() == 0)
			break;
		al = 0;
		b++;
		b1 = false;
		while (a.size() > 0 && al<b)
		{
			if (a.size() == 0)
				break;
			alice += a[0];
			al += a[0];
			a.erase(a.begin());
			b1 = true;
		}
		if (b1)
			c++;
		if (a.size() == 0)
			break;
	}
	cout << c << " ";
	cout << alice << " " << bob << "\n";

}
int32_t main()
{
	fast_io;
	int t;
	cin >> t;
	//t = 1;
	while (t--)
		solve();
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <deque>
#define int long long
#define fast_io ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,start,end) for(int i=start;i<end;i++)
#define per(i,start,end) for (int i=end-1;i>=start;i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define vp vector<pair<int,int>>
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
const int N = 1e5;
using namespace std;
void solve()
{
	int n;
	cin >> n;
	vector<int> a(n);
	rep(i, 0, n)
		cin >> a[i];
	if (n == 1)
	{
		cout << "1\n";
		cout << a[0] << " " << 0 << "\n";
		return;
	}
	int alice = a[0], bob = 0;
	int al = a[0];

	a.erase(a.begin());
	int c = 1;
	while (a.size() > 0)
	{
		int b = 0;
		al++;
		bool b1 = false;
		while (a.size()>0 && b<al)
		{
			b += a[a.size() - 1];
			bob += a[a.size() - 1];
			a.erase(a.begin() + a.size() - 1);
			b1 = true;
		}
		if (b1)
			c++;

		if (a.size() == 0)
			break;
		al = 0;
		b++;
		b1 = false;
		while (a.size() > 0 && al<b)
		{
			if (a.size() == 0)
				break;
			alice += a[0];
			al += a[0];
			a.erase(a.begin());
			b1 = true;
		}
		cout << "al is " << al <<  "\n";
		if (b1)
			c++;
		if (a.size() == 0)
			break;
	}
	cout << c << " ";
	cout << alice << " " << bob << "\n";

}
int32_t main()
{
	fast_io;
	int t;
	cin >> t;
	//t = 1;
	while (t--)
		solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector< pair<ll, ll> > vll;

#define rep(n) for(ll i = 0; i < n; i++)
#define fr(i,j,k) for(ll i = j; i < k; i++)
#define FR(i,j,k) for(ll i = j; i >= 0; i--)
#define MOD 1000000007
#define INF 1000000000
#define invec(a, n) for(ll i = 0; i < n; i++) {cin >> a[i];}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define pl cout<<endl;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define ff first
#define ss second
#define pls(x) cout<<x<<' ';
#define pln(x) cout<<x<<"\n";
#define ini(a, i) memset(a, i, sizeof(a))
#define ub(a,x) upper_bound(all(a),x) - a.begin()
#define lb(a,x) lower_bound(all(a),x) - a.begin()

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template<typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)),  __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgm() {;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
const long long INFll = 1ll * INF * INF;

string d[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int n, kk;
int change[2005][10];
int dp[2005][2005][10];

string ans = "";

bool knapsack(int ind, int tot, int dig)
{
	if (ind == n)
	{
		if (tot == 0)
		{
			return dp[ind][tot][dig] = 1;
		}
		else
		{
			return dp[ind][tot][dig] = 0;
		}
	}

	if (tot < 0) return dp[ind][tot][dig] = 0;

	if (dp[ind][tot][dig] != -1)
		return dp[ind][tot][dig];

	int pos = 0;
	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];

		if (cost != -1 && cost <= tot)
		{
			pos |= knapsack(ind + 1, tot - cost, i);
		}

	}
	return dp[ind][tot][dig] = pos;

}

void find_ans(int ind, int tot, int dig)
{
	if (ind == n)
		return;

	//cout << ind;

	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];
		if (dp[ind + 1][tot - cost][i] == 1)
		{
			ans += (i + 48);
			find_ans(ind + 1, tot - cost, i);

			return;
		}
		//return;
	}
}

int main()
{
	fast();
	//#ifndef ONLINE_JUDGE
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\in.txt","r",stdin);
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\out.txt","w",stdout);
	//#endif

	cin >> n >> kk;

	ini(dp, -1);
	ini(change, -1);

	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;

		for (int j = 0; j < 10; j++)
		{
			int cnt = 0;
			string change_to = d[j];

			//dbg(change_to);

			for (int k = 0; k < 7; k++)
			{
				//cout << s[k] << " ";
				if ((change_to[k] == '1') && (s[k] == '0'))
				{
					cnt++;
				}
				else if (change_to[k] != s[k])
				{
					cnt = -1;
					break;
				}
			}
			change[i][j] = cnt;
			//pl;
		}
	}

	//dbg(dp);

	//dbg(change);

	if (!knapsack(0, kk, 0))
	{
		cout << -1;
		pl;
		return 0;
	}

	find_ans(0, kk, 0);

	cout << ans;
	pl;

}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector< pair<ll, ll> > vll;

#define rep(n) for(ll i = 0; i < n; i++)
#define fr(i,j,k) for(ll i = j; i < k; i++)
#define FR(i,j,k) for(ll i = j; i >= 0; i--)
#define MOD 1000000007
#define INF 1000000000
#define invec(a, n) for(ll i = 0; i < n; i++) {cin >> a[i];}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define pl cout<<endl;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define ff first
#define ss second
#define pls(x) cout<<x<<' ';
#define pln(x) cout<<x<<"\n";
#define ub(a,x) upper_bound(all(a),x) - a.begin()
#define lb(a,x) lower_bound(all(a),x) - a.begin()

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template<typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)),  __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgm() {;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
const long long INFll = 1ll * INF * INF;

string d[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int n, kk;
int change[2001][10];
int dp[2001][2001][10];

string ans = "";

bool knapsack(int ind, int tot, int dig)
{
	if (ind == n)
	{
		if (tot == 0)
		{
			return dp[ind][tot][dig] = 1;
		}
		else
		{
			return dp[ind][tot][dig] = 0;
		}
	}

	if (tot < 0) return dp[ind][tot][dig] = 0;

	if (dp[ind][tot][dig] != -1)
		return dp[ind][tot][dig];

	bool pos = 0;
	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];

		if (cost != -1 && cost <= tot)
		{
			pos = max(pos, knapsack(ind + 1, tot - cost, i) );
		}

	}
	return dp[ind][tot][dig] = pos;
}

void find_ans(int ind, int tot, int dig)
{
	if (ind == n)
		return;

	//cout << ind;

	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];
		if (cost <= tot && cost != -1 && dp[ind + 1][tot - cost][i] == 1)
		{
			ans += (i + 48);
			find_ans(ind + 1, tot - cost, i);

			return;
		}
		//return;
	}
}

int main()
{
	fast();
	//#ifndef ONLINE_JUDGE
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\in.txt","r",stdin);
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\out.txt","w",stdout);
	//#endif

	cin >> n >> kk;

	for (int i = 0; i < (n + 10); i++)
	{
		for (int j = 0; j < (n + 10); j++)
		{
			for (int k = 0; k < (kk + 10); k++)
			{
				dp[i][j][k] = -1;
			}
		}
	}

	for (int i = 0; i < (n + 10); i++)
	{
		for (int j = 0; j < (kk + 10); j++)
		{
			change[i][j] = -1;
		}
	}

	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;

		for (int j = 0; j < 10; j++)
		{
			int cnt = 0;
			string change_to = d[j];

			//dbg(change_to);

			for (int k = 0; k < 7; k++)
			{
				//cout << s[k] << " ";
				if ((change_to[k] == '1') && (s[k] == '0'))
				{
					cnt++;
				}
				else if (change_to[k] != s[k])
				{
					cnt = -1;
					break;
				}
			}
			change[i][j] = cnt;
			//pl;
		}
	}

	//dbg(dp);

	//dbg(change);

	if (!knapsack(0, kk, 0))
	{
		cout << -1;
		pl;
		return 0;
	}

	find_ans(0, kk, 0);

	cout << ans;
	pl;

}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector< pair<ll, ll> > vll;

#define rep(n) for(ll i = 0; i < n; i++)
#define fr(i,j,k) for(ll i = j; i < k; i++)
#define FR(i,j,k) for(ll i = j; i >= 0; i--)
#define MOD 1000000007
#define INF 1000000000
#define invec(a, n) for(ll i = 0; i < n; i++) {cin >> a[i];}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define pl cout<<endl;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define ff first
#define ss second
#define pls(x) cout<<x<<' ';
#define pln(x) cout<<x<<"\n";
#define ub(a,x) upper_bound(all(a),x) - a.begin()
#define lb(a,x) lower_bound(all(a),x) - a.begin()

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template<typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)),  __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgm() {;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
const long long INFll = 1ll * INF * INF;

string d[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int n, kk;
int change[2001][10];
int dp[2001][2001][10];

string ans = "";

bool knapsack(int ind, int tot, int dig)
{
	if (ind == n)
	{
		if (tot == 0)
		{
			return dp[ind][tot][dig] = 1;
		}
		else
		{
			return dp[ind][tot][dig] = 0;
		}
	}

	if (dp[ind][tot][dig] != -1)
		return dp[ind][tot][dig];

	bool pos = 0;
	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];

		if (cost != -1 && cost <= tot)
		{
			pos = max(pos, knapsack(ind + 1, tot - cost, i) );
		}

	}
	return dp[ind][tot][dig] = pos;
}

void find_ans(int ind, int tot, int dig)
{
	if (ind == n)
		return;

	//cout << ind;

	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];
		if (cost <= tot && cost != -1 && dp[ind + 1][tot - cost][i] == 1)
		{
			ans += (i + 48);
			find_ans(ind + 1, tot - cost, i);

			return;
		}
		//return;
	}
}

int main()
{
	fast();
	//#ifndef ONLINE_JUDGE
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\in.txt","r",stdin);
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\out.txt","w",stdout);
	//#endif

	cin >> n >> kk;

	for (int i = 0; i < (n + 10); i++)
	{
		for (int j = 0; j < (n + 10); j++)
		{
			for (int k = 0; k < (kk + 10); k++)
			{
				dp[i][j][k] = -1;
			}
		}
	}

	for (int i = 0; i < (n + 10); i++)
	{
		for (int j = 0; j < (kk + 10); j++)
		{
			change[i][j] = -1;
		}
	}

	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;

		for (int j = 0; j < 10; j++)
		{
			int cnt = 0;
			string change_to = d[j];

			//dbg(change_to);

			for (int k = 0; k < 7; k++)
			{
				//cout << s[k] << " ";
				if ((change_to[k] == '1') && (s[k] == '0'))
				{
					cnt++;
				}
				else if (change_to[k] != s[k])
				{
					cnt = -1;
					break;
				}
			}
			change[i][j] = cnt;
			//pl;
		}
	}

	//dbg(dp);

	//dbg(change);

	if (!knapsack(0, kk, 0))
	{
		cout << -1;
		pl;
		return 0;
	}

	find_ans(0, kk, 0);

	cout << ans;
	pl;

}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector< pair<ll, ll> > vll;

#define rep(n) for(ll i = 0; i < n; i++)
#define fr(i,j,k) for(ll i = j; i < k; i++)
#define FR(i,j,k) for(ll i = j; i >= 0; i--)
#define MOD 1000000007
#define INF 1000000000
#define invec(a, n) for(ll i = 0; i < n; i++) {cin >> a[i];}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define pl cout<<endl;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define ff first
#define ss second
#define pls(x) cout<<x<<' ';
#define pln(x) cout<<x<<"\n";
#define ub(a,x) upper_bound(all(a),x) - a.begin()
#define lb(a,x) lower_bound(all(a),x) - a.begin()

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template<typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)),  __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgm() {;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
const long long INFll = 1ll * INF * INF;

string d[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int n, kk;
int change[2001][10];
int dp[2001][2001][10];

string ans = "";

bool knapsack(int ind, int tot, int dig)
{
	if (ind == n)
	{
		if (tot == 0)
		{
			return dp[ind][tot][dig] = 1;
		}
		else
		{
			return dp[ind][tot][dig] = 0;
		}
	}

	if (tot < 0) return dp[ind][tot][dig] = 0;

	if (dp[ind][tot][dig] != -1)
		return dp[ind][tot][dig];

	int pos = 0;
	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];

		if (cost != -1 && cost <= tot)
		{
			pos |= knapsack(ind + 1, tot - cost, i);
		}

	}
	return dp[ind][tot][dig] = pos;

}

void find_ans(int ind, int tot, int dig)
{
	if (ind == n)
		return;

	//cout << ind;

	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];
		if (cost <= tot && cost != -1 && dp[ind + 1][tot - cost][i] == 1)
		{
			ans += (i + 48);
			find_ans(ind + 1, tot - cost, i);

			return;
		}
		//return;
	}
}

int main()
{
	fast();
	//#ifndef ONLINE_JUDGE
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\in.txt","r",stdin);
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\out.txt","w",stdout);
	//#endif

	cin >> n >> kk;

	for (int i = 0; i < (n + 10); i++)
	{
		for (int j = 0; j < (n + 10); j++)
		{
			for (int k = 0; k < 10; k++)
			{
				dp[i][j][k] = -1;
			}
		}
	}

	for (int i = 0; i < (n + 10); i++)
	{
		for (int j = 0; j < (10); j++)
		{
			change[i][j] = -1;
		}
	}

	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;

		for (int j = 0; j < 10; j++)
		{
			int cnt = 0;
			string change_to = d[j];

			//dbg(change_to);

			for (int k = 0; k < 7; k++)
			{
				//cout << s[k] << " ";
				if ((change_to[k] == '1') && (s[k] == '0'))
				{
					cnt++;
				}
				else if (change_to[k] != s[k])
				{
					cnt = -1;
					break;
				}
			}
			change[i][j] = cnt;
			//pl;
		}
	}

	//dbg(dp);

	//dbg(change);

	if (!knapsack(0, kk, 0))
	{
		cout << -1;
		pl;
		return 0;
	}

	find_ans(0, kk, 0);

	cout << ans;
	pl;

}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector< pair<ll, ll> > vll;

#define rep(n) for(ll i = 0; i < n; i++)
#define fr(i,j,k) for(ll i = j; i < k; i++)
#define FR(i,j,k) for(ll i = j; i >= 0; i--)
#define MOD 1000000007
#define INF 1000000000
#define invec(a, n) for(ll i = 0; i < n; i++) {cin >> a[i];}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define pl cout<<endl;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define ff first
#define ss second
#define pls(x) cout<<x<<' ';
#define pln(x) cout<<x<<"\n";
#define ub(a,x) upper_bound(all(a),x) - a.begin()
#define lb(a,x) lower_bound(all(a),x) - a.begin()

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template<typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)),  __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgm() {;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
const long long INFll = 1ll * INF * INF;

string d[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int n, kk;
int change[2005][10];
int dp[2005][2005][10];

string ans = "";

bool knapsack(int ind, int tot, int dig)
{
	if (ind == n)
	{
		if (tot == 0)
		{
			return dp[ind][tot][dig] = 1;
		}
		else
		{
			return dp[ind][tot][dig] = 0;
		}
	}

	if (tot < 0) return dp[ind][tot][dig] = 0;

	if (dp[ind][tot][dig] != -1)
		return dp[ind][tot][dig];

	int pos = 0;
	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];

		if (cost != -1 && cost <= tot)
		{
			pos |= knapsack(ind + 1, tot - cost, i);
		}

	}
	return dp[ind][tot][dig] = pos;

}

void find_ans(int ind, int tot, int dig)
{
	if (ind == n)
		return;

	//cout << ind;

	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];
		if (cost <= tot && cost != -1 && dp[ind + 1][tot - cost][i] == 1)
		{
			ans += (i + 48);
			find_ans(ind + 1, tot - cost, i);

			return;
		}
		//return;
	}
}

int main()
{
	fast();
	//#ifndef ONLINE_JUDGE
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\in.txt","r",stdin);
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\out.txt","w",stdout);
	//#endif

	cin >> n >> kk;

	for (int i = 0; i < (n + 1); i++)
	{
		for (int j = 0; j < (kk + 1); j++)
		{
			for (int k = 0; k < 10; k++)
			{
				dp[i][j][k] = -1;
			}
		}
	}

	for (int i = 0; i < (n + 1); i++)
	{
		for (int j = 0; j < (10); j++)
		{
			change[i][j] = -1;
		}
	}

	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;

		for (int j = 0; j < 10; j++)
		{
			int cnt = 0;
			string change_to = d[j];

			//dbg(change_to);

			for (int k = 0; k < 7; k++)
			{
				//cout << s[k] << " ";
				if ((change_to[k] == '1') && (s[k] == '0'))
				{
					cnt++;
				}
				else if (change_to[k] != s[k])
				{
					cnt = -1;
					break;
				}
			}
			change[i][j] = cnt;
			//pl;
		}
	}

	//dbg(dp);

	//dbg(change);

	if (!knapsack(0, kk, 0))
	{
		cout << -1;
		pl;
		return 0;
	}

	find_ans(0, kk, 0);

	cout << ans;
	pl;

}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector< pair<ll, ll> > vll;

#define rep(n) for(ll i = 0; i < n; i++)
#define fr(i,j,k) for(ll i = j; i < k; i++)
#define FR(i,j,k) for(ll i = j; i >= 0; i--)
#define MOD 1000000007
#define INF 1000000000
#define invec(a, n) for(ll i = 0; i < n; i++) {cin >> a[i];}
#define mp make_pair
#define pb push_back
#define endl '\n'
#define pl cout<<endl;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define ff first
#define ss second
#define pls(x) cout<<x<<' ';
#define pln(x) cout<<x<<"\n";
#define ini(a, i) memset(a, i, sizeof(a))
#define ub(a,x) upper_bound(all(a),x) - a.begin()
#define lb(a,x) lower_bound(all(a),x) - a.begin()

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template<typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)),  __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += n_l; return res; } void dbgm() {;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
const long long INFll = 1ll * INF * INF;

string d[10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int n, kk;
int change[2005][10];
int dp[2005][2005][10];

string ans = "";

bool knapsack(int ind, int tot, int dig)
{
	if (ind == n)
	{
		if (tot == 0)
		{
			return dp[ind][tot][dig] = 1;
		}
		else
		{
			return dp[ind][tot][dig] = 0;
		}
	}

	if (tot < 0) return dp[ind][tot][dig] = 0;

	if (dp[ind][tot][dig] != -1)
		return dp[ind][tot][dig];

	int pos = 0;
	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];

		if (cost != -1 && cost <= tot)
		{
			pos |= knapsack(ind + 1, tot - cost, i);
		}

	}
	return dp[ind][tot][dig] = pos;

}

void find_ans(int ind, int tot, int dig)
{
	if (ind == n)
		return;

	//cout << ind;

	for (int i = 9; i >= 0; i--)
	{
		int cost = change[ind][i];
		if (cost <= tot && cost != -1 && dp[ind + 1][tot - cost][i] == 1)
		{
			ans += (i + 48);
			find_ans(ind + 1, tot - cost, i);

			return;
		}
		//return;
	}
}

int main()
{
	fast();
	//#ifndef ONLINE_JUDGE
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\in.txt","r",stdin);
	//freopen("C:\\Users\\bharath\\Desktop\\Coding\\InOut\\out.txt","w",stdout);
	//#endif

	cin >> n >> kk;

	ini(dp, -1);
	ini(change, -1);

	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;

		for (int j = 0; j < 10; j++)
		{
			int cnt = 0;
			string change_to = d[j];

			//dbg(change_to);

			for (int k = 0; k < 7; k++)
			{
				//cout << s[k] << " ";
				if ((change_to[k] == '1') && (s[k] == '0'))
				{
					cnt++;
				}
				else if (change_to[k] != s[k])
				{
					cnt = -1;
					break;
				}
			}
			change[i][j] = cnt;
			//pl;
		}
	}

	//dbg(dp);

	//dbg(change);

	if (!knapsack(0, kk, 0))
	{
		cout << -1;
		pl;
		return 0;
	}

	find_ans(0, kk, 0);

	cout << ans;
	pl;

}


//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repp(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define perr(i,a,b) for(int i=a;i>b;i--)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-12;
const double PI=acos(-1.0);
const int mod=998244353;

char dig[10][10]=
{
    " 1110111",
    " 0010010",
    " 1011101",
    " 1011011",
    " 0111010",
    " 1101011",
    " 1101111",
    " 1010010",
    " 1111111",
    " 1111011"
};
char mp[2050][10];
int ar[2050][11];//��i�����ֱ��j�Ļ���
char ans[2050];
void solve()
{
    int n,k,d,mn,mnp,mns=0;
    cin>>n>>k;
    rep(i,1,n)
    {
        cin>>(mp[i]+1);
        mn=10;
        rep(j,0,9)
        {
            d=0;
            rep(k,1,7)
            {
                if(mp[i][k]=='0'&&dig[j][k]=='1')
                    d++;
                else if(mp[i][k]=='1'&&dig[j][k]=='0')
                {
                    d=-1;
                    break;
                }
            }
            //cout<<j<<' '<<d<<'\n';
            ar[i][j]=d;
            if(d!=-1&&mn>d)
                mn=d,mnp=j;
        }
        ar[i][10]=mnp; //��С���ѵ�����
        mns+=mn;
    }
    if(mns>k)
    {
        cout<<"-1\n";
        return;
    }
    k-=mns;
    int p=1;
    for(;p<=n&&k>0;p++)
    {
        per(j,9,0)
        {
            if(ar[p][j]!=-1&&k>=ar[p][j]-ar[p][ar[p][10]])
            {
                ans[p]=j+'0';
                k-=ar[p][j]-ar[p][ar[p][10]];
                break;
            }
        }
    }
    for(;p<=n;p++)
    {
        per(j,9,0)
        {
            ans[p]=ar[p][10]+'0';
        }
    }
    ans[n+1]='\0';
    if(k)
    {
        for(int i=n;i>0&&k>0;i--)
        {
            if(ans[i]=='9')
            {
                ans[i]='8';
                k--;
            }
            else if(ans[i]=='7')
            {
                if(k>=3)
                {
                    ans[i]='0';
                    k-=3;
                }
                else if(k==2)
                {
                    ans[i]='3';
                    k-=2;
                }
            }
            else if(ans[i]=='1')
            {
                if(k>=4)
                {
                    ans[i]='0';
                    k-=4;
                }
            }
        }
    }
    if(k)
        cout<<"-1\n";
    else
        cout<<(ans+1)<<'\n';
}
int main()
{
    closeSync;
    //multiCase
    {
        solve();
    }
    return 0;
}


//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repp(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define perr(i,a,b) for(int i=a;i>b;i--)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-12;
const double PI=acos(-1.0);
const int mod=998244353;

char dig[10][10]=
{
    " 1110111",
    " 0010010",
    " 1011101",
    " 1011011",
    " 0111010",
    " 1101011",
    " 1101111",
    " 1010010",
    " 1111111",
    " 1111011"
};
char mp[2050][10];
int ar[2050][11];//��i�����ֱ��j�Ļ���
char ans[2050];
void solve()
{
    int n,k,d,mn,mnp,mns=0;
    cin>>n>>k;
    rep(i,1,n)
    {
        cin>>(mp[i]+1);
        mn=10;
        rep(j,0,9)
        {
            d=0;
            rep(k,1,7)
            {
                if(mp[i][k]=='0'&&dig[j][k]=='1')
                    d++;
                else if(mp[i][k]=='1'&&dig[j][k]=='0')
                {
                    d=-1;
                    break;
                }
            }
            //cout<<j<<' '<<d<<'\n';
            ar[i][j]=d;
            if(d!=-1&&mn>d)
                mn=d,mnp=j;
        }
        ar[i][10]=mnp; //��С���ѵ�����
        mns+=mn;
    }
    if(mns>k)
    {
        cout<<"-1\n";
        return;
    }
    k-=mns;
    int p=1;
    for(;p<=n&&k>0;p++)
    {
        per(j,9,0)
        {
            if(ar[p][j]!=-1&&k>=ar[p][j]-ar[p][ar[p][10]])
            {
                ans[p]=j+'0';
                k-=ar[p][j]-ar[p][ar[p][10]];
                break;
            }
        }
    }
    for(;p<=n;p++)
    {
        ans[p]=ar[p][10]+'0';
    }
    ans[n+1]='\0';
        for(int i=n;i>0&&k>0;i--)
        {
            if(ans[i]=='9')
            {
                ans[i]='8';
                k--;
            }
            else if(ans[i]=='7')
            {
                if(k>=3)
                {
                    ans[i]='0';
                    k-=3;
                }
                else if(k==2)
                {
                    ans[i]='3';
                    k-=2;
                }
            }
            else if(ans[i]=='1')
            {
                if(k>=4)
                {
                    ans[i]='0';
                    k-=4;
                }
            }
        }
    if(k>0)
        cout<<"-1\n";
    else
        cout<<(ans+1)<<'\n';
}
int main()
{
    closeSync;
    //multiCase
    {
        solve();
    }
    return 0;
}


//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repp(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define perr(i,a,b) for(int i=a;i>b;i--)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-12;
const double PI=acos(-1.0);
const int mod=998244353;

char dig[10][10]=
{
    " 1110111",
    " 0010010",
    " 1011101",
    " 1011011",
    " 0111010",
    " 1101011",
    " 1101111",
    " 1010010",
    " 1111111",
    " 1111011"
};
char mp[2050][10];
int ar[2050][11];//第i个数字变成j的花费
char ans[2050];
void solve()
{
    int n,k,d,mn,mnp,mns=0;
    cin>>n>>k;
    rep(i,1,n)
    {
        cin>>(mp[i]+1);
        mn=10;
        rep(j,0,9)
        {
            d=0;
            rep(k,1,7)
            {
                if(mp[i][k]=='0'&&dig[j][k]=='1')
                    d++;
                else if(mp[i][k]=='1'&&dig[j][k]=='0')
                {
                    d=-1;
                    break;
                }
            }
            //cout<<j<<' '<<d<<'\n';
            ar[i][j]=d;
            if(d!=-1&&mn>d)
                mn=d,mnp=j;
        }
        ar[i][10]=mnp; //最小花费的数字
        mns+=mn;
    }
    if(mns>k)
    {
        cout<<"-1\n";
        return;
    }
    k-=mns;
    int p=1;
    for(;p<=n&&k>0;p++)
    {
        per(j,9,0)
        {
            if(ar[p][j]!=-1&&k>=ar[p][j]-ar[p][ar[p][10]])
            {
                ans[p]=j+'0';
                k-=ar[p][j]-ar[p][ar[p][10]];
                break;
            }
        }
    }
    for(;p<=n;p++)
    {
        per(j,9,0)
        {
            ans[p]=ar[p][10]+'0';
        }
    }
    ans[n+1]='\0';
    if(k)
    {
        per(i,n,1)
        {
            if(k==0)
                break;
            if(ans[i]=='9')
            {
                ans[i]='8';
                k++;
            }
        }
    }
    cout<<(ans+1)<<'\n';
}
int main()
{
    closeSync;
    //multiCase
    {
        solve();
    }
    return 0;
}


//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repp(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define perr(i,a,b) for(int i=a;i>b;i--)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-12;
const double PI=acos(-1.0);
const int mod=998244353;

char dig[10][10]=
{
    " 1110111",
    " 0010010",
    " 1011101",
    " 1011011",
    " 0111010",
    " 1101011",
    " 1101111",
    " 1010010",
    " 1111111",
    " 1111011"
};
char mp[2050][10];
int ar[2050][11];//��i�����ֱ��j�Ļ���
char ans[2050];
void solve()
{
    int n,k,d,mn,mnp,mns=0;
    cin>>n>>k;
    rep(i,1,n)
    {
        cin>>(mp[i]+1);
        mn=10;
        rep(j,0,9)
        {
            d=0;
            rep(k,1,7)
            {
                if(mp[i][k]=='0'&&dig[j][k]=='1')
                    d++;
                else if(mp[i][k]=='1'&&dig[j][k]=='0')
                {
                    d=-1;
                    break;
                }
            }
            //cout<<j<<' '<<d<<'\n';
            ar[i][j]=d;
            if(d!=-1&&mn>d)
                mn=d,mnp=j;
        }
        ar[i][10]=mnp; //��С���ѵ�����
        mns+=mn;
    }
    if(mns>k)
    {
        cout<<"-1\n";
        return;
    }
    k-=mns;
    int p=1;
    for(;p<=n&&k>0;p++)
    {
        per(j,9,0)
        {
            if(ar[p][j]!=-1&&k>=ar[p][j]-ar[p][ar[p][10]])
            {
                ans[p]=j+'0';
                k-=ar[p][j]-ar[p][ar[p][10]];
                break;
            }
        }
    }
    for(;p<=n;p++)
    {
        per(j,9,0)
        {
            ans[p]=ar[p][10]+'0';
        }
    }
    ans[n+1]='\0';
    if(k)
    {
        for(int i=n;i>0&&k>0;i--)
        {
            if(ans[i]=='9')
            {
                ans[i]='8';
                k--;
            }
            else if(ans[i]=='7')
            {
                if(k>=4)
                {
                    ans[i]='8';
                    k-=4;
                }
                else if(k==3)
                {
                    ans[i]='9';
                    k-=3;
                }
                else if(k==2)
                {
                    ans[i]='3';
                    k-=2;
                }
                else if(k==1)
                {
                    continue;
                }
            }
            else if(ans[i]=='6')
            {
                ans[i]='8';
                k--;
            }
            else if(ans[i]=='5')
            {
                if(k>=2)
                {
                    ans[i]='8';
                    k-=2;
                }
                else if(k==1)
                {
                    ans[i]='9';
                    k--;
                }
            }
            else if(ans[i]=='4')
            {
                if(k>=3)
                {
                    ans[i]='8';
                    k-=3;
                }
                else if(k==2)
                {
                    ans[i]='9';
                    k-=2;
                }
                else
                    continue;
            }
            else if(ans[i]=='3')
            {
                if(k>=2)
                {
                    ans[i]='8';
                    k-=2;
                }
                else if(k==1)
                {
                    ans[i]='9';
                    k--;
                }
            }
            else if(ans[i]=='2')
            {
                if(k>=2)
                {
                    ans[i]='8';
                    k-=2;
                }
                else
                    continue;
            }
            else if(ans[i]=='1')
            {
                if(k>=5)
                {
                    ans[i]='8';
                    k-=5;
                }
                else if(k==4)
                {
                    ans[i]='9';
                    k-=4;
                }
                else if(k==3)
                {
                    ans[i]='3';
                    k-=3;
                }
                else if(k==2)
                {
                    ans[i]='4';
                    k-=2;
                }
                else if(k==1)
                {
                    ans[i]='7';
                    k--;
                }
            }
            else if(ans[i]=='0')
            {
                ans[i]='8';
                k--;
            }
        }
    }
    if(k)
        cout<<"-1\n";
    else
        cout<<(ans+1)<<'\n';
}
int main()
{
    closeSync;
    //multiCase
    {
        solve();
    }
    return 0;
}


//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#include<unordered_map>
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repp(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define perr(i,a,b) for(int i=a;i>b;i--)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-12;
const double PI=acos(-1.0);
const int mod=998244353;

char dig[10][10]=
{
    " 1110111",
    " 0010010",
    " 1011101",
    " 1011011",
    " 0111010",
    " 1101011",
    " 1101111",
    " 1010010",
    " 1111111",
    " 1111011"
};
char mp[2050][10];
int ar[2050][11];//第i个数字变成j的花费
char ans[2050];
void solve()
{
    int n,k,d,mn,mnp,mns=0;
    cin>>n>>k;
    rep(i,1,n)
    {
        cin>>(mp[i]+1);
        mn=10;
        rep(j,0,9)
        {
            d=0;
            rep(k,1,7)
            {
                if(mp[i][k]=='0'&&dig[j][k]=='1')
                    d++;
                else if(mp[i][k]=='1'&&dig[j][k]=='0')
                {
                    d=-1;
                    break;
                }
            }
            //cout<<j<<' '<<d<<'\n';
            ar[i][j]=d;
            if(d!=-1&&mn>d)
                mn=d,mnp=j;
        }
        ar[i][10]=mnp; //最小花费的数字
        mns+=mn;
    }
    if(mns>k)
    {
        cout<<"-1\n";
        return;
    }
    k-=mns;
    int p=1;
    for(;p<=n&&k>0;p++)
    {
        per(j,9,0)
        {
            if(ar[p][j]!=-1&&k>=ar[p][j]-ar[p][ar[p][10]])
            {
                ans[p]=j+'0';
                k-=ar[p][j]-ar[p][ar[p][10]];
                break;
            }
        }
    }
    for(;p<=n;p++)
    {
        per(j,9,0)
        {
            ans[p]=ar[p][10]+'0';
        }
    }
    ans[n+1]='\0';
    if(k)
    {
        per(i,n,1)
        {
            if(k==0)
                break;
            if(ans[i]=='9')
            {
                ans[i]='8';
                k--;
            }
        }
    }
    cout<<(ans+1)<<'\n';
}
int main()
{
    closeSync;
    //multiCase
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

char num[10][10]=
{
    "1110111",
    "0010010",
    "1011101",
    "1011011",
    "0111010",
    "1101011",
    "1101111",
    "1010010",
    "1111111",
    "1111011"
}; //0~9的数码板样式

char str[10],ans[2050];

int cost[2050][10]; //cost[i][j] 第i个数字变成j的操作数

bool dp[2050][2050]; //dp[i][j] 从后开始往前到第i个数码板，点亮j根能否构造出数字

void solve()
{
    int n,k,d,i,j,u;
    cin>>n>>k;

    for(i=1;i<=n;i++)
    {
        cin>>str;
        for(j=0;j<=9;j++)
        {
            for(u=0;u<7;u++)
            {
                if(str[u]=='0'&&num[j][u]=='1') //如果当前为0但是数字j为1，操作数+1
                    cost[i][j]++;
                else if(str[u]=='1'&&num[j][u]=='0') //如果当前为1但是数字j为0，则无法变成j，置-1
                {
                    cost[i][j]=-1;
                    break;
                }
            }
        }
    }

    dp[n+1][0]=true; //初始化，第n+1位即没有数码板时点亮0根置true
    for(i=n;i;i--) //从后往前处理
    {
        for(j=0;j<=9;j++)
        {
            if(cost[i][j]==-1) //第i个数码板无法变成数字j
                continue;
            for(u=cost[i][j];u<=k;u++)
            {
                dp[i][u]|=dp[i+1][u-cost[i][j]]; //换言之，如果到第i+1位点亮u根能够构造出数字，那么到第i位点亮u+cost[i][j]根也能构造出数字
            }
        }
    }

    if(!dp[1][k]) //全部处理完后如果点亮k根无法构造出数字，则
    {
        cout<<"-1\n";
        return;
    }

    for(i=1;i<=n;i++)
    {
        //bool flag=false;
        for(j=9;j>=0;j--) //贪心，要使答案数字最大，尽量大数位在前
        {
            if(cost[i][j]==-1)
                continue;
            if(k>=cost[i][j]&&dp[i+1][k-cost[i][j]]) //如果k能满足在第i位构造出数字j，且还能支持i+1位以后构造出数字
            {
                ans[i]=j+'0'; //j即为第i位的答案
                k-=cost[i][j];
                //flag=true;
                break;
            }
        }
        /*if(!flag) //这一位无法找到答案，说明给定的k过小
        {
            cout<<"-1\n";
            return;
        }*/
    }

    ans[n+1]='\0';
    cout<<(ans+1)<<'\n';

}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    solve();
    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);

string dp[2][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        string segment;
        cin >> segment;
        if (i == 0) {
            for (int j = 0; j < 10; j++) {
                int add = count(segment, j);
                if (add != -1) {
                    dp[0][add] = (char) (j + '0');
                }
            }
        } else {
            int change[10];
            for (int j = 0; j < 10; j++) {
                change[j] = count(segment, j);
            }
            for (int j = 0; j <= k; j++) {
                if (dp[(i + 1) % 2][j].size() == i + 1) {
                    for (int z = 0; z < 10; z++) {
                        int add = change[z];
                        if (add != -1 && j + add <= k) {
                            dp[i % 2][j + add] = max(dp[i % 2][j + add],
                                                     dp[(i + 1) % 2][j] + (char) (z + '0'));
                        }
                    }
                }
            }
        }
    }
    if (dp[(n - 1) % 2][k].size() == 0) cout << "-1\n";
    else cout << dp[(n - 1) % 2][k];
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);

string dp[2][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        string segment;
        cin >> segment;
        if (i == 0) {
            for (int j = 0; j < 10; j++) {
                int add = count(segment, j);
                if (add != -1) {
                    dp[0][add] = (char) (j + '0');
                }
            }
        } else {
            int change[10];
            for (int j = 0; j < 10; j++) {
                change[j] = count(segment, j);
            }
            for (int j = 0; j <= k; j++) {
                if (dp[(i + 1) % 2][j].size() != 0) {
                    for (int z = 0; z < 10; z++) {
                        int add = change[z];
                        if (add != -1 && j + add <= k) {
                            dp[i % 2][j + add] = max(dp[i % 2][j + add],
                                                     dp[(i + 1) % 2][j] + (char) (z + '0'));
                        }
                    }
                }
                dp[(i + 1) % 2][j] = "";
            }
        }
    }
    if (dp[(n - 1) % 2][k].size() == 0) cout << "-1\n";
    else cout << dp[(n - 1) % 2][k];
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);

bool dp[2005][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int change[2005][10];

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n, k;
    cin >> n >> k;
    string segments[n];
    for (int i = 0; i < n; i++) cin >> segments[i];
    memset(dp, false, sizeof(dp));
    dp[n][0] = true;
    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < 10; j++) {
            change[i][j] = count(segments[i], j);
        }
        for (int j = 0; j <= k; j++) {
            if (dp[i + 1][j]) {
                for (int z = 0; z <= 9; z++) {
                    int add = change[i][z];
                    if (add != -1 && j + add <= k) {
                        dp[i][j + add] = true;
                    }
                }
            }
        }
    }
    if (!dp[0][k]) {
        cout << "-1";
        return 0;
    }
    string ans = "";
    for (int i = 0; i < n; i++) {
        for (int j = 9; j >= 0; j--) {
            int temp = k - change[i][j];
            if (temp >= 0 && dp[i + 1][k - change[i][j]]) {
                k -= change[i][j];
                ans += (char)(j + '0');
                break;
            }
        }
    }

    cout << ans;
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);

bool dp[2005][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int change[2005][10];

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n, k;
    cin >> n >> k;
    string segments[n];
    for (int i = 0; i < n; i++) cin >> segments[i];
    memset(dp, false, sizeof(dp));
    dp[n][0] = true;
    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < 10; j++) {
            change[i][j] = count(segments[i], j);
        }
        for (int j = 0; j <= k; j++) {
            if (dp[i + 1][j]) {
                for (int z = 0; z <= 9; z++) {
                    int add = change[i][z];
                    if (add != -1 && j + add <= k) {
                        dp[i][j + add] = true;
                    }
                }
            }
        }
    }
    if (!dp[0][k]) {
        cout << "-1";
        return 0;
    }
    string ans = "";
    for (int i = 0; i < n; i++) {
        for (int j = 9; j >= 0; j--) {
            if (dp[i + 1][k - change[i][j]]) {
                k -= change[i][j];
                ans += (char)(j + '0');
                break;
            }
        }
    }

    cout << ans;
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);

bool dp[2005][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int change[2005][10];

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n, k;
    cin >> n >> k;
    string segments[n];
    for (int i = 0; i < n; i++) cin >> segments[i];
    memset(dp, false, sizeof(dp));
    dp[n][0] = true;
    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < 10; j++) {
            change[i][j] = count(segments[i], j);
        }
        for (int j = 0; j <= k; j++) {
            if (dp[i + 1][j]) {
                for (int z = 0; z <= 9; z++) {
                    int add = change[i][z];
                    if (add != -1 && j + add <= k) {
                        dp[i][j + add] = true;
                    }
                }
            }
        }
    }
    if (!dp[0][k]) {
        cout << "-1";
        return 0;
    }
    string ans = "";
    for (int i = 0; i < n; i++) {
        for (int j = 9; j >= 0; j--) {
            int add = change[i][j];
            if (add != -1 && dp[i + 1][k - change[i][j]]) {
                k -= change[i][j];
                ans += (char)(j + '0');
                break;
            }
        }
    }

    cout << ans;
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);
ll gcd(ll a, ll b);

string dp[2][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n,k;
    cin>>n>>k;
    for (int i = 0; i < n; i++) {
        string segment;
        cin>>segment;
        if (i == 0) {
            for (int j = 0; j < 10; j++) {
                int add = count(segment, j);
                if (add != -1) {
                    dp[0][add] = (char)(j + '0');
                }
            }
        } else {
            for (int j = 0; j <= k; j++) {
                dp[i % 2][j] = "";
            }
            for (int j = 0; j <= k; j++) {
                if (dp[(i + 1) % 2][j].size() != 0) {
                    for (int z = 0; z < 10; z++) {
                        int add = count(segment, z);
                        if (add != -1 && j + add <= k) {
                            dp[i % 2][j + add] = max(dp[i % 2][j + add],
                                                     dp[(i + 1) % 2][j] + (char)(z + '0'));
                        }
                    }
                }
            }
        }
    }
    if (dp[(n - 1) % 2][k].size() == 0) cout << "-1\n";
    else cout << dp[(n - 1) % 2][k];
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);

string dp[2][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        string segment;
        cin >> segment;
        if (i == 0) {
            for (int j = 0; j < 10; j++) {
                int add = count(segment, j);
                if (add != -1) {
                    dp[0][add] = (char) (j + '0');
                }
            }
        } else {
            for (int j = 0; j <= k; j++) {
                dp[i % 2][j] = "";
            }
            int change[10];
            for (int j = 0; j < 10; j++) {
                change[j] = count(segment, j);
            }
            for (int j = 0; j <= k; j++) {
                if (dp[(i + 1) % 2][j].size() != 0) {
                    for (int z = 0; z < 10; z++) {
                        int add = change[z];
                        if (add != -1 && j + add <= k) {
                            dp[i % 2][j + add] = max(dp[i % 2][j + add],
                                                     dp[(i + 1) % 2][j] + (char) (z + '0'));
                        }
                    }
                }
            }
        }
    }
    if (dp[(n - 1) % 2][k].size() == 0) cout << "-1\n";
    else cout << dp[(n - 1) % 2][k];
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);

string dp[2][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        string segment;
        cin >> segment;
        if (i == 0) {
            for (int j = 0; j < 10; j++) {
                int add = count(segment, j);
                if (add != -1) {
                    dp[0][add] = (char) (j + '0');
                }
            }
        } else {
            int change[10];
            for (int j = 0; j < 10; j++) {
                change[j] = count(segment, j);
            }
            for (int j = 0; j <= k; j++) {
                if (dp[(i + 1) % 2][j].size() == i) {
                    for (int z = 0; z < 10; z++) {
                        int add = change[z];
                        if (add != -1 && j + add <= k) {
                            dp[i % 2][j + add] = max(dp[i % 2][j + add],
                                                     dp[(i + 1) % 2][j] + (char) (z + '0'));
                        }
                    }
                }
            }
        }
    }
    if (dp[(n - 1) % 2][k].size() != n) cout << "-1\n";
    else cout << dp[(n - 1) % 2][k];
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include <bits/stdc++.h>

#define ll long long
#define MAX 300005
#define pp pair<int,pair<int,int>>
#define pb push_back
#define mod 1000000007
using namespace std;

/*
 * author: Kruti_20
 */

ll power(ll x, ll y);

ll gcd(ll a, ll b);

string dp[2][2005];
string code[10];

void initialise() {
    code[0] = "1110111";
    code[1] = "0010010";
    code[2] = "1011101";
    code[3] = "1011011";
    code[4] = "0111010";
    code[5] = "1101011";
    code[6] = "1101111";
    code[7] = "1010010";
    code[8] = "1111111";
    code[9] = "1111011";
}

int count(string segment, int val) {
    int ans = 0;
    for (int i = 0; i < 8; i++) {
        if (code[val][i] == '1') {
            if (segment[i] == '0') ans++;
        } else {
            if (segment[i] == '1') return -1;
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    initialise();
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        string segment;
        cin >> segment;
        if (i == 0) {
            for (int j = 0; j < 10; j++) {
                int add = count(segment, j);
                if (add != -1) {
                    dp[0][add] = (char) (j + '0');
                }
            }
        } else {
            int change[10];
            for (int j = 0; j < 10; j++) {
                change[j] = count(segment, j);
            }
            for (int j = 0; j <= k; j++) {
                if (dp[(i + 1) % 2][j].size() == i) {
                    for (int z = 0; z < 10; z++) {
                        int add = change[z];
                        if (add != -1 && j + add <= k) {
                            dp[i % 2][j + add] = max(dp[i % 2][j + add],
                                                     dp[(i + 1) % 2][j] + (char) (z + '0'));
                        }
                    }
                }
            }
        }
    }
    if (dp[(n - 1) % 2][k].size() == 0) cout << "-1\n";
    else cout << dp[(n - 1) % 2][k];
    return 0;
}

ll power(ll x, ll y) {
    ll res = 1;
    x = x % mod;
    while (y > 0) {
        if (y & 1) res = (res * x) % mod;
        y = y >> 1;
        x = (x * x) % mod;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


#include<bits/stdc++.h>
#include <bitset>
#define ll long long int
#define N 200005
#define endl '\n'
#define speed        ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

using namespace std;

void solve()
{
    int n,k;cin >>n >>k;
    string s[10];
    s[0] = "1110111";
    s[1] = "0010010";
    s[2] = "1011101";
    s[3] = "1011011";
    s[4] = "0111010";
    s[5] = "1101011";
    s[6] = "1101111";
    s[7] = "1010010";
    s[8] = "1111111";
    s[9] = "1111011";
    vector<vector<int>> v(128,vector<int>(8,-1));
    for(int mask=0;mask<128;mask++)
    {
        bitset<7> b(mask);
        string bi = b.to_string();
        for(int i=0;i<10;i++)
        {
            int x=0;
            for(int j=0;j<7;j++)
            {
                if(s[i][j]=='0' and bi[j]=='1')
                {
                    x=-1;
                    break;
                }
                if(s[i][j]=='1' and bi[j]=='0')
                    x++;
            }
            if(x!=-1)
            {
                v[mask][x] = max(v[mask][x],i);
            }
        }
    }
    vector<vector<string>> dp(2001,vector<string>(2001,""));
    //string dp[2005][2005];
    string t;
    cin >>t;
    bitset<7> b(t);
    int mask = b.to_ulong();
    //cout << n <<endl;
    for(int i=0;i<8;i++)
    {
        if(v[mask][i]!=-1)
            dp[1][i] = '0'+v[mask][i];
    }
    for(int i=2;i<=n;i++)
    {
        cin >>t;
        bitset<7> bi(t);
        int mk = bi.to_ulong();
        for(int j=0;j<8;j++)
        {
            if(v[mk][j]==-1)
                continue;
            for(int p=0;p<=2000-j;p++)
            {
                if(dp[i-1][p].empty())
                    continue;
                if(dp[i][p+j].empty())
                    dp[i][p+j] = dp[i-1][p]+char('0'+v[mk][j]);
                else
                    dp[i][p+j] = max(dp[i][p+j],dp[i-1][p]+char('0'+v[mk][j]));
            }
        }
    }
    if(dp[n][k].empty())
        cout <<-1 <<endl;
    else
        cout <<dp[n][k] <<endl;
}

int main()
{
    speed;
    ll t=1;
    //cin >>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


//#pragma GCC         optimize("O3")
#include            <bits/stdc++.h>
#include            <ext/pb_ds/assoc_container.hpp>
#include            <ext/pb_ds/tree_policy.hpp>
#define FastIO      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define pb          push_back
#define mp          make_pair
#define mem(x,i)    memset(x,i,sizeof(x))
#define ff          first
#define ss          second
#define all(x)      x.begin(),x.end()
#define Case(t)     for(int ks=1;ks<=t;ks++)
#define fileout     freopen("output.txt","w",stdout)
#define filein      freopen("input.txt","r",stdin)
#define D(x)        cerr<< __LINE__ <<": "<< #x << " = "<<(x) <<'\n';

using namespace std;
using namespace __gnu_pbds;

using ll  = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;

const int INF    = 0x3f3f3f3f;
const ll LL_INF  = 0x3f3f3f3f3f3f3f3f;
const double PI  = acos(-1);
const double eps = 1e-10;
const int mod    = 1000000007;
const int mx     = 100005;
template <typename T>  using orderedSet =
    tree<T, null_type, less_equal<T>,
    rb_tree_tag, tree_order_statistics_node_update>;//find_by_order,order_of_key

/*------------------------------Graph Moves----------------------------*/
//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

//ll powmod(ll a,ll b){ll res=1;a%=mod; assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
/*----------------------BitMask-----------------------------------------*/
//inline int setBit(int N,int pos){return N=N | (1<<pos);}
//inline int resetBit(int N,int pos){return N= N & ~(1<<pos);}
//inline bool checkBit(int N,int pos){return (bool) (N& (1<<pos));}
/*----------------------------------------------------------------------*/
bool dp[2020][2020];
string nums[]={"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int checkC(string str,int vals)
{
    int cnt=0;
    for(int i=0;i<8;i++)
    {
        if(str[i]=='0' && nums[vals][i]=='1') cnt++;
        else if(str[i]=='1' && nums[vals][i]=='0') return -1;
    }
    return cnt;
}
string ans;
char arr[2020][10];
int n,k;
void call(int pos,int rem,string str)
{
    if(rem<0) return ;
    if(pos==n)
    {
        if(rem==0) ans=max(ans,str);
        return;
    }
    if(dp[pos][rem]) return ;
    for(int i=9;i>=0;i--)
    {
        int val=checkC(arr[pos],i);
        char cs= i+'0';
       // D(cs);D(val);
        if(val!=-1) call(pos+1,rem-val,str+cs);
    }
    dp[pos][rem]=1;
    return ;

}
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>arr[i];
    call(0,k,"");
    if(ans=="") cout<<-1;
    else cout<<ans;
}


#pragma GCC         optimize("O3")
#include            <bits/stdc++.h>
#include            <ext/pb_ds/assoc_container.hpp>
#include            <ext/pb_ds/tree_policy.hpp>
#define FastIO      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define pb          push_back
#define mp          make_pair
#define mem(x,i)    memset(x,i,sizeof(x))
#define ff          first
#define ss          second
#define all(x)      x.begin(),x.end()
#define Case(t)     for(int ks=1;ks<=t;ks++)
#define fileout     freopen("output.txt","w",stdout)
#define filein      freopen("input.txt","r",stdin)
#define D(x)        cerr<< __LINE__ <<": "<< #x << " = "<<(x) <<'\n';

using namespace std;
using namespace __gnu_pbds;

using ll  = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;

const int INF    = 0x3f3f3f3f;
const ll LL_INF  = 0x3f3f3f3f3f3f3f3f;
const double PI  = acos(-1);
const double eps = 1e-10;
const int mod    = 1000000007;
const int mx     = 100005;
template <typename T>  using orderedSet =
    tree<T, null_type, less_equal<T>,
    rb_tree_tag, tree_order_statistics_node_update>;//find_by_order,order_of_key

/*------------------------------Graph Moves----------------------------*/
//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

//ll powmod(ll a,ll b){ll res=1;a%=mod; assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
/*----------------------BitMask-----------------------------------------*/
//inline int setBit(int N,int pos){return N=N | (1<<pos);}
//inline int resetBit(int N,int pos){return N= N & ~(1<<pos);}
//inline bool checkBit(int N,int pos){return (bool) (N& (1<<pos));}
/*----------------------------------------------------------------------*/
bool dp[2020][2020];
string nums[]= {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int checkC(string str,int vals)
{
    int cnt=0;
    for(int i=0; i<8; i++)
    {
        if(str[i]=='0' && nums[vals][i]=='1')
            cnt++;
        else if(str[i]=='1' && nums[vals][i]=='0')
            return -1;
    }
    return cnt;
}
string ans;
char arr[2020][10];
int muve[2020][10];
int n,k;
void call(int pos,int rem)
{
    if(rem<0)
        return ;
    if(pos==n)
    {
        if(rem==0)
        {
            cout<<ans<<endl;
            exit(0);
        }
        return ;
    }
    if(dp[pos][rem])
        return ;
    dp[pos][rem]=1;
    for(int i=9; i>=0; i--)
    {
        int val=muve[pos][i];
        char cs= i+'0';
        ans[pos]=cs;
        if(val!=-1 && rem-val>=0)
            call(pos+1,rem-val);
    }
    return ;

}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0; i<n; i++)
        scanf("%s",arr[i]);
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<10; j++)
        {
            muve[i][j]=checkC(arr[i],j);
        }
    }
    ans.resize(n);
    call(0,k);
    cout<<-1;
}


//#pragma GCC         optimize("O3")
#include            <bits/stdc++.h>
#include            <ext/pb_ds/assoc_container.hpp>
#include            <ext/pb_ds/tree_policy.hpp>
#define FastIO      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define pb          push_back
#define mp          make_pair
#define mem(x,i)    memset(x,i,sizeof(x))
#define ff          first
#define ss          second
#define all(x)      x.begin(),x.end()
#define Case(t)     for(int ks=1;ks<=t;ks++)
#define fileout     freopen("output.txt","w",stdout)
#define filein      freopen("input.txt","r",stdin)
#define D(x)        cerr<< __LINE__ <<": "<< #x << " = "<<(x) <<'\n';

using namespace std;
using namespace __gnu_pbds;

using ll  = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;

const int INF    = 0x3f3f3f3f;
const ll LL_INF  = 0x3f3f3f3f3f3f3f3f;
const double PI  = acos(-1);
const double eps = 1e-10;
const int mod    = 1000000007;
const int mx     = 100005;
template <typename T>  using orderedSet =
    tree<T, null_type, less_equal<T>,
    rb_tree_tag, tree_order_statistics_node_update>;//find_by_order,order_of_key

/*------------------------------Graph Moves----------------------------*/
//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

//ll powmod(ll a,ll b){ll res=1;a%=mod; assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
/*----------------------BitMask-----------------------------------------*/
//inline int setBit(int N,int pos){return N=N | (1<<pos);}
//inline int resetBit(int N,int pos){return N= N & ~(1<<pos);}
//inline bool checkBit(int N,int pos){return (bool) (N& (1<<pos));}
/*----------------------------------------------------------------------*/
bool dp[2020][2020];
string nums[]={"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int checkC(string str,int vals)
{
    int cnt=0;
    for(int i=0;i<8;i++)
    {
        if(str[i]=='0' && nums[vals][i]=='1') cnt++;
        else if(str[i]=='1' && nums[vals][i]=='0') return -1;
    }
    return cnt;
}
vector<string>ans;
char arr[2020][10];
int n,k;
int stp;
void call(int pos,int rem,string str)
{
    stp++;
    if(rem<0) return ;
    if(pos==n)
    {
        if(rem==0) ans.pb(str);
        return;
    }
    if(dp[pos][rem]) return ;
    dp[pos][rem]=1;
    for(int i=9;i>=0;i--)
    {
        int v=ans.size();
        int val=checkC(arr[pos],i);
        char cs= i+'0';
        if(val!=-1 && rem-val>=0) call(pos+1,rem-val,str+cs);
        if(ans.size()>v) break;
    }

    return ;

}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++) scanf("%s",arr[i]);
    call(0,k,"");
   // D(stp);
    if(ans.empty()) cout<<-1;
    else {sort(all(ans));cout<<ans.back();}
}


//#pragma GCC         optimize("O3")
#include            <bits/stdc++.h>
#include            <ext/pb_ds/assoc_container.hpp>
#include            <ext/pb_ds/tree_policy.hpp>
#define FastIO      ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define pb          push_back
#define mp          make_pair
#define mem(x,i)    memset(x,i,sizeof(x))
#define ff          first
#define ss          second
#define all(x)      x.begin(),x.end()
#define Case(t)     for(int ks=1;ks<=t;ks++)
#define fileout     freopen("output.txt","w",stdout)
#define filein      freopen("input.txt","r",stdin)
#define D(x)        cerr<< __LINE__ <<": "<< #x << " = "<<(x) <<'\n';

using namespace std;
using namespace __gnu_pbds;

using ll  = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;

const int INF    = 0x3f3f3f3f;
const ll LL_INF  = 0x3f3f3f3f3f3f3f3f;
const double PI  = acos(-1);
const double eps = 1e-10;
const int mod    = 1000000007;
const int mx     = 100005;
template <typename T>  using orderedSet =
    tree<T, null_type, less_equal<T>,
    rb_tree_tag, tree_order_statistics_node_update>;//find_by_order,order_of_key

/*------------------------------Graph Moves----------------------------*/
//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

//ll powmod(ll a,ll b){ll res=1;a%=mod; assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
/*----------------------BitMask-----------------------------------------*/
//inline int setBit(int N,int pos){return N=N | (1<<pos);}
//inline int resetBit(int N,int pos){return N= N & ~(1<<pos);}
//inline bool checkBit(int N,int pos){return (bool) (N& (1<<pos));}
/*----------------------------------------------------------------------*/
bool dp[2020][2020];
string nums[]={"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int checkC(string str,int vals)
{
    int cnt=0;
    for(int i=0;i<8;i++)
    {
        if(str[i]=='0' && nums[vals][i]=='1') cnt++;
        else if(str[i]=='1' && nums[vals][i]=='0') return -1;
    }
    return cnt;
}
string ans;
char arr[2020][10];
int n,k;
void call(int pos,int rem,string str)
{
    if(rem<0) return ;
    if(pos==n)
    {
        if(rem==0) ans=max(ans,str);
        return;
    }
    if(dp[pos][rem]) return ;
    for(int i=9;i>=0;i--)
    {
        int val=checkC(arr[pos],i);
        char cs= i+'0';
       // D(cs);D(val);
        if(val!=-1) call(pos+1,rem-val,str+cs);
    }
    return ;

}
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>arr[i];
    call(0,k,"");
    if(ans=="") cout<<-1;
    else cout<<ans;
}