import static java.lang.Math.*;

import java.util.stream.*;

public class B {

	String [] D = {  "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" };

	public Object solve () {
		int N = sc.nextInt(), K = sc.nextInt();
		String [] Y = sc.next(N);

		int [] Z = new int [10];
		for (int i : rep(10))
			Z[i] = Integer.parseInt(D[i], 2);
		int [] BZ = new int [10];
		for (int i : rep(10))
			BZ[i] = Integer.bitCount(Z[i]);

		int [] X = new int [N];
		for (int i :rep(N))
			X[i] = Integer.parseInt(Y[i], 2);
		int [] BX = new int [N];
		for (int i : rep(N))
			BX[i] = Integer.bitCount(X[i]);

		boolean [][] F = new boolean [N+1][K+1]; F[N][0] = true;
		for (int i : sep(N))
			for (int j : rep(10))
				if ((X[i] | Z[j]) == Z[j]) {
					int q = BZ[j] - BX[i];
					for (int k : req(K - q))
						F[i][k + q] = F[i][k + q] || F[i+1][k];
				}

		if (F[0][K]) {
			StringBuilder res = new StringBuilder(); int Q = K;
			for (int i : rep(N))
				for (int j : sep(10))
					if ((X[i] | Z[j]) == Z[j]) {
						int q = BZ[j] - BX[i];
						if (q <= Q && F[i+1][Q - q]) {
							res.append(j);
							Q -= q;
							break;
						}
					}
			return res;
		}

		return -1;
	}

	private static final int CONTEST_TYPE = 1;

	private static void init () {
	}

	private static int [] rep (int N) { return rep(0, N); }
	private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
	private static int [] req (int N) { return req(0, N); }
	private static int [] req (int S, int T) { return rep(S, T+1); }
	private static int [] sep (int N) { return sep(0, N); }
	private static int [] sep (final int S, final int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[T-1-i] = i; return res; }
	//////////////////////////////////////////////////////////////////////////////////// OFF
	private static IOUtils.MyScanner sc = new IOUtils.MyScanner();
	private static class IOUtils {
		public static class MyScanner {
			public String next () { newLine(); return line[index++]; }
			public int nextInt () { return Integer.parseInt(next()); }
			public String[] next (int N) { return IntStream.range(0, N).mapToObj(i -> next()).toArray(String[]::new); }
			//////////////////////////////////////////////
			private boolean eol () { return index == line.length; }
			private String readLine () {
				try {
					return r.readLine();
				} catch (Exception e) {
					throw new Error (e);
				}
			}
			private final java.io.BufferedReader r;
			private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
			private MyScanner (java.io.BufferedReader r) {
				try {
					this.r = r;
					while (!r.ready())
						Thread.sleep(1);
					start();
				} catch (Exception e) {
					throw new Error(e);
				}
			}
			private String [] line;
			private int index;
			private void newLine () {
				if (line == null || eol()) {
					line = split(readLine());
					index = 0;
				}
			}
			private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
		}
		private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); }
		private static String buildDelim (String delim, Object o, Object ... A) {
			StringBuilder b = new StringBuilder();
			append(b, o, delim);
			for (Object p : A)
				append(b, p, delim);
			return b.substring(min(b.length(), delim.length()));
		}
		//////////////////////////////////////////////////////////////////////////////////
		private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########");
		private static void start () { if (t == 0) t = millis(); }
		private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, final Object o) {
			if (o.getClass().isArray()) {
				int len = java.lang.reflect.Array.getLength(o);
				for (int i = 0; i < len; ++i)
					f.accept(java.lang.reflect.Array.get(o, i));
			}
			else if (o instanceof Iterable<?>)
				((Iterable<?>)o).forEach(f::accept);
			else
				g.accept(o instanceof Double ? formatter.format(o) : o);
		}
		private static void append (final StringBuilder b, Object o, final String delim) {
			append(x -> { append(b, x, delim); }, x -> b.append(delim).append(x), o);
		}
		private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
		private static void print (Object o, Object ... A) {
			String res = build(o, A);
			if (DEBUG == 2)
				err(res, '(', time(), ')');
			if (res.length() > 0)
				pw.println(res);
			if (DEBUG == 1) {
				pw.flush();
				System.out.flush();
			}
		}
		private static void err (Object o, Object ... A) { System.err.println(build(o, A)); }
		private static int DEBUG;
		private static void exit () {
			String end = "------------------" + System.lineSeparator() + time();
			switch(DEBUG) {
			case 1: print(end); break;
			case 2: err(end); break;
			}
			IOUtils.pw.close();
			System.out.flush();
			System.exit(0);
		}
		private static long t;
		private static long millis () { return System.currentTimeMillis(); }
		private static String time () { return "Time: " + (millis() - t) / 1000.0; }
		private static void run (int N) {
			try { DEBUG = Integer.parseInt(System.getProperties().get("DEBUG").toString()); }
			catch (Throwable t) {}

			for (int n = 1; n <= N; ++n) {
				Object res = new B().solve();
				if (res != null) {
					@SuppressWarnings("all")
					Object o = CONTEST_TYPE == 0 ? "Case #" + n + ": " + build(res) : res;
					print(o);
				}
			}
			exit();
		}
	}
	////////////////////////////////////////////////////////////////////////////////////
	public static void main (String[] args) {
		init();
		@SuppressWarnings("all")
		int N = CONTEST_TYPE == 1 ? 1 : sc.nextInt();
		IOUtils.run(N);
	}
}


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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
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;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        String[] str = new String[]{"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            int K = in.nextInt();
            int[][][] str = new int[N][K + 1][2];
            int[] s = new int[N];
            for (int i = 0; i < N; i++) {
                s[i] = Integer.parseInt(in.next(), 2);
            }
            for (int i = 0; i < N; i++) {
                for (int k = 0; k <= K; k++) {
                    Arrays.fill(str[i][k], -1);
                }
            }
            for (int i = 0; i <= Math.min(K, 7); i++) {
                str[N - 1][i][0] = maximize(s[N - 1], i);
                str[N - 1][i][1] = 0;
            }
            for (int i = N - 2; i >= 0; i--) {
                int a = s[i];
                int[] opt = new int[8];
                for (int k = 0; k <= 7; k++) {
                    opt[k] = maximize(a, k);
                }
                for (int k = 0; k <= K; k++) {
                    for (int add = 0; add <= Math.min(k, 7); add++) {
                        if (str[i + 1][k - add][0] != -1 && opt[add] != -1) {
                            if (opt[add] > str[i][k][0]) {
                                str[i][k][0] = opt[add];
                                str[i][k][1] = k - add;
                            }
                        }
                    }
                }
            }
            out.println(rev(str));
        }

        int maximize(int bits, int K) {
            for (int i = 9; i >= 0; i--) {
                if (diff(bits, Integer.parseInt(str[i], 2)) == K) {
                    return i;
                }
            }
            return -1;
        }

        int diff(int a, int b) {
            //a is subset
            int cnt = 0;
            for (int i = 0; i <= 7; i++) {
                if (set(a, i) && !(set(b, i))) {
                    return -1;
                } else if (!set(a, i) && set(b, i)) {
                    cnt++;
                }
            }
            return cnt;
        }

        boolean set(int a, int i) {
            return ((a & (1 << i)) > 0);
        }

        String rev(int[][][] dp) {
            StringBuilder str = new StringBuilder();
            int K = dp[0].length - 1;
            for (int i = 0; i < dp.length; i++) {
                if (dp[i][K][0] == -1) {
                    return "-1";
                }
                str.append(dp[i][K][0]);
                K = dp[i][K][1];
            }
            return str.toString();
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

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

    }
}


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

public class B1340 {

	static int[] arr, num;

	public static int get(int have, int need) {
		int ans = 0;
		for (int i = 0; i < 8; i++) {
			boolean h = (have & (1 << i)) != 0;
			boolean n = (need & (1 << i)) != 0;
			if (h && !n) {
				return -1;
			}
			if (!h && n) {
				ans++;
			}
		}
		return ans;
	}

	static int[][] dp;
	static Queue<Integer> q = new LinkedList<Integer>();

	public static int solve(int idx, int rem) {
		if (rem < 0)
			return 10;
		if (idx == arr.length) {
			if (rem == 0) {
				return 0;
			} else {
				return 10;
			}
		}
		if (dp[idx][rem] != -1)
			return dp[idx][rem];
		for (int i = 9; i >= 0; i--) {
			int x = get(arr[idx], num[i]);
			if (x != -1) {
				if (solve(idx + 1, rem - x) != 10) {
					return dp[idx][rem] = i;
				}
			}
		}
		return dp[idx][rem] = 10;
	}

