import sys

sys.setrecursionlimit(10 ** 6)

inf = 10 ** 4


def larger(x, y):
    lx, ly = len(x), len(y)
    if lx > ly:
        return x
    elif lx < ly:
        return y
    else:
        x.sort(reverse=True)
        y.sort(reverse=True)
        for xx, yy in zip(x, y):
            if xx > yy:
                return x
            elif xx < yy:
                return y
        return x


def dfs(rest):
    if memo[rest] is not None:
        return memo[rest]

    res = []
    for use, number in d.items():
        if rest - use >= 0 and dp[rest - use] == dp[rest] - 1:
            res = larger(res, [number] + dfs(rest - use))

    memo[rest] = res
    return res


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

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

for i in range(1, 10):
    if i not in a:
        needs[i] = inf

d = {needs[i]: i for i in range(1, 10) if needs[i] != inf}
# d[本数] = その本数で作れる最大の数値

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

for j in range(2, n + 1):
    for use, number in d.items():
        if j - use >= 0:
            dp[j] = max(dp[j], dp[j - use] + 1)

# print(dp)

memo = [None] * (n + 1)

ans = dfs(n)
ans.sort(reverse=True)
print(*ans, sep='') import sys sys sys.setrecursionlimit(10 ** 6) sys.setrecursionlimit sys setrecursionlimit 10 ** 6 10 6 inf = 10 ** 4 inf 10 ** 4 10 4 def larger(x, y):
    lx, ly = len(x), len(y)
    if lx > ly:
        return x
    elif lx < ly:
        return y
    else:
        x.sort(reverse=True)
        y.sort(reverse=True)
        for xx, yy in zip(x, y):
            if xx > yy:
                return x
            elif xx < yy:
                return y
        return x larger lx, ly = len(x), len(y) lx len(x) len x ly len(y) len y if lx > ly:
        return x
    elif lx < ly:
        return y
    else:
        x.sort(reverse=True)
        y.sort(reverse=True)
        for xx, yy in zip(x, y):
            if xx > yy:
                return x
            elif xx < yy:
                return y
        return x lx > ly lx ly return x x elif lx < ly:
        return y
     lx < ly lx ly return y y x.sort(reverse=True) x.sort x sort reverse=True reverse True y.sort(reverse=True) y.sort y sort reverse=True reverse True for xx, yy in zip(x, y):
            if xx > yy:
                return x
            elif xx < yy:
                return y
         xx yy zip(x, y) zip x y if xx > yy:
                return x
            elif xx < yy:
                return y
         xx > yy xx yy return x x elif xx < yy:
                return y
         xx < yy xx yy return y y return x x x x y y def dfs(rest):
    if memo[rest] is not None:
        return memo[rest]

    res = []
    for use, number in d.items():
        if rest - use >= 0 and dp[rest - use] == dp[rest] - 1:
            res = larger(res, [number] + dfs(rest - use))

    memo[rest] = res
    return res dfs if memo[rest] is not None:
        return memo[rest]

     memo[rest] is not None memo[rest] memo rest None return memo[rest] memo[rest] memo rest res = [] res [] for use, number in d.items():
        if rest - use >= 0 and dp[rest - use] == dp[rest] - 1:
            res = larger(res, [number] + dfs(rest - use))

     use number d.items() d.items d items if rest - use >= 0 and dp[rest - use] == dp[rest] - 1:
            res = larger(res, [number] + dfs(rest - use))

     rest - use >= 0 and dp[rest - use] == dp[rest] - 1 rest - use >= 0 rest - use rest use 0 dp[rest - use] == dp[rest] - 1 dp[rest - use] dp rest - use rest use dp[rest] - 1 dp[rest] dp rest 1 res = larger(res, [number] + dfs(rest - use)) res larger(res, [number] + dfs(rest - use)) larger res [number] + dfs(rest - use) [number] number dfs(rest - use) dfs rest - use rest use memo[rest] = res memo[rest] memo rest res return res res rest rest 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 needs = [inf, 2, 5, 5, 4, 5, 6, 3, 7, 6] needs [inf, 2, 5, 5, 4, 5, 6, 3, 7, 6] inf 2 5 5 4 5 6 3 7 6 for i in range(1, 10):
    if i not in a:
        needs[i] = inf i range(1, 10) range 1 10 if i not in a:
        needs[i] = inf i not in a i a needs[i] = inf needs[i] needs i inf needs[i]: i for i in range(1, 10) if needs[i] != inf for i in range(1, 10) if needs[i] != inf i range(1, 10) range 1 10 needs[i] needs[i] != inf needs[i] needs i inf if needs[i] != inf needs[i] needs i i d = {needs[i]: i for i in range(1, 10) if needs[i] != inf} d {needs[i]: i for i in range(1, 10) if needs[i] != inf} 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 d.items():
        if j - use >= 0:
            dp[j] = max(dp[j], dp[j - use] + 1)

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

