#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,string str)
{
    if(rem<0) return ;
    if(pos==n)
    {
        if(rem==0) ans=str;
        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';
        if(val!=-1 && rem-val>=0) call(pos+1,rem-val,str+cs);
        if(ans!="") {cout<<ans<<endl;exit(0);}
    }
    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);
        }
    }
    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;
}
stack<char> ans;
string pest;
char arr[2020][10];
int muve[2020][10];
int n,k;
void call(int pos,int rem,stack<char>str)
{
    if(rem<0)
        return ;
    if(pos==n)
    {
        if(rem==0)
        {
            while(str.size())
            {
                pest+=str.top();
                str.pop();
            }
            reverse(all(pest));
            cout<<pest<<'\n'; 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';
        str.push(cs);
        if(val!=-1 && rem-val>=0)
            call(pos+1,rem-val,str);
        str.pop();
    }
    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);
        }
    }
    stack<char>x;
    call(0,k,x);
    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;
}
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+1);
    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;
void call(int pos,int rem,string str)
{
    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 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.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 muve[2020][10];
int n,k;
void call(int pos,int rem,string str)
{
    if(rem<0) return ;
    if(pos==n)
    {
        if(rem==0) {cout<<str<<'\n';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';
        if(val!=-1 && rem-val>=0) call(pos+1,rem-val,str+cs);
    }
    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);
        }
    }
    call(0,k,"");
    cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mod 1000000007
string r[10];