	public static void trace(int idx, int rem) {

		if (idx == arr.length) {
			return;
		}

		for (int i = 9; i >= 0; i--) {
			int x = get(arr[idx], num[i]);
			if (x != -1) {
				if (solve(idx + 1, rem - x) != 10) {
					q.add(i);
					trace(idx + 1, rem - x);
					return;
				}
			}
		}
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		int k = sc.nextInt();
		arr = sc.nextIntArr(n);
		String[] s = { "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010",
				"1111111", "1111011" };
		num = new int[10];
		for (int i = 0; i < s.length; i++) {
			num[i] = Integer.parseInt(s[i], 2);
		}
		dp = new int[n][k + 1];
		for (int[] x : dp) {
			Arrays.fill(x, -1);
		}
		if (solve(0, k) == 10) {
			pw.println(-1);
		} else {
			trace(0, k);
			while (!q.isEmpty()) {
				pw.print(q.poll());
			}
			pw.println();
		}

		pw.close();

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader r) {
			br = new BufferedReader(r);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

		public int nextBinaryInt() throws IOException {
			return Integer.parseInt(next(), 2);
		}

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

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < arr.length; i++)
				arr[i] = nextBinaryInt();
			return arr;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class B {
	static PrintWriter out = new PrintWriter(System.out);

	static int N,K;
	static int ar[];
	static int dp[][];
	static String ss[] = new String[] {
			"1110111",
			"0010010",
			"1011101",
			"1011011",
			"0111010",
			"1101011",
			"1101111",
			"1010010",
			"1111111",
			"1111011",
	};
	static int vs[];

	static int answer[];

	public static void main(String[] args) {
		FS in = new FS();
		N = in.nextInt();
		K = in.nextInt();
		ar = new int[N];
		for(int i = 0; i < N; i++) {
			char c[] = in.next().toCharArray();
			ar[i] = 0;
			int shift = 0;
			for(int j = c.length-1; j >= 0; j--) {
				if(c[j] == '1') ar[i] |= (1<<shift);
				shift++;
			}
		}

		vs = new int[10];
		for(int i = 0; i < 10; i++) {
			vs[i] = Integer.parseInt(ss[i], 2);
		}

		dp = new int[N+1][K+1];
		for(int a[] : dp) Arrays.fill(a, -1);

		int res = go(0, K);
		if(res == 0) {
			out.println("-1");
		}
		else {
			answer = new int[N];
			bb(0,K);
			for(int ii : answer) out.print(ii);;
			out.println();
		}
		out.close();
	}

	static int go(int id, int k) {
		if(id >= N) return k == 0 ? 1 : 0;
		if(dp[id][k] != -1) return dp[id][k];
		int res = 0;

		for(int to = 0; to <= 9; to++) {
			if( (vs[to] | ar[id]) != vs[to]) continue;
			int bc = Integer.bitCount(vs[to] ^ ar[id]);
			if(bc <= k) res |= go(id+1, k-bc);
		}

		return dp[id][k] = res;
	}

	static void bb(int id, int k) {
		if(id >= N) return;
		int res = 0;
		for(int to = 9; to >= 0; to--) {
			if( (vs[to] | ar[id]) != vs[to]) continue;
			int bc = Integer.bitCount(vs[to] ^ ar[id]);
			if(bc <= k) res |= go(id+1, k-bc);
			if(res == 1) {
				answer[id] = to;
				bb(id+1, k-bc);
				return;
			}
		}
	}

	static class FS{
		BufferedReader br;
		StringTokenizer st;
		public FS() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {st = new StringTokenizer(br.readLine());}
				catch(Exception e) { throw null;}
			}
			return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next());}
		double nextDouble() { return Double.parseDouble(next());}
		long nextLong() { return Long.parseLong(next());}
		int[] NIA(int n) {
			int r[] = new int[n];
			for(int i = 0; i < n; i++) r[i] = nextInt();
			return r;
		}
		long[] NLA(int n) {
			long r[] = new long[n];
			for(int i = 0; i < n; i++) r[i] = nextLong();
			return r;
		}
		char[][] grid(int r, int c){
			char res[][] = new char[r][c];
			for(int i = 0; i < r; i++) {
				char l[] = next().toCharArray();
				for(int j = 0; j < c; j++) {
					res[i][j] = l[j];
				}
			}
			return res;
		}
	}

}


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

public class B implements Runnable {
	boolean judge = false;
	FastReader scn;
	PrintWriter out;
	String INPUT = "2000 2000";

	void solve() {
		int n = scn.nextInt(), k = scn.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = Integer.valueOf(scn.next(), 2);
		}

		ans = new int[n];
		Arrays.fill(ans, -1);

		init();

		dp = new boolean[n][k + 1];
		vis = new boolean[n][k + 1];