# print(dp) use number d.items() d.items d items if j - use >= 0:
            dp[j] = max(dp[j], dp[j - use] + 1)

# print(dp) j - use >= 0 j - use j use 0 dp[j] = max(dp[j], dp[j - use] + 1) dp[j] dp j max(dp[j], dp[j - use] + 1) max dp[j] dp j dp[j - use] + 1 dp[j - use] dp j - use j use 1 memo = [None] * (n + 1) memo [None] * (n + 1) [None] None n + 1 n 1 ans = dfs(n) ans dfs(n) dfs n ans.sort(reverse=True) ans.sort ans sort reverse=True reverse True print(*ans, sep='') print *ans ans sep='' sep '' a = tuple(map(int, input().split())) tuple(map(int, input().split())) a memo = [None] * (n + 1) [None] * (n + 1) memo needs = [inf, 2, 5, 5, 4, 5, 6, 3, 7, 6] [inf, 2, 5, 5, 4, 5, 6, 3, 7, 6] needs inf = 10 ** 4 10 ** 4 inf ans = dfs(n) dfs(n) ans def dfs(rest):
    if memo[rest] is not None:
        return memo[rest]

    res = []
    for use, number in d.items():
        if rest - use >= 0 and dp[rest - use] == dp[rest] - 1:
            res = larger(res, [number] + dfs(rest - use))

    memo[rest] = res
    return res def dfs(rest):
    if memo[rest] is not None:
        return memo[rest]

    res = []
    for use, number in d.items():
        if rest - use >= 0 and dp[rest - use] == dp[rest] - 1:
            res = larger(res, [number] + dfs(rest - use))

    memo[rest] = res
    return res dfs def larger(x, y):
    lx, ly = len(x), len(y)
    if lx > ly:
        return x
    elif lx < ly:
        return y
    else:
        x.sort(reverse=True)
        y.sort(reverse=True)
        for xx, yy in zip(x, y):
            if xx > yy:
                return x
            elif xx < yy:
                return y
        return x def larger(x, y):
    lx, ly = len(x), len(y)
    if lx > ly:
        return x
    elif lx < ly:
        return y
    else:
        x.sort(reverse=True)
        y.sort(reverse=True)
        for xx, yy in zip(x, y):
            if xx > yy:
                return x
            elif xx < yy:
                return y
        return x larger d = {needs[i]: i for i in range(1, 10) if needs[i] != inf} {needs[i]: i for i in range(1, 10) if needs[i] != inf} d n, m = map(int, input().split()) map(int, input().split()) n dp = [-inf] * (n + 1) [-inf] * (n + 1) dp m = map(int, input().split()) map(int, input().split()) m
import sys

sys.setrecursionlimit(10 ** 6)

inf = 10 ** 4


def larger(x, y):
    lx, ly = len(x), len(y)
    if lx > ly:
        return x
    elif lx < ly:
        return y
    else:
        x.sort(reverse=True)
        y.sort(reverse=True)
        for xx, yy in zip(x, y):
            if xx > yy:
                return x
            elif xx < yy:
                return y
        return x


def dfs(rest):
    if memo[rest] is not None:
        return memo[rest]

    res = []
    for use, number in d.items():
        if rest - use >= 0 and dp[rest - use] == dp[rest] - 1:
            res = larger(res, [number] + dfs(rest - use))

    memo[rest] = res
    return res


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

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

for i in range(1, 10):
    if i not in a:
        needs[i] = inf

d = {needs[i]: i for i in range(1, 10) if needs[i] != inf}
# d[本数] = その本数で作れる最大の数値

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

for j in range(2, n + 1):
    for use, number in d.items():
        if j - use >= 0:
            dp[j] = max(dp[j], dp[j - use] + 1)

# print(dp)

memo = [None] * (n + 1)

ans = dfs(n)
ans.sort(reverse=True)
print(*ans, sep='')