// order_of_key and *find_by_order
int  ex(string s)
{

    for (int i = 0; i < 10; i++)
    {

        if (s == r[i])
            return i;
    }
    return -1;
}
int cnt0(string s)
{
    int a = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '1')
            a++;
    }
    return a;
}
bool cand(string s, string t)
{
    int a = cnt0(s);
    int b = cnt0(t);
    //cout << a << " " << b << "\n";
    if (b < a)
        return 0;
    for (int i = 0; i < s.length(); i++)
    {
        //cout << s[i] << " " << t[i] << "\n";
        if (s[i] == '1')
        {
            if (t[i] == '0')
                return 0;
        }
    }
    return 1;
}
int32_t main()
{
    fast;
    r[0] = "1110111";
    r[1] = "0010010";
    r[2] = "1011101";
    r[3] = "1011011";
    r[4] = "0111010";
    r[5] = "1101011";
    r[6] = "1101111";
    r[7] = "1010010";
    r[8] = "1111111";
    r[9] = "1111011";
    int n, k;
    cin >> n >> k;
    string s[n];
    int a[10];
    string ac = "";
    int fl = 1;
    a[0] = 6; a[1] = 2; a[2] = 6; a[3] = 5; a[4] = 4; a[5] = 5; a[6] = 6; a[7] = 3; a[8] = 7; a[9] = 6;
    int b[n + 1] = {};
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
        if (ex(s[i]) != -1)
        {
            ac += (char)('0' + ex(s[i]));
        }
        else
        {
            ac += (char)'*';
        }
        b[i] = cnt0(s[i]);
    }
    cout << ac << "\n";
    string ans = "";
    int pre[n + 1] = {};
    for (int i = n - 1; i >= 0; i--)
    {
        pre[i] += pre[i + 1] + b[i];
    }
    int c[n + 1] = {};
    // cout << cand("1010011", "1110111") << "\n";
    for (int i = n - 1; i >= 0; i--)
    {

        if (ac[i] == '*')
        {
            c[i] = 1e18;
            for (int j = 0; j < 10; j++)
            {
                //cout << s[i] << " " << r[j] << "->";
                if (cand(s[i], r[j])) {
                    //cout << s[i] << " " << r[j] << " " << j << " " << cnt0(r[j]) - cnt0(s[i]) << " ";
                    c[i] = min(c[i], cnt0(r[j]) - cnt0(s[i]));
                }
                //cout << "\n";
            }
            // cout << "\n";
        }
        c[i] += c[i + 1];
    }
    for (int i = 0; i < n; i++)
    {
        cout << c[i] << " ";
    }
    cout << "\n";

    int rem = k;
    for (int i = 0; i < n; i++)
    {
        int fl = 0;
        for (int j = 9; j >= 0 ; j--)
        {
            if (cand(s[i], r[j]))
            {
                int noc = cnt0(s[i]);
                int nec = a[j];
                int di = nec - noc;
                int su = 7 * (n - i - 1) - pre[i + 1];
                //cout << noc << " " << nec << " ";
                //cout << di << " " << su << " " << rem << " " << c[i + 1] << "\n";
                if (di <= rem and rem - di >= c[i + 1] and rem - di <= su)
                {
                    rem -= di;
                    fl = 1;
                    ans += (char)('0' + j);
                    break;
                }
            }
        }
        if (fl == 0)
        {
            cout << -1;
            return 0;
        }
    }
    cout << ans;

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mod 1000000007
// order_of_key and *find_by_order
int ans[2001];
string s[2001];
int n, k;
string r[10] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int cnt1(string s)
{
	int a = 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '1')
			a++;
	}
	return a;
}
int dp[2001][2001];
bool vis[2001][2001];
int cand(string s, string t)
{
	if (cnt1(s) > cnt1(t))
		return 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (t[i] == '0')
		{
			if (s[i] == '1')
				return 0;
		}
	}
	return 1;
}
int dos(int i, int rem)
{
	if (i == n )
	{
		if (rem == 0)
			return 0;
		else
			return -1;
	}
	if (rem < 0) {
		vis[i][rem] = 1;
		return dp[i][rem] = -1;
	}
	if (vis[i][rem])
	{
		return dp[i][rem];
	}
	for (int j = 9; j >= 0; j--)
	{
		if (cand(s[i], r[j]))
		{
			//cout << j << " "; cout << cnt1(r[j]) << " " << cnt1(r[i]) << " ";
			//cout << (cnt1(r[j]) - cnt1(r[i])) << "\n ";
			if (dos(i + 1, rem - (cnt1(r[j]) - cnt1(s[i]))) >= 0)
			{
				vis[i][rem] = 1;
				dp[i][rem] = j;
				return dp[i][rem];
			}

		}
	}
	vis[i][rem] = 1;
	return dp[i][rem] = -1;
}
int32_t main()
{
	fast;
	cin >> n >> k;
	for (int i = 0; i < 2001; i++)
	{
		for (int j = 0; j < 2001; j++)
		{
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	int g = dos(0, k);
	if (g == -1)
	{
		cout << -1;
		return 0;

	}
	for (int i = 0; i < n; i++)
	{
		int ans = 0;
		for (int j = 0; j < 2001; j++)
		{
			ans = max(ans, dp[i][j]);
		}
		cout << ans;
	}

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mod 1000000007
// order_of_key and *find_by_order
int ans[2001];
string s[2001];
int n, k;
string r[10] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int cnt1(string s)
{
	int a = 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '1')
			a++;
	}
	return a;
}
int dp[2001][2001];
int cand(string s, string t)
{
	if (cnt1(s) > cnt1(t))
		return 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (t[i] == '0')
		{
			if (s[i] == '1')
				return 0;
		}
	}
	return 1;
}
int dos(int i, int rem)
{
	if (i == n )
	{
		if (rem == 0)
			return 0;
		else
			return -1;
	}
	if (rem < 0)
		return -1;
	if (dp[i][rem] != -1)
		return dp[i][rem];
	for (int j = 9; j >= 0; j--)
	{
		if (cand(s[i], r[j]))
		{
			//cout << j << " "; cout << cnt1(r[j]) << " " << cnt1(r[i]) << " ";
			//cout << (cnt1(r[j]) - cnt1(r[i])) << "\n ";
			if (dos(i + 1, rem - (cnt1(r[j]) - cnt1(s[i]))) != -1)
			{
				dp[i][rem] = j;
				return dp[i][rem];
			}

		}
	}
	return dp[i][rem] = -1;
}
int32_t main()
{
	fast;
	cin >> n >> k;
	for (int i = 0; i < 2001; i++)
	{
		for (int j = 0; j < 2001; j++)
		{
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	int g = dos(0, k);
	if (g == -1)
	{
		cout << -1;
		return 0;

	}
	for (int i = 0; i < n; i++)
	{
		int ans = 0;
		for (int j = 0; j < 2001; j++)
		{
			ans = max(ans, dp[i][j]);
		}
		cout << ans;
	}

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mod 1000000007
string r[10];

// order_of_key and *find_by_order
int  ex(string s)
{

    for (int i = 0; i < 10; i++)
    {

        if (s == r[i])
            return i;
    }
    return -1;
}
int cnt0(string s)
{
    int a = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '1')
            a++;
    }
    return a;
}
bool cand(string s, string t)
{
    int a = cnt0(s);
    int b = cnt0(t);
    //cout << a << " " << b << "\n";
    if (b < a)
        return 0;
    for (int i = 0; i < s.length(); i++)
    {
        //cout << s[i] << " " << t[i] << "\n";
        if (s[i] == '1')
        {
            if (t[i] == '0')
                return 0;
        }
    }
    return 1;
}
int32_t main()
{
    fast;
    r[0] = "1110111";
    r[1] = "0010010";
    r[2] = "1011101";
    r[3] = "1011011";
    r[4] = "0111010";
    r[5] = "1101011";
    r[6] = "1101111";
    r[7] = "1010010";
    r[8] = "1111111";
    r[9] = "1111011";
    int n, k;
    cin >> n >> k;
    string s[n];
    int a[10];
    string ac = "";
    int fl = 1;
    a[0] = 6; a[1] = 2; a[2] = 6; a[3] = 5; a[4] = 4; a[5] = 5; a[6] = 6; a[7] = 3; a[8] = 7; a[9] = 6;
    int b[n + 1] = {};
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
        if (ex(s[i]) != -1)
        {
            ac += (char)('0' + ex(s[i]));
        }
        else
        {
            ac += (char)'*';
        }
        b[i] = cnt0(s[i]);
    }
    // cout << ac << "\n";
    string ans = "";
    int pre[n + 1] = {};
    for (int i = n - 1; i >= 0; i--)
    {
        pre[i] += pre[i + 1] + b[i];
    }
    int c[n + 1] = {};
    // cout << cand("1010011", "1110111") << "\n";
    for (int i = n - 1; i >= 0; i--)
    {

        if (ac[i] == '*')
        {
            c[i] = 1e18;
            for (int j = 0; j < 10; j++)
            {
                //cout << s[i] << " " << r[j] << "->";
                if (cand(s[i], r[j])) {
                    //cout << s[i] << " " << r[j] << " " << j << " " << cnt0(r[j]) - cnt0(s[i]) << " ";
                    c[i] = min(c[i], cnt0(r[j]) - cnt0(s[i]));
                }
                //cout << "\n";
            }
            // cout << "\n";
        }
        c[i] += c[i + 1];
    }
    // for (int i = 0; i < n; i++)
    // {
    //     cout << c[i] << " ";
    // }
    // cout << "\n";

    int rem = k;
    for (int i = 0; i < n; i++)
    {
        int fl = 0;
        for (int j = 9; j >= 0 ; j--)
        {
            if (cand(s[i], r[j]))
            {
                int noc = cnt0(s[i]);
                int nec = a[j];
                int di = nec - noc;
                int su = 7 * (n - i - 1) - pre[i + 1];
                //cout << noc << " " << nec << " ";
                //cout << di << " " << su << " " << rem << " " << c[i + 1] << "\n";
                if (di <= rem and rem - di >= c[i + 1] and rem - di <= su)
                {
                    rem -= di;
                    fl = 1;
                    ans += (char)('0' + j);
                    break;
                }
            }
        }
        if (fl == 0)
        {
            cout << -1;
            return 0;
        }
    }
    cout << ans;

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mod 1000000007
// order_of_key and *find_by_order
int ans[2001];
string s[2001];
int n, k;
string r[10] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int cnt1(string s)
{
	int a = 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '1')
			a++;
	}
	return a;
}
int dp[2001][2001];
bool vis[2001][2001];
int cand(string s, string t)
{
	if (cnt1(s) > cnt1(t))
		return 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (t[i] == '0')
		{
			if (s[i] == '1')
				return 0;
		}
	}
	return 1;
}
vector<int > v[2001];
int dos(int i, int rem)
{
	if (i == n )
	{
		if (rem == 0)
			return 0;
		else
			return -1;
	}
	if (rem < 0) {
		vis[i][rem] = 1;
		return dp[i][rem] = -1;
	}
	if (vis[i][rem])
	{
		return dp[i][rem];
	}
	vis[i][rem] = 1;
	for (int j : v[i])
	{
		if (dos(i + 1, rem - (cnt1(r[j]) - cnt1(s[i]))) >= 0)
		{
			dp[i][rem] = j;
			return dp[i][rem];
		}
	}
	return dp[i][rem] = -1;
}
int32_t main()
{
	fast;
	cin >> n >> k;
	for (int i = 0; i < 2001; i++)
	{
		for (int j = 0; j < 2001; j++)
		{
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
		for (int j = 9; j >= 0; j--)
		{
			if (cand(s[i], r[j]))
			{
				v[i].push_back(j);
			}
		}
	}
	int g = dos(0, k);
	if (g == -1)
	{
		cout << -1;
		return 0;

	}
	for (int i = 0; i < n; i++)
	{
		int ans = 0;
		for (int j = 0; j < 2001; j++)
		{
			ans = max(ans, dp[i][j]);
		}
		cout << ans;
	}

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mod 1000000007
// order_of_key and *find_by_order
int ans[2001];
string s[2001];
int n, k;
string r[10] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int cnt1(string s)
{
	int a = 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '1')
			a++;
	}
	return a;
}
int dp[2001][2001];
int cand(string s, string t)
{
	if (cnt1(s) > cnt1(t))
		return 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (t[i] == '0')
		{
			if (s[i] == '1')
				return 0;
		}
	}
	return 1;
}
int dos(int i, int rem)
{
	if (i == n )
	{
		if (rem == 0)
			return 0;
		else
			return -2;
	}
	if (rem < 0)
		return -1;
	if (dp[i][rem] != -1)
		return dp[i][rem];
	for (int j = 9; j >= 0; j--)
	{
		if (cand(s[i], r[j]))
		{
			//cout << j << " "; cout << cnt1(r[j]) << " " << cnt1(r[i]) << " ";
			//cout << (cnt1(r[j]) - cnt1(r[i])) << "\n ";
			if (dos(i + 1, rem - (cnt1(r[j]) - cnt1(s[i]))) != -2)
			{
				dp[i][rem] = j;
				return dp[i][rem];
			}

		}
	}
	return dp[i][rem] = -2;
}
int32_t main()
{
	fast;
	cin >> n >> k;
	for (int i = 0; i < 2001; i++)
	{
		for (int j = 0; j < 2001; j++)
		{
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	int g = dos(0, k);
	if (g == -1)
	{
		cout << -1;
		return 0;

	}
	for (int i = 0; i < n; i++)
	{
		int ans = 0;
		for (int j = 0; j < 2001; j++)
		{
			ans = max(ans, dp[i][j]);
		}
		cout << ans;
	}

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mod 1000000007
string r[10];

// order_of_key and *find_by_order
int  ex(string s)
{

    for (int i = 0; i < 10; i++)
    {

        if (s == r[i])
            return i;
    }
    return -1;
}
int cnt0(string s)
{
    int a = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '1')
            a++;
    }
    return a;
}
bool cand(string s, string t)
{
    int a = cnt0(s);
    int b = cnt0(t);
    //cout << a << " " << b << "\n";
    if (b < a)
        return 0;
    for (int i = 0; i < s.length(); i++)
    {
        //cout << s[i] << " " << t[i] << "\n";
        if (s[i] == '1')
        {
            if (t[i] == '0')
                return 0;
        }
    }
    return 1;
}
int32_t main()
{
    fast;
    r[0] = "1110111";
    r[1] = "0010010";
    r[2] = "1011101";
    r[3] = "1011011";
    r[4] = "0111010";
    r[5] = "1101011";
    r[6] = "1101111";
    r[7] = "1010010";
    r[8] = "1111111";
    r[9] = "1111011";
    int n, k;
    cin >> n >> k;
    string s[n];
    int a[10];
    string ac = "";
    int fl = 1;
    a[0] = 6; a[1] = 2; a[2] = 6; a[3] = 5; a[4] = 4; a[5] = 5; a[6] = 6; a[7] = 3; a[8] = 7; a[9] = 6;
    int b[n + 1] = {};
    for (int i = 0; i < n; i++)
    {
        cin >> s[i];
        if (ex(s[i]) != -1)
        {
            ac += (char)('0' + ex(s[i]));
        }
        else
        {
            ac += (char)'*';
        }
        b[i] = cnt0(s[i]);
    }
    // cout << ac << "\n";
    string ans = "";
    int pre[n + 1] = {};
    for (int i = n - 1; i >= 0; i--)
    {
        pre[i] += pre[i + 1] + b[i];
    }
    int c[n + 1] = {};
    // cout << cand("1010011", "1110111") << "\n";
    for (int i = n - 1; i >= 0; i--)
    {

        if (ac[i] == '*')
        {
            c[i] = 1e18;
            for (int j = 0; j < 10; j++)
            {
                //cout << s[i] << " " << r[j] << "->";
                if (cand(s[i], r[j])) {
                    //cout << s[i] << " " << r[j] << " " << j << " " << cnt0(r[j]) - cnt0(s[i]) << " ";
                    c[i] = min(c[i], cnt0(r[j]) - cnt0(s[i]));
                }
                //cout << "\n";
            }
            // cout << "\n";
        }
        c[i] += c[i + 1];
    }
    for (int i = 0; i < n; i++)
    {
        cout << c[i] << " ";
    }
    cout << "\n";

    int rem = k;
    for (int i = 0; i < n; i++)
    {
        int fl = 0;
        for (int j = 9; j >= 0 ; j--)
        {
            if (cand(s[i], r[j]))
            {
                int noc = cnt0(s[i]);
                int nec = a[j];
                int di = nec - noc;
                int su = 7 * (n - i - 1) - pre[i + 1];
                //cout << noc << " " << nec << " ";
                //cout << di << " " << su << " " << rem << " " << c[i + 1] << "\n";
                if (di <= rem and rem - di >= c[i + 1] and rem - di <= su)
                {
                    rem -= di;
                    fl = 1;
                    ans += (char)('0' + j);
                    break;
                }
            }
        }
        if (fl == 0)
        {
            cout << -1;
            return 0;
        }
    }
    cout << ans;

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mod 1000000007
// order_of_key and *find_by_order
int ans[2001];
string s[2001];
int n, k;
string r[10] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
int cnt1(string s)
{
	int a = 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '1')
			a++;
	}
	return a;
}
int dp[2001][2001];
int cand(string s, string t)
{
	if (cnt1(s) > cnt1(t))
		return 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (t[i] == '0')
		{
			if (s[i] == '1')
				return 0;
		}
	}
	return 1;
}
int dos(int i, int rem)
{
	if (i == n )
	{
		if (rem == 0)
			return 0;
		else
			return -2;
	}
	if (rem < 0)
		return -2;
	if (dp[i][rem] != -1)
		return dp[i][rem];
	for (int j = 9; j >= 0; j--)
	{
		if (cand(s[i], r[j]))
		{
			//cout << j << " "; cout << cnt1(r[j]) << " " << cnt1(r[i]) << " ";
			//cout << (cnt1(r[j]) - cnt1(r[i])) << "\n ";
			if (dos(i + 1, rem - (cnt1(r[j]) - cnt1(s[i]))) >= 0)
			{
				dp[i][rem] = j;
				return dp[i][rem];
			}

		}
	}
	return dp[i][rem] = -2;
}
int32_t main()
{
	fast;
	cin >> n >> k;
	for (int i = 0; i < 2001; i++)
	{
		for (int j = 0; j < 2001; j++)
		{
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	int g = dos(0, k);
	if (g == -2)
	{
		cout << -1;
		return 0;

	}
	for (int i = 0; i < n; i++)
	{
		int ans = 0;
		for (int j = 0; j < 2001; j++)
		{
			ans = max(ans, dp[i][j]);
		}
		cout << ans;
	}

}


/*input
2 5
0010010
0010010
*/
#include<bits/stdc++.h>
using namespace std;

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
vector<vector<pair<int,int>>> vcc;
int n,k;
string ans;
// vector<vector<bool>> dp;
bool dp[2001][2001][12];

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

void find2(int idx,int cost,int x,string &curr){
	if(idx==n){
		if(cost==k){
			ans = max(ans,curr);
			return;
		}
		return;
	}
	if(dp[idx][cost][x]) return;
	for(auto it: vcc[idx]){
		// cout << it.X << " " << it.second << "--\n";
		if(cost+it.Y<=k){
			curr.push_back(it.X+'0');
			int new_c = cost+it.Y;
			find2(idx+1,new_c,it.X,curr);
			curr.pop_back();
		}
	}
	dp[idx][cost][x] = 1;
}

// bool comp(pair<int,int> a, pair<int,int> b){
// 	if(a.Y==b.Y)
// 		return a.X>b.X;
// 	return a.Y<b.Y;
// }

void solve(){

	cin >> n >> k;
	vcc.resize(n);
	string temp;
	ans = "";
	int cost;
	// dp.resize(n+1,vector<bool>(k+1,false));
	memset(dp,false,sizeof dp);
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			cost = find(temp,j);
			if(cost!=-1){
				vcc[i].push_back({j,cost});
			}
		}
		// sort(all(vcc[i]),comp);
	}
	string curr = "";
	find2(0,0,-1,curr);
	if(ans.size()==0)
		ans = "-1";
	cout << ans << "\n";

}

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