		if (func(arr, 0, k)) {
			for (int a : ans) {
				out.print(a);
			}
			out.println();
		} else {
			out.println(-1);
		}
	}

	int[] ans;
	boolean[][] dp, vis;

	boolean func(int[] arr, int pos, int k) {
		if (pos == arr.length) {
			if (k == 0) {
				return true;
			}
			return false;
		}

		if (vis[pos][k]) {
			return dp[pos][k];
		}
		vis[pos][k] = true;

		for (int num = 9; num >= 0; num--) {
			int x = map[num];
			int y = arr[pos];
			int diff = Integer.bitCount(x) - Integer.bitCount(y);
			if ((x | y) > x || diff > k) {
				continue;
			}
			ans[pos] = num;
			if (func(arr, pos + 1, k - diff)) {
				return dp[pos][k] = true;
			}
			ans[pos] = -1;
		}

		return dp[pos][k] = false;
	}

	int[] map = new int[10];

	void init() {
		map[0] = Integer.valueOf("1110111", 2);
		map[1] = Integer.valueOf("0010010", 2);
		map[2] = Integer.valueOf("1011101", 2);
		map[3] = Integer.valueOf("1011011", 2);
		map[4] = Integer.valueOf("0111010", 2);
		map[5] = Integer.valueOf("1101011", 2);
		map[6] = Integer.valueOf("1101111", 2);
		map[7] = Integer.valueOf("1010010", 2);
		map[8] = Integer.valueOf("1111111", 2);
		map[9] = Integer.valueOf("1111011", 2);
	}

	public void run() {
		long time = System.currentTimeMillis();
		boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
		out = new PrintWriter(System.out);
		scn = new FastReader(oj);
		solve();
		out.flush();
		if (!oj) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

	public static void main(String[] args) {
		new Thread(null, new B(), "Main", 1 << 28).start();
	}

	class FastReader {
		InputStream is;

		public FastReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		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++];
		}

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

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

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

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

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

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

		char[] next(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);
		}

		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();
			}
		}

		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();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

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

		long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		int[][] next2DInt(int n, int m) {
			int[][] arr = new int[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextIntArray(m);
			}
			return arr;
		}

		long[][] next2DLong(int n, int m) {
			long[][] arr = new long[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLongArray(m);
			}
			return arr;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				int c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		long[] shuffle(long[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				long c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		int[] uniq(int[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			int[] rv = new int[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		long[] uniq(long[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			long[] rv = new long[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		int[] reverse(int[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		long[] reverse(long[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		int[] compress(int[] arr) {
			int n = arr.length;
			int[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}

		long[] compress(long[] arr) {
			int n = arr.length;
			long[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}
	}
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class NastyaAndScoreboard {

	private static int solve(int n, int k, List<char[]> digits) {
        return 8;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();    // no of nodes
        int k = sc.nextInt();   // no of  edges
        sc.nextLine();
        List<char[]> digits = new ArrayList<>(n);
        for (int i = 1; i <= n; i++) {
            digits.add(sc.next().toCharArray());
            sc.nextLine();
        }
        System.out.println(solve(n, k, digits));
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class NastyaAndScoreboard {
	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray()
	};

	private static String solve(int n, int k, char[][] given, int[][] changes) {
    	int[][][] dp = new int[2005][2005][11];
    	for(int[][] a : dp) {
    		for(int[] b: a) {
    			Arrays.fill(b, -1);
    		}
    	}
    	dfs(0, k, 0, n, k, given, changes, dp);
    	return findans(0, k, 0, new char[2005], n, changes, dp);
    }

    private static String findans(int index, int remk, int prevval, char[] result, int n, int[][] changes, int[][][] dp) {
    	if(dp[index][remk][prevval] < 0) return "-1";

    	if(index == n) {
    		return new String(result);
    	}

    	int val = dp[index][remk][prevval];
    	int count = changes[index][val];
    	result[index] = (char)(val + 48);
    	return findans(index + 1, remk - count, val, result, n, changes, dp);
    }

    private static boolean dfs(int index, int remk, int prevval, int n, int k, char[][] given, int[][] changes, int[][][] dp) {
    	if(index == n) {
    		if(remk == 0) {
    			dp[index][remk][prevval] = 0;
    			return true;
    		} else {
    			dp[index][remk][prevval] = -2;
    			return false;
    		}
    	}

    	if(dp[index][remk][prevval] == -2) return false;

    	if(dp[index][remk][prevval] >= 0) return true;

    	for(int val = 9; val >= 0; val--) {
    		int count = changes[index][val];
    		if(count != -1 && count <= remk) {
    			if(dfs(index + 1, remk - count, val, n, k, given, changes, dp)) {
    				dp[index][remk][prevval] = val;
    				return true;
    			}
    		}
    	}
    	dp[index][remk][prevval] = -2;
    	return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        sc.nextLine();
        int[][] changes = new int[2005][10];
        char[][] given = new char[2005][7];
        for (int i = 0; i < n; i++) {
            given[i] = sc.next().toCharArray();
            for(int c = 0; c <= 9; c++) {
            	int count = 0;
            	for(int j = 0; j < 7; j++) {
            		if(given[i][j] == '0' && digits[c][j] == '1') {
            			count++;
            		} else if(given[i][j] == '1' && digits[c][j] == '0') {
            			count = -1;
            			break;
            		}
            	}
            	changes[i][c] = count;
            }
            sc.nextLine();
        }

        System.out.println(solve(n, k, given, changes));
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class NastyaAndScoreboard {
	private static char[][] digits = new char[][] { "1110111".toCharArray(), "0010010".toCharArray(),
			"1011101".toCharArray(), "1011011".toCharArray(), "0111010".toCharArray(), "1101011".toCharArray(),
			"1101111".toCharArray(), "1010010".toCharArray(), "1111111".toCharArray(), "1111011".toCharArray() };

	private static String solve(int n, int k, char[][] given, int[][] changes) {
		int[][][] dp = new int[n + 1][k + 1][10];
		for (int[][] a : dp) {
			for (int[] b : a) {
				Arrays.fill(b, -1);
			}
		}
		dfs(0, k, 0, n, k, given, changes, dp);
		return findans(0, k, 0, new char[n], n, changes, dp);
	}

	private static String findans(int index, int remk, int prevval, char[] result, int n, int[][] changes,
			int[][][] dp) {
		if (dp[index][remk][prevval] < 0)
			return "-1";

		if (index == n) {
			return new String(result);
		}

		int val = dp[index][remk][prevval];
		int count = changes[index][val];
		result[index] = (char) (val + 48);
		return findans(index + 1, remk - count, val, result, n, changes, dp);
	}

	private static boolean dfs(int index, int remk, int prevval, int n, int k, char[][] given, int[][] changes,
			int[][][] dp) {
		if (index == n) {
			if (remk == 0) {
				dp[index][remk][prevval] = 0;
				return true;
			} else {
				dp[index][remk][prevval] = -2;
				return false;
			}
		}

		if (dp[index][remk][prevval] == -2)
			return false;

		if (dp[index][remk][prevval] >= 0)
			return true;

		for (int val = 9; val >= 0; val--) {
			int count = changes[index][val];
			if (count != -1 && count <= remk) {
				if (dfs(index + 1, remk - count, val, n, k, given, changes, dp)) {
					dp[index][remk][prevval] = val;
					return true;
				}
			}
		}
		dp[index][remk][prevval] = -2;
		return false;
	}

	public static void main(String[] args) {
		int n, k;
		int[][] changes;
		char[][] given;

		try (Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
			k = sc.nextInt();
			sc.nextLine();
			changes = new int[n][10];
			given = new char[n][7];
			for (int i = 0; i < n; i++) {
				given[i] = sc.next().toCharArray();
				for (int c = 0; c <= 9; c++) {
					int count = 0;
					for (int j = 0; j < 7; j++) {
						if (given[i][j] == '0' && digits[c][j] == '1') {
							count++;
						} else if (given[i][j] == '1' && digits[c][j] == '0') {
							count = -1;
							break;
						}
					}
					changes[i][c] = count;
				}
				sc.nextLine();
			}
		}

		System.out.println(solve(n, k, given, changes));
	}

}


import java.util.Arrays;
import java.util.Scanner;

public class NastyaAndScoreboard {
	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray()
	};

	private static String solve(int n, int k, char[][] given, int[][] changes) {
    	int[][][] dp = new int[2005][2005][11];
    	for(int[][] a : dp) {
    		for(int[] b: a) {
    			Arrays.fill(b, -1);
    		}
    	}
    	dfs(0, k, 0, n, k, given, changes, dp);
    	return findans(0, k, 0, "", n, changes, dp);
    }

    private static String findans(int index, int remk, int prevval, String ans, int n, int[][] changes, int[][][] dp) {
    	if(index == n) {
    		if(dp[index][remk][prevval] == 0) {
    			return ans;
    		} else {
    			return "-1";
    		}
    	}
    	int val = dp[index][remk][prevval];
    	if(val < 0) return "-1";
    	int count = changes[index][val];
    	return findans(index + 1, remk - count, val, ans + Integer.toString(val), n, changes, dp);
    }

    private static boolean dfs(int index, int remk, int prevval, int n, int k, char[][] given, int[][] changes, int[][][] dp) {
    	if(index == n) {
    		if(remk == 0) {
    			dp[index][remk][prevval] = 0;
    			return true;
    		} else {
    			dp[index][remk][prevval] = -2;
    			return false;
    		}
    	}

    	if(dp[index][remk][prevval] == -2) return false;

    	if(dp[index][remk][prevval] >= 0) return true;

    	for(int val = 9; val >= 0; val--) {
    		int count = changes[index][val];
    		if(count != -1 && count <= remk) {
    			if(dfs(index + 1, remk - count, val, n, k, given, changes, dp)) {
    				dp[index][remk][prevval] = val;
    				return true;
    			}
    		}
    	}
    	dp[index][remk][prevval] = -2;
    	return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        sc.nextLine();
        int[][] changes = new int[2005][10];
        char[][] given = new char[2005][7];
        for (int i = 0; i < n; i++) {
            given[i] = sc.next().toCharArray();
            for(int c = 0; c <= 9; c++) {
            	int count = 0;
            	for(int j = 0; j < 7; j++) {
            		if(given[i][j] == '0' && digits[c][j] == '1') {
            			count++;
            		} else if(given[i][j] == '1' && digits[c][j] == '0') {
            			count = -1;
            			break;
            		}
            	}
            	changes[i][c] = count;
            }
            sc.nextLine();
        }

        System.out.println(solve(n, k, given, changes));
    }

}


package codeforces.cbr1340;

import java.util.Scanner;

public class NastyaAndScoreboard {
	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray() };

	private static void solve(int n, int k, char[][] given, int[][] changes) {

		boolean[][] dp = new boolean[n + 1][k + 1];

		dp[n][0] = true;

		for(int i = n; i >0; i--) {
			for(int j = 0; j <= k; j++) {
				if(dp[i][j]) {
					for(int d = 0; d < 10; d++) {
						if(changes[i-1][d] != -1 && j + changes[i-1][d] <= k) {
							dp[i-1][j + changes[i-1][d]] = true;
						}
					}
				}
			}
		}

		if(!dp[0][k]) {
			System.out.println("-1");
			return;
		}

		for(int i = 0; i < n; i++) {
			for(int d = 9; d >=0; d--) {
				if(changes[i][d] != -1 && k >= changes[i][d] && dp[i + 1][k - changes[i][d]]) {
					System.out.print(d);
					k -= changes[i][d];
					break;
				}
			}
		}
	}

	public static void main(String[] args) {

		try (Scanner sc = new Scanner(System.in)) {
			int n = sc.nextInt();
			int k = sc.nextInt();
			sc.nextLine();
			int[][] changes = new int[n][10];
			char[][] given = new char[n][7];
			for (int i = 0; i < n; i++) {
				given[i] = sc.next().toCharArray();
				for (int c = 0; c <= 9; c++) {
					for (int j = 0; j < 7; j++) {
						if (given[i][j] == '0' && digits[c][j] == '1') {
							changes[i][c]++;
						} else if (given[i][j] == '1' && digits[c][j] == '0') {
							changes[i][c] = -1;
							break;
						}
					}
				}
				sc.nextLine();
			}
			solve(n, k, given, changes);
		}
	}

}


import java.util.Arrays;
import java.util.Scanner;

public class NastyaAndScoreboard {
	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray()
	};

	private static String solve(int n, int k, char[][] given, int[][] changes) {
    	int[][][] dp = new int[2005][2005][11];
    	for(int[][] a : dp) {
    		for(int[] b: a) {
    			Arrays.fill(b, -1);
    		}
    	}
    	dfs(0, k, 0, n, k, given, changes, dp);
    	return findans(0, k, 0, new char[n], n, changes, dp);
    }

    private static String findans(int index, int remk, int prevval, char[] result, int n, int[][] changes, int[][][] dp) {
    	if(dp[index][remk][prevval] < 0) return "-1";

    	if(index == n) {
    		return new String(result);
    	}

    	int val = dp[index][remk][prevval];
    	int count = changes[index][val];
    	result[index] = (char)(val + 48);
    	return findans(index + 1, remk - count, val, result, n, changes, dp);
    }

    private static boolean dfs(int index, int remk, int prevval, int n, int k, char[][] given, int[][] changes, int[][][] dp) {
    	if(index == n) {
    		if(remk == 0) {
    			dp[index][remk][prevval] = 0;
    			return true;
    		} else {
    			dp[index][remk][prevval] = -2;
    			return false;
    		}
    	}

    	if(dp[index][remk][prevval] == -2) return false;

    	if(dp[index][remk][prevval] >= 0) return true;

    	for(int val = 9; val >= 0; val--) {
    		int count = changes[index][val];
    		if(count != -1 && count <= remk) {
    			if(dfs(index + 1, remk - count, val, n, k, given, changes, dp)) {
    				dp[index][remk][prevval] = val;
    				return true;
    			}
    		}
    	}
    	dp[index][remk][prevval] = -2;
    	return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        sc.nextLine();
        int[][] changes = new int[2005][10];
        char[][] given = new char[2005][7];
        for (int i = 0; i < n; i++) {
            given[i] = sc.next().toCharArray();
            for(int c = 0; c <= 9; c++) {
            	int count = 0;
            	for(int j = 0; j < 7; j++) {
            		if(given[i][j] == '0' && digits[c][j] == '1') {
            			count++;
            		} else if(given[i][j] == '1' && digits[c][j] == '0') {
            			count = -1;
            			break;
            		}
            	}
            	changes[i][c] = count;
            }
            sc.nextLine();
        }

        System.out.println(solve(n, k, given, changes));
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Solution implements Runnable {

	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray() };

	private String solve(int n, int k, char[][] given, int[][] changes) {
		String[][][] dp = new String[n + 1][k + 1][10];
		for (String[][] a : dp) {
			for (String[] b : a) {
				Arrays.fill(b, "-1");
			}
		}
		dfs(0, k, 0, n, k, given, changes, dp);
		return !dp[0][k][0].equals("-1") && !dp[0][k][0].equals("-2") ? dp[0][k][0] : "-1";
	}

	private boolean dfs(int index, int remk, int prevval, int n, int k, char[][] given, int[][] changes,	String[][][] dp) {
		if (index == n) {
			if (remk == 0) {
				dp[index][remk][prevval] = "";
				return true;
			} else {
				dp[index][remk][prevval] = "-2";
				return false;
			}
		}

		if (dp[index][remk][prevval].equals("-2")) return false;

		if (!dp[index][remk][prevval].equals("-1")) return true;

		for (int val = 9; val >= 0; val--) {
			int count = changes[index][val];
			if (count != -1 && count <= remk) {
				if (dfs(index + 1, remk - count, val, n, k, given, changes, dp)) {
					dp[index][remk][prevval] = (char)(val + 48) + dp[index + 1][remk - count][val];
					return true;
				}
			}
		}
		dp[index][remk][prevval] = "-2";
		return false;
	}

    public void solve() throws Exception {

        int n = sc.nextInt(), k = sc.nextInt();
        char[][] given = new char[n][7];
        int[][] changes = new int[n][10];

        for (int i = 0; i < n; i++) {
            given[i] = in.readLine().toCharArray();
            for (int c = 0; c <= 9; c++) {
				int count = 0;
				for (int j = 0; j < 7; j++) {
					if (given[i][j] == '0' && digits[c][j] == '1') {
						count++;
					} else if (given[i][j] == '1' && digits[c][j] == '0') {
						count = -1;
						break;
					}
				}
				changes[i][c] = count;
			}
        }
        String result = solve(n, k, given, changes);
        out.println(result);
    }

    static Throwable uncaught;

    BufferedReader in;
    FastScanner sc;
    PrintWriter out;

    @Override
    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
            sc = new FastScanner(in);
            solve();
        } catch (Throwable uncaught) {
            Solution.uncaught = uncaught;
        } finally {
            out.close();
        }
    }

    public static void main(String[] args) throws Throwable {
        Thread thread = new Thread(null, new Solution(), "", (1 << 26));
        thread.start();
        thread.join();
        if (Solution.uncaught != null) {
            throw Solution.uncaught;
        }
    }

}

class FastScanner {

    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }

    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }

}


import java.util.Scanner;

public class NastyaAndScoreboard {
	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray() };

	private static void solve(int n, int k, char[][] given, int[][] changes) {

		boolean[][] dp = new boolean[n + 1][k + 1];

		dp[n][0] = true;

		for(int i = n; i >0; i--) {
			for(int j = 0; j <= k; j++) {
				if(dp[i][j]) {
					for(int d = 0; d < 10; d++) {
						if(changes[i-1][d] != -1 && j + changes[i-1][d] <= k) {
							dp[i-1][j + changes[i-1][d]] = true;
						}
					}
				}
			}
		}

		if(!dp[0][k]) {
			System.out.println("-1");
			return;
		}

		for(int i = 0; i < n; i++) {
			for(int d = 9; d >=0; d--) {
				if(changes[i][d] != -1 && k >= changes[i][d] && dp[i + 1][k - changes[i][d]]) {
					System.out.print(d);
					k -= changes[i][d];
					break;
				}
			}
		}
	}

	public static void main(String[] args) {

		try (Scanner sc = new Scanner(System.in)) {
			int n = sc.nextInt();
			int k = sc.nextInt();
			sc.nextLine();
			int[][] changes = new int[n][10];
			char[][] given = new char[n][7];
			for (int i = 0; i < n; i++) {
				given[i] = sc.next().toCharArray();
				for (int c = 0; c <= 9; c++) {
					for (int j = 0; j < 7; j++) {
						if (given[i][j] == '0' && digits[c][j] == '1') {
							changes[i][c]++;
						} else if (given[i][j] == '1' && digits[c][j] == '0') {
							changes[i][c] = -1;
							break;
						}
					}
				}
				sc.nextLine();
			}
			solve(n, k, given, changes);
		}
	}

}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class NastyaAndScoreboard {

	private static int solve(int n, int k, List<char[]> digits) {
        return 20;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();    // no of nodes
        int k = sc.nextInt();   // no of  edges
        sc.nextLine();
        List<char[]> digits = new ArrayList<>(n);
        for (int i = 1; i <= n; i++) {
            digits.add(sc.next().toCharArray());
            sc.nextLine();
        }
        System.out.println(solve(n, k, digits));
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class NastyaAndScoreboard2 {
	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray() };

	private static String solve(int n, int k, char[][] given, int[][] changes) {
		String[][][] dp = new String[n + 1][k + 1][10];
		for (String[][] a : dp) {
			for (String[] b : a) {
				Arrays.fill(b, "-1");
			}
		}
		dfs(0, k, 0, n, k, given, changes, dp);
		return dp[0][k][0];
	}

	private static boolean dfs(int index, int remk, int prevval, int n, int k, char[][] given, int[][] changes,	String[][][] dp) {
		if (index == n) {
			if (remk == 0) {
				dp[index][remk][prevval] = "";
				return true;
			} else {
				dp[index][remk][prevval] = "-2";
				return false;
			}
		}

		if (dp[index][remk][prevval].equals("-2")) return false;

		if (!dp[index][remk][prevval].equals("-1")) return true;

		for (int val = 9; val >= 0; val--) {
			int count = changes[index][val];
			if (count != -1 && count <= remk) {
				if (dfs(index + 1, remk - count, val, n, k, given, changes, dp)) {
					dp[index][remk][prevval] = (char)(val + 48) + dp[index + 1][remk - count][val];
					return true;
				}
			}
		}
		dp[index][remk][prevval] = "-2";
		return false;
	}

	public static void main(String[] args) {
		int n, k;
		int[][] changes;
		char[][] given;

		try (Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
			k = sc.nextInt();
			sc.nextLine();
			changes = new int[n][10];
			given = new char[n][7];
			for (int i = 0; i < n; i++) {
				given[i] = sc.next().toCharArray();
				for (int c = 0; c <= 9; c++) {
					int count = 0;
					for (int j = 0; j < 7; j++) {
						if (given[i][j] == '0' && digits[c][j] == '1') {
							count++;
						} else if (given[i][j] == '1' && digits[c][j] == '0') {
							count = -1;
							break;
						}
					}
					changes[i][c] = count;
				}
				sc.nextLine();
			}
		}

		System.out.println(solve(n, k, given, changes));
	}

}


import java.util.Arrays;
import java.util.Scanner;

public class NastyaAndScoreboard2 {
	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray() };

	private static String solve(int n, int k, char[][] given, int[][] changes) {
		String[][][] dp = new String[n + 1][k + 1][10];
		for (String[][] a : dp) {
			for (String[] b : a) {
				Arrays.fill(b, "-1");
			}
		}
		dfs(0, k, 0, n, k, given, changes, dp);
		return !dp[0][k][0].equals("-1") && !dp[0][k][0].equals("-2") ? dp[0][k][0] : "-1";
	}

	private static boolean dfs(int index, int remk, int prevval, int n, int k, char[][] given, int[][] changes,	String[][][] dp) {
		if (index == n) {
			if (remk == 0) {
				dp[index][remk][prevval] = "";
				return true;
			} else {
				dp[index][remk][prevval] = "-2";
				return false;
			}
		}

		if (dp[index][remk][prevval].equals("-2")) return false;

		if (!dp[index][remk][prevval].equals("-1")) return true;

		for (int val = 9; val >= 0; val--) {
			int count = changes[index][val];
			if (count != -1 && count <= remk) {
				if (dfs(index + 1, remk - count, val, n, k, given, changes, dp)) {
					dp[index][remk][prevval] = (char)(val + 48) + dp[index + 1][remk - count][val];
					return true;
				}
			}
		}
		dp[index][remk][prevval] = "-2";
		return false;
	}

	public static void main(String[] args) {
		int n, k;
		int[][] changes;
		char[][] given;

		try (Scanner sc = new Scanner(System.in)) {
			n = sc.nextInt();
			k = sc.nextInt();
			sc.nextLine();
			changes = new int[n][10];
			given = new char[n][7];
			for (int i = 0; i < n; i++) {
				given[i] = sc.next().toCharArray();
				for (int c = 0; c <= 9; c++) {
					int count = 0;
					for (int j = 0; j < 7; j++) {
						if (given[i][j] == '0' && digits[c][j] == '1') {
							count++;
						} else if (given[i][j] == '1' && digits[c][j] == '0') {
							count = -1;
							break;
						}
					}
					changes[i][c] = count;
				}
				sc.nextLine();
			}
		}

		System.out.println(solve(n, k, given, changes));
	}

}


import java.util.Arrays;
import java.util.Scanner;

public class NastyaAndScoreboard {
	private static char[][] digits = new char[][] {
		"1110111".toCharArray(),
		"0010010".toCharArray(),
		"1011101".toCharArray(),
		"1011011".toCharArray(),
		"0111010".toCharArray(),
		"1101011".toCharArray(),
		"1101111".toCharArray(),
		"1010010".toCharArray(),
		"1111111".toCharArray(),
		"1111011".toCharArray()
	};

	private static int solve(int n, int k, char[][] given, int[][] changes) {
    	int[][][] dp = new int[2005][2005][11];
    	for(int[][] a : dp) {
    		for(int[] b: a) {
    			Arrays.fill(b, -1);
    		}
    	}
    	dfs(0, k, 0, n, k, given, changes, dp);
    	return findans(0, k, 0, 0, n, changes, dp);
    }

    private static int findans(int index, int remk, int prevval, int ans, int n, int[][] changes, int[][][] dp) {
    	if(index == n) {
    		if(dp[index][remk][prevval] == 0) {
    			return ans;
    		} else {
    			return -1;
    		}
    	}
    	int val = dp[index][remk][prevval];
    	if(val < 0) return -1;
    	int count = changes[index][val];
    	return findans(index + 1, remk - count, val, ans * 10 + val, n, changes, dp);
    }

    private static boolean dfs(int index, int remk, int prevval, int n, int k, char[][] given, int[][] changes, int[][][] dp) {
    	if(index == n) {
    		if(remk == 0) {
    			dp[index][remk][prevval] = 0;
    			return true;
    		} else {
    			dp[index][remk][prevval] = -2;
    			return false;
    		}
    	}

    	if(dp[index][remk][prevval] == -2) return false;

    	if(dp[index][remk][prevval] >= 0) return true;

    	for(int val = 9; val >= 0; val--) {
    		int count = changes[index][val];
    		if(count != -1 && count <= remk) {
    			if(dfs(index + 1, remk - count, val, n, k, given, changes, dp)) {
    				dp[index][remk][prevval] = val;
    				return true;
    			}
    		}
    	}
    	dp[index][remk][prevval] = -2;
    	return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        sc.nextLine();
        int[][] changes = new int[2005][10];
        char[][] given = new char[2005][7];
        for (int i = 0; i < n; i++) {
            given[i] = sc.next().toCharArray();
            for(int c = 0; c <= 9; c++) {
            	int count = 0;
            	for(int j = 0; j < 7; j++) {
            		if(given[i][j] == '0' && digits[c][j] == '1') {
            			count++;
            		} else if(given[i][j] == '1' && digits[c][j] == '0') {
            			count = -1;
            			break;
            		}
            	}
            	changes[i][c] = count;
            }
            sc.nextLine();
        }

        System.out.println(solve(n, k, given, changes));
    }

}


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

public class B {
    public static void main(String[] args) throws IOException{
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int[] nums = new int[]{119, 18, 93, 91, 58, 107, 111, 82, 127, 123};
        StringTokenizer st = new StringTokenizer(f.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[n-i-1] = Integer.parseInt(f.readLine(), 2);
        }
        int[][] dp = new int[n+1][k+1];
        int[][] par = new int[n+1][k+1];
        for(int i = 0; i < n+1; i++) Arrays.fill(dp[i], -1);
        dp[0][0] = 0;
        for(int i = 0; i < n; i++){
            TreeMap<Integer, Integer> possible = new TreeMap<Integer, Integer>(Comparator.reverseOrder());
            for(int q = 0; q < 10; q++){
                int a = nums[q];
                int b = arr[i];
                boolean good = true;
                int count = 0;
                while(a != 0 || b != 0){
                    if(b % 2 == 1 && a % 2 == 0){
                        good = false;
                        break;
                    }else if(b % 2 == 0 && a % 2 == 1){{
                        count++;
                    }}
                    a/=2;
                    b/=2;
                }
                if(good) possible.put(q, count);
            }
            for(int j = 0; j <= k; j++){
                if(dp[i][j] == -1) continue;
                for(int p: possible.keySet()){
                    int val = possible.get(p);
                    if(j+val <= k){
                        if(dp[i+1][j+val] < p){
                            dp[i+1][j+val] = p;
                            par[i+1][j+val] = j;
                        }
                    }
                }
            }
        }
        StringBuilder ans = new StringBuilder();
        if(dp[n][k] == -1){
            out.println(-1);
        }else{
            int idx = n;
            int val = k;
            while(idx > 0) {
                ans.append(dp[idx][val]);
                val = par[idx][val];
                idx--;
            }
            out.println(ans.toString());
        }
        out.close();
    }
}


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

import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.exit;

public class Solution {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;
    static boolean isLocal = false;

    int n, k;
    short dp[][];
    String[] s;
    String[] seq = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
    Map<String, Integer> map = new HashMap<>();

    int get(String a, String b) { // b => a ( replacing 0 by 1 )
        int diff = 0;
        for (int i = 0; i < 7; i++)
            if (b.charAt(i) == '1' && a.charAt(i) == '0')
                return -1;
            else if (b.charAt(i) == '0' && a.charAt(i) == '1')
                diff++;
        return diff;
    }

    boolean go(int i, int k) {
        if (i == n) return k == 0;
        if (k < 0) return false;
        if (dp[i][k] != -1) return dp[i][k] == 1;
        boolean ans = false;
        for (String x : seq) {
            int diff = get(x, s[i]);
            if (diff != -1 && diff <= k)
                ans |= go(i + 1, k - diff);
        }
        dp[i][k] = (short) (ans ? 1 : 0);
        return ans;
    }

    void print() {
        for (int i = 0; i < n; i++) {
            for (int j = 9; j >= 0; j--) {
                int diff = get(seq[j], s[i]);
                if (diff != -1 && go(i + 1, k - diff)) {
                    k -= diff;
                    out.print(map.get(seq[j]));
                    break;
                }
            }
        }
    }

    void Case() throws IOException {
        n = nextInt();
        k = nextInt();
        dp = new short[n][k + 1];
        for (int i = 0; i < n; i++) Arrays.fill(dp[i], (short) -1);
        s = new String[n];
        for (int i = 0; i < seq.length; i++) map.put(seq[i], i);
        for (int i = 0; i < n; i++) s[i] = next();
        if (go(0, k))
            print();
        else
            out.println(-1);
    }

    void solve() throws Exception {
        int t = 1;
        for (int i = 1; i <= t; i++) {
            Case();
        }
    }

    int min(int x, int y) {
        return Integer.min(x, y);
    }

    int max(int x, int y) {
        return Integer.max(x, y);
    }

    long min(long x, long y) {
        return Long.min(x, y);
    }

    long max(long x, long y) {
        return Long.max(x, y);
    }

    int[] sort(int[] arr) {
        sort(arr, 0, arr.length - 1);
        return arr;
    }

    void sort(int arr[], int l, int r) {
        if (l < r) {
            int m = (l + r) / 2;
            sort(arr, l, m);
            sort(arr, m + 1, r);
            merge(arr, l, m, r);
        }
    }

    void merge(int arr[], int l, int m, int r) {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int[n1];
        int R[] = new int[n2];
        for (int i = 0; i < n1; ++i)
            L[i] = arr[l + i];
        for (int j = 0; j < n2; ++j)
            R[j] = arr[m + 1 + j];
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    class Seg implements Comparable<Seg> {
        int st, end;

        public Seg(int st, int end) {
            this.st = st;
            this.end = end;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Seg seg = (Seg) o;
            return st == seg.st &&
                    end == seg.end;
        }

        @Override
        public int hashCode() {
            return Objects.hash(st, end);
        }

        @Override
        public int compareTo(Seg seg) {
            return st == seg.st ? Integer.compare(end, seg.end) : Integer.compare(st, seg.st);
        }
    }

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

    private long[] nal(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) a[i] = nextLong();
        return a;
    }

    int nextInt() throws IOException {
        return parseInt(next());
    }

    long nextLong() throws IOException {
        return parseLong(next());
    }

    double nextDouble() throws IOException {
        return parseDouble(next());
    }

    String next() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    public static void main(String[] args) throws Exception {
        try {
            if (isLocal) {
                in = new BufferedReader(new FileReader("src/tests/sol.in"));
                out = new PrintWriter(new BufferedWriter(new FileWriter("src/tests/sol.out")));
            } else {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(new OutputStreamWriter(System.out));
            }
//            long lStartTime = System.currentTimeMillis();
            new Solution().solve();
//            long lEndTime = System.currentTimeMillis();
//            out.println("Elapsed time in seconds: " + (double) (lEndTime - lStartTime) / 1000.0);
            in.close();
            out.close();
        } catch (Throwable e) {
            e.printStackTrace();
            exit(1);
        }
    }
}


import java.util.*;
import java.io.*;
public class b {
	static final FS sc = new FS();
	static final PrintWriter pw = new PrintWriter(System.out);

	static final int min = -(Integer.MAX_VALUE / 8), m = 10;
	static final String[] temp = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};

	static int n, k;
	static int[][] d;
	static int[][] dp;
	public static void main(String[] args) {
		n = sc.nextInt();
		k = sc.nextInt();
		d = new int[m][n];
		for(int i = 0; i < n; ++i) {
			char[] in = sc.next().toCharArray();
			for(int j = 0; j < m; ++j) d[j][i] = getd(in, temp[j].toCharArray());
		}

		dp = new int[n + 1][k + 1];
		for(int[] z : dp) Arrays.fill(z, -1);

		if(go(0, k) == -1) {
			System.out.println(-1);
			return;
		}

		int num = k;
		for(int i = 0; i < n; ++i) {
			int curr = go(i, num);
			System.out.print(curr);
			num -= d[curr][i];
		}
		System.out.println();
	}
	static int go(int idx, int num) {
		if(dp[idx][num] != -1) return dp[idx][num];

		if(idx == n) return dp[idx][num] = (num == 0 ? 0 : -1);

		for(int j = m - 1; j >= 0; --j) {
			if(d[j][idx] == -1) continue;

			int newNum = num - d[j][idx];
			if(newNum >= 0 && go(idx + 1, newNum) != -1) return dp[idx][num] = j;
		}

		return dp[idx][num] = -1;
	}
	static int getd(char[] a, char[] b) {
		int out = 0;
		for(int i = 0; i < 7; ++i) {
			if(a[i] == '1' && b[i] == '0') return -1;
			if(a[i] == '0' && b[i] == '1') ++out;
		}
		return out;
	}
	static class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while(!st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(Exception e) {}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.util.*;
import java.io.*;
public class b {
	static final FS sc = new FS();
	static final PrintWriter pw = new PrintWriter(System.out);

	static final int m = 10, not = -2;
	static final String[] temp = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};

	static int n, k;
	static int[][] d;
	static int[][] dp;
	public static void main(String[] args) {
		n = sc.nextInt();
		k = sc.nextInt();
		d = new int[m][n];
		for(int i = 0; i < n; ++i) {
			char[] in = sc.next().toCharArray();
			for(int j = 0; j < m; ++j) d[j][i] = getd(in, temp[j].toCharArray());
		}

		dp = new int[n + 1][k + 1];
		for(int[] z : dp) Arrays.fill(z, not);

		dp[n][0] = 0;
		for(int num = 1; num <= k; ++num) dp[n][num] = -1;

		if(go(0, k) == -1) {
			System.out.println(-1);
			return;
		}

		int num = k;
		for(int i = 0; i < n; ++i) {
			int curr = go(i, num);
			System.out.print(curr);
			num -= d[curr][i];
		}
		System.out.println();
	}
	static int go(int idx, int num) {
		if(dp[idx][num] != not) return dp[idx][num];

		for(int j = m - 1; j >= 0; --j) {
			if(d[j][idx] == -1) continue;

			int newNum = num - d[j][idx];
			if(newNum >= 0 && go(idx + 1, newNum) != -1) return dp[idx][num] = j;
		}

		return dp[idx][num] = -1;
	}
	static int getd(char[] a, char[] b) {
		int out = 0;
		for(int i = 0; i < 7; ++i) {
			if(a[i] == '1' && b[i] == '0') return -1;
			if(a[i] == '0' && b[i] == '1') ++out;
		}
		return out;
	}
	static class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while(!st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(Exception e) {}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}
/*
1 0
1111001

*/


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class B {

	static String[] on= {
			"1110111",
			"0010010",
			"1011101",
			"1011011",
			"0111010",
			"1101011",
			"1101111",
			"1010010",
			"1111111",
			"1111011"
	};

	static int[] onBits=new int[10];
	static int[] lockedOn;
	static int[][] dp;

	public static void main(String[] args) {
		precomp();

		FastScanner fs=new FastScanner();
		int nNumbers=fs.nextInt(), toTurnOn=fs.nextInt();
		lockedOn=new int[nNumbers];
		for (int i=0; i<nNumbers; i++) {
			char[] line=fs.next().toCharArray();
			int val=0;
			for (int x=0; x<7; x++)
				if (line[x]=='1') val+=1<<x;
			lockedOn[i]=val;
		}
		dp=new int[nNumbers+1][2001];
		for (int i=0; i<dp.length; i++) Arrays.fill(dp[i], -1);

		PrintWriter out=new PrintWriter(System.out);
		ArrayList<Integer> ans=new ArrayList<>();
		outer:for (int index=0; index<nNumbers; index++) {
			for (int toPlace=9; toPlace>=0; toPlace--) {
				if ((lockedOn[index] & onBits[toPlace])!=lockedOn[index]) continue;
				int diff=Integer.bitCount(onBits[toPlace]-lockedOn[index]);
//				System.out.println(toPlace+" "+diff);
				boolean futureRes=go(index+1, toTurnOn-diff);
				if (futureRes) {
					ans.add(toPlace);
					toTurnOn-=diff;
					continue outer;
				}
			}
			out.println(-1);
			out.close();
			return;
		}
		for (int i:ans) out.print(i);
		out.println();
		out.close();
	}

	static boolean go(int index, int onBitsLeft) {
		if (onBitsLeft<0) return false;
		if (index==lockedOn.length) {
			return onBitsLeft==0;
		}
		if (dp[index][onBitsLeft]!=-1) return dp[index][onBitsLeft]==1;
		for (int toPlace=0; toPlace<10; toPlace++) {
			if ((lockedOn[index] & onBits[toPlace])!=lockedOn[index]) continue;
			int diff=Integer.bitCount(onBits[toPlace]-lockedOn[index]);
			boolean futureRes=go(index+1, onBitsLeft-diff);
			if (futureRes) {
				dp[index][onBitsLeft]=1;
				return true;
			}
		}
		dp[index][onBitsLeft]=0;
		return false;
	}

	static void precomp() {
		for (int n=0; n<10; n++) {
			int val=0;
			for (int x=0; x<7; x++) {
				if (on[n].charAt(x)=='1') val+=1<<x;
			}
			onBits[n]=val;
		}
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

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

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

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

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

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

	}

}


import javax.swing.*;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
  final static int MAXN = 200000 + 5;
  static int[] A;
  static int N;
  static int M;
  static int T;
  static int K;
  final static int MOD = 1000000007;

  public static void main(String[] args) {
    InputReader scanner = new InputReader(System.in);
    PrintStream out = new PrintStream(new BufferedOutputStream(System.out));
    solve(scanner, out);
    out.flush();
    out.close();
  }

  static int[] NUMS = new int[10];

  static {
    String[] ss =new String[] {
      "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"
    };

    for (int i = 0; i < 10; i++) {
      NUMS[i] = strToInt(ss[i]);
    }
  }

  static int diff(int va, int vb) {
    int d = 0;
    for (int i = 0; i < 7; i++) {
      boolean aone = (va & (1 << i)) > 0;
      boolean bone = (vb & (1 << i)) > 0;
      if (aone && !bone) {
        return -1;
      }

      if (!aone && bone) {
        d += 1;
      }
    }

    return d;
  }

  static int strToInt(String s) {
    int v = 0;
    for (int j = 0; j < s.length(); j++) {
      v <<= 1;
      v |= s.charAt(j) == '1' ? 1 : 0;
    }

    return v;
  }

  static void solve(InputReader scanner, PrintStream out) {
    N = scanner.nextInt();
    K = scanner.nextInt();

    A = new int[N+1];
    for (int i = 1; i <= N; i++) {
      String s = scanner.nextLine();
      A[N-i+1] = strToInt(s);
    }

    int[][] dist = new int[128][10];
    for (int s = 0; s < 128; s++) {
      for (int t = 0; t < 10; t++) {
        dist[s][t] = diff(s, NUMS[t]);
      }
    }

    boolean[][] dp = new boolean[N+1][K+1];
    for (int i = 0; i < N+1; i++) {
      for (int j = 0; j < K+1; j++) {
        dp[i][j] = false;
      }
    }

    dp[0][0] = true;
    for (int i = 1; i <= N; i++) {
      int s = A[i];
      for (int j = 0; j <= Math.min(7*i, K); j++) {
        for (int t = 0; t <= 9; t++) {
          int d = dist[s][t];
          if (d >= 0 && j-d >= 0 && dp[i-1][j-d]) {
            dp[i][j] = true;
            break;
          }
        }
      }
    }

    if (!dp[N][K]) {
      out.println(-1);
      return;
    }

    StringBuilder ans = new StringBuilder();
    int index = N;
    int k = K;
    while (index > 0) {
      int s = A[index];
      for (int t = 9; t >= 0; t--) {
        int d = dist[s][t];
        if (d >= 0 && d <= k && dp[index-1][k-d]) {
          ans.append(t);
          index -= 1;
          k -= d;
          break;
        }
      }
    }

    out.println(ans.toString());

  }

  static long pow(long a, long b) {
    if (a == 0) {
      return 0;
    }
    if (b == 0) {
      return 1;
    }

    if (b == 1) {
      return a;
    }

    long h = pow(a, b / 2);
    long ans = h * h;
    ans %= MOD;
    if (b % 2 != 0) {
      ans *= a;
      ans %= MOD;
    }

    return ans;
  }
}

class Pair implements Comparable<Pair>{
  public int x;
  public int y;

  public Pair(int x, int y) {
    this.x = x;
    this.y = y;
  }

  @Override
  public int compareTo(Pair o) {
    if (this.x != o.x) {
      return this.x - o.x;
    }
    return this.y - o.y;
  }
}

class InputReader {

  private final InputStream stream;
  private final byte[] buf = new byte[8192];
  private int curChar, snumChars;
  private SpaceCharFilter filter;

  public InputReader(InputStream stream) {
    this.stream = stream;
  }

  public int snext() {
    if (snumChars == -1)
      throw new InputMismatchException();
    if (curChar >= snumChars) {
      curChar = 0;
      try {
        snumChars = stream.read(buf);
      } catch (IOException e) {
        throw new InputMismatchException();
      }
      if (snumChars <= 0)
        return -1;
    }
    return buf[curChar++];
  }

  public int nextInt() {
    int c = snext();
    while (isSpaceChar(c)) {
      c = snext();
    }
    int sgn = 1;
    if (c == '-') {
      sgn = -1;
      c = snext();
    }
    int res = 0;
    do {
      if (c < '0' || c > '9')
        throw new InputMismatchException();
      res *= 10;
      res += c - '0';
      c = snext();
    } while (!isSpaceChar(c));
    return res * sgn;
  }

  public long nextLong() {
    int c = snext();
    while (isSpaceChar(c)) {
      c = snext();
    }
    int sgn = 1;
    if (c == '-') {
      sgn = -1;
      c = snext();
    }
    long res = 0;
    do {
      if (c < '0' || c > '9')
        throw new InputMismatchException();
      res *= 10;
      res += c - '0';
      c = snext();
    } while (!isSpaceChar(c));
    return res * sgn;
  }

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

  public String readString() {
    int c = snext();
    while (isSpaceChar(c)) {
      c = snext();
    }
    StringBuilder res = new StringBuilder();
    do {
      res.appendCodePoint(c);
      c = snext();
    } while (!isSpaceChar(c));
    return res.toString();
  }

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

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

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

  public interface SpaceCharFilter {
    public boolean isSpaceChar(int ch);
  }
}


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

public class Solution {
    static int n,k;
    static int dp[][];
    static int arr[];
    static int ans[][];
    static int nos[];
    static String ss[]={"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
    public static void main(String[] args) throws Exception {
        FastReader sc = new FastReader();
        n=sc.nextInt();k=sc.nextInt();
        dp=new int[n][k+1];
        for (int a[]:dp)Arrays.fill(a,-2);
        nos=new int[10];
        for (int i=0;i<10;i++){
            nos[i]=get(ss[i]);
        }
        arr=new int[n];
        for (int i=0;i<n;i++){
            arr[i]=get(sc.next());
        }
        ans=new int[n][k+1];
        if (!dp(0,k)){
            System.out.println(-1);
            return;
        }
        StringBuilder sb=new StringBuilder();
        int i=0,j=k;
        while (i<n){
            sb.append(dp[i][j]);
            j=ans[i][j];
            i++;
        }
        System.out.println(sb);

    }
    static boolean dp(int i,int j){
        if (i==n){
            if (j==0)return true;
            return false;
        }
        if (dp[i][j]!=-2){
            if (dp[i][j]==-1)return false;
            return true;
        }
        int max=-1;
        for (int ll=0;ll<10;ll++){
            int x=ok(ll,arr[i]);
            if (x==-1)continue;
            if(j-x>=0) {
                if (dp(i + 1, j - x)) {
                    ans[i][j] = j - x;
                    max = ll;
                }
            }
        }
        dp[i][j]=max;
        if (max==-1)return false;
        return true;
    }
    static int ok(int x,int m2){
        int m1=nos[x];
        int c=0;
        for (int i=0;i<7;i++){
            if ((m1&(1<<i))!=0 && (m2&(1<<i))!=0)continue;
            if ((m1&(1<<i))==0 && (m2&(1<<i))==0)continue;
            if ((m1&(1<<i))!=0 && (m2&(1<<i))==0)c++;
            if ((m1&(1<<i))==0 && (m2&(1<<i))!=0)return -1;
        }
        return c;
    }
    static int get(String s){
        int x=0;
        int c=0;
        for (int i=6;i>=0;i--){
            if (s.charAt(i)=='1')x+=(1<<c);
            c++;
        }
        return x;
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            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;
        }
    }
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class codeforces
{
   static class Student{
       int x,y;
        Student(int x,int y){
            this.x=x;
            this.y=y;
            //this.z=z;
        }
    }
    static int prime[];
    static void sieveOfEratosthenes(int n)
    {
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        int pos=0;
        prime= new int[n+1];
        for(int p = 2; p*p <=n; p++)
        {
            // If prime[p] is not changed, then it is a prime
            if(prime[p] == 0)
            {
                // Update all multiples of p
                prime[p]=p;
                for(int i = p*p; i <= n; i += p)
                    if(prime[i]==0)
                    prime[i] = p;
            }
        }
    }
static    class Sortbyroll implements Comparator<Student>
{
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student c, Student b)
    {
        return c.x-b.x;
    }
}
static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            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 class Edge{
        int a,b;
        Edge(int a,int b){
            this.a=a;
            this.b=b;
        }
    }
    static class Trie{
        HashMap<Character,Trie>map;
         int c;
        Trie(){
            map=new HashMap<>();
            //z=null;
            //o=null;
            c=0;
        }
    }
    //static long ans;
    static int parent[];
    static int rank[];
    static int b[][];
    static int bo[];
    static int ho[];
    static int seg[];
    //static int pos;
   // static long mod=1000000007;
    //static int dp[][];
    static HashMap<Integer,Integer>map;
    static PriorityQueue<Student>q=new PriorityQueue<>();
    //static Stack<Integer>st;
   // static ArrayList<Character>ans;
    static ArrayList<ArrayList<Integer>>adj;
    //static long ans;
    static Trie root;
    static long fac[];
    static int gw,gb;
    static long mod=(long)(1000000007);
    //static int ans;
    static void solve()throws IOException{
        FastReader sc=new FastReader();
        int n,k,i,j,l,max,c1,c2,in,x,y,t;
        n=sc.nextInt();
        k=sc.nextInt();
        String s[]=new String[n];
        String p[]=new String[10];
        p[0]="1110111";
        p[1]="0010010";
        p[2]="1011101";
        p[3]="1011011";
        p[4]="0111010";
        p[5]="1101011";
        p[6]="1101111";
        p[7]="1010010";
        p[8]="1111111";
        p[9]="1111011";
        for(i=0;i<n;i++)
            s[i]=sc.nextLine();
        int dp[][]=new int[n][k+1];
        for(i=0;i<n;i++){
            for(j=0;j<=k;j++)
                dp[i][j]=-1;
        }
        t=0;
        for(l=0;l<=9;l++){
                if(p[l].equals(s[0]))
                    t=l;
        }
        for(i=0;i<=9;i++){
            x=bits(p[i]);
            y=bits(s[0]);
            if(i<t)
                continue;
            if((int)(x&y)==y){
                x=count(p[i]);
                y=count(s[0]);
                if(x-y<=k)
                    dp[0][x-y]=Math.max(dp[0][x-y],i);
            }
        }
        //System.out.println(dp[0][4]);
        for(i=1;i<n;i++){
            t=0;
            for(l=0;l<=9;l++){
                if(p[l].equals(s[i]))
                    t=l;
            }
            y=bits(s[i]);
            c1=count(s[i]);
            for(j=0;j<=k;j++){
                max=-1;
                in=-1;
                for(l=0;l<=9;l++){
                    x=bits(p[l]);
                    if((int)(x&y)!=y||l<t)
                        continue;
                    c2=count(p[l]);
                    if(c2-c1>j||dp[i-1][j+c1-c2]==-1)
                        continue;
                    if(dp[i-1][j+c1-c2]>max){
                        max=dp[i-1][j+c1-c2];
                        in=l;
                    }
                }
                dp[i][j]=in;
            }
        }
        if(dp[n-1][k]==-1)
            System.out.println(-1);
        else{
            StringBuilder sb=new StringBuilder();
            j=n-1;
            i=k;
            while(j>=0){
                sb.append(dp[j][i]);
                c1=count(p[dp[j][i]]);
                c2=count(s[j]);
                j-=1;
                i-=c1-c2;
            }
            System.out.println(sb.reverse().toString());
        }
    }
    static int count(String s){
        int c=0;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='1')
                ++c;
        }
        return c;
    }
    static int bits(String s){
        int ans=0,y=1;
        for(int i=s.length()-1;i>=0;i--){
            ans+=(s.charAt(i)-'0')*y;
            y=y*2;
        }
        return ans;
    }
    static int tr(int i,int n,int dp[][],int a[],int h,int l,int r,int in){
        if(i>=n)
            return 0;
        if(dp[i][in]!=-1)
            return dp[i][in];
        int mod1,mod2,ans1=0,ans2=0;
        mod1=(a[i]+in)%h;
        if(mod1>=l&&mod1<=r)
            ans1=1+tr(i+1,n,dp,a,h,l,r,mod1);
        else
            ans1=tr(i+1,n,dp,a,h,l,r,mod1);
        mod2=(a[i]+in-1)%h;
        if(mod2>=l&&mod2<=r)
            ans2=1+tr(i+1,n,dp,a,h,l,r,mod2);
        else
            ans2=tr(i+1,n,dp,a,h,l,r,mod2);
        dp[i][in]=Math.max(ans1,ans2);
        return dp[i][in];
    }
    static long nCr(long n, long r,
                                    long p)
    {
        return (fac[(int)n]* modInverse(fac[(int)r], p)
                % p * modInverse(fac[(int)(n-r)], p)
                                    % p) % p;
    }
    //static int prime[];
    //static int dp[];
    public static void main(String[] args){
        //long sum=0;
        try {
                codeforces.solve();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }
       static long modInverse(long n, long p)
    {
        return power(n, p-(long)2,p);
    }
 static long power(long x, long y, long p)
{
    long res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y %(long)2!=0)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res%p;
 }
    /*public static long power(long x,long a) {
		if(a==0) return 1;
		if(a%2==1)return (x*1L*power(x,a-1))%mod;
		return (power((int)((x*1L*x)%mod),a/2))%mod;
	}*/
   static int find(int x)
    {
        // Finds the representative of the set
        // that x is an element of
        while(parent[x]!=x)
        {
            // if x is not the parent of itself
            // Then x is not the representative of
            // his set,
            x=parent[x];

            // so we recursively call Find on its parent
            // and move i's node directly under the
            // representative of this set
        }

        return x;
    }
    static void union(int x, int y)
    {
        // Find representatives of two sets
        int xRoot = find(x), yRoot = find(y);

        // Elements are in the same set, no need
        // to unite anything.
        if (xRoot == yRoot)
            return;

         // If x's rank is less than y's rank
        if (rank[xRoot] < rank[yRoot])

            // Then move x under y  so that depth
            // of tree remains less
            parent[xRoot] = yRoot;

        // Else if y's rank is less than x's rank
        else if (rank[yRoot] < rank[xRoot])

            // Then move y under x so that depth of
            // tree remains less
            parent[yRoot] = xRoot;

        else // if ranks are the same
        {
            // Then move y under x (doesn't matter
            // which one goes where)
            parent[yRoot] = xRoot;

            // And increment the the result tree's
            // rank by 1
            rank[xRoot] = rank[xRoot] + 1;
        }
    }
    static long gcd(long a, long b)
    {
        if (a == 0){
             //ans+=b;
            return b;
        }
        return gcd(b % a, a);
    }
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class codeforces
{
   static class Student{
       int x,y;
        Student(int x,int y){
            this.x=x;
            this.y=y;
            //this.z=z;
        }
    }
    static int prime[];
    static void sieveOfEratosthenes(int n)
    {
        // Create a boolean array "prime[0..n]" and initialize
        // all entries it as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
        int pos=0;
        prime= new int[n+1];
        for(int p = 2; p*p <=n; p++)
        {
            // If prime[p] is not changed, then it is a prime
            if(prime[p] == 0)
            {
                // Update all multiples of p
                prime[p]=p;
                for(int i = p*p; i <= n; i += p)
                    if(prime[i]==0)
                    prime[i] = p;
            }
        }
    }
static    class Sortbyroll implements Comparator<Student>
{
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student c, Student b)
    {
        return c.x-b.x;
    }
}
static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            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 class Edge{
        int a,b;
        Edge(int a,int b){
            this.a=a;
            this.b=b;
        }
    }
    static class Trie{
        HashMap<Character,Trie>map;
         int c;
        Trie(){
            map=new HashMap<>();
            //z=null;
            //o=null;
            c=0;
        }
    }
    //static long ans;
    static int parent[];
    static int rank[];
    static int b[][];
    static int bo[];
    static int ho[];
    static int seg[];
    //static int pos;
   // static long mod=1000000007;
    //static int dp[][];
    static HashMap<Integer,Integer>map;
    static PriorityQueue<Student>q=new PriorityQueue<>();
    //static Stack<Integer>st;
   // static ArrayList<Character>ans;
    static ArrayList<ArrayList<Integer>>adj;
    //static long ans;
    static Trie root;
    static long fac[];
    static int gw,gb;
    static long mod=(long)(1000000007);
    //static int ans;
    static void solve()throws IOException{
        FastReader sc=new FastReader();
        int n,k,i,j,l,max,c1,c2,in,x,y,t;
        n=sc.nextInt();
        k=sc.nextInt();
        String s[]=new String[n];
        String p[]=new String[10];
        p[0]="1110111";
        p[1]="0010010";
        p[2]="1011101";
        p[3]="1011011";
        p[4]="0111010";
        p[5]="1101011";
        p[6]="1101111";
        p[7]="1010010";
        p[8]="1111111";
        p[9]="1111011";
        for(i=0;i<n;i++)
            s[i]=sc.nextLine();
        int dp[][]=new int[n][k+1];
        for(i=0;i<n;i++){
            for(j=0;j<=k;j++)
                dp[i][j]=-1;
        }
        for(i=0;i<=9;i++){
            x=bits(p[i]);
            y=bits(s[n-1]);
            if((int)(x&y)==y){
                x=count(p[i]);
                y=count(s[n-1]);
                if(x-y<=k)
                    dp[n-1][x-y]=Math.max(dp[n-1][x-y],i);
            }
        }
        //System.out.println(dp[0][4]);
        for(i=n-2;i>=0;i--){
            y=bits(s[i]);
            c1=count(s[i]);
            for(j=0;j<=k;j++){
                max=-1;
                in=-1;
                for(l=0;l<=9;l++){
                    x=bits(p[l]);
                    if((int)(x&y)!=y)
                        continue;
                    c2=count(p[l]);
                    if(c2-c1>j||dp[i+1][j+c1-c2]==-1)
                        continue;
                    dp[i][j]=Math.max(dp[i][j],l);
                }
            }
        }
        if(dp[0][k]==-1)
            System.out.println(-1);
        else{
            StringBuilder sb=new StringBuilder();
            j=0;
            i=k;
            while(j<n){
                sb.append(dp[j][i]);
                c1=count(p[dp[j][i]]);
                c2=count(s[j]);
                j+=1;
                i-=c1-c2;
            }
            System.out.println(sb.toString());
        }
    }
    static int count(String s){
        int c=0;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='1')
                ++c;
        }
        return c;
    }
    static int bits(String s){
        int ans=0,y=1;
        for(int i=s.length()-1;i>=0;i--){
            ans+=(s.charAt(i)-'0')*y;
            y=y*2;
        }
        return ans;
    }
    static int tr(int i,int n,int dp[][],int a[],int h,int l,int r,int in){
        if(i>=n)
            return 0;
        if(dp[i][in]!=-1)
            return dp[i][in];
        int mod1,mod2,ans1=0,ans2=0;
        mod1=(a[i]+in)%h;
        if(mod1>=l&&mod1<=r)
            ans1=1+tr(i+1,n,dp,a,h,l,r,mod1);
        else
            ans1=tr(i+1,n,dp,a,h,l,r,mod1);
        mod2=(a[i]+in-1)%h;
        if(mod2>=l&&mod2<=r)
            ans2=1+tr(i+1,n,dp,a,h,l,r,mod2);
        else
            ans2=tr(i+1,n,dp,a,h,l,r,mod2);
        dp[i][in]=Math.max(ans1,ans2);
        return dp[i][in];
    }
    static long nCr(long n, long r,
                                    long p)
    {
        return (fac[(int)n]* modInverse(fac[(int)r], p)
                % p * modInverse(fac[(int)(n-r)], p)
                                    % p) % p;
    }
    //static int prime[];
    //static int dp[];
    public static void main(String[] args){
        //long sum=0;
        try {
                codeforces.solve();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }
       static long modInverse(long n, long p)
    {
        return power(n, p-(long)2,p);
    }
 static long power(long x, long y, long p)
{
    long res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y %(long)2!=0)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res%p;
 }
    /*public static long power(long x,long a) {
		if(a==0) return 1;
		if(a%2==1)return (x*1L*power(x,a-1))%mod;
		return (power((int)((x*1L*x)%mod),a/2))%mod;
	}*/
   static int find(int x)
    {
        // Finds the representative of the set
        // that x is an element of
        while(parent[x]!=x)
        {
            // if x is not the parent of itself
            // Then x is not the representative of
            // his set,
            x=parent[x];

            // so we recursively call Find on its parent
            // and move i's node directly under the
            // representative of this set
        }

        return x;
    }
    static void union(int x, int y)
    {
        // Find representatives of two sets
        int xRoot = find(x), yRoot = find(y);

        // Elements are in the same set, no need
        // to unite anything.
        if (xRoot == yRoot)
            return;

         // If x's rank is less than y's rank
        if (rank[xRoot] < rank[yRoot])

            // Then move x under y  so that depth
            // of tree remains less
            parent[xRoot] = yRoot;

        // Else if y's rank is less than x's rank
        else if (rank[yRoot] < rank[xRoot])

            // Then move y under x so that depth of
            // tree remains less
            parent[yRoot] = xRoot;

        else // if ranks are the same
        {
            // Then move y under x (doesn't matter
            // which one goes where)
            parent[yRoot] = xRoot;

            // And increment the the result tree's
            // rank by 1
            rank[xRoot] = rank[xRoot] + 1;
        }
    }
    static long gcd(long a, long b)
    {
        if (a == 0){
             //ans+=b;
            return b;
        }
        return gcd(b % a, a);
    }
}