import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from copy import deepcopy
def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B

def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)    
if __name__ == '__main__':
    main() import sys sys sys.setrecursionlimit(10000000) sys.setrecursionlimit sys setrecursionlimit 10000000 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 INF = 10 ** 15 INF 10 ** 15 10 15 from copy import deepcopy def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B compare tot_a = sum(A) tot_a sum(A) sum A tot_b = sum(B) tot_b sum(B) sum B if tot_a > tot_b:
        return A
     tot_a > tot_b tot_a tot_b return A A if tot_b > tot_a:
        return B
     tot_b > tot_a tot_b tot_a return B B for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B i range(9,0,-1) range 9 0 -1 if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B A[i] > B[i] A[i] A i B[i] B i return A A elif A[i] < B[i]:
            return B A[i] < B[i] A[i] A i B[i] B i return B B A A B B def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)     main 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 match = (-1,2,5,5,4,5,6,3,7,6) match (-1,2,5,5,4,5,6,3,7,6) -1 2 5 5 4 5 6 3 7 6 [0]*10 for _ in range(1 + N) for _ in range(1 + N) _ range(1 + N) range 1 + N 1 N for _ in range(1 + N) [0]*10 [0] 0 10 dp = [[0]*10 for _ in range(1 + N)] dp [[0]*10 for _ in range(1 + N)] changed = [0] * (N + 1) changed [0] * (N + 1) [0] 0 N + 1 N 1 changed[0] = 1 changed[0] changed 0 1 for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
     i range(N) range N for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
     j A if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
     i + 1 - match[j] >= 0 and changed[i + 1 - match[j]] i + 1 - match[j] >= 0 i + 1 - match[j] i + 1 i 1 match[j] match j 0 changed[i + 1 - match[j]] changed i + 1 - match[j] i + 1 i 1 match[j] match j B = deepcopy(dp[i + 1 - match[j]]) B deepcopy(dp[i + 1 - match[j]]) deepcopy dp[i + 1 - match[j]] dp i + 1 - match[j] i + 1 i 1 match[j] match j B[j] += 1 B[j] B j 1 if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
     compare(dp[i + 1],B) == B compare(dp[i + 1],B) compare dp[i + 1] dp i + 1 i 1 B B dp[i + 1] = B dp[i + 1] dp i + 1 i 1 B changed[i + 1] = 1 changed[i + 1] changed i + 1 i 1 1 ans = '' ans '' for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
     i range(9,0,-1) range 9 0 -1 ans += str(i) * dp[-1][i] ans str(i) * dp[-1][i] str(i) str i dp[-1][i] [-1] dp -1 i print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B compare INF = 10 ** 15 10 ** 15 INF def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)     def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)     main
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from copy import deepcopy
def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B

def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)    
if __name__ == '__main__':
    main()