import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int[] digitMasks = {
                    0b1110111,
                    0b0010010,
                    0b1011101,
                    0b1011011,
                    0b0111010,
                    0b1101011,
                    0b1101111,
                    0b1010010,
                    0b1111111,
                    0b1111011,
            };
            for (int i = 0; i < 10; i++) {
                int rev = 0;
                for (int j = 0; j < 7; j++) {
                    rev |= ((digitMasks[i] >> j) & 1) << (7 - 1 - j);
                }
                digitMasks[i] = rev;
            }
            int[] counts = new int[10];
            for (int i = 0; i < 10; i++) {
                counts[i] = Integer.bitCount(digitMasks[i]);
            }
            int n = in.nextInt();
            int k = in.nextInt();
            int[] a = new int[n];
            int[] b = new int[n];
            for (int i = 0; i < n; i++) {
                char[] s = in.next().toCharArray();
                a[i] = 0;
                for (int j = 0; j < s.length; j++) {
                    a[i] |= (s[j] - '0') << j;
                }
                b[i] = Integer.bitCount(a[i]);
            }

            boolean[][] can = new boolean[n + 1][k + 1];
            can[n][0] = true;
            for (int i = n - 1; i >= 0; i--) {
                for (int j = 0; j <= k; j++) {
                    for (int d = 0; d < 10; d++) {
                        if ((a[i] & digitMasks[d]) != a[i]) {
                            continue;
                        }
                        int nj = j - (counts[d] - b[i]);
                        if (nj >= 0 && can[i + 1][nj]) {
                            can[i][j] = true;
                        }
                    }
                }
            }
            if (!can[0][k]) {
                out.println(-1);
                return;
            }

            char[] ans = new char[n];
            int j = k;
            for (int i = 0; i < n; i++) {
                for (int d = 9; d >= 0; d--) {
                    if ((a[i] & digitMasks[d]) != a[i]) {
                        continue;
                    }
                    int nj = j - (counts[d] - b[i]);
                    if (nj >= 0 && can[i + 1][nj]) {
                        ans[i] = (char) (d + '0');
                        j = nj;
                        break;
                    }
                }
            }
            out.println(new String(ans));
        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.util.*;
import java.io.*;
import java.text.*;

public class Main{
    //SOLUTION BEGIN
    //Into the Hardware Mode
    void pre() throws Exception{}
    void solve(int TC) throws Exception {
        int n = ni(), k = ni();
        int[] mask = new int[1+n];
        for(int i = 1; i<= n; i++)mask[n-i+1] = Integer.parseInt(n(), 2);
        boolean[][] DP = new boolean[1+n][1+k];
        DP[0][0] = true;
        for(int i = 1; i<= n; i++){
            for(int d = 0; d< 10; d++){
                int cost = cost(mask[i], d);
                if(cost == -1)continue;
                for(int use = 0; use+cost <= k; use++)
                    if(DP[i-1][use])
                        DP[i][use+cost] = true;
            }
        }
        int[] ans = new int[1+n];
        int use = k;
        if(!DP[n][use]){
            pn(-1);
            return;
        }
        for(int i = n; i >= 1; i--){
            for(int d = 9; d >= 0; d--){
                int c = cost(mask[i], d);
                if(c == -1 || use-c < 0 || !DP[i-1][use-c])continue;
                ans[i] = d;
                use -= c;
                break;
            }
        }
        for(int i = n; i>= 1; i--)p(ans[i]);pn("");
    }
    int cost(int cur, int target){
        target = V[target];
        if((cur&target) != cur)return -1;
        return bit(target^cur);
    }
    int[] V = new int[]{Integer.parseInt("1110111", 2),
            Integer.parseInt("0010010", 2),
            Integer.parseInt("1011101", 2),
            Integer.parseInt("1011011", 2),
            Integer.parseInt("0111010", 2),
            Integer.parseInt("1101011", 2),
            Integer.parseInt("1101111", 2),
            Integer.parseInt("1010010", 2),
            Integer.parseInt("1111111", 2),
            Integer.parseInt("1111011", 2),};

    //SOLUTION END
    void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
    void exit(boolean b){if(!b)System.exit(0);}
    final long IINF = (long)2e18;
    final int INF = (int)1e9+2, MX = (int)2e6+5;
    DecimalFormat df = new DecimalFormat("0.00000000000");
    double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-8;
    static boolean multipleTC = false, memory = false, fileIO = false;
    FastReader in;PrintWriter out;
    void run() throws Exception{
        long ct = System.currentTimeMillis();
        if (fileIO) {
            in = new FastReader("");
            out = new PrintWriter("");
        } else {
            in = new FastReader();
            out = new PrintWriter(System.out);
        }
        //Solution Credits: Taranpreet Singh
        int T = (multipleTC) ? ni() : 1;
        pre();
        for (int t = 1; t <= T; t++) solve(t);
        out.flush();
        out.close();
        System.err.println(System.currentTimeMillis() - ct);
    }
    public static void main(String[] args) throws Exception{
        if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start();
        else new Main().run();
    }
    int[][] make(int n, int[] from, int[] to, int e, boolean f){
        int[][] g = new int[n][];int[]cnt = new int[n];
        for(int i = 0; i< e; i++){
            cnt[from[i]]++;
            if(f)cnt[to[i]]++;
        }
        for(int i = 0; i< n; i++)g[i] = new int[cnt[i]];
        for(int i = 0; i< e; i++){
            g[from[i]][--cnt[from[i]]] = to[i];
            if(f)g[to[i]][--cnt[to[i]]] = from[i];
        }
        return g;
    }
    int[][][] makeS(int n, int[] from, int[] to, boolean f){
        int[][][] g = new int[n][][];int[]cnt = new int[n];
        for(int i:from)cnt[i]++;if(f)for(int i:to)cnt[i]++;
        for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][];
        for(int i = 0; i< from.length; i++){
            g[from[i]][--cnt[from[i]]] = new int[]{to[i], i};
            if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i};
        }
        return g;
    }
    int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));}
    int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;}
    long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
    int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
    int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
    void p(Object o){out.print(o);}
    void pn(Object o){out.println(o);}
    void pni(Object o){out.println(o);out.flush();}
    String n()throws Exception{return in.next();}
    String nln()throws Exception{return in.nextLine();}
    int ni()throws Exception{return Integer.parseInt(in.next());}
    long nl()throws Exception{return Long.parseLong(in.next());}
    double nd()throws Exception{return Double.parseDouble(in.next());}

    class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws Exception{
            br = new BufferedReader(new FileReader(s));
        }

        String next() throws Exception{
            while (st == null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (IOException  e){
                    throw new Exception(e.toString());
                }
            }
            return st.nextToken();
        }

        String nextLine() throws Exception{
            String str;
            try{
                str = br.readLine();
            }catch (IOException e){
                throw new Exception(e.toString());
            }
            return str;
        }
    }
}