/*input
2 5
0010010
0010010

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

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
vector<vector<pair<int,int>>> vcc;
int n,k;
string ans;
bool dp[3001][3001][15];

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

string maxi(string &a, string &b){
	for(int i=0;i<a.size();i++){
		if(a[i]>b[i])
			return a;
	}
	return a;
}

void find2(int idx,int cost,int x,string curr){
	if(idx==n){
		if(cost==k){
			ans = max(ans,curr);
			return;
		}
		return;
	}
	if(dp[idx][cost][x]) return;
	for(auto it: vcc[idx]){
		if(cost+it.Y<=k){
			curr.push_back(it.X+'0');
			int new_c = cost+it.Y;
			find2(idx+1,new_c,it.X,curr);
			curr.pop_back();
		}
	}
	dp[idx][cost][x] = 1;
}

void solve(){

	cin >> n >> k;
	vcc = vector<vector<pair<int,int>>>(n);
	string temp;
	ans = "";
	int cost;
	memset(dp,false,sizeof dp);
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			cost = find(temp,j);
			if(cost!=-1){
				vcc[i].push_back({j,cost});
			}
		}
		// sort(all(vcc[i]),comp);
	}
	// for(int i=0;i<n;i++){
	// 	for(auto it: vcc[i])
	// 		cout << it.X << " " << it.Y << "\n";
	// 	cout << "--\n";
	// }
	string curr = "";
	find2(0,0,11,curr);
	if(ans.size()==0)
		ans = "-1";
	cout << ans << "\n";

}

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


/*input
2 5
0010010
0010010

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

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
vector<vector<pair<int,int>>> vcc;
int n,k;
string ans;
bool dp[3001][3001][15];

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

string maxi(string &a, string &b){
	for(int i=0;i<a.size();i++){
		if(a[i]>b[i])
			return a;
	}
	return a;
}

void find2(int idx,int cost,int x,string curr){
	if(idx==n){
		if(cost==k){
			ans = max(ans,curr);
			return;
		}
		return;
	}
	if(dp[idx][k-cost][x]) return;
	for(auto it: vcc[idx]){
		if((cost+it.Y)<=k){
			curr.push_back(it.X+'0');
			int new_c = cost+it.Y;
			find2(idx+1,new_c,it.X,curr);
			curr.pop_back();
		}
	}
	dp[idx][k-cost][x] = 1;
}

bool comp(pair<int,int> a, pair<int,int> b){
	if(a.Y==b.Y)
		return a.X>b.X;
	return a.Y<b.Y;
}

void solve(){

	cin >> n >> k;
	vcc = vector<vector<pair<int,int>>>(n);
	string temp;
	ans = "";
	int cost;
	memset(dp,false,sizeof dp);
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			cost = find(temp,j);
			if(cost!=-1){
				vcc[i].push_back({j,cost});
			}
		}
		sort(all(vcc[i]),comp);
	}
	// for(int i=0;i<n;i++){
	// 	for(auto it: vcc[i])
	// 		cout << it.X << " " << it.Y << "\n";
	// 	cout << "--\n";
	// }
	string curr = "";
	find2(0,0,11,curr);
	if(ans.size()==0)
		ans = "-1";
	cout << ans << "\n";

}

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


/*input
2 5
0010010
0010010
*/
#include<bits/stdc++.h>
using namespace std;

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
vector<vector<pair<int,int>>> vcc;
int n,k;
string ans;
// vector<vector<bool>> dp;
bool dp[1001][2001][11];

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

void find2(int idx,int cost,int x,string &curr){
	if(idx==n){
		if(cost==k){
			ans = max(ans,curr);
			return;
		}
		return;
	}
	if(dp[idx][cost][x]) return;
	for(auto it: vcc[idx]){
		// cout << it.X << " " << it.second << "--\n";
		if(cost+it.Y<=k){
			curr.push_back(it.X+'0');
			int new_c = cost+it.Y;
			find2(idx+1,new_c,it.X,curr);
			curr.pop_back();
		}
	}
	dp[idx][cost][x] = 1;
}

bool comp(pair<int,int> a, pair<int,int> b){
	if(a.Y==b.Y)
		return a.X>b.X;
	return a.Y<b.Y;
}

void solve(){

	cin >> n >> k;
	vcc.resize(n);
	string temp;
	ans = "";
	int cost;
	// dp.resize(n+1,vector<bool>(k+1,false));
	memset(dp,false,sizeof dp);
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			cost = find(temp,j);
			if(cost!=-1){
				vcc[i].push_back({j,cost});
			}
		}
		// sort(all(vcc[i]),comp);
	}
	string curr = "";
	find2(0,0,-1,curr);
	if(ans.size()==0)
		ans = "-1";
	cout << ans << "\n";

}

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


/*input
3 5
0100001
1001001
1010011
*/
#include<bits/stdc++.h>
using namespace std;

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
int vcc[2001][10];
int n,k;
int dp[2001][2001][10];

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

