from operator import itemgetter

inf = 10 ** 4

n, m = map(int, input().split())
a = tuple(map(int, input().split()))

use = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6]
# use[i] : 数字iを作るのに必要なマッチの本数

use_num = {use[i]: i for i in range(1, 10) if i in a}
use_num = tuple(sorted(use_num.items()))
# 本数昇順
# print(use_num)

dp = [-inf] * (n + 1)
dp[0] = 0
# dp[j] : j本から構成できる桁数の最大値

for j in range(2, n + 1):
    for use, number in use_num:
        k = j - use
        if k >= 0:
            dp[j] = max(dp[j], dp[k] + 1)
# print(dp)

use_num = tuple(sorted(use_num, key=itemgetter(1), reverse=True))
# 数字降順

rest = n
ans = ''
while rest > 0:
    for use, number in use_num:
        k = rest - use
        if (k >= 0) and (dp[k] == dp[rest] - 1):
            ans += str(number)
            rest -= use
            break
print(ans) from operator import itemgetter inf = 10 ** 4 inf 10 ** 4 10 4 n, m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m a = tuple(map(int, input().split())) a tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split use = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] use [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] None 2 5 5 4 5 6 3 7 6 use[i]: i for i in range(1, 10) if i in a for i in range(1, 10) if i in a i range(1, 10) range 1 10 use[i] i in a i a if i in a use[i] use i i use_num = {use[i]: i for i in range(1, 10) if i in a} use_num {use[i]: i for i in range(1, 10) if i in a} use_num = tuple(sorted(use_num.items())) use_num tuple(sorted(use_num.items())) tuple sorted(use_num.items()) sorted use_num.items() use_num.items use_num items dp = [-inf] * (n + 1) dp [-inf] * (n + 1) [-inf] -inf inf n + 1 n 1 dp[0] = 0 dp[0] dp 0 0 for j in range(2, n + 1):
    for use, number in use_num:
        k = j - use
        if k >= 0:
            dp[j] = max(dp[j], dp[k] + 1)
# print(dp) j range(2, n + 1) range 2 n + 1 n 1 for use, number in use_num:
        k = j - use
        if k >= 0:
            dp[j] = max(dp[j], dp[k] + 1)
# print(dp) use number use_num k = j - use k j - use j use if k >= 0:
            dp[j] = max(dp[j], dp[k] + 1)
# print(dp) k >= 0 k 0 dp[j] = max(dp[j], dp[k] + 1) dp[j] dp j max(dp[j], dp[k] + 1) max dp[j] dp j dp[k] + 1 dp[k] dp k 1 use_num = tuple(sorted(use_num, key=itemgetter(1), reverse=True)) use_num tuple(sorted(use_num, key=itemgetter(1), reverse=True)) tuple sorted(use_num, key=itemgetter(1), reverse=True) sorted use_num key=itemgetter(1) key itemgetter(1) itemgetter 1 reverse=True reverse True rest = n rest n ans = '' ans '' while rest > 0:
    for use, number in use_num:
        k = rest - use
        if (k >= 0) and (dp[k] == dp[rest] - 1):
            ans += str(number)
            rest -= use
            break rest > 0 rest 0 for use, number in use_num:
        k = rest - use
        if (k >= 0) and (dp[k] == dp[rest] - 1):
            ans += str(number)
            rest -= use
            break use number use_num k = rest - use k rest - use rest use if (k >= 0) and (dp[k] == dp[rest] - 1):
            ans += str(number)
            rest -= use
            break (k >= 0) and (dp[k] == dp[rest] - 1) k >= 0 k 0 dp[k] == dp[rest] - 1 dp[k] dp k dp[rest] - 1 dp[rest] dp rest 1 ans += str(number) ans str(number) str number rest -= use rest use break print(ans) print ans dp = [-inf] * (n + 1) [-inf] * (n + 1) dp inf = 10 ** 4 10 ** 4 inf k = j - use j - use k use_num = tuple(sorted(use_num.items())) tuple(sorted(use_num.items())) use_num ans = '' '' ans rest = n n rest use = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] [None, 2, 5, 5, 4, 5, 6, 3, 7, 6] use ans += str(number) str(number) ans k = rest - use rest - use k use_num = {use[i]: i for i in range(1, 10) if i in a} {use[i]: i for i in range(1, 10) if i in a} use_num a = tuple(map(int, input().split())) tuple(map(int, input().split())) a use_num = tuple(sorted(use_num, key=itemgetter(1), reverse=True)) tuple(sorted(use_num, key=itemgetter(1), reverse=True)) use_num rest -= use use rest n, m = map(int, input().split()) map(int, input().split()) n m = map(int, input().split()) map(int, input().split()) m
from operator import itemgetter

inf = 10 ** 4

n, m = map(int, input().split())
a = tuple(map(int, input().split()))

use = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6]
# use[i] : 数字iを作るのに必要なマッチの本数

use_num = {use[i]: i for i in range(1, 10) if i in a}
use_num = tuple(sorted(use_num.items()))
# 本数昇順
# print(use_num)

dp = [-inf] * (n + 1)
dp[0] = 0
# dp[j] : j本から構成できる桁数の最大値

for j in range(2, n + 1):
    for use, number in use_num:
        k = j - use
        if k >= 0:
            dp[j] = max(dp[j], dp[k] + 1)
# print(dp)

use_num = tuple(sorted(use_num, key=itemgetter(1), reverse=True))
# 数字降順

rest = n
ans = ''
while rest > 0:
    for use, number in use_num:
        k = rest - use
        if (k >= 0) and (dp[k] == dp[rest] - 1):
            ans += str(number)
            rest -= use
            break
print(ans)