N,M = list(map(int,input().split()))

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

weight = [0,2,5,5,4,5,6,3,7,6]

dp=[-1]*(N+1)

dp[0] = 0



for i in range(N+1):

  if dp[i] == -1: continue

  for a in A:

    if i+weight[a]<N+1:

      dp[i+weight[a]] = max(dp[i+weight[a]],dp[i]*10+a)



print((dp[-1]))


N, M = list(map(int, input().split()))

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

numbers = [0,2,5,5,4,5,6,3,7,6]





dp = ["-" for i in range(11000)]

dp[0] = ""



def cal(x, y):

  lenx = len(x)

  leny = len(y)

  

  if lenx > leny:

    return x

  if leny > lenx:

    return y

  

  return max(x, y)



for i in range(N):

  if dp[i] == "-": 

    continue



  for j in range(M):

    num = numbers[A[j]]

    dp[i+num] = cal(dp[i] + str(A[j]), dp[i+num])



print((dp[N]))
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N + 1):
    if dp[i] == -1:
        continue
    for a in A:
        if i + weight[a] < N + 1:
            dp[i + weight[a]] = max(dp[i + weight[a]], dp[i] * 10 + a)
print((dp[-1]))

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
numbers = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = ["-" for i in range(11000)]
dp[0] = ""


def cal(x, y):
    lenx = len(x)
    leny = len(y)
    if lenx > leny:
        return x
    if leny > lenx:
        return y
    return max(x, y)


for i in range(N):
    if dp[i] == "-":
        continue
    for j in range(M):
        num = numbers[A[j]]
        dp[i + num] = cal(dp[i] + str(A[j]), dp[i + num])
print((dp[N]))

-weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] -dp = [-1] * (N + 1) -dp[0] = 0 -for i in range(N + 1): -    if dp[i] == -1: +numbers = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] +dp = ["-" for i in range(11000)] +dp[0] = "" + + +def cal(x, y): +    lenx = len(x) +    leny = len(y) +    if lenx > leny: +        return x +    if leny > lenx: +        return y +    return max(x, y) + + +for i in range(N): +    if dp[i] == "-": -    for a in A: -        if i + weight[a] < N + 1: -            dp[i + weight[a]] = max(dp[i + weight[a]], dp[i] * 10 + a) -print((dp[-1])) +    for j in range(M): +        num = numbers[A[j]] +        dp[i + num] = cal(dp[i] + str(A[j]), dp[i + num]) +print((dp[N]))