int make_path(int idx,int sum,int num){
	if(idx==n){
		if(sum==0){
			// cout << cost << " " << idx << " " << x << "\n";
			return dp[idx][sum][num] = 1;
		}
		return dp[idx][sum][num] = 0;
	}
	if(dp[idx][sum][num] != -1) return dp[idx][sum][num];
	int ans = 0;
	for(int i=9;i>=0;i--){
		if(vcc[idx][i]!=-1 && vcc[idx][i]<=sum){
			ans = ans | make_path(idx+1,sum-vcc[idx][i],i);
		}
	}
	// cout << idx << " " << cost << " " << x << "\n";

	return dp[idx][sum][num] = ans;
}
void make_it(string &ans, int idx, int sum,int digit){
	// if(cost>k) return;
	if(idx==n) return;
	for(int i=9;i>=0;i--){
		if(vcc[idx][i]!=-1 && dp[idx+1][sum-vcc[idx][i]][i] && sum-vcc[idx][i]>=0){
			ans.push_back(i+'0');
			make_it(ans,idx+1,sum-vcc[idx][i],i);
			return;
		}
	}
}

void solve(){

	cin >> n >> k;
	string temp;
	memset(dp,-1,sizeof dp);
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			vcc[i][j] = find(temp,j);
			// cout << i << " " << j << " " << vcc[i][j] << "\n";
		}
	}

	make_path(0,k,0);
	// cout << dp[0][0][0] << "\n";

	string ans = "";

	make_it(ans,0,k,0);

	if(!ans.size())
		cout << "-1";
	else
		cout << ans;

}

int main(){
	FIN;
   	solve();
}


/*input
1 7
0000000
*/
#include<bits/stdc++.h>
using namespace std;

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
vector<vector<pair<int,int>>> vcc;
int n,k;
string ans;
vector<vector<bool>> dp;

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

void find2(int idx,int cost,string &curr){
	if(idx==n){

		if(cost==k){
			// cout << idx << " " << cost << " "  << curr<<  "\n";
			ans = max(ans,curr);
			return;
		}
		return;
	}
	if(dp[idx][k-cost]) return;
	for(auto it: vcc[idx]){
		// cout << it.X << " " << it.second << "--\n";
		if(cost+it.Y<=k){
			curr.push_back(it.X+'0');
			int new_c = cost+it.Y;
			// cout << idx+1 << " " << new_c << " " << curr << "\n";
			find2(idx+1,new_c,curr);
			curr.pop_back();
		}
	}
	dp[idx][k-cost] = 1;
}

bool comp(pair<int,int> a, pair<int,int> b){
	if(a.Y==b.Y)
		return a.X>b.X;
	return a.Y<b.Y;
}

void solve(){

	cin >> n >> k;
	vcc.resize(n);
	string temp;
	ans = "";
	int cost;
	dp.resize(n,vector<bool>(k,false));
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			cost = find(temp,j);
			if(cost!=-1){
				vcc[i].push_back({j,cost});
			}
		}
		sort(all(vcc[i]),comp);
	}
	// for(int i=0;i<n;i++){
	// 	for(auto it: vcc[i])
	// 		cout << it.X << " " << it.Y << "\n";
	// 	cout << "---\n";
	// }
	string curr = "";
	find2(0,0,curr);
	if(ans.size()==0)
		ans = "-1";
	cout << ans << "\n";

}

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


/*input
2 5
0010010
0010010
*/
#include<bits/stdc++.h>
using namespace std;

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
vector<vector<pair<int,int>>> vcc;
int n,k;
string ans;
vector<vector<bool>> dp;

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

void find2(int idx,int cost,string &curr){
	if(idx==n){

		if(cost==k){
			// cout << idx << " " << cost << " "  << curr<<  "\n";
			ans = max(ans,curr);
			return;
		}
		return;
	}
	if(dp[idx][cost]) return;
	for(auto it: vcc[idx]){
		// cout << it.X << " " << it.second << "--\n";
		if(cost+it.Y<=k){
			curr.push_back(it.X+'0');
			int new_c = cost+it.Y;
			// cout << idx+1 << " " << new_c << " " << curr << "\n";
			find2(idx+1,new_c,curr);
			curr.pop_back();
		}
	}
	dp[idx][cost] = 1;
}

bool comp(pair<int,int> a, pair<int,int> b){
	if(a.Y==b.Y)
		return a.X>b.X;
	return a.Y<b.Y;
}

void solve(){

	cin >> n >> k;
	vcc.resize(n);
	string temp;
	ans = "";
	int cost;
	dp.resize(n,vector<bool>(k,false));
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			cost = find(temp,j);
			if(cost!=-1){
				vcc[i].push_back({j,cost});
			}
		}
		sort(all(vcc[i]),comp);
	}
	string curr = "";
	find2(0,0,curr);
	if(ans.size()==0)
		ans = "-1";
	cout << ans << "\n";

}

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


/*input
2 5
0010010
0010010

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

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
vector<vector<pair<int,int>>> vcc;
int n,k;
string ans;
bool dp[3001][3001][15];

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

string maxi(string &a, string &b){
	for(int i=0;i<a.size();i++){
		if(a[i]>b[i])
			return a;
	}
	return a;
}

void find2(int idx,int cost,int x,string curr){
	if(idx==n){
		if(cost==k){
			ans = max(ans,curr);
			return;
		}
		return;
	}
	if(dp[idx][k-cost][x]) return;
	for(auto it: vcc[idx]){
		if((cost+it.Y)<=k){
			curr.push_back(it.X+'0');
			int new_c = cost+it.Y;
			find2(idx+1,new_c,it.X,curr);
			curr.pop_back();
		}
	}
	dp[idx][k-cost][x] = 1;
}

void solve(){

	cin >> n >> k;
	vcc = vector<vector<pair<int,int>>>(n);
	string temp;
	ans = "";
	int cost;
	memset(dp,false,sizeof dp);
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			cost = find(temp,j);
			if(cost!=-1){
				vcc[i].push_back({j,cost});
			}
		}
		// sort(all(vcc[i]),comp);
	}
	// for(int i=0;i<n;i++){
	// 	for(auto it: vcc[i])
	// 		cout << it.X << " " << it.Y << "\n";
	// 	cout << "--\n";
	// }
	string curr = "";
	find2(0,0,11,curr);
	if(ans.size()==0)
		ans = "-1";
	cout << ans << "\n";

}

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


/*input
3 5
0100001
1001001
1010011
*/
#include<bits/stdc++.h>
using namespace std;

#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define X first
#define Y second
#define pb push_back
#define all(s) s.begin(),s.end()

typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9+7;

vector<string> seg = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };
vector<vector<pair<int,int>>> vcc;
int n,k;
string ans;
// vector<vector<bool>> dp;

int find(string temp, int val){
	int cost = 0;
	string tobe = seg[val];
	for(int i=0;i<7;i++){
		if(tobe[i]=='0' && temp[i]=='1')
			return -1;
		if(tobe[i]=='1' && temp[i]=='0')
			cost++;
	}
	return cost;
}

void find2(int idx,int cost,string &curr){
	if(idx==n){

		if(cost==k){
			// cout << idx << " " << cost << " "  << curr<<  "\n";
			ans = max(ans,curr);
			return;
		}
		return;
	}
	// if(dp[idx][cost]) return;
	for(auto it: vcc[idx]){
		// cout << it.X << " " << it.second << "--\n";
		if(cost+it.Y<=k){
			curr.push_back(it.X+'0');
			int new_c = cost+it.Y;
			// cout << idx+1 << " " << new_c << " " << curr << "\n";
			find2(idx+1,new_c,curr);
			curr.pop_back();
		}
	}
	// dp[idx][cost] = 1;
}

void solve(){

	cin >> n >> k;
	vcc.resize(n);
	string temp;
	ans = "";
	int cost;
	// dp.resize(n,vector<bool>(k,false));
	for(int i=0;i<n;i++){
		cin >> temp;
		for(int j=0;j<10;j++){
			cost = find(temp,j);
			if(cost!=-1){
				vcc[i].push_back({j,cost});
			}
		}
	}
	// for(int i=0;i<n;i++){
	// 	for(auto it: vcc[i])
	// 		cout << it.X << " " << it.Y << "\n";
	// 	cout << "---\n";
	// }
	string curr = "";
	find2(0,0,curr);
	if(ans.size()==0)
		ans = "-1";
	cout << ans << "\n";

}

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


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

typedef long long int ll;
typedef long double ld;

const int INF = 1000000009;

vector<string> _a = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
set<int> a;
vector<int> b;
map<int,int> m;

const int N = 2005;

bool dp[N][N];

void solve(){
  int n, k;
  cin >> n >> k;

  for(int i = 0; i < (int)_a.size(); i++){
    int x = 0;
    for(int j = 0; j < (int)_a[i].size(); j++)
      x |= ((_a[i][j]-'0') << j);
    a.insert(x);
    b.push_back(x);
    m[x] = i;
  }

  vector<int> s(n);
  for(int i = 0; i < n; i++){
    string x;
    cin >> x;
    for(int j = 0; j < 7; j++)
      s[i] |= ((x[j]-'0')<<j);
  }

  for(int j = 0; j <= k; j++){
    for(int l : b){
      if(__builtin_popcount(l^s[0]) == j)
        dp[0][j] = true;
    }
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j <= k; j++){
      for(int l : b){
        if(__builtin_popcount(l^s[i]) <= j)
          dp[i][j] |= dp[i-1][j-__builtin_popcount(l^s[i])];
      }
    }
  }
  string num;
  int x = n-1, y = k;

  if(!dp[n-1][k]){
    cout << "-1\n";
    return;
  }

  while(x > 0 && y > 0){
    int mx = -1, lx = -1;
    for(int l : b){
      if(__builtin_popcount(l^s[x]) <= y &&
         dp[x-1][y-__builtin_popcount(l^s[x])] &&
        mx < m[l])
          mx = m[l], lx = l;
    }
    x--, y -= __builtin_popcount(lx^s[x]);
    // cout << mx << "\n";
    num += mx+48;
  }
  int mx = -1;
  for(int l : b){
    if(__builtin_popcount(l^s[x]) == y && mx < m[l])
      mx = m[l];
  }
  num += mx+48;

  sort(num.rbegin(), num.rend());
  cout << num << "\n";
}

