import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

import re
# import numpy as np
# from scipy.misc import comb

# 再帰がやばいとき
# import sys
# sys.setrecursionlimit(10**9)

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e50)
MOD = int(1e9)+7 # 10^9 + 7

# field[H][W]
def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]

########

def main():
    H, M = l_inpl()
    A = l_inpl()
    d = {
        "1": 2,
        "2": 5,
        "3": 5,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 3,
        "8": 7,
        "9": 6
    }

    new_d = {}
    for Ai in A:
        new_d[Ai] = d[str(Ai)]

    dp = [-INF] * (H+1) # ちょうどi本のマッチ棒を使って，条件を満たす整数を作るときの最大桁数
    dp[0] = 0

    for i in range(1, H+1):
        tmp = []
        for k, v in new_d.items():
            if i-v < 0:
                tmp.append(-INF)
            else:
                tmp.append(dp[i-v]+1)
        dp[i] = max(tmp)

    s = sorted(new_d.items(), key=lambda x:-x[0])

    ans = ""
    remain = H

    while remain > 0:
        for k, v in s:
            if remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1: # 一桁だけ減少している
                ans += str(k)
                remain -= v
                break
    print(ans)
if __name__ == "__main__":
    main() import math math import copy copy from operator import mul from functools import reduce from collections import defaultdict from collections import Counter from collections import deque from itertools import product from itertools import permutations from itertools import combinations from itertools import accumulate from bisect import bisect_left, bisect_right import re re def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上 inside return 0 <= y < H and 0 <= x < W 0 <= y < H and 0 <= x < W 0 <= y < H 0 <= y 0 y H 0 <= x < W 0 <= x 0 x W y y x x H H W W dy = [0, -1, 0, 1] dy [0, -1, 0, 1] 0 -1 0 1 dx = [1, 0, -1, 0] dx [1, 0, -1, 0] 1 0 -1 0 def i_inpl(): return int(input()) i_inpl def l_inpl(): return list(map(int, input().split())) l_inpl i_inpl() for _ in range(x) for _ in range(x) _ range(x) range x for _ in range(x) i_inpl() i_inpl def line_inpl(x): return [i_inpl() for _ in range(x)] line_inpl x x INF = int(1e50) INF int(1e50) int 1e50 MOD = int(1e9)+7 MOD int(1e9)+7 int(1e9) int 1e9 7 def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]

######## create_grid [ value for _ in range(W)] for _ in range(H) for _ in range(H) _ range(H) range H for _ in range(H) [ value for _ in range(W)] return [[ value for _ in range(W)] for _ in range(H)] [[ value for _ in range(W)] for _ in range(H)] H H W W value = 0 value 0 def main():
    H, M = l_inpl()
    A = l_inpl()
    d = {
        "1": 2,
        "2": 5,
        "3": 5,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 3,
        "8": 7,
        "9": 6
    }

    new_d = {}
    for Ai in A:
        new_d[Ai] = d[str(Ai)]

    dp = [-INF] * (H+1) # ちょうどi本のマッチ棒を使って，条件を満たす整数を作るときの最大桁数
    dp[0] = 0

    for i in range(1, H+1):
        tmp = []
        for k, v in new_d.items():
            if i-v < 0:
                tmp.append(-INF)
            else:
                tmp.append(dp[i-v]+1)
        dp[i] = max(tmp)

    s = sorted(new_d.items(), key=lambda x:-x[0])

    ans = ""
    remain = H

    while remain > 0:
        for k, v in s:
            if remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1: # 一桁だけ減少している
                ans += str(k)
                remain -= v
                break
    print(ans) main H, M = l_inpl() H l_inpl() l_inpl M A = l_inpl() A l_inpl() l_inpl d = {
        "1": 2,
        "2": 5,
        "3": 5,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 3,
        "8": 7,
        "9": 6
    } d {
        "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 new_d = {} new_d {} for Ai in A:
        new_d[Ai] = d[str(Ai)]

     Ai A new_d[Ai] = d[str(Ai)] new_d[Ai] new_d Ai d[str(Ai)] d str(Ai) str Ai dp = [-INF] * (H+1) dp [-INF] * (H+1) [-INF] -INF INF H+1 H 1 dp[0] = 0 dp[0] dp 0 0 for i in range(1, H+1):
        tmp = []
        for k, v in new_d.items():
            if i-v < 0:
                tmp.append(-INF)
            else:
                tmp.append(dp[i-v]+1)
        dp[i] = max(tmp)

     i range(1, H+1) range 1 H+1 H 1 tmp = [] tmp [] for k, v in new_d.items():
            if i-v < 0:
                tmp.append(-INF)
            else:
                tmp.append(dp[i-v]+1)
         k v new_d.items() new_d.items new_d items if i-v < 0:
                tmp.append(-INF)
            else:
                tmp.append(dp[i-v]+1)
         i-v < 0 i-v i v 0 tmp.append(-INF) tmp.append tmp append -INF INF tmp.append(dp[i-v]+1) tmp.append tmp append dp[i-v]+1 dp[i-v] dp i-v i v 1 dp[i] = max(tmp) dp[i] dp i max(tmp) max tmp s = sorted(new_d.items(), key=lambda x:-x[0]) s sorted(new_d.items(), key=lambda x:-x[0]) sorted new_d.items() new_d.items new_d items key=lambda x:-x[0] key lambda x:-x[0] -x[0] x[0] x 0 x ans = "" ans "" remain = H remain H while remain > 0:
        for k, v in s:
            if remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1: # 一桁だけ減少している
                ans += str(k)
                remain -= v
                break
     remain > 0 remain 0 for k, v in s:
            if remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1: # 一桁だけ減少している
                ans += str(k)
                remain -= v
                break
     k v s if remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1: # 一桁だけ減少している
                ans += str(k)
                remain -= v
                break
     remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1 remain >= v and dp[remain-v] != -INF remain >= v remain v dp[remain-v] != -INF dp[remain-v] dp remain-v remain v -INF INF dp[remain-v] == dp[remain] - 1 dp[remain-v] dp remain-v remain v dp[remain] - 1 dp[remain] dp remain 1 ans += str(k) ans str(k) str k remain -= v remain v break print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main MOD = int(1e9)+7 int(1e9)+7 MOD def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]

