INF = float('inf')

n, m = list(map(int, input().split()))

a = list(map(int, input().split()))

cost = [2, 5, 5, 4, 5, 6, 3, 7, 6]

l = [[x, cost[x-1]] for x in a]

l = sorted(l, key = lambda x: x[1]*10-x[0])

r = l[0]

_max = [0] + [-INF]*n

for i in range(1, n+1):

  for x in l:

    ind = i-x[1]

    if ind >= 0:

      _max[i] = max(_max[i], _max[ind]*10 + x[0])

ans = str(_max[n])

print(ans)
INF = float('inf')

n, m = list(map(int, input().split()))

a = list(map(int, input().split()))

cost = [2, 5, 5, 4, 5, 6, 3, 7, 6]

l = [[x, cost[x-1]] for x in a]

l = sorted(l, key = lambda x: x[1]*10-x[0])

r = l[0]

_max = [0] + [-INF]*n

for i in range(1, n+1):

  if 42 < i < n-42:

    _max[i] = max(_max[i], _max[i-r[1]]*10 + r[0])

  else:

    for x in l:

      ind = i-x[1]

      if ind >= 0:

        _max[i] = max(_max[i], _max[ind]*10 + x[0])

ans = str(_max[n])

print(ans)
INF = float("inf")
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
cost = [2, 5, 5, 4, 5, 6, 3, 7, 6]
l = [[x, cost[x - 1]] for x in a]
l = sorted(l, key=lambda x: x[1] * 10 - x[0])
r = l[0]
_max = [0] + [-INF] * n
for i in range(1, n + 1):
    for x in l:
        ind = i - x[1]
        if ind >= 0:
            _max[i] = max(_max[i], _max[ind] * 10 + x[0])
ans = str(_max[n])
print(ans)

INF = float("inf")
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
cost = [2, 5, 5, 4, 5, 6, 3, 7, 6]
l = [[x, cost[x - 1]] for x in a]
l = sorted(l, key=lambda x: x[1] * 10 - x[0])
r = l[0]
_max = [0] + [-INF] * n
for i in range(1, n + 1):
    if 42 < i < n - 42:
        _max[i] = max(_max[i], _max[i - r[1]] * 10 + r[0])
    else:
        for x in l:
            ind = i - x[1]
            if ind >= 0:
                _max[i] = max(_max[i], _max[ind] * 10 + x[0])
ans = str(_max[n])
print(ans)

-    for x in l: -        ind = i - x[1] -        if ind >= 0: -            _max[i] = max(_max[i], _max[ind] * 10 + x[0]) +    if 42 < i < n - 42: +        _max[i] = max(_max[i], _max[i - r[1]] * 10 + r[0]) +    else: +        for x in l: +            ind = i - x[1] +            if ind >= 0: +                _max[i] = max(_max[i], _max[ind] * 10 + x[0])