int main(){
#ifndef LOCAL
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#endif
  // int t;cin >> t;while(t--) solve();
  solve();
  cerr << "\nExecution time: " << (double)clock()/CLOCKS_PER_SEC << "\n";
}


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

typedef long long int ll;
typedef long double ld;

const int INF = 1000000009;

vector<string> _a = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
vector<int> b;
map<int,int> m;

const int N = 2005;

bool dp[N][N];

bool can(int x, int y){ // x -> y
  for(int i = 0; i < 7; i++)
    if((x&(1<<i)) && (!(y&(1<<i)))) return false;
  return true;
}

void solve(){
  int n, k;
  cin >> n >> k;

  for(int i = 0; i < (int)_a.size(); i++){
    int x = 0;
    for(int j = 0; j < 7; j++)
      x |= ((_a[i][j]-'0') << j);
    b.push_back(x);
    m[x] = i;
  }

  vector<int> s(n);
  for(int i = 0; i < n; i++){
    string x;
    cin >> x;
    for(int j = 0; j < 7; j++)
      s[i] |= ((x[j]-'0')<<j);
  }
  reverse(s.begin(), s.end());

  for(int j = 0; j <= k; j++){
    for(int l : b){
      if(can(s[0], l) &&__builtin_popcount(l^s[0]) == j)
        dp[0][j] = true;
    }
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j <= k; j++){
      for(int l : b){
        if(can(s[i], l) && __builtin_popcount(l^s[i]) <= j)
          dp[i][j] |= dp[i-1][j-__builtin_popcount(l^s[i])];
      }
    }
  }
  if(!dp[n-1][k]){
    cout << "-1\n";
    return;
  }

  string num;
  int x = n-1, y = k;

  while(x > 0 && y >= 0){
    int lx = -1;
    for(int l : b){
      if(__builtin_popcount(l^s[x]) <= y &&
         dp[x-1][y-__builtin_popcount(l^s[x])] &&
        (lx == -1 || m[lx] < m[l]))
          lx = l;
    }
    y -= __builtin_popcount(lx^s[x]);
    x--;
    // cout << mx << "\n";
    num += char(m[lx]+48);
  }
  // cout << "y = " << y << "\n";
  int mx = -1;
  for(int l : b){
    if(can(s[0], l) && __builtin_popcount(l^s[0]) == y && mx < m[l])
      mx = m[l];
  }
  num += char(mx+48);

  cout << num << "\n";
}

int main(){
#ifndef LOCAL
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#endif
  // int t;cin >> t;while(t--) solve();
  solve();
  cerr << "\nExecution time: " << (double)clock()/CLOCKS_PER_SEC << "\n";
}


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

typedef long long int ll;
typedef long double ld;

const int INF = 1000000009;

vector<string> _a = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
set<int> a;
vector<int> b;
map<int,int> m;

const int N = 2005;

bool dp[N][N];

void solve(){
  int n, k;
  cin >> n >> k;

  for(int i = 0; i < (int)_a.size(); i++){
    int x = 0;
    for(int j = 0; j < (int)_a[i].size(); j++)
      x |= ((_a[i][j]-'0') << j);
    a.insert(x);
    b.push_back(x);
    m[x] = i;
  }

  vector<int> s(n);
  for(int i = 0; i < n; i++){
    string x;
    cin >> x;
    for(int j = 0; j < 7; j++)
      s[i] |= ((x[j]-'0')<<j);
  }
  reverse(s.begin(), s.end());

  for(int j = 0; j <= k; j++){
    for(int l : b){
      if(__builtin_popcount(l^s[0]) == j)
        dp[0][j] = true;
    }
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j <= k; j++){
      for(int l : b){
        if(__builtin_popcount(l^s[i]) <= j)
          dp[i][j] |= dp[i-1][j-__builtin_popcount(l^s[i])];
      }
    }
  }
  string num;
  int x = n-1, y = k;
/*
  for(int j = 0; j < 7; j++){
    for(int i = 0; i < n; i++)
      cout << dp[i][j] << " ";
    cout << "\n";
  }
*/
  if(!dp[n-1][k]){
    cout << "-1\n";
    return;
  }

  while(x > 0 && y >= 0){
    int lx = -1;
    for(int l : b){
      if(__builtin_popcount(l^s[x]) <= y &&
         dp[x-1][y-__builtin_popcount(l^s[x])] &&
        m[lx] < m[l])
          lx = l;
    }
    // cout << "lx = " << lx << " " << m[lx] << " " << "\n";
    y -= __builtin_popcount(lx^s[x]);
    x--;
    // cout << mx << "\n";
    num += m[lx]+48;
  }
  // cout << "y = " << y << "\n";
  int mx = -1;
  for(int l : b){
    if(__builtin_popcount(l^s[x]) == y && mx < m[l])
      mx = m[l];
  }
  num += mx+48;

  cout << num << "\n";
}

int main(){
#ifndef LOCAL
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#endif
  // int t;cin >> t;while(t--) solve();
  solve();
  cerr << "\nExecution time: " << (double)clock()/CLOCKS_PER_SEC << "\n";
}


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

typedef long long int ll;
typedef long double ld;

const int INF = 1000000009;

vector<string> _a = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
vector<int> b;
map<int,int> m;

const int N = 2005;

bool dp[N][N];

bool can(int x, int y){ // x -> y
  for(int i = 0; i < 7; i++)
    if((x&(1<<i)) && !(y&(1<<i))) return false;
  return true;
}

void solve(){
  int n, k;
  cin >> n >> k;

  for(int i = 0; i < (int)_a.size(); i++){
    int x = 0;
    for(int j = 0; j < 7; j++)
      x |= ((_a[i][j]-'0') << j);
    b.push_back(x);
    m[x] = i;
  }

  vector<int> s(n);
  for(int i = 0; i < n; i++){
    string x;
    cin >> x;
    for(int j = 0; j < 7; j++)
      s[i] |= ((x[j]-'0')<<j);
  }
  reverse(s.begin(), s.end());

  for(int j = 0; j <= k; j++){
    for(int l : b){
      if(can(s[0], l) &&__builtin_popcount(l^s[0]) == j)
        dp[0][j] = true;
    }
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j <= k; j++){
      for(int l : b){
        if(can(s[i], l) && __builtin_popcount(l^s[i]) <= j)
          dp[i][j] |= dp[i-1][j-__builtin_popcount(l^s[i])];
      }
    }
  }
  if(!dp[n-1][k]){
    cout << "-1\n";
    return;
  }

  string num;
  int x = n-1, y = k;

  while(x > 0 && y >= 0){
    int lx = -1;
    for(int l : b){
      if(__builtin_popcount(l^s[x]) <= y &&
         dp[x-1][y-__builtin_popcount(l^s[x])] &&
        (lx == -1 || m[lx] < m[l]))
          lx = l;
    }
    y -= __builtin_popcount(lx^s[x]);
    x--;
    // cout << mx << "\n";
    num += char(m[lx]+48);
  }
  // cout << "y = " << y << "\n";
  int mx = -1;
  for(int l : b){
    if(__builtin_popcount(l^s[0]) == y && mx < m[l])
      mx = m[l];
  }
  num += char(mx+48);

  cout << num << "\n";
}

int main(){
#ifndef LOCAL
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#endif
  // int t;cin >> t;while(t--) solve();
  solve();
  cerr << "\nExecution time: " << (double)clock()/CLOCKS_PER_SEC << "\n";
}


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

typedef long long int ll;
typedef long double ld;

const int INF = 1000000009;

vector<string> _a = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
vector<int> b;
map<int,int> m;

const int N = 2005;

bool dp[N][N];

