from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = float('INF')
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
A = LI()
L = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
match_set = {L[a] for a in A}
dp = [-INF] * (n + 1)
dp[0] = 0
for i in range(n + 1):
    for s in match_set:
        if i - s >= 0:
            dp[i] = max(dp[i - s] + 1, dp[i])

D = defaultdict(int)
for a in A:
    D[L[a]] = max(D[L[a]], a)

kv = sorted(D.items())
digit = dp[n]
ans = ''
while n:
    max_num = 0
    for cost, num in kv:
        if n - cost >= 0 and dp[n - cost] == digit - 1:
            if num > max_num:
                max_num = num
                c = cost
    ans += str(max_num)
    digit -= 1
    n -= c


print(ans)









 from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify from bisect import bisect_right, bisect_left import random random from itertools import permutations, accumulate, combinations, product import sys sys import string string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor, gamma, log from operator import mul from functools import reduce sys.setrecursionlimit(2147483647) sys.setrecursionlimit sys setrecursionlimit 2147483647 INF = float('INF') INF float('INF') float 'INF' def LI(): return list(map(int, sys.stdin.buffer.readline().split())) LI def I(): return int(sys.stdin.buffer.readline()) I def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() LS def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') S I() for i in range(n) for i in range(n) i range(n) range n for i in range(n) I() I def IR(n): return [I() for i in range(n)] IR n n LI() for i in range(n) for i in range(n) i range(n) range n for i in range(n) LI() LI def LIR(n): return [LI() for i in range(n)] LIR n n S() for i in range(n) for i in range(n) i range(n) range n for i in range(n) S() S def SR(n): return [S() for i in range(n)] SR n n LS() for i in range(n) for i in range(n) i range(n) range n for i in range(n) LS() LS def LSR(n): return [LS() for i in range(n)] LSR n n list(S()) for i in range(n) for i in range(n) i range(n) range n for i in range(n) list(S()) list S() S def SRL(n): return [list(S()) for i in range(n)] SRL n n [int(j) for j in list(S())] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [int(j) for j in list(S())] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] MSRL n n mod = 1000000007 mod 1000000007 n, m = LI() n LI() LI m A = LI() A LI() LI L = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] L [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 L[a] for a in A for a in A a A for a in A L[a] L a match_set = {L[a] for a in A} match_set dp = [-INF] * (n + 1) dp [-INF] * (n + 1) [-INF] -INF INF n + 1 n 1 dp[0] = 0 dp[0] dp 0 0 for i in range(n + 1):
    for s in match_set:
        if i - s >= 0:
            dp[i] = max(dp[i - s] + 1, dp[i]) i range(n + 1) range n + 1 n 1 for s in match_set:
        if i - s >= 0:
            dp[i] = max(dp[i - s] + 1, dp[i]) s match_set if i - s >= 0:
            dp[i] = max(dp[i - s] + 1, dp[i]) i - s >= 0 i - s i s 0 dp[i] = max(dp[i - s] + 1, dp[i]) dp[i] dp i max(dp[i - s] + 1, dp[i]) max dp[i - s] + 1 dp[i - s] dp i - s i s 1 dp[i] dp i D = defaultdict(int) D defaultdict(int) defaultdict int for a in A:
    D[L[a]] = max(D[L[a]], a) a A D[L[a]] = max(D[L[a]], a) D[L[a]] D L[a] L a max(D[L[a]], a) max D[L[a]] D L[a] L a a kv = sorted(D.items()) kv sorted(D.items()) sorted D.items() D.items D items digit = dp[n] digit dp[n] dp n ans = '' ans '' while n:
    max_num = 0
    for cost, num in kv:
        if n - cost >= 0 and dp[n - cost] == digit - 1:
            if num > max_num:
                max_num = num
                c = cost
    ans += str(max_num)
    digit -= 1
    n -= c n max_num = 0 max_num 0 for cost, num in kv:
        if n - cost >= 0 and dp[n - cost] == digit - 1:
            if num > max_num:
                max_num = num
                c = cost
     cost num kv if n - cost >= 0 and dp[n - cost] == digit - 1:
            if num > max_num:
                max_num = num
                c = cost
     n - cost >= 0 and dp[n - cost] == digit - 1 n - cost >= 0 n - cost n cost 0 dp[n - cost] == digit - 1 dp[n - cost] dp n - cost n cost digit - 1 digit 1 if num > max_num:
                max_num = num
                c = cost
     num > max_num num max_num max_num = num max_num num c = cost c cost ans += str(max_num) ans str(max_num) str max_num digit -= 1 digit 1 n -= c n c print(ans) print ans c = cost cost c n -= c c n INF = float('INF') float('INF') INF kv = sorted(D.items()) sorted(D.items()) kv def SRL(n): return [list(S()) for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] SRL D = defaultdict(int) defaultdict(int) D def SR(n): return [S() for i in range(n)] def SR(n): return [S() for i in range(n)] SR def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') S ans = '' '' ans ans += str(max_num) str(max_num) ans digit -= 1 1 digit def I(): return int(sys.stdin.buffer.readline()) def I(): return int(sys.stdin.buffer.readline()) I L = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] L def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() LS def LSR(n): return [LS() for i in range(n)] def LSR(n): return [LS() for i in range(n)] LSR A = LI() LI() A def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) LI def IR(n): return [I() for i in range(n)] def IR(n): return [I() for i in range(n)] IR n, m = LI() LI() n match_set = {L[a] for a in A} match_set dp = [-INF] * (n + 1) [-INF] * (n + 1) dp m = LI() LI() m def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] MSRL max_num = num num max_num def LIR(n): return [LI() for i in range(n)] def LIR(n): return [LI() for i in range(n)] LIR digit = dp[n] dp[n] digit mod = 1000000007 1000000007 mod max_num = 0 0 max_num
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = float('INF')
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
A = LI()
L = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
match_set = {L[a] for a in A}
dp = [-INF] * (n + 1)
dp[0] = 0
for i in range(n + 1):
    for s in match_set:
        if i - s >= 0:
            dp[i] = max(dp[i - s] + 1, dp[i])

D = defaultdict(int)
for a in A:
    D[L[a]] = max(D[L[a]], a)

kv = sorted(D.items())
digit = dp[n]
ans = ''
while n:
    max_num = 0
    for cost, num in kv:
        if n - cost >= 0 and dp[n - cost] == digit - 1:
            if num > max_num:
                max_num = num
                c = cost
    ans += str(max_num)
    digit -= 1
    n -= c


print(ans)