import java.io.*;
import java.util.*;

public class CodeForces {

    static class Reader {
        BufferedReader br;
        StringTokenizer st;

        public Reader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    static int n, k;
    static String[] inp;
    static String[] num_sorted = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111",
            "1010010", "1111111", "1111011" };
    static int[][] dp;
    static char[][] c;
    static int[][] change;
    static HashMap<Integer, Integer>[] maps;

    public static void main(String[] args) throws Exception {
        Reader in = new Reader();
        // Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int K = in.nextInt();

        inp = new String[n];
        for (int i = 0; i < n; i++)
            inp[i] = in.next();

        dp = new int[n][K + 1];
        for (int i = 0; i < n; i++)
            Arrays.fill(dp[i], -1);

        c = new char[n][10];
        change = new int[n][10];
        maps = new HashMap[n];

        for (int i = n - 1; i >= 0; i--) {
            c[i] = inp[i].toCharArray();
            maps[i] = new HashMap<>();
            for (int j = 0; j <= 9; j++) {
                change[i][j] = diff(c[i], j);
                if (change[i][j] == -1)
                    continue;
                maps[i].put(change[i][j], j);
                // dp[i][change[i][j]] = j;
            }
        }
        // CommonDebugCode.printMatrix(change);
        // System.out.println();

        for (int k = 0; k <= K; k++) {
            for (int l = 9; l >= 0; l--) {
                if(k == change[n-1][l]){
                    dp[n-1][k] = l;
                    break;
                }
            }
        }

        for (int i = n - 2; i >= 0; i--) {
            for (int k = 0; k <= K; k++) {
                for (int l = 9; l >= 0; l--) {
                    if(change[i][l] == -1)
                        continue;
                    int temp = k - change[i][l];
                    if (temp < 0 || temp > K)
                        continue;

                    if (dp[i + 1][temp] != -1){
                        dp[i][k] = l;
                        break;
                    }
                }
            }
        }

        // CommonDebugCode.printMatrix(dp);
        if(dp[0][K] == -1){
            System.out.println(-1);
            System.exit(0);
        }
        String ans = "";
        for(int i = 0;i < n;i++)
        {
            ans += dp[i][K];
            K = K - change[i][dp[i][K]];
        }
        System.out.println(ans);

    }

    static int diff(char[] x, int ind) {
        int count = 0;
        for (int i = 0; i < 7; i++) {
            if (x[i] == '1' && num_sorted[ind].charAt(i) == '0')
                return -1;
            if (x[i] == '0' && num_sorted[ind].charAt(i) == '1')
                count++;
        }
        return count;
    }

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

}


