import sys
sys.setrecursionlimit(1000000)

n, m = map(int, input().split())
a = list(map(int, input().split()))
cost = [float("inf"), 2, 5, 5, 4, 5, 6, 3, 7, 6]
mincost = min(cost)

dp = [None] * (n + 1)
def dfs(at):
    if at < 0:
        return -1
    if at == 0:
        return 0
    if dp[at] is not None:
        return dp[at]
    r = max(dfs(at - cost[v]) * 10 + v for v in a)
    dp[at] = r
    return r


print(dfs(n)) import sys sys sys.setrecursionlimit(1000000) sys.setrecursionlimit sys setrecursionlimit 1000000 n, m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m a = list(map(int, input().split())) a list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split cost = [float("inf"), 2, 5, 5, 4, 5, 6, 3, 7, 6] cost [float("inf"), 2, 5, 5, 4, 5, 6, 3, 7, 6] float("inf") float "inf" 2 5 5 4 5 6 3 7 6 mincost = min(cost) mincost min(cost) min cost dp = [None] * (n + 1) dp [None] * (n + 1) [None] None n + 1 n 1 def dfs(at):
    if at < 0:
        return -1
    if at == 0:
        return 0
    if dp[at] is not None:
        return dp[at]
    r = max(dfs(at - cost[v]) * 10 + v for v in a)
    dp[at] = r
    return r dfs if at < 0:
        return -1
     at < 0 at 0 return -1 -1 if at == 0:
        return 0
     at == 0 at 0 return 0 0 if dp[at] is not None:
        return dp[at]
     dp[at] is not None dp[at] dp at None return dp[at] dp[at] dp at r = max(dfs(at - cost[v]) * 10 + v for v in a) r max(dfs(at - cost[v]) * 10 + v for v in a) max dfs(at - cost[v]) * 10 + v dfs(at - cost[v]) * 10 dfs(at - cost[v]) dfs at - cost[v] at cost[v] cost v 10 v dp[at] = r dp[at] dp at r return r r at at print(dfs(n)) print dfs(n) dfs n n, m = map(int, input().split()) map(int, input().split()) n cost = [float("inf"), 2, 5, 5, 4, 5, 6, 3, 7, 6] [float("inf"), 2, 5, 5, 4, 5, 6, 3, 7, 6] cost mincost = min(cost) min(cost) mincost m = map(int, input().split()) map(int, input().split()) m a = list(map(int, input().split())) list(map(int, input().split())) a def dfs(at):
    if at < 0:
        return -1
    if at == 0:
        return 0
    if dp[at] is not None:
        return dp[at]
    r = max(dfs(at - cost[v]) * 10 + v for v in a)
    dp[at] = r
    return r def dfs(at):
    if at < 0:
        return -1
    if at == 0:
        return 0
    if dp[at] is not None:
        return dp[at]
    r = max(dfs(at - cost[v]) * 10 + v for v in a)
    dp[at] = r
    return r dfs dp = [None] * (n + 1) [None] * (n + 1) dp
import sys
sys.setrecursionlimit(1000000)

n, m = map(int, input().split())
a = list(map(int, input().split()))
cost = [float("inf"), 2, 5, 5, 4, 5, 6, 3, 7, 6]
mincost = min(cost)

dp = [None] * (n + 1)
def dfs(at):
    if at < 0:
        return -1
    if at == 0:
        return 0
    if dp[at] is not None:
        return dp[at]
    r = max(dfs(at - cost[v]) * 10 + v for v in a)
    dp[at] = r
    return r


print(dfs(n))
