// Задача "Цифровой дисплей"
// Шестая Интернет-олимпиада сезона 2009-2010, усложненный уровень
// Автор задачи: Максим Буздалов, buzdalov@rain.ifmo.ru
// Автор решения: Нияз Нигматуллин, niyaz.nigmatullin@gmail.com

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

public class digits_nn_wrong implements Runnable {
	public static void main(String[] args) {
		new digits_nn_wrong().run();
	}

	BufferedReader br;
	PrintWriter out;

	private static final String FN = "digits";

	public void run() {
		Locale.setDefault(Locale.US);
		try {
			br = new BufferedReader(new FileReader(FN + ".in"));
			out = new PrintWriter(FN + ".out");
			solve();
			br.close();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	void doAssert(boolean isRight, String s) {
		if (!isRight)
			throw new AssertionError(s);
	}

	private final static int MAXK = 100, MAXN = 100000;
	private final static String[] numMasks = { "1011111", "0000101", "1110110",
			"1110101", "0101101", "1111001", "1111011", "1000101", "1111111",
			"1111101" };

	void calc(int[][] give, int[][] get) {
		int[] p = new int[10];
		for (int i = 0; i < 10; i++) {
			p[i] = Integer.parseInt(numMasks[i], 2);
		}
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				give[i][j] = (p[i] ^ p[j]) & p[j];
				get[i][j] = (p[i] ^ p[j]) & ~p[j];
			}
		}
	}

	public String solve(String s, int k) {
		int[][] give = new int[10][10];
		int[][] get = new int[10][10];
		calc(give, get);
		char[] c = new StringBuilder(s).reverse().toString().toCharArray();
		int n = c.length;
		doAssert(1 <= n && n <= MAXN,
				"the length of the number is out of range");
		doAssert(1 <= k && k <= MAXK, "k is out of range");
		int[][] dp = new int[2 * k + 1][n + 1];
		for (int[] d : dp) {
			Arrays.fill(d, 2 * MAXK);
		}
		dp[k][0] = 0;
		for (int j = 1; j <= n; j++) {
			int num = c[j - 1] - '0';
			for (int i = 0; i <= 2 * k; ++i) {
				for (int t = 0; t < 10; ++t) {
					int newI = i + get[num][t] - give[num][t];
					if (newI >= 0 && newI <= 2 * k) {
						dp[i][j] = Math.min(dp[i][j], dp[newI][j - 1]
								+ get[num][t]);
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			int num = c[i] - '0';
			for (int j = num + 1; j < 10; j++) {
				int balance = give[num][j] - get[num][j];
				if (balance >= 0 && balance <= 2 * k) {
					if (dp[balance][i] + get[num][j] <= k) {
						char[] ans = c.clone();
						ans[i] = (char) (j + '0');
						loop: for (int e = i; e > 0; --e) {
							num = c[i - 1] - '0';
							for (int t = 0; t < 10; ++t) {
								int newBalance = balance + give[num][t]
										- get[num][t];
								if (newBalance >= 0 && newBalance <= 2 * k) {
									if (dp[balance][e] == dp[newBalance][e - 1]
											+ get[num][t]) {
										balance = newBalance;
										ans[e - 1] = (char) (t + '0');
										continue loop;
									}
								}
							}
							throw new AssertionError(
									"couldn't restore the answer");
						}
						return new StringBuilder(new String(ans)).reverse()
								.toString();
					}
				}
			}
		}
		return "NO SOLUTION";
	}

	private void solve() throws IOException {
		int k = Integer.parseInt(br.readLine());
		String s = br.readLine();
		out.println(solve(s, k));
	}
}
