# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N,M = map(int,input().split())
A = list(map(int,input().split()))

# 各数字に必要なマッチの本数
num = [0,2,5,5,4,5,6,3,7,6]

# dp : i本のマッチで作ることが可能な最大の桁数
dp = [-float("inf")]*(N+1)
# dp初期化 
#   - 0本 -> 0桁
#   - 各数字を1つ作る場合は1桁
dp[0] = 0
for a in A:
    if num[a] <= N:
        dp[num[a]] = 1

# dp更新
for i in range(1,N+1):
    for a in A:
        if i - num[a] >= 0:
            dp[i] = max(dp[i], dp[i-num[a]] + 1)

# 復元
A.sort(reverse=True)
ans = ""
for i in range(dp[N]):
    for a in A:
        if N-num[a] >= 0 and dp[N-num[a]] == dp[N]-1:
            ans += str(a)
            N -= num[a]
            break

print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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 num = [0,2,5,5,4,5,6,3,7,6] num [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 dp = [-float("inf")]*(N+1) dp [-float("inf")]*(N+1) [-float("inf")] -float("inf") float("inf") float "inf" N+1 N 1 dp[0] = 0 dp[0] dp 0 0 for a in A:
    if num[a] <= N:
        dp[num[a]] = 1

# dp更新 a A if num[a] <= N:
        dp[num[a]] = 1

# dp更新 num[a] <= N num[a] num a N dp[num[a]] = 1 dp[num[a]] dp num[a] num a 1 for i in range(1,N+1):
    for a in A:
        if i - num[a] >= 0:
            dp[i] = max(dp[i], dp[i-num[a]] + 1)

# 復元 i range(1,N+1) range 1 N+1 N 1 for a in A:
        if i - num[a] >= 0:
            dp[i] = max(dp[i], dp[i-num[a]] + 1)

# 復元 a A if i - num[a] >= 0:
            dp[i] = max(dp[i], dp[i-num[a]] + 1)

# 復元 i - num[a] >= 0 i - num[a] i num[a] num a 0 dp[i] = max(dp[i], dp[i-num[a]] + 1) dp[i] dp i max(dp[i], dp[i-num[a]] + 1) max dp[i] dp i dp[i-num[a]] + 1 dp[i-num[a]] dp i-num[a] i num[a] num a 1 A.sort(reverse=True) A.sort A sort reverse=True reverse True ans = "" ans "" for i in range(dp[N]):
    for a in A:
        if N-num[a] >= 0 and dp[N-num[a]] == dp[N]-1:
            ans += str(a)
            N -= num[a]
            break i range(dp[N]) range dp[N] dp N for a in A:
        if N-num[a] >= 0 and dp[N-num[a]] == dp[N]-1:
            ans += str(a)
            N -= num[a]
            break a A if N-num[a] >= 0 and dp[N-num[a]] == dp[N]-1:
            ans += str(a)
            N -= num[a]
            break N-num[a] >= 0 and dp[N-num[a]] == dp[N]-1 N-num[a] >= 0 N-num[a] N num[a] num a 0 dp[N-num[a]] == dp[N]-1 dp[N-num[a]] dp N-num[a] N num[a] num a dp[N]-1 dp[N] dp N 1 ans += str(a) ans str(a) str a N -= num[a] N num[a] num a break print(ans) print ans ans = "" "" ans M = map(int,input().split()) map(int,input().split()) M N -= num[a] num[a] N num = [0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] num N,M = map(int,input().split()) map(int,input().split()) N A = list(map(int,input().split())) list(map(int,input().split())) A dp = [-float("inf")]*(N+1) [-float("inf")]*(N+1) dp ans += str(a) str(a) ans input = sys.stdin.readline sys.stdin.readline input
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N,M = map(int,input().split())
A = list(map(int,input().split()))

# 各数字に必要なマッチの本数
num = [0,2,5,5,4,5,6,3,7,6]

# dp : i本のマッチで作ることが可能な最大の桁数
dp = [-float("inf")]*(N+1)
# dp初期化 
#   - 0本 -> 0桁
#   - 各数字を1つ作る場合は1桁
dp[0] = 0
for a in A:
    if num[a] <= N:
        dp[num[a]] = 1

# dp更新
for i in range(1,N+1):
    for a in A:
        if i - num[a] >= 0:
            dp[i] = max(dp[i], dp[i-num[a]] + 1)

# 復元
A.sort(reverse=True)
ans = ""
for i in range(dp[N]):
    for a in A:
        if N-num[a] >= 0 and dp[N-num[a]] == dp[N]-1:
            ans += str(a)
            N -= num[a]
            break

print(ans)