/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;

   public class B2
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int N = Integer.parseInt(st.nextToken());
         int K = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(infile.readLine(), 2);
         int[][] dp = new int[N][K+1];
         for(int i=0; i < N; i++)
            Arrays.fill(dp[i], -1);
         int[] res = new int[N];
         for(int k=0; k <= K; k++)
            for(int d=9; d >= 0; d--)
            {
               int val = check(arr[N-1],d);
               if(val != -1 && val == k)
                  dp[N-1][k] = Math.max(dp[N-1][k], d);
            }
         //parents
         Node[][] par = new Node[N][K+1];
         for(int k=0; k <= K; k++)
            if(dp[N-1][k] != -1)
               par[N-1][k] = new Node(N, 69);
         for(int i=N-2; i >= 0; i--)
            for(int d=9; d >= 0; d--)
            {
               int val = check(arr[i], d);
               if(val == -1)
                  continue;
               for(int k=0; k <= K; k++)
                  if(val <= k && dp[i+1][k-val] != -1 && dp[i][k] == -1)
                  {
                     dp[i][k] = d;
                     par[i][k] = new Node(i+1,k-val);
                  }
            }
         if(dp[0][K] == -1)
         {
            System.out.println(-1);
            return;
         }
         Node curr = new Node(0, K);
         while(curr.n < N)
         {
            if(curr == null)
            {
               System.out.println(-1);
               return;
            }
            res[curr.n] = dp[curr.n][curr.k];
            curr = par[curr.n][curr.k];
         }
         StringBuilder sb = new StringBuilder();
         for(int x: res)
            sb.append(x);
         System.out.println(sb);
      }
      static int[] map = {119,18,93,91,58,107,111,82,127,123};
      public static int check(int x, int d)
      {
         int res = 0;
         d = map[d];
         for(int b=0; b < 7; b++)
         {
            int a = (x&(1<<b));
            int c = (d&(1<<b));
            if(a > 0 && c == 0)
               return -1;
            res += Math.min(1,c-a);
         }
         return res;
      }
   }
   class Node
   {
      public int n;
      public int k;

      public Node(int a, int b)
      {
         n = a;
         k = b;
      }
   }


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter pw = new PrintWriter(System.out);
    static int MOD = 1000000007;

    public static void main(String[] args) throws IOException {
        Main m = new Main();
        m.solve();
        m.close();
    }

    void close() throws IOException {
        pw.flush();
        pw.close();
        br.close();
    }

    int ri() throws IOException {
        return Integer.parseInt(br.readLine());
    }

    long rl() throws IOException {
        return Long.parseLong(br.readLine());
    }

    int[] ril(int n) throws IOException {
        int[] nums = new int[n];
        int c = 0;
        for (int i = 0; i < n; i++) {
            int sign = 1;
            c = br.read();
            int x = 0;
            if (c == '-') {
                sign = -1;
                c = br.read();
            }
            while (c >= '0' && c <= '9') {
                x = x * 10 + c - '0';
                c = br.read();
            }
            nums[i] = x * sign;
        }
        while (c != '\n' && c != -1) c = br.read();
        return nums;
    }

    long[] rll(int n) throws IOException {
        long[] nums = new long[n];
        int c = 0;
        for (int i = 0; i < n; i++) {
            int sign = 1;
            c = br.read();
            long x = 0;
            if (c == '-') {
                sign = -1;
                c = br.read();
            }
            while (c >= '0' && c <= '9') {
                x = x * 10 + c - '0';
                c = br.read();
            }
            nums[i] = x * sign;
        }
        while (c != '\n' && c != -1) c = br.read();
        return nums;
    }

    char[] rs() throws IOException {
        return br.readLine().toCharArray();
    }

    void solve() throws IOException {
        int[] nk = ril(2);
        int n = nk[0];
        int k = nk[1];
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine(), 2);
        }
        // dp[i][k] is max i can make with digits a[i..n-1] and k segments
        // dp[n][.] is a dummy number for easier base case
        int[][] dp = new int[n+1][k+1];
        Arrays.fill(dp[n], -1);
        dp[n][0] = 0;
        for (int i = n-1; i >= 0; i--) {
            for (int j = 0; j <= k; j++) {
                dp[i][j] = -1;
                for (int d = 0; d <= 9; d++) {
                    int dsegment = digitToSegment[d];
                    if ((dsegment | a[i]) != dsegment) continue;
                    int need = Integer.bitCount(dsegment ^ a[i]);
                    if (j >= need && dp[i+1][j-need] != -1) dp[i][j] = d;
                }
            }
        }
        if (dp[0][k] == -1) pw.println("-1");
        else {
            for (int i = 0; i < n; i++) {
                pw.print(dp[i][k]);
                int need = Integer.bitCount(digitToSegment[dp[i][k]] ^ a[i]);
                k -= need;
            }
            pw.println();
        }
    }

    static int[] digitToSegment = new int[]{
        0b1110111,
        0b0010010,
        0b1011101,
        0b1011011,
        0b0111010,
        0b1101011,
        0b1101111,
        0b1010010,
        0b1111111,
        0b1111011,
    };
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "khokharnikunj8", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            BNastyaAndScoreboard solver = new BNastyaAndScoreboard();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class BNastyaAndScoreboard {
        int[] mask = new int[]{0b1110111, 0b0010010, 0b1011101, 0b1011011, 0b0111010, 0b1101011, 0b1101111, 0b1010010, 0b1111111, 0b1111011};

        public int indexOf(int msk) {
            for (int i = 0; i < mask.length; i++) {
                if (mask[i] == msk) return i;
            }
            return -1;
        }

        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.readInt();
            int k = in.readInt();
            String[] s = new String[n];
            for (int i = n - 1; i >= 0; i--) s[i] = in.readString();
            int[] ar = new int[n];
            for (int i = 0; i < n; i++) ar[i] = Integer.parseInt(s[i], 2);
            ArrayList<int[]>[] which = new ArrayList[1 << 7];
            for (int i = 0; i < 1 << 7; i++) {
                which[i] = new ArrayList<>();
                for (int mask : this.mask) {
                    int change = 0;
                    for (int j = 0; j < 7; j++) {
                        int bit1 = Bits.bitAt(mask, j);
                        int bit2 = Bits.bitAt(i, j);
                        if (bit1 == bit2) continue;
                        else if (bit2 == 1 && bit1 == 0) {
                            change = -1;
                            break;
                        } else change++;
                    }
                    if (change != -1) which[i].add(new int[]{mask, change});
                }
            }

            int[][] where = new int[n + 1][k + 1];
            int[][] max = new int[n + 1][k + 1];
            SequenceUtils.deepFill(where, -1);
            where[0][0] = 0;
            for (int i = 1; i <= n; i++) {
                for (int j = 0; j <= k; j++) {
                    for (int[] temp : which[ar[i - 1]]) {
                        if (j - temp[1] >= 0 && j - temp[1] <= k && where[i - 1][j - temp[1]] != -1) {
                            where[i][j] = j - temp[1];
                            max[i][j] = temp[0];
                        }
                    }
                }
            }
            if (where[n][k] == -1) {
                out.println("-1");
                return;
            }
            int idx = n;
            int yes = k;
            while (idx >= 1) {
                out.append(indexOf(max[idx][yes]));
                yes = where[idx][yes];
                idx--;
            }
        }

    }

    static class SequenceUtils {
        public static void deepFill(Object array, int val) {
            if (!array.getClass().isArray()) {
                throw new IllegalArgumentException();
            }
            if (array instanceof int[]) {
                int[] intArray = (int[]) array;
                Arrays.fill(intArray, val);
            } else {
                Object[] objArray = (Object[]) array;
                for (Object obj : objArray) {
                    deepFill(obj, val);
                }
            }
        }

    }

    static class FastInput {
        private final InputStream is;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
        private byte[] buf = new byte[1 << 20];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private StringBuilder cache = new StringBuilder(1 << 20);
        private final Writer os;

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(int c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(String c) {
            cache.append(c);
            return this;
        }

        public FastOutput println(String c) {
            return append(c).println();
        }

        public FastOutput println() {
            cache.append(System.lineSeparator());
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class Bits {
        private Bits() {
        }

        public static int bitAt(int x, int i) {
            return (x >>> i) & 1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            BNastyaAndScoreboard solver = new BNastyaAndScoreboard();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class BNastyaAndScoreboard {
        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.readInt();
            int k = in.readInt();
            int[] bits = new int[10];
            bits[0] = parse(123567);
            bits[1] = parse(36);
            bits[2] = parse(13457);
            bits[3] = parse(13467);
            bits[4] = parse(2346);
            bits[5] = parse(12467);
            bits[6] = parse(124567);
            bits[7] = parse(136);
            bits[8] = parse(1234567);
            bits[9] = parse(123467);

//        for (int i = 0; i < 10; i++) {
//            debug.debug("i", i);
//            debug.debug("bits[i]", Integer.toString(bits[i], 2));
//        }

            int[] now = new int[n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < 7; j++) {
                    if (in.readChar() == '1') {
                        now[i] |= 1 << j;
                    }
                }
//            debug.debug("i", i);
//            debug.debug("now[i]", Integer.toString(now[i], 2));
            }

            SequenceUtils.reverse(now, 0, n - 1);
            int[][] dp = new int[n + 1][k + 1];
            SequenceUtils.deepFill(dp, -1);
            dp[0][0] = 0;

            for (int i = 1; i <= n; i++) {
                int v = now[i - 1];
                for (int j = 0; j < 10; j++) {
                    if ((bits[j] | v) != bits[j]) {
                        continue;
                    }
                    int change = Integer.bitCount(bits[j] - v);
                    for (int t = change; t <= k; t++) {
                        if (dp[i - 1][t - change] >= 0) {
                            dp[i][t] = j;
                        }
                    }
                }
            }

            //  debug.debug("dp", dp);

            if (dp[n][k] == -1) {
                out.println(-1);
                return;
            }

            int j = k;
            for (int i = n; i >= 1; i--) {
                int v = now[i - 1];
                out.append(dp[i][j]);
                j -= Integer.bitCount(bits[dp[i][j]] - v);
            }

        }

        public int parse(int val) {
            if (val == 0) {
                return 0;
            }
            return parse(val / 10) | (1 << (val % 10 - 1));
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private StringBuilder cache = new StringBuilder(10 << 20);
        private final Writer os;

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(int c) {
            cache.append(c);
            return this;
        }

        public FastOutput println(int c) {
            return append(c).println();
        }

        public FastOutput println() {
            cache.append(System.lineSeparator());
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 20];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public char readChar() {
            skipBlank();
            char c = (char) next;
            next = read();
            return c;
        }

    }

    static class SequenceUtils {
        public static void swap(int[] data, int i, int j) {
            int tmp = data[i];
            data[i] = data[j];
            data[j] = tmp;
        }

        public static void deepFill(Object array, int val) {
            if (!array.getClass().isArray()) {
                throw new IllegalArgumentException();
            }
            if (array instanceof int[]) {
                int[] intArray = (int[]) array;
                Arrays.fill(intArray, val);
            } else {
                Object[] objArray = (Object[]) array;
                for (Object obj : objArray) {
                    deepFill(obj, val);
                }
            }
        }

        public static void reverse(int[] data, int l, int r) {
            while (l < r) {
                swap(data, l, r);
                l++;
                r--;
            }
        }

    }
}


import java.io.*;
import java.util.*;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class NastyaAndScoreboard {
    static int n;
    static int[][] changes;
    static int[][][] dp;
    static StringBuilder ans = new StringBuilder();

    public static void main(String[] args)throws Exception {

        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastInput in = new FastInput(inputStream);
        FastOutput out = new FastOutput(outputStream);

        char[][] digit = new char[10][8];
        dp = new int[11][2005][2005];
        for (int[][] x : dp) {
            for (int[] y : x) {
                Arrays.fill(y, -1);
            }
        }

        for (int i = 0; i < 10; i++) {
            String str = "01111111";
            for (int j = 0; j < 8; j++) {
                digit[i] = str.toCharArray();
            }
        }
        digit[0][4] = '0';
        digit[1][1] = digit[1][2] = digit[1][4] = digit[1][5] = digit[1][7] = '0';
        digit[2][2] = digit[2][6] = '0';
        digit[3][2] = digit[3][5] = '0';
        digit[4][1] = digit[4][5] = digit[4][7] = '0';
        digit[5][3] = digit[5][5] = '0';
        digit[6][3] = '0';
        digit[7][2] = digit[7][4] = digit[7][5] = digit[7][7] = '0';
        digit[9][5] = '0';

        String[] digits = new String[10];

        for (int i = 0; i < 10; i++) {
            StringBuilder str = new StringBuilder();
            for (int j = 0; j < 8; j++) {
                str.append(digit[i][j]);
            }
            digits[i] = str.toString();
        }

        n = in.readInt();
        int k = in.readInt();
        String[] cal = new String[n];
        for (int i = 0; i < n; i++) {
            cal[i] = '0' + in.readString();
        }

        changes = new int[10][2005];

        for (int i = 9; i >= 0; i--) {
            for (int ind = 0; ind < n; ind++) {
                int cnt = 0;
                String s1 = digits[i];
                String s2 = cal[ind];

                for (int j = 0; j < 8; j++) {
                    if (s1.charAt(j) == '1' && s2.charAt(j) == '0') {
                        cnt++;
                    } else if (s1.charAt(j) == '0' && s2.charAt(j) == '1') {
                        cnt = -1;
                        break;
                    }
                }
                changes[i][ind] = cnt;
            }
        }

        int val = compute(0, k, 0);

        findans(0, k, 0);
        if (ans.length() == 0) {
            out.println("-1");
        } else {
            out.println(ans.toString());
        }
        out.flush();
    }

    public static void findans(int ind, int sum, int num) {
        if (ind == n)
            return;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            if (sum - cnt >= 0 && cnt != -1 && dp[i][ind + 1][sum - cnt] == 1) {
                char c = (char) (48 + i);
                ans.append(c);
                findans(ind + 1, sum - changes[i][ind], i);
                return;
            }
        }
    }

    public static int compute(int ind, int sum, int num) {

        if (ind == n) {
            if (sum == 0) {
                return dp[num][ind][sum] = 1;
            }
            return dp[num][ind][sum] = 0;
        }

        if (sum < 0)
            return dp[num][ind][sum] = 0;

        if (dp[num][ind][sum] != -1)
            return dp[num][ind][sum];

        int maxi = 0;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            char c = (char) (48 + i);
            if (cnt != -1 && cnt <= sum)
                maxi = Math.max(maxi, compute(ind + 1, sum - cnt, i));
        }
        return dp[num][ind][sum] = maxi;
    }

    static class FastInput {
        private final InputStream is;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
        private byte[] buf = new byte[1 << 20];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private StringBuilder cache = new StringBuilder(1 << 20);
        private final Writer os;

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(int c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(String c) {
            cache.append(c);
            return this;
        }

        public FastOutput println(String c) {
            return append(c).println();
        }

        public FastOutput println() {
            cache.append(System.lineSeparator());
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static class Bits {
        private Bits() {
        }

        public static int bitAt(int x, int i) {
            return (x >>> i) & 1;
        }

    }

    static class SequenceUtils {
        public static void swap(int[] data, int i, int j) {
            int tmp = data[i];
            data[i] = data[j];
            data[j] = tmp;
        }

        public static void deepFill(Object array, int val) {
            if (!array.getClass().isArray()) {
                throw new IllegalArgumentException();
            }
            if (array instanceof int[]) {
                int[] intArray = (int[]) array;
                Arrays.fill(intArray, val);
            } else {
                Object[] objArray = (Object[]) array;
                for (Object obj : objArray) {
                    deepFill(obj, val);
                }
            }
        }

        public static void reverse(int[] data, int l, int r) {
            while (l < r) {
                swap(data, l, r);
                l++;
                r--;
            }
        }

    }

}


//package Problemset.Tag.DynamicProgramming;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

public class NastyaAndScoreboard {
    static int n;
    static int[][] changes;
    static int[][][] dp;
    static StringBuilder ans = new StringBuilder();

    public static void main(String[] args) {
        InputReader s = new InputReader(System.in);

        char[][] digit = new char[10][8];
        dp = new int[11][2005][2005];
        for (int[][] x : dp) {
            for (int[] y : x) {
                Arrays.fill(y, -1);
            }
        }

        for (int i = 0; i < 10; i++) {
            String str = "01111111";
            for (int j = 0; j < 8; j++) {
                digit[i] = str.toCharArray();
            }
        }
        digit[0][4] = '0';
        digit[1][1] = digit[1][2] = digit[1][4] = digit[1][5] = digit[1][7] = '0';
        digit[2][2] = digit[2][6] = '0';
        digit[3][2] = digit[3][5] = '0';
        digit[4][1] = digit[4][5] = digit[4][7] = '0';
        digit[5][3] = digit[5][5] = '0';
        digit[6][3] = '0';
        digit[7][2] = digit[7][4] = digit[7][5] = digit[7][7] = '0';
        digit[9][5] = '0';

        String[] digits = new String[10];

        for (int i = 0; i < 10; i++) {
            StringBuilder str = new StringBuilder();
            for (int j = 0; j < 8; j++) {
                str.append(digit[i][j]);
            }
            digits[i] = str.toString();
        }

        n = s.nextInt();
        int k = s.nextInt();
        String[] cal = new String[n];
        for (int i = 0; i < n; i++) {
            cal[i] = '0' + s.nextString();
        }

        changes = new int[10][2005];

        for (int i = 9; i >= 0; i--) {
            for (int ind = 0; ind < n; ind++) {
                int cnt = 0;
                String s1 = digits[i];
                String s2 = cal[ind];

                for (int j = 0; j < 8; j++) {
                    if (s1.charAt(j) == '1' && s2.charAt(j) == '0') {
                        cnt++;
                    } else if (s1.charAt(j) == '0' && s2.charAt(j) == '1') {
                        cnt = -1;
                        break;
                    }
                }
                changes[i][ind] = cnt;
            }
        }

        int val = compute(0, k, 0);

        findans(0, k, 0);
        PrintWriter out = new PrintWriter(System.out);
        if (ans.length() == 0) {
            out.println(-1);
        } else {
            out.println(ans);
        }
        out.flush();
    }

    public static void findans(int ind, int sum, int num) {
        if (ind == n)
            return;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            if (sum - cnt >= 0 && cnt != -1 && dp[i][ind + 1][sum - cnt] == 1) {
                char c = (char) (48 + i);
                ans.append(c);
                findans(ind + 1, sum - changes[i][ind], i);
                return;
            }
        }
    }

    public static int compute(int ind, int sum, int num) {

        if (ind == n) {
            if (sum == 0) {
                return dp[num][ind][sum] = 1;
            }
            return dp[num][ind][sum] = 0;
        }

        if (sum < 0)
            return dp[num][ind][sum] = 0;

        if (dp[num][ind][sum] != -1)
            return dp[num][ind][sum];

        int maxi = 0;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            char c = (char) (48 + i);
            if (cnt != -1 && cnt <= sum)
                maxi = Math.max(maxi, compute(ind + 1, sum - cnt, i));
        }
        return dp[num][ind][sum] = maxi;
    }

    // Input
    private static class InputReader {
        private final InputStream is;
        private final byte[] inbuf = new byte[1024];
        private int lenbuf = 0;
        private int ptrbuf = 0;

        public InputReader(InputStream stream) {
            is = stream;
        }

        private int readByte() {
            if (lenbuf == -1)
                throw new InputMismatchException();
            if (ptrbuf >= lenbuf) {
                ptrbuf = 0;
                try {
                    lenbuf = is.read(inbuf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0)
                    return -1;
            }
            return inbuf[ptrbuf++];
        }

        private boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        private int skip() {
            int b;
            while ((b = readByte()) != -1 && isSpaceChar(b))
                ;
            return b;
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

        public char nextChar() {
            return (char) skip();
        }

        public String nextString() {
            int b = skip();
            StringBuilder sb = new StringBuilder();
            while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public char[] nextString(int n) {
            char[] buf = new char[n];
            int b = skip(), p = 0;
            while (p < n && !(isSpaceChar(b))) {
                buf[p++] = (char) b;
                b = readByte();
            }
            return n == p ? buf : Arrays.copyOf(buf, p);
        }

        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
                ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

        public long nextLong() {
            long num = 0;
            int b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
                ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }
    }
}


//package Problemset.Tag.DynamicProgramming;

import java.util.*;
import java.io.*;

public class NastyaAndScoreboard {
    static int n;
    static int[][] changes;
    static int[][][] dp;
    static StringBuilder ans = new StringBuilder();

    public static void main(String[] args)throws Exception {
        Reader s = new Reader();

        char[][] digit = new char[10][8];
        dp = new int[2005][2005][11];
        for (int i = 0; i < 2005; i++) {
            for (int j = 0; j < 2005; j++) {
                for (int l = 0; l < 11; l++) {
                    dp[i][j][l] = -1;
                }
            }
        }

        for (int i = 0; i < 10; i++) {
            String str = "01111111";
            for (int j = 0; j < 8; j++) {
                digit[i] = str.toCharArray();
            }
        }
        digit[0][4] = '0';
        digit[1][1] = digit[1][2] = digit[1][4] = digit[1][5] = digit[1][7] = '0';
        digit[2][2] = digit[2][6] = '0';
        digit[3][2] = digit[3][5] = '0';
        digit[4][1] = digit[4][5] = digit[4][7] = '0';
        digit[5][3] = digit[5][5] = '0';
        digit[6][3] = '0';
        digit[7][2] = digit[7][4] = digit[7][5] = digit[7][7] = '0';
        digit[9][5] = '0';

        String[] digits = new String[10];

        for (int i = 0; i < 10; i++) {
            String str = "";
            for (int j = 0; j < 8; j++) {
                str = str + digit[i][j];
            }
            digits[i] = str;
        }

        n = s.nextInt();
        int k = s.nextInt();
        String[] cal = new String[n];
        for (int i = 0; i < n; i++) {
            cal[i] = '0' + s.readLine();
        }

        changes = new int[2005][10];

        for (int ind = 0; ind < n; ind++) {
            for (int i = 9; i >= 0; i--) {
                int cnt = 0;
                String s1 = digits[i];
                String s2 = cal[ind];

                for (int j = 0; j < 8; j++) {
                    if (s1.charAt(j) == '1' && s2.charAt(j) == '0') {
                        cnt++;
                    } else if (s1.charAt(j) == '0' && s2.charAt(j) == '1') {
                        cnt = -1;
                        break;
                    }
                }
                changes[ind][i] = cnt;
            }
        }

        int val = compute(0, k, 0);

        findans(0, k, 0);
        if (ans.length() != 0) {
            System.out.println(ans);
        } else{
            System.out.println(-1);
        }
    }

    public static void findans(int ind, int sum, int num) {
        if (ind == n)
            return;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[ind][i];
            if (sum - cnt >= 0 && cnt != -1 && dp[ind + 1][sum - cnt][i] == 1) {
                char c = (char) (48 + i);
                ans.append(c);
                findans(ind + 1, sum - changes[ind][i], i);
                return;
            }
        }
    }

    public static int compute(int ind, int sum, int num) {

        if (ind == n) {
            if (sum == 0) {
                return dp[ind][sum][num] = 1;
            }
            return dp[ind][sum][num] = 0;
        }

        if (sum < 0)
            return dp[ind][sum][num] = 0;

        if (dp[ind][sum][num] != -1)
            return dp[ind][sum][num];

        int maxi = 0;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[ind][i];
            char c = (char) (48 + i);
            if (cnt != -1 && cnt <= sum)
                maxi = Math.max(maxi, compute(ind + 1, sum - cnt, i));
        }
        return dp[ind][sum][num] = maxi;
    }

}

class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}


import java.io.*;
import java.util.*;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class NastyaAndScoreboard {
    static int n;
    static int[][] changes;
    static int[][][] dp;
    static StringBuilder ans = new StringBuilder();

    public static void main(String[] args)throws Exception {
        Reader s = new Reader();

        char[][] digit = new char[10][8];
        dp = new int[11][2005][2005];
        for (int[][] x : dp) {
            for (int[] y : x) {
                Arrays.fill(y, -1);
            }
        }

        for (int i = 0; i < 10; i++) {
            String str = "01111111";
            for (int j = 0; j < 8; j++) {
                digit[i] = str.toCharArray();
            }
        }
        digit[0][4] = '0';
        digit[1][1] = digit[1][2] = digit[1][4] = digit[1][5] = digit[1][7] = '0';
        digit[2][2] = digit[2][6] = '0';
        digit[3][2] = digit[3][5] = '0';
        digit[4][1] = digit[4][5] = digit[4][7] = '0';
        digit[5][3] = digit[5][5] = '0';
        digit[6][3] = '0';
        digit[7][2] = digit[7][4] = digit[7][5] = digit[7][7] = '0';
        digit[9][5] = '0';

        String[] digits = new String[10];

        for (int i = 0; i < 10; i++) {
            StringBuilder str = new StringBuilder();
            for (int j = 0; j < 8; j++) {
                str.append(digit[i][j]);
            }
            digits[i] = str.toString();
        }

        n = s.nextInt();
        int k = s.nextInt();
        String[] cal = new String[n];
        for (int i = 0; i < n; i++) {
            cal[i] = '0' + s.readLine();
        }

        changes = new int[10][2005];

        for (int i = 9; i >= 0; i--) {
            for (int ind = 0; ind < n; ind++) {
                int cnt = 0;
                String s1 = digits[i];
                String s2 = cal[ind];

                for (int j = 0; j < 8; j++) {
                    if (s1.charAt(j) == '1' && s2.charAt(j) == '0') {
                        cnt++;
                    } else if (s1.charAt(j) == '0' && s2.charAt(j) == '1') {
                        cnt = -1;
                        break;
                    }
                }
                changes[i][ind] = cnt;
            }
        }

        int val = compute(0, k, 0);

        findans(0, k, 0);
        PrintWriter out = new PrintWriter(System.out);
        if (ans.length() == 0) {
            out.println(-1);
        } else {
            out.println(ans);
        }
        out.flush();
    }

    public static void findans(int ind, int sum, int num) {
        if (ind == n)
            return;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            if (sum - cnt >= 0 && cnt != -1 && dp[i][ind + 1][sum - cnt] == 1) {
                char c = (char) (48 + i);
                ans.append(c);
                findans(ind + 1, sum - changes[i][ind], i);
                return;
            }
        }
    }

    public static int compute(int ind, int sum, int num) {

        if (ind == n) {
            if (sum == 0) {
                return dp[num][ind][sum] = 1;
            }
            return dp[num][ind][sum] = 0;
        }

        if (sum < 0)
            return dp[num][ind][sum] = 0;

        if (dp[num][ind][sum] != -1)
            return dp[num][ind][sum];

        int maxi = 0;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            char c = (char) (48 + i);
            if (cnt != -1 && cnt <= sum)
                maxi = Math.max(maxi, compute(ind + 1, sum - cnt, i));
        }
        return dp[num][ind][sum] = maxi;
    }

    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }

}


