import collections
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

d0 = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
}

n, m = ril()
ls = ril()

d = collections.defaultdict(int)
for x in ls:
    d[d0[x]] = max(x, d[d0[x]])

dp = [(0, 0)] * (n + 1)
for i in range(2, n + 1):
    for count, num in d.items():
        if count > i:
            continue
        k, l = dp[i - count]
        if count != i and l == 0:
            continue
        dp[i] = (max(dp[i][0], 10 * k + num), max(dp[i][1], l + 1))
        if k != 0:
            dp[i] = (max(dp[i][0], num * 10**l + k), max(dp[i][1], l + 1))
print(dp[n][0]) import collections collections import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline ri = lambda: int(input()) ri lambda: int(input()) int(input()) int input() input rs = lambda: input().rstrip() rs lambda: input().rstrip() input().rstrip() ().rstrip () input rstrip ril = lambda: list(map(int, input().split())) ril lambda: list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split rsl = lambda: input().rstrip().split() rsl lambda: input().rstrip().split() input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split ri() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) ri() ri ris = lambda n: [ri() for _ in range(n)] ris lambda n: [ri() for _ in range(n)] [ri() for _ in range(n)] n rs() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) rs() rs rss = lambda n: [rs() for _ in range(n)] rss lambda n: [rs() for _ in range(n)] [rs() for _ in range(n)] n ril() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) ril() ril rils = lambda n: [ril() for _ in range(n)] rils lambda n: [ril() for _ in range(n)] [ril() for _ in range(n)] n rsl() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) rsl() rsl rsls = lambda n: [rsl() for _ in range(n)] rsls lambda n: [rsl() for _ in range(n)] [rsl() for _ in range(n)] n d0 = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
} d0 {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 n, m = ril() n ril() ril m ls = ril() ls ril() ril d = collections.defaultdict(int) d collections.defaultdict(int) collections.defaultdict collections defaultdict int for x in ls:
    d[d0[x]] = max(x, d[d0[x]]) x ls d[d0[x]] = max(x, d[d0[x]]) d[d0[x]] d d0[x] d0 x max(x, d[d0[x]]) max x d[d0[x]] d d0[x] d0 x dp = [(0, 0)] * (n + 1) dp [(0, 0)] * (n + 1) [(0, 0)] (0, 0) 0 0 n + 1 n 1 for i in range(2, n + 1):
    for count, num in d.items():
        if count > i:
            continue
        k, l = dp[i - count]
        if count != i and l == 0:
            continue
        dp[i] = (max(dp[i][0], 10 * k + num), max(dp[i][1], l + 1))
        if k != 0:
            dp[i] = (max(dp[i][0], num * 10**l + k), max(dp[i][1], l + 1)) i range(2, n + 1) range 2 n + 1 n 1 for count, num in d.items():
        if count > i:
            continue
        k, l = dp[i - count]
        if count != i and l == 0:
            continue
        dp[i] = (max(dp[i][0], 10 * k + num), max(dp[i][1], l + 1))
        if k != 0:
            dp[i] = (max(dp[i][0], num * 10**l + k), max(dp[i][1], l + 1)) count num d.items() d.items d items if count > i:
            continue
         count > i count i continue k, l = dp[i - count] k dp[i - count] dp i - count i count l if count != i and l == 0:
            continue
         count != i and l == 0 count != i count i l == 0 l 0 continue dp[i] = (max(dp[i][0], 10 * k + num), max(dp[i][1], l + 1)) dp[i] dp i (max(dp[i][0], 10 * k + num), max(dp[i][1], l + 1)) max(dp[i][0], 10 * k + num) max dp[i][0] [i] dp i 0 10 * k + num 10 * k 10 k num max(dp[i][1], l + 1) max dp[i][1] [i] dp i 1 l + 1 l 1 if k != 0:
            dp[i] = (max(dp[i][0], num * 10**l + k), max(dp[i][1], l + 1)) k != 0 k 0 dp[i] = (max(dp[i][0], num * 10**l + k), max(dp[i][1], l + 1)) dp[i] dp i (max(dp[i][0], num * 10**l + k), max(dp[i][1], l + 1)) max(dp[i][0], num * 10**l + k) max dp[i][0] [i] dp i 0 num * 10**l + k num * 10**l num 10**l 10 l k max(dp[i][1], l + 1) max dp[i][1] [i] dp i 1 l + 1 l 1 print(dp[n][0]) print dp[n][0] [n] dp n 0 ris = lambda n: [ri() for _ in range(n)] lambda n: [ri() for _ in range(n)] ris rss = lambda n: [rs() for _ in range(n)] lambda n: [rs() for _ in range(n)] rss ri = lambda: int(input()) lambda: int(input()) ri dp = [(0, 0)] * (n + 1) [(0, 0)] * (n + 1) dp rsl = lambda: input().rstrip().split() lambda: input().rstrip().split() rsl rsls = lambda n: [rsl() for _ in range(n)] lambda n: [rsl() for _ in range(n)] rsls d = collections.defaultdict(int) collections.defaultdict(int) d rs = lambda: input().rstrip() lambda: input().rstrip() rs d0 = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
} {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
} d0 ls = ril() ril() ls ril = lambda: list(map(int, input().split())) lambda: list(map(int, input().split())) ril k, l = dp[i - count] dp[i - count] k input = sys.stdin.readline sys.stdin.readline input l = dp[i - count] dp[i - count] l n, m = ril() ril() n m = ril() ril() m rils = lambda n: [ril() for _ in range(n)] lambda n: [ril() for _ in range(n)] rils
import collections
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

d0 = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
}

n, m = ril()
ls = ril()

d = collections.defaultdict(int)
for x in ls:
    d[d0[x]] = max(x, d[d0[x]])

dp = [(0, 0)] * (n + 1)
for i in range(2, n + 1):
    for count, num in d.items():
        if count > i:
            continue
        k, l = dp[i - count]
        if count != i and l == 0:
            continue
        dp[i] = (max(dp[i][0], 10 * k + num), max(dp[i][1], l + 1))
        if k != 0:
            dp[i] = (max(dp[i][0], num * 10**l + k), max(dp[i][1], l + 1))
print(dp[n][0])