######## def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]

######## create_grid def main():
    H, M = l_inpl()
    A = l_inpl()
    d = {
        "1": 2,
        "2": 5,
        "3": 5,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 3,
        "8": 7,
        "9": 6
    }

    new_d = {}
    for Ai in A:
        new_d[Ai] = d[str(Ai)]

    dp = [-INF] * (H+1) # ちょうどi本のマッチ棒を使って，条件を満たす整数を作るときの最大桁数
    dp[0] = 0

    for i in range(1, H+1):
        tmp = []
        for k, v in new_d.items():
            if i-v < 0:
                tmp.append(-INF)
            else:
                tmp.append(dp[i-v]+1)
        dp[i] = max(tmp)

    s = sorted(new_d.items(), key=lambda x:-x[0])

    ans = ""
    remain = H

    while remain > 0:
        for k, v in s:
            if remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1: # 一桁だけ減少している
                ans += str(k)
                remain -= v
                break
    print(ans) def main():
    H, M = l_inpl()
    A = l_inpl()
    d = {
        "1": 2,
        "2": 5,
        "3": 5,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 3,
        "8": 7,
        "9": 6
    }

    new_d = {}
    for Ai in A:
        new_d[Ai] = d[str(Ai)]

    dp = [-INF] * (H+1) # ちょうどi本のマッチ棒を使って，条件を満たす整数を作るときの最大桁数
    dp[0] = 0

    for i in range(1, H+1):
        tmp = []
        for k, v in new_d.items():
            if i-v < 0:
                tmp.append(-INF)
            else:
                tmp.append(dp[i-v]+1)
        dp[i] = max(tmp)

    s = sorted(new_d.items(), key=lambda x:-x[0])

    ans = ""
    remain = H

    while remain > 0:
        for k, v in s:
            if remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1: # 一桁だけ減少している
                ans += str(k)
                remain -= v
                break
    print(ans) main INF = int(1e50) int(1e50) INF def l_inpl(): return list(map(int, input().split())) def l_inpl(): return list(map(int, input().split())) l_inpl dy = [0, -1, 0, 1] [0, -1, 0, 1] dy dx = [1, 0, -1, 0] [1, 0, -1, 0] dx def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上 def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上 inside def i_inpl(): return int(input()) def i_inpl(): return int(input()) i_inpl def line_inpl(x): return [i_inpl() for _ in range(x)] def line_inpl(x): return [i_inpl() for _ in range(x)] line_inpl
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

import re
# import numpy as np
# from scipy.misc import comb

# 再帰がやばいとき
# import sys
# sys.setrecursionlimit(10**9)

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e50)
MOD = int(1e9)+7 # 10^9 + 7

# field[H][W]
def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]

########

def main():
    H, M = l_inpl()
    A = l_inpl()
    d = {
        "1": 2,
        "2": 5,
        "3": 5,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 3,
        "8": 7,
        "9": 6
    }

    new_d = {}
    for Ai in A:
        new_d[Ai] = d[str(Ai)]

    dp = [-INF] * (H+1) # ちょうどi本のマッチ棒を使って，条件を満たす整数を作るときの最大桁数
    dp[0] = 0

    for i in range(1, H+1):
        tmp = []
        for k, v in new_d.items():
            if i-v < 0:
                tmp.append(-INF)
            else:
                tmp.append(dp[i-v]+1)
        dp[i] = max(tmp)

    s = sorted(new_d.items(), key=lambda x:-x[0])

    ans = ""
    remain = H

    while remain > 0:
        for k, v in s:
            if remain >= v and dp[remain-v] != -INF and dp[remain-v] == dp[remain] - 1: # 一桁だけ減少している
                ans += str(k)
                remain -= v
                break
    print(ans)
if __name__ == "__main__":
    main()