//import java.io.*;
import java.util.*;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class NastyaAndScoreboard {
    static int n;
    static int[][] changes;
    static int[][][] dp;
    static StringBuilder ans = new StringBuilder();

    public static void main(String[] args)throws Exception {
        Reader s = new Reader();

        char[][] digit = new char[10][8];
        dp = new int[11][2005][2005];
        for (int[][] x : dp) {
            for (int[] y : x) {
                Arrays.fill(y, -1);
            }
        }

        for (int i = 0; i < 10; i++) {
            String str = "01111111";
            for (int j = 0; j < 8; j++) {
                digit[i] = str.toCharArray();
            }
        }
        digit[0][4] = '0';
        digit[1][1] = digit[1][2] = digit[1][4] = digit[1][5] = digit[1][7] = '0';
        digit[2][2] = digit[2][6] = '0';
        digit[3][2] = digit[3][5] = '0';
        digit[4][1] = digit[4][5] = digit[4][7] = '0';
        digit[5][3] = digit[5][5] = '0';
        digit[6][3] = '0';
        digit[7][2] = digit[7][4] = digit[7][5] = digit[7][7] = '0';
        digit[9][5] = '0';

        String[] digits = new String[10];

        for (int i = 0; i < 10; i++) {
            StringBuilder str = new StringBuilder();
            for (int j = 0; j < 8; j++) {
                str.append(digit[i][j]);
            }
            digits[i] = str.toString();
        }

        n = s.nextInt();
        int k = s.nextInt();
        String[] cal = new String[n];
        for (int i = 0; i < n; i++) {
            cal[i] = '0' + s.readLine();
        }

        changes = new int[10][2005];

        for (int i = 9; i >= 0; i--) {
            for (int ind = 0; ind < n; ind++) {
                int cnt = 0;
                String s1 = digits[i];
                String s2 = cal[ind];

                for (int j = 0; j < 8; j++) {
                    if (s1.charAt(j) == '1' && s2.charAt(j) == '0') {
                        cnt++;
                    } else if (s1.charAt(j) == '0' && s2.charAt(j) == '1') {
                        cnt = -1;
                        break;
                    }
                }
                changes[i][ind] = cnt;
            }
        }

        int val = compute(0, k, 0);

        findans(0, k, 0);
        PrintWriter out = new PrintWriter(System.out);
        if (ans.length() == 0) {
            out.println(-1);
        } else {
            out.println(ans);
        }
        out.flush();
    }

    public static void findans(int ind, int sum, int num) {
        if (ind == n)
            return;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            if (sum - cnt >= 0 && cnt != -1 && dp[i][ind + 1][sum - cnt] == 1) {
                char c = (char) (48 + i);
                ans.append(c);
                findans(ind + 1, sum - changes[i][ind], i);
                return;
            }
        }
    }

    public static int compute(int ind, int sum, int num) {

        if (ind == n) {
            if (sum == 0) {
                return dp[num][ind][sum] = 1;
            }
            return dp[num][ind][sum] = 0;
        }

        if (sum < 0)
            return dp[num][ind][sum] = 0;

        if (dp[num][ind][sum] != -1)
            return dp[num][ind][sum];

        int maxi = 0;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            char c = (char) (48 + i);
            if (cnt != -1 && cnt <= sum)
                maxi = Math.max(maxi, compute(ind + 1, sum - cnt, i));
        }
        return dp[num][ind][sum] = maxi;
    }

    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }

}


