"""
- minでの桁数
- その後の余りでgreedy
- 
"""

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

N, M = list(map(int, input().split()))
t_dic = {}
A = list(map(int, input().split()))
A = sorted(A)[::-1]
for a in A:
    t_dic[a] = num_dic[a]
min_v = min(t_dic.values())

dp = [0] * (N+8)
for i in range(N):
    for a in A:
        num = t_dic[a]
        if i !=0 and dp[i] == 0:
            continue
        dp[i+num] = max(10 * dp[i]+ a, dp[i+num])

print(dp[N]) """
- minでの桁数
- その後の余りでgreedy
- 
""" num_dic = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} num_dic {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 = list(map(int, input().split())) N list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split M t_dic = {} t_dic {} A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split A = sorted(A)[::-1] A sorted(A)[::-1] (A) sorted A ::-1 -1 for a in A:
    t_dic[a] = num_dic[a] a A t_dic[a] = num_dic[a] t_dic[a] t_dic a num_dic[a] num_dic a min_v = min(t_dic.values()) min_v min(t_dic.values()) min t_dic.values() t_dic.values t_dic values dp = [0] * (N+8) dp [0] * (N+8) [0] 0 N+8 N 8 for i in range(N):
    for a in A:
        num = t_dic[a]
        if i !=0 and dp[i] == 0:
            continue
        dp[i+num] = max(10 * dp[i]+ a, dp[i+num]) i range(N) range N for a in A:
        num = t_dic[a]
        if i !=0 and dp[i] == 0:
            continue
        dp[i+num] = max(10 * dp[i]+ a, dp[i+num]) a A num = t_dic[a] num t_dic[a] t_dic a if i !=0 and dp[i] == 0:
            continue
         i !=0 and dp[i] == 0 i !=0 i 0 dp[i] == 0 dp[i] dp i 0 continue dp[i+num] = max(10 * dp[i]+ a, dp[i+num]) dp[i+num] dp i+num i num max(10 * dp[i]+ a, dp[i+num]) max 10 * dp[i]+ a 10 * dp[i] 10 dp[i] dp i a dp[i+num] dp i+num i num print(dp[N]) print dp[N] dp N num_dic = {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} num_dic t_dic = {} {} t_dic min_v = min(t_dic.values()) min(t_dic.values()) min_v dp = [0] * (N+8) [0] * (N+8) dp num = t_dic[a] t_dic[a] num M = list(map(int, input().split())) list(map(int, input().split())) M A = sorted(A)[::-1] sorted(A)[::-1] A N, M = list(map(int, input().split())) list(map(int, input().split())) N A = list(map(int, input().split())) list(map(int, input().split())) A
"""
- minでの桁数
- その後の余りでgreedy
- 
"""

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

N, M = list(map(int, input().split()))
t_dic = {}
A = list(map(int, input().split()))
A = sorted(A)[::-1]
for a in A:
    t_dic[a] = num_dic[a]
min_v = min(t_dic.values())

dp = [0] * (N+8)
for i in range(N):
    for a in A:
        num = t_dic[a]
        if i !=0 and dp[i] == 0:
            continue
        dp[i+num] = max(10 * dp[i]+ a, dp[i+num])

print(dp[N])