void solve(){
  int n, k;
  cin >> n >> k;

  for(int i = 0; i < (int)_a.size(); i++){
    int x = 0;
    for(int j = 0; j < 7; j++)
      x |= ((_a[i][j]-'0') << j);
    b.push_back(x);
    m[x] = i;
  }

  vector<int> s(n);
  for(int i = 0; i < n; i++){
    string x;
    cin >> x;
    for(int j = 0; j < 7; j++)
      s[i] |= ((x[j]-'0')<<j);
  }
  reverse(s.begin(), s.end());

  for(int j = 0; j <= k; j++){
    for(int l : b){
      if(__builtin_popcount(l^s[0]) == j)
        dp[0][j] = true;
    }
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j <= k; j++){
      for(int l : b){
        if(__builtin_popcount(l^s[i]) <= j)
          dp[i][j] |= dp[i-1][j-__builtin_popcount(l^s[i])];
      }
    }
  }
  if(!dp[n-1][k]){
    cout << "-1\n";
    return;
  }

  string num;
  int x = n-1, y = k;

  while(x > 0 && y >= 0){
    int lx = -1;
    for(int l : b){
      if(__builtin_popcount(l^s[x]) <= y &&
         dp[x-1][y-__builtin_popcount(l^s[x])] &&
        m[lx] < m[l])
          lx = l;
    }
    y -= __builtin_popcount(lx^s[x]);
    x--;
    // cout << mx << "\n";
    num += char(m[lx]+48);
  }
  // cout << "y = " << y << "\n";
  int mx = -1;
  for(int l : b){
    if(__builtin_popcount(l^s[0]) == y && mx < m[l])
      mx = m[l];
  }
  num += char(mx+48);

  cout << num << "\n";
}

int main(){
#ifndef LOCAL
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#endif
  // int t;cin >> t;while(t--) solve();
  solve();
  cerr << "\nExecution time: " << (double)clock()/CLOCKS_PER_SEC << "\n";
}


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

typedef long long int ll;
typedef long double ld;

const int INF = 1000000009;

vector<string> _a = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
set<int> a;
vector<int> b;
map<int,int> m;

const int N = 2005;

bool dp[N][N];

void solve(){
  int n, k;
  cin >> n >> k;

  for(int i = 0; i < (int)_a.size(); i++){
    int x = 0;
    for(int j = 0; j < (int)_a[i].size(); j++)
      x |= ((_a[i][j]-'0') << j);
    a.insert(x);
    b.push_back(x);
    m[x] = i;
  }

  vector<int> s(n);
  for(int i = 0; i < n; i++){
    string x;
    cin >> x;
    for(int j = 0; j < 7; j++)
      s[i] |= ((x[j]-'0')<<j);
  }

  for(int j = 0; j <= k; j++){
    for(int l : b){
      if(__builtin_popcount(l^s[0]) == j)
        dp[0][j] = true;
    }
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j <= k; j++){
      for(int l : b){
        if(__builtin_popcount(l^s[i]) <= j)
          dp[i][j] |= dp[i-1][j-__builtin_popcount(l^s[i])];
      }
    }
  }
  string num;
  int x = n-1, y = k;

  if(!dp[n-1][k]){
    cout << "-1\n";
    return;
  }

  while(x > 0 && y > 0){
    int mx = -1, lx = -1;
    for(int l : b){
      if(__builtin_popcount(l^s[x]) <= y &&
         dp[x-1][y-__builtin_popcount(l^s[x])] &&
        mx < m[l])
          mx = m[l], lx = l;
    }
    x--, y -= __builtin_popcount(lx^s[x]);
    // cout << mx << "\n";
    num += mx+48;
  }
  int mx = -1;
  for(int l : b){
    if(__builtin_popcount(l^s[x]) == y && mx < m[l])
      mx = m[l];
  }
  if(mx == -1){
    cout << "-1\n";
    return;
  }
  num += mx+48;

  cout << num << "\n";
}

int main(){
#ifndef LOCAL
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#endif
  // int t;cin >> t;while(t--) solve();
  solve();
  cerr << "\nExecution time: " << (double)clock()/CLOCKS_PER_SEC << "\n";
}


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

typedef long long int ll;
typedef long double ld;

const int INF = 1000000009;

vector<string> _a = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
vector<int> b;
map<int,int> m;

const int N = 2005;

bool dp[N][N];

void solve(){
  int n, k;
  cin >> n >> k;

  for(int i = 0; i < (int)_a.size(); i++){
    int x = 0;
    for(int j = 0; j < 7; j++)
      x |= ((_a[i][j]-'0') << j);
    b.push_back(x);
    m[x] = i;
  }

  vector<int> s(n);
  for(int i = 0; i < n; i++){
    string x;
    cin >> x;
    for(int j = 0; j < 7; j++)
      s[i] |= ((x[j]-'0')<<j);
  }
  reverse(s.begin(), s.end());

  for(int j = 0; j <= k; j++){
    for(int l : b){
      if(__builtin_popcount(l^s[0]) == j)
        dp[0][j] = true;
    }
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j <= k; j++){
      for(int l : b){
        if(__builtin_popcount(l^s[i]) <= j)
          dp[i][j] |= dp[i-1][j-__builtin_popcount(l^s[i])];
      }
    }
  }
  if(!dp[n-1][k]){
    cout << "-1\n";
    return;
  }

  string num;
  int x = n-1, y = k;

  while(x > 0 && y >= 0){
    int lx = -1;
    for(int l : b){
      if(__builtin_popcount(l^s[x]) <= y &&
         dp[x-1][y-__builtin_popcount(l^s[x])] &&
        (lx == -1 || m[lx] < m[l]))
          lx = l;
    }
    y -= __builtin_popcount(lx^s[x]);
    x--;
    // cout << mx << "\n";
    num += char(m[lx]+48);
  }
  // cout << "y = " << y << "\n";
  int mx = -1;
  for(int l : b){
    if(__builtin_popcount(l^s[0]) == y && mx < m[l])
      mx = m[l];
  }
  num += char(mx+48);

  cout << num << "\n";
}

int main(){
#ifndef LOCAL
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#endif
  // int t;cin >> t;while(t--) solve();
  solve();
  cerr << "\nExecution time: " << (double)clock()/CLOCKS_PER_SEC << "\n";
}


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

typedef long long int ll;
typedef long double ld;

const int INF = 1000000009;

vector<string> _a = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
vector<int> b;
map<int,int> m;

const int N = 2005;

bool dp[N][N];

bool can(int x, int y){ // x -> y
  for(int i = 0; i < 7; i++)
    if((x&(1<<i)) && (!(y&(1<<i)))) return false;
  return true;
}

void print(int x){
  for(int i = 0; i < 7; i++)
    if(x&(1<<i))  cout << '1';
    else          cout << '0';
  cout << '\n';
}

void solve(){
  int n, k;
  cin >> n >> k;

  for(int i = 0; i < (int)_a.size(); i++){
    int x = 0;
    for(int j = 0; j < 7; j++)
      x |= ((_a[i][j]-'0') << j);
    b.push_back(x);
    m[x] = i;
  }

  vector<int> s(n);
  for(int i = 0; i < n; i++){
    string x;
    cin >> x;
    for(int j = 0; j < 7; j++)
      s[i] |= ((x[j]-'0')<<j);
  }
  reverse(s.begin(), s.end());

  for(int j = 0; j <= k; j++){
    for(int l : b){
      if(can(s[0], l) &&__builtin_popcount(l^s[0]) == j)
        dp[0][j] = true;
    }
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j <= k; j++){
      for(int l : b){
        if(can(s[i], l) && __builtin_popcount(l^s[i]) <= j)
          dp[i][j] |= dp[i-1][j-__builtin_popcount(l^s[i])];
      }
    }
  }
  if(!dp[n-1][k]){
    cout << "-1\n";
    return;
  }

  string num;
  int x = n-1, y = k;

  while(x > 0 && y >= 0){
    int lx = -1;
    for(int l : b){
      if(can(s[x], l) && __builtin_popcount(l^s[x]) <= y &&
         dp[x-1][y-__builtin_popcount(l^s[x])] &&
        (lx == -1 || m[lx] < m[l]))
        lx = l;
    }
    /*
    cout << "lx = ";
    print(lx);
    cout << "s  = ";
    print(s[x]);
    cout << "\n";
    */
    y -= __builtin_popcount(lx^s[x]);
    x--;
    // cout << mx << "\n";
    num += char(m[lx]+48);
  }
  // cout << "y = " << y << "\n";
  int mx = -1;
  for(int l : b){
    if(can(s[0], l) && __builtin_popcount(l^s[0]) == y && mx < m[l])
      mx = m[l];
  }
  num += char(mx+48);

  cout << num << "\n";
}