package Problemset.Tag.DynamicProgramming;

import java.io.*;
import java.util.*;
import java.io.IOException;
public class NastyaAndScoreboard {
    static int n;
    static int[][] changes;
    static int[][][] dp;
    static StringBuilder ans = new StringBuilder();

    public static void main(String[] args)throws Exception {

        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastInput in = new FastInput(inputStream);
        FastOutput out = new FastOutput(outputStream);

        char[][] digit = new char[10][8];
        dp = new int[11][2005][2005];
        for (int[][] x : dp) {
            for (int[] y : x) {
                Arrays.fill(y, -1);
            }
        }

        for (int i = 0; i < 10; i++) {
            String str = "01111111";
            for (int j = 0; j < 8; j++) {
                digit[i] = str.toCharArray();
            }
        }
        digit[0][4] = '0';
        digit[1][1] = digit[1][2] = digit[1][4] = digit[1][5] = digit[1][7] = '0';
        digit[2][2] = digit[2][6] = '0';
        digit[3][2] = digit[3][5] = '0';
        digit[4][1] = digit[4][5] = digit[4][7] = '0';
        digit[5][3] = digit[5][5] = '0';
        digit[6][3] = '0';
        digit[7][2] = digit[7][4] = digit[7][5] = digit[7][7] = '0';
        digit[9][5] = '0';

        String[] digits = new String[10];

        for (int i = 0; i < 10; i++) {
            StringBuilder str = new StringBuilder();
            for (int j = 0; j < 8; j++) {
                str.append(digit[i][j]);
            }
            digits[i] = str.toString();
        }

        n = in.readInt();
        int k = in.readInt();
        String[] cal = new String[n];
        for (int i = 0; i < n; i++) {
            cal[i] = '0' + in.readString();
        }

        changes = new int[10][2005];

        for (int i = 9; i >= 0; i--) {
            for (int ind = 0; ind < n; ind++) {
                int cnt = 0;
                String s1 = digits[i];
                String s2 = cal[ind];

                for (int j = 0; j < 8; j++) {
                    if (s1.charAt(j) == '1' && s2.charAt(j) == '0') {
                        cnt++;
                    } else if (s1.charAt(j) == '0' && s2.charAt(j) == '1') {
                        cnt = -1;
                        break;
                    }
                }
                changes[i][ind] = cnt;
            }
        }

        int val = compute(0, k, 0);

        findans(0, k, 0);
        if (ans.length() == 0) {
            out.println("-1");
        } else {
            out.println(ans.toString());
        }
        out.flush();
    }