int main(){
#ifndef LOCAL
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
#endif
  // int t;cin >> t;while(t--) solve();
  solve();
  cerr << "\nExecution time: " << (double)clock()/CLOCKS_PER_SEC << "\n";
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
int pow10[7];
int dp[2001][2001];

void pre() {

	pow10[0] = 1;
	for(int i=1;i<7;++i) {
		pow10[i] = pow10[i-1]*10;
	}

	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

int getNum(int st) {
	for(int i=0;i<10;++i) {
		if(state[i] == st) return i;
	}
	return -1;
}

int solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return 0;
		}
		return INT_MAX;
	}
	if(dp[curr][rem]!=-1) return dp[curr][rem];
	int res = INT_MIN;
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	int whatNum = getNum(a[curr]);
	if(whatNum!=-1) {
		int recd = solve(curr+1,rem,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*whatNum+recd);
		}
	}
	for(auto i:possibles) {
		int recd = solve(curr+1,rem-i.second,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*i.first+recd);
		}
	}
	if(res == INT_MIN) res =  INT_MAX;
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;

	pre();
	mset(dp,-1);

	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	reverse(a,a+n);
	int ans = solve(0,k,a,n);
	if(ans == INT_MAX) {
		cout<<-1;
	}
	else {
		string strAns = to_string(ans);
		string tmp;
		while(strAns.size()+tmp.size()<n) {
			tmp+='0';
		}
		tmp+=strAns;
		cout<<tmp;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
#define		IOS 			ios_base::sync_with_stdio(false);cin.tie(nullptr)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
string dp[2001][2001];

void pre() {
	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=9;i>=0;--i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

string solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return "";
		}
		return "-1";
	}
	if(!dp[curr][rem].empty()) return dp[curr][rem];
	string res = " ";
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	for(auto i:possibles) {
		string recd = solve(curr+1,rem-i.second,a,n);
		if(recd!="-1") {
			string _=to_string(i.first);
			_+=recd;
			res = max(res, _);
			break;
		}
	}
	if(res == " ") res =  "-1";
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;
	IOS;

	pre();
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	string ans = solve(0,k,a,n);
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
int pow10[7];
int dp[2001][2001];

void pre() {

	pow10[0] = 1;
	for(int i=1;i<7;++i) {
		pow10[i] = pow10[i-1]*10;
	}

	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

int getNum(int st) {
	for(int i=0;i<10;++i) {
		if(state[i] == st) return i;
	}
	return -1;
}

int solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return 0;
		}
		return INT_MAX;
	}
	if(dp[curr][rem]!=-1) return dp[curr][rem];
	int res = INT_MIN;
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	int whatNum = getNum(a[curr]);
	if(whatNum!=-1) {
		int recd = solve(curr+1,rem,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*whatNum+recd);
		}
	}
	for(auto i:possibles) {
		int recd = solve(curr+1,rem-i.second,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*i.first+recd);
		}
	}
	if(res == INT_MIN) res =  INT_MAX;
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;

	pre();
	mset(dp,-1);

	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	int ans = solve(0,k,a,n);
	if(ans == INT_MAX) {
		cout<<-1;
	}
	else {
		string strAns = to_string(ans);
		string tmp;
		while(strAns.size()+tmp.size()<n) {
			tmp+='0';
		}
		tmp+=strAns;
		cout<<tmp;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
unordered_map<int,string> dp[2001];

void pre() {
	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

string solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return "";
		}
		return "-1";
	}
	if(!dp[curr][rem].empty()) return dp[curr][rem];
	string res = " ";
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	for(auto i:possibles) {
		string recd = solve(curr+1,rem-i.second,a,n);
		if(recd!="-1") {
			string _=to_string(i.first);
			_+=recd;
			res = max(res, _);
		}
	}
	if(res == " ") res =  "-1";
	return dp[curr][rem] = res;
}

int main() {
	FILE_READ;
	FILE_WRITE;
	pre();
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	string ans = solve(0,k,a,n);
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
unordered_map<int,string> dp[2001];

void pre() {
	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

string solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return "";
		}
		return "-1";
	}
	if(!dp[curr][rem].empty()) return dp[curr][rem];
	string res = " ";
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	for(auto i:possibles) {
		string recd = solve(curr+1,rem-i.second,a,n);
		if(recd!="-1") {
			string _=to_string(i.first);
			_+=recd;
			res = max(res, _);
		}
	}
	if(res == " ") res =  "-1";
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;
	pre();
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	string ans = solve(0,k,a,n);
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
int pow10[7];
int dp[2001][2001];

void pre() {

	pow10[0] = 1;
	for(int i=1;i<7;++i) {
		pow10[i] = pow10[i-1]*10;
	}

	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

int solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return 0;
		}
		return INT_MAX;
	}
	if(dp[curr][rem]!=-1) return dp[curr][rem];
	int res = INT_MIN;
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	for(auto i:possibles) {
		int recd = solve(curr+1,rem-i.second,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*i.first+recd);
		}
	}
	if(res == INT_MIN) res =  INT_MAX;
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;

	pre();
	mset(dp,-1);

	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	int ans = solve(0,k,a,n);
	if(ans == INT_MAX) {
		cout<<-1;
	}
	else {
		string strAns = to_string(ans);
		string tmp;
		while(strAns.size()+tmp.size()<n) {
			tmp+='0';
		}
		tmp+=strAns;
		cout<<tmp;
	}

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
int pow10[7];
int dp[2001][2001];

void pre() {
	noOfOnsInDigits[0] = 6;
	noOfOnsInDigits[1] = 2;
	noOfOnsInDigits[2] = 5;
	noOfOnsInDigits[3] = 5;
	noOfOnsInDigits[4] = 4;
	noOfOnsInDigits[5] = 5;
	noOfOnsInDigits[6] = 6;
	noOfOnsInDigits[7] = 3;
	noOfOnsInDigits[8] = 7;
	noOfOnsInDigits[9] = 6;

	pow10[0] = 1;
	for(int i=1;i<7;++i) {
		pow10[i] = pow10[i-1]*10;
	}

	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

int getNum(int st) {
	for(int i=0;i<10;++i) {
		if(state[i] == st) return i;
	}
	return -1;
}

int solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return 0;
		}
		return INT_MAX;
	}
	if(dp[curr][rem]!=-1) return dp[curr][rem];
	int res = INT_MIN;
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	int whatNum = getNum(a[curr]);
	if(whatNum!=-1) {
		int recd = solve(curr+1,rem,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*whatNum+recd);
		}
	}
	for(auto i:possibles) {
		int recd = solve(curr+1,rem-i.second,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*i.first+recd);
		}
	}
	if(res == INT_MIN) res =  INT_MAX;
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;

	pre();
	mset(dp,-1);

	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	int ans = solve(0,k,a,n);
	if(ans == INT_MAX) {
		cout<<-1;
	}
	else cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
string dp[2001][2001];

void pre() {
	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

string solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return "";
		}
		return "-1";
	}
	if(!dp[curr][rem].empty()) return dp[curr][rem];
	string res = " ";
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	for(auto i:possibles) {
		string recd = solve(curr+1,rem-i.second,a,n);
		if(recd!="-1") {
			string _=to_string(i.first);
			_+=recd;
			res = max(res, _);
		}
	}
	if(res == " ") res =  "-1";
	return dp[curr][rem] = res;
}

int main() {
	FILE_READ;
	FILE_WRITE;
	pre();
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	string ans = solve(0,k,a,n);
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
string dp[2001][2001];

void pre() {
	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

string solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return "";
		}
		return "-1";
	}
	if(!dp[curr][rem].empty()) return dp[curr][rem];
	string res = " ";
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	for(auto i:possibles) {
		string recd = solve(curr+1,rem-i.second,a,n);
		if(recd!="-1") {
			string _=to_string(i.first);
			_+=recd;
			res = max(res, _);
		}
	}
	if(res == " ") res =  "-1";
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;
	pre();
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	string ans = solve(0,k,a,n);
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
unordered_map<int,string> dp[2001];

void pre() {
	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
		noOfOnsInDigits[i]=__builtin_popcount(state[i]);
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=9;i>=0;--i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

string solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return "";
		}
		return "-1";
	}
	if(!dp[curr][rem].empty()) return dp[curr][rem];
	string res = " ";
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	for(auto i:possibles) {
		string recd = solve(curr+1,rem-i.second,a,n);
		if(recd!="-1") {
			string _=to_string(i.first);
			_+=recd;
			res = max(res, _);
			break;
		}
	}
	if(res == " ") res =  "-1";
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;
	pre();
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	string ans = solve(0,k,a,n);
	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define     mkp(a,b)        make_pair(a,b)
#define     PI              3.14159265359
#define     ll              long long
#define     ul              unsigned long long
#define     ld              long double
#define     mset(arr,val)   memset(arr,val,sizeof arr)
#define     MAXN            (long long)2e5+5
#define     eb              emplace_back
#define     mod             (long long)(1e9+7)
#define     FILE_READ       freopen("input.txt","r",stdin)
#define     FILE_WRITE      freopen("output.txt","w",stdout)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int noOfOnsInDigits[10];
int state[10];
int pow10[7];
int dp[2001][2001];

void pre() {
	noOfOnsInDigits[0] = 6;
	noOfOnsInDigits[1] = 2;
	noOfOnsInDigits[2] = 5;
	noOfOnsInDigits[3] = 5;
	noOfOnsInDigits[4] = 4;
	noOfOnsInDigits[5] = 5;
	noOfOnsInDigits[6] = 6;
	noOfOnsInDigits[7] = 3;
	noOfOnsInDigits[8] = 7;
	noOfOnsInDigits[9] = 6;

	pow10[0] = 1;
	for(int i=1;i<7;++i) {
		pow10[i] = pow10[i-1]*10;
	}

	string stateStr[] = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	for(int i=0;i<10;++i) {
		state[i] = bitset<7>(stateStr[i]).to_ulong();
	}
}

vector<pii> digitsCanBeFormed(int currState, int lim) {
	vector<pii> ans;
	for(int i=0;i<10;++i) {
		if((currState&state[i]) == currState) {
			int extras = noOfOnsInDigits[i]-__builtin_popcount(currState);
			if(extras <= lim) {
				ans.eb(i,extras);
			}
		}
	}
	return ans;
}