    public static void findans(int ind, int sum, int num) {
        if (ind == n)
            return;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            if (sum - cnt >= 0 && cnt != -1 && dp[i][ind + 1][sum - cnt] == 1) {
                char c = (char) (48 + i);
                ans.append(c);
                findans(ind + 1, sum - changes[i][ind], i);
                return;
            }
        }
    }

    public static int compute(int ind, int sum, int num) {

        if (ind == n) {
            if (sum == 0) {
                return dp[num][ind][sum] = 1;
            }
            return dp[num][ind][sum] = 0;
        }

        if (sum < 0)
            return dp[num][ind][sum] = 0;

        if (dp[num][ind][sum] != -1)
            return dp[num][ind][sum];

        int maxi = 0;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[i][ind];
            char c = (char) (48 + i);
            if (cnt != -1 && cnt <= sum)
                maxi = Math.max(maxi, compute(ind + 1, sum - cnt, i));
        }
        return dp[num][ind][sum] = maxi;
    }

    static class FastInput {
        private final InputStream is;
        private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
        private byte[] buf = new byte[1 << 20];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }

    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private StringBuilder cache = new StringBuilder(1 << 20);
        private final Writer os;

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(int c) {
            cache.append(c);
            return this;
        }

        public FastOutput append(String c) {
            cache.append(c);
            return this;
        }

        public FastOutput println(String c) {
            return append(c).println();
        }

        public FastOutput println() {
            cache.append(System.lineSeparator());
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

}