int getNum(int st) {
	for(int i=0;i<10;++i) {
		if(state[i] == st) return i;
	}
	return -1;
}

int solve(int curr, int rem, int a[], int &n) {
	if(curr == n) {
		if(rem == 0) {
			return 0;
		}
		return INT_MAX;
	}
	if(dp[curr][rem]!=-1) return dp[curr][rem];
	int res = INT_MIN;
	vector<pii> possibles = digitsCanBeFormed(a[curr], rem);
	int whatNum = getNum(a[curr]);
	if(whatNum!=-1) {
		int recd = solve(curr+1,rem,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*whatNum+recd);
		}
	}
	for(auto i:possibles) {
		int recd = solve(curr+1,rem-i.second,a,n);
		if(recd!=INT_MAX) {
			res = max(res, pow10[n-curr-1]*i.first+recd);
		}
	}
	if(res == INT_MIN) res =  INT_MAX;
	return dp[curr][rem] = res;
}

int main() {
	// FILE_READ;
	// FILE_WRITE;

	pre();
	mset(dp,-1);

	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;++i) {
		string s;
		cin>>s;
		a[i] = bitset<7>(s).to_ulong();
	}
	int ans = solve(0,k,a,n);
	if(ans == INT_MAX) {
		cout<<-1;
	}
	else {
		string strAns = to_string(ans);
		string tmp;
		while(strAns.size()+tmp.size()<n) {
			tmp+='0';
		}
		tmp+=strAns;
		cout<<tmp;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<string> nums;
unordered_map<int, int> cnt;
unordered_map<int, string> enc;
unordered_map<int, vector<int>> nxt;
unordered_map<string, vector<pair<int, int>>> nbrs;
int n;
int dp[2005][2005];
vector<int> ans(2005);
inline int reqbits(string str1, int val)
{
    string str2 = enc[val];

    int cnt = 0;
    for (int i = 0; i < 7; i++)
    {
        if (str1[i] == '0')
        {
            if (str2[i] == '1')
                cnt++;
        }
        else
        {
            if (str2[i] == '0')
                return -1;
        }
    }
    return cnt;
}

int find(int ind, int av)
{
    if (av == 0 && ind == n)
        return 1;
    if (ind == n)
        return 0;
    if (dp[ind][av] != -1)
        return dp[ind][av];

    vector<pair<int, int>> nbr = nbrs[nums[ind]];
    int sz = nbr.size();
    for (int j = 0; j < sz; j++)
    {
        int i = nbr[j].first;
        int req = nbr[j].second;
        if (req > av)
            continue;
        if (find(ind + 1, av - req) == 1)
        {
            ans[ind] = i;
            return dp[ind][av] = 1;
        }
    }
    return dp[ind][av] = 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    for (int i = 0; i < 2005; i++)
    {
        for (int j = 0; j < 2005; j++)
            dp[i][j] = -1;
    }
    // cout<<"yo";
    cnt[0] = 6;
    cnt[1] = 2;
    cnt[2] = 5;
    cnt[3] = 5;
    cnt[4] = 4;
    cnt[5] = 5;
    cnt[6] = 6;
    cnt[7] = 3;
    cnt[8] = 7;
    cnt[9] = 6;
    // cout<<"yooy"<<endl;
    enc[0] = "1110111";
    enc[1] = "0010010";
    enc[2] = "1011101";
    enc[3] = "1011011";
    enc[4] = "0111010";
    enc[5] = "1101011";
    enc[6] = "1101111";
    enc[7] = "1010010";
    enc[8] = "1111111";
    enc[9] = "1111011";
    // cout<<"yoyoyooyoy"<<endl;
    nxt[0] = {8};
    nxt[1] = {9, 8, 7, 4, 3, 0};
    nxt[2] = {8};
    nxt[3] = {9, 8};
    nxt[4] = {9, 8};
    nxt[5] = {9, 8};
    nxt[6] = {8};
    nxt[7] = {9, 8, 3, 0};
    nxt[9] = {8};
    // cout<<"sasfsdf";
    int k;
    cin >> n >> k;
    string str;
    for (int i = 0; i < n; i++)
    {
        cin >> str;
        nums.push_back(str);
        for (int j = 9; j >= 0; j--)
        {
            int r = reqbits(str, j);
            if (r != -1)
            {
                nbrs[str].push_back(make_pair(j, r));
            }
        }
    }

    if (find(0, k))
    {
        for (int i = 0; i < n; i++)
        {
            cout << ans[i];
        }
        cout << endl;
    }
    else
        cout << -1 << endl;

    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define o2(x) (x) * (x)
#define mk make_pair
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset((a), (b), sizeof((a)))
#define rep(i, s, t) for(register int i = (s), LIM=(t); i < LIM; ++i)
#define per(i, s, t) for(register int i = (s), LIM=(t); i >= LIM; --i)
#define GKD std::ios::sync_with_stdio(false);cin.tie(0)
#define my_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long LL;
typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
// mt19937 rng(time(NULL));//std::clock()
// mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(arr, arr + n, rng64);
inline int64 read() {
    int64 x = 0;int las = 0;char ch = getchar();
    while (ch < '0' || ch > '9') las |= (ch == '-'), ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch =
    getchar(); return x = las ? -x : x;
}
inline void write(int64 x, bool las = true) {
    if (x == 0) {putchar('0'); if(las)putchar('\n');else putchar(' ');return;}
    if (x < 0) {putchar('-');x = -x;}
    static char s[23];
    int l = 0;
    while (x != 0)s[l++] = x % 10 + 48, x /= 10;
    while (l)putchar(s[--l]);
    if(las)putchar('\n');else putchar(' ');
}
int lowbit(int x) { return x & (-x); }
template <class T>
T big(const T &a1, const T &a2) {return a1 > a2 ? a1 : a2;}
template <class T>
T sml(const T &a1, const T &a2) {return a1 < a2 ? a1 : a2;}
template <typename T, typename... R>
T big(const T &las, const R &... r) {return big(las, big(r...));}
template <typename T, typename... R>
T sml(const T &las, const R &... r) {return sml(las, sml(r...));}
void debug_out() { cout << '\n'; }
template <typename T, typename... R>
void debug_out(const T &las, const R &... r) {
    cout << las << " ";
    debug_out(r...);
}
#ifdef LH_LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__);
#else
#define debug(...) ;
#endif
/*================Header Template==============*/
const int mod = 998244353;// 998244353
const int INF = 0x3f3f3f3f;
int ksm(int a, int64 b, int kmod = mod) {int res = 1;for(;b > 0;b >>= 1, a = (int64)a * a % kmod) if(b &1) res = (int64)res * a % kmod;return res;}
const int MXN = 1e5 + 5;

int n, m, ret;
string q[4];
int ar[MXN], ans[MXN];
map<int, int> mp;
void ask(int type, int a, int b) {
	cout << q[type] << " " << a << " " << b << endl;
	cin >> ret;
}
void work() {
	int las = 2, flag = 0, one = 2;
	for(int i = 2; i <= n; ++i) {
		ask(3, 1, i);
		ar[i] = ret;
		las = i;
		if(mp[ret]) {//a[mp[ret]]和a[i]相同
			flag = 1;
			break;
		}else if(ret == 0) {//a[1]和a[i]相同
			flag = 2;
			break;
		}
		if(ret == 1) one = i;
		mp[ret] = i;
	}
	if(flag == 2) {
		ask(1, 1, las);
		ans[1] = ans[las] = ret;
		for(int i = las + 1; i <= n; ++i) ask(3, 1, i), ar[i] = ret;
	}else if(flag == 1) {
		int s = mp[ret];
		ask(1, s, las);
		ans[s] = ans[las] = ret;
		ans[1] = ans[las] ^ ar[las];
		for(int i = las + 1; i <= n; ++i) ask(3, 1, i), ar[i] = ret;
	}else {
		ask(1, 1, one);
		int a = ret, j = (one == n?n - 1:n + 1);
		ask(2, one, j);
		if(ar[j] % 2 == ret % 2) {
			ans[1] = a, ans[one] = a + 1;
		}else {
			ans[1] = a + 1, ans[one] = a;
		}
	}
	for(int i = 2; i <= n; ++i) ans[i] = ans[1] ^ ar[i];
	cout << "!";
	for(int i = 1; i <= n; ++i) cout << " " << ans[i];
	cout << endl;
}
int main() {
#ifdef LH_LOCAL
    freopen("D:/ACM/mtxt/in.txt", "r", stdin);
    // freopen("D:/ACM/mtxt/out.txt", "w", stdout);
#endif
	q[1] = "AND", q[2] = "OR", q[3] = "XOR";
    for(int cas = 1, tim = 1; cas <= tim; ++ cas) {
		cin >> n;
        work();
    }
#ifdef LH_LOCAL
    cout << "time cost:" << 1.0 * clock() / CLOCKS_PER_SEC << "s" << endl;
#endif
    return 0;
}