//package Problemset.Tag.DynamicProgramming;

import java.util.*;
import java.io.*;

public class NastyaAndScoreboard {
    static int n;
    static int[][] changes;
    static int[][][] dp;
    static StringBuilder ans = new StringBuilder();

    public static void main(String[] args)throws Exception {
        Reader s = new Reader();

        char[][] digit = new char[10][8];
        dp = new int[2005][2005][11];
        for (int i = 0; i < 2005; i++) {
            for (int j = 0; j < 2005; j++) {
                for (int l = 0; l < 11; l++) {
                    dp[i][j][l] = -1;
                }
            }
        }

        for (int i = 0; i < 10; i++) {
            String str = "01111111";
            for (int j = 0; j < 8; j++) {
                digit[i] = str.toCharArray();
            }
        }
        digit[0][4] = '0';
        digit[1][1] = digit[1][2] = digit[1][4] = digit[1][5] = digit[1][7] = '0';
        digit[2][2] = digit[2][6] = '0';
        digit[3][2] = digit[3][5] = '0';
        digit[4][1] = digit[4][5] = digit[4][7] = '0';
        digit[5][3] = digit[5][5] = '0';
        digit[6][3] = '0';
        digit[7][2] = digit[7][4] = digit[7][5] = digit[7][7] = '0';
        digit[9][5] = '0';

        String[] digits = new String[10];

        for (int i = 0; i < 10; i++) {
            String str = "";
            for (int j = 0; j < 8; j++) {
                str = str + digit[i][j];
            }
            digits[i] = str;
        }

        n = s.nextInt();
        int k = s.nextInt();
        String[] cal = new String[n];
        for (int i = 0; i < n; i++) {
            cal[i] = '0' + s.next();
        }

        changes = new int[2005][10];

        for (int ind = 0; ind < n; ind++) {
            for (int i = 9; i >= 0; i--) {
                int cnt = 0;
                String s1 = digits[i];
                String s2 = cal[ind];

                for (int j = 0; j < 8; j++) {
                    if (s1.charAt(j) == '1' && s2.charAt(j) == '0') {
                        cnt++;
                    } else if (s1.charAt(j) == '0' && s2.charAt(j) == '1') {
                        cnt = -1;
                        break;
                    }
                }
                changes[ind][i] = cnt;
            }
        }

        int val = compute(0, k, 0);

        findans(0, k, 0);
        if (ans.length() != 0) {
            System.out.println(ans);
        } else{
            System.out.println(-1);
        }
    }

    public static void findans(int ind, int sum, int num) {
        if (ind == n)
            return;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[ind][i];
            if (sum - cnt >= 0 && cnt != -1 && dp[ind + 1][sum - cnt][i] == 1) {
                char c = (char) (48 + i);
                ans.append(c);
                findans(ind + 1, sum - changes[ind][i], i);
                return;
            }
        }
    }

    public static int compute(int ind, int sum, int num) {

        if (ind == n) {
            if (sum == 0) {
                return dp[ind][sum][num] = 1;
            }
            return dp[ind][sum][num] = 0;
        }

        if (sum < 0)
            return dp[ind][sum][num] = 0;

        if (dp[ind][sum][num] != -1)
            return dp[ind][sum][num];

        int maxi = 0;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[ind][i];
            char c = (char) (48 + i);
            if (cnt != -1 && cnt <= sum)
                maxi = Math.max(maxi, compute(ind + 1, sum - cnt, i));
        }
        return dp[ind][sum][num] = maxi;
    }

}

class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String nextLine() throws IOException {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isEndOfLine(c));
        return res.toString();
    }

    public String next() throws IOException {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public int nextInt() throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public int[] nextIntArray(int n) throws IOException {
        int a[] = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public int[][] next2dIntArray(int n, int m) throws IOException {
        int a[][] = new int[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                a[i][j] = nextInt();
        return a;
    }

    public char nextChar() throws IOException {
        return next().charAt(0);
    }

    public long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (c == '.') {
            while ((c = read()) >= '0' && c <= '9') {
                ret += (c - '0') / (div *= 10);
            }
        }
        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException {
        if (din == null)
            return;
        din.close();
    }

    public boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    private boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }

    public static long[] gcdExtended(int a, int b) {
        if (a == 0)
            return new long[]{0, 1, b};
        long[] ans = gcdExtended(b % a, a);
        return new long[]{ans[1] - (b / a) * ans[0], ans[0], ans[2]};
    }

    public static long gcd(long a, long b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    public static long modularExpo(long x, long y, long p) {
        long res = 1;
        x %= p;
        if (x == 0)
            return 0;
        while (y > 0) {
            if ((y & 1) == 1)
                res = (res * x) % p;
            y >>= 1;
            x = (x * x) % p;
        }
        return res;
    }
}


//package Problemset.Tag.DynamicProgramming;

import java.util.*;
import java.io.*;

public class NastyaAndScoreboard {
    static int n;
    static int[][] changes;
    static int[][][] dp;
    static String ans = "";

    public static void main(String[] args)throws Exception {
        Reader s = new Reader();

        char[][] digit = new char[10][8];
        dp = new int[2005][2005][11];
        for (int i = 0; i < 2005; i++) {
            for (int j = 0; j < 2005; j++) {
                for (int l = 0; l < 11; l++) {
                    dp[i][j][l] = -1;
                }
            }
        }

        for (int i = 0; i < 10; i++) {
            String str = "01111111";
            for (int j = 0; j < 8; j++) {
                digit[i] = str.toCharArray();
            }
        }
        digit[0][4] = '0';
        digit[1][1] = digit[1][2] = digit[1][4] = digit[1][5] = digit[1][7] = '0';
        digit[2][2] = digit[2][6] = '0';
        digit[3][2] = digit[3][5] = '0';
        digit[4][1] = digit[4][5] = digit[4][7] = '0';
        digit[5][3] = digit[5][5] = '0';
        digit[6][3] = '0';
        digit[7][2] = digit[7][4] = digit[7][5] = digit[7][7] = '0';
        digit[9][5] = '0';

        String[] digits = new String[10];

        for (int i = 0; i < 10; i++) {
            String str = "";
            for (int j = 0; j < 8; j++) {
                str = str + digit[i][j];
            }
            digits[i] = str;
        }

        n = s.nextInt();
        int k = s.nextInt();
        String[] cal = new String[n];
        for (int i = 0; i < n; i++) {
            cal[i] = '0' + s.next();
        }

        changes = new int[2005][10];

        for (int ind = 0; ind < n; ind++) {
            for (int i = 9; i >= 0; i--) {
                int cnt = 0;
                String s1 = digits[i];
                String s2 = cal[ind];

                for (int j = 0; j < 8; j++) {
                    if (s1.charAt(j) == '1' && s2.charAt(j) == '0') {
                        cnt++;
                    } else if (s1.charAt(j) == '0' && s2.charAt(j) == '1') {
                        cnt = -1;
                        break;
                    }
                }
                changes[ind][i] = cnt;
            }
        }

        int val = compute(0, k, 0);

        findans(0, k, 0);
        if (ans == "") {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }

    public static void findans(int ind, int sum, int num) {
        if (ind == n)
            return;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[ind][i];
            if (sum - cnt >= 0 && cnt != -1 && dp[ind + 1][sum - cnt][i] == 1) {
                char c = (char) (48 + i);
                ans += c;
                findans(ind + 1, sum - changes[ind][i], i);
                return;
            }
        }
    }

    public static int compute(int ind, int sum, int num) {

        if (ind == n) {
            if (sum == 0) {
                return dp[ind][sum][num] = 1;
            }
            return dp[ind][sum][num] = 0;
        }

        if (sum < 0)
            return dp[ind][sum][num] = 0;

        if (dp[ind][sum][num] != -1)
            return dp[ind][sum][num];

        int maxi = 0;

        for (int i = 9; i >= 0; i--) {
            int cnt = changes[ind][i];
            char c = (char) (48 + i);
            if (cnt != -1 && cnt <= sum)
                maxi = Math.max(maxi, compute(ind + 1, sum - cnt, i));
        }
        return dp[ind][sum][num] = maxi;
    }

}

class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String nextLine() throws IOException {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isEndOfLine(c));
        return res.toString();
    }

    public String next() throws IOException {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public int nextInt() throws IOException {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public int[] nextIntArray(int n) throws IOException {
        int a[] = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public int[][] next2dIntArray(int n, int m) throws IOException {
        int a[][] = new int[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                a[i][j] = nextInt();
        return a;
    }

    public char nextChar() throws IOException {
        return next().charAt(0);
    }

    public long nextLong() throws IOException {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        } while ((c = read()) >= '0' && c <= '9');
        if (c == '.') {
            while ((c = read()) >= '0' && c <= '9') {
                ret += (c - '0') / (div *= 10);
            }
        }
        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException {
        if (din == null)
            return;
        din.close();
    }

    public boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    private boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }

    public static long[] gcdExtended(int a, int b) {
        if (a == 0)
            return new long[]{0, 1, b};
        long[] ans = gcdExtended(b % a, a);
        return new long[]{ans[1] - (b / a) * ans[0], ans[0], ans[2]};
    }

    public static long gcd(long a, long b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    public static long modularExpo(long x, long y, long p) {
        long res = 1;
        x %= p;
        if (x == 0)
            return 0;
        while (y > 0) {
            if ((y & 1) == 1)
                res = (res * x) % p;
            y >>= 1;
            x = (x * x) % p;
        }
        return res;
    }
}