import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,M=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    for a in A:
        if a==3:
            try:
                A.remove(2)
            except:
                pass
        elif a==5:
            try:
                A.remove(2)
            except:
                pass
            try:
                A.remove(3)
            except:
                pass
        elif a==9:
            try:
                A.remove(6)
            except:
                pass
    d={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
    l=[d[x] for x in A]
    dp=[[0]*(N+1) for _ in range(len(l)+1)]
    prev_w=[[0]*(N+1) for _ in range(len(l)+1)]
    real_w=[[0]*(N+1) for _ in range(len(l)+1)]
    for num in range(len(l)):
        for w in range(N+1):
            if w>=l[num]:
                if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
            if dp[num+1][w]<dp[num][w]:
                dp[num+1][w]=dp[num][w]
                prev_w[num+1][w]=w
                real_w[num+1][w]=w
    l2=[]
    cur_w=N
    for num in range(len(l)-1,-1,-1):
        while True:
            if prev_w[num+1][cur_w]==cur_w-l[num]:
                l2.append(str(A[num]))
                cur_w=prev_w[num+1][cur_w]
            else:
                cur_w=prev_w[num+1][cur_w]
                break
    print(''.join(l2))

if __name__ == '__main__':
    main() import sys sys sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 INF=10**18 INF 10**18 10 18 MOD=10**9+7 MOD 10**9+7 10**9 10 9 7 def input():
    return sys.stdin.readline().rstrip() input return sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip def main():
    N,M=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    for a in A:
        if a==3:
            try:
                A.remove(2)
            except:
                pass
        elif a==5:
            try:
                A.remove(2)
            except:
                pass
            try:
                A.remove(3)
            except:
                pass
        elif a==9:
            try:
                A.remove(6)
            except:
                pass
    d={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
    l=[d[x] for x in A]
    dp=[[0]*(N+1) for _ in range(len(l)+1)]
    prev_w=[[0]*(N+1) for _ in range(len(l)+1)]
    real_w=[[0]*(N+1) for _ in range(len(l)+1)]
    for num in range(len(l)):
        for w in range(N+1):
            if w>=l[num]:
                if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
            if dp[num+1][w]<dp[num][w]:
                dp[num+1][w]=dp[num][w]
                prev_w[num+1][w]=w
                real_w[num+1][w]=w
    l2=[]
    cur_w=N
    for num in range(len(l)-1,-1,-1):
        while True:
            if prev_w[num+1][cur_w]==cur_w-l[num]:
                l2.append(str(A[num]))
                cur_w=prev_w[num+1][cur_w]
            else:
                cur_w=prev_w[num+1][cur_w]
                break
    print(''.join(l2)) 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 A.sort() A.sort A sort for a in A:
        if a==3:
            try:
                A.remove(2)
            except:
                pass
        elif a==5:
            try:
                A.remove(2)
            except:
                pass
            try:
                A.remove(3)
            except:
                pass
        elif a==9:
            try:
                A.remove(6)
            except:
                pass
     a A if a==3:
            try:
                A.remove(2)
            except:
                pass
        elif a==5:
            try:
                A.remove(2)
            except:
                pass
            try:
                A.remove(3)
            except:
                pass
        elif a==9:
            try:
                A.remove(6)
            except:
                pass
     a==3 a 3 try:
                A.remove(2)
            except:
                pass
         except:
                pass
         A.remove(2) A.remove A remove 2 elif a==5:
            try:
                A.remove(2)
            except:
                pass
            try:
                A.remove(3)
            except:
                pass
         a==5 a 5 try:
                A.remove(2)
            except:
                pass
             except:
                pass
             A.remove(2) A.remove A remove 2 try:
                A.remove(3)
            except:
                pass
         except:
                pass
         A.remove(3) A.remove A remove 3 elif a==9:
            try:
                A.remove(6)
            except:
                pass
     a==9 a 9 try:
                A.remove(6)
            except:
                pass
     except:
                pass
     A.remove(6) A.remove A remove 6 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 d[x] for x in A for x in A x A for x in A d[x] d x l=[d[x] for x in A] l [d[x] for x in A] [0]*(N+1) for _ in range(len(l)+1) for _ in range(len(l)+1) _ range(len(l)+1) range len(l)+1 len(l) len l 1 for _ in range(len(l)+1) [0]*(N+1) [0] 0 N+1 N 1 dp=[[0]*(N+1) for _ in range(len(l)+1)] dp [[0]*(N+1) for _ in range(len(l)+1)] [0]*(N+1) for _ in range(len(l)+1) for _ in range(len(l)+1) _ range(len(l)+1) range len(l)+1 len(l) len l 1 for _ in range(len(l)+1) [0]*(N+1) [0] 0 N+1 N 1 prev_w=[[0]*(N+1) for _ in range(len(l)+1)] prev_w [[0]*(N+1) for _ in range(len(l)+1)] [0]*(N+1) for _ in range(len(l)+1) for _ in range(len(l)+1) _ range(len(l)+1) range len(l)+1 len(l) len l 1 for _ in range(len(l)+1) [0]*(N+1) [0] 0 N+1 N 1 real_w=[[0]*(N+1) for _ in range(len(l)+1)] real_w [[0]*(N+1) for _ in range(len(l)+1)] for num in range(len(l)):
        for w in range(N+1):
            if w>=l[num]:
                if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
            if dp[num+1][w]<dp[num][w]:
                dp[num+1][w]=dp[num][w]
                prev_w[num+1][w]=w
                real_w[num+1][w]=w
     num range(len(l)) range len(l) len l for w in range(N+1):
            if w>=l[num]:
                if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
            if dp[num+1][w]<dp[num][w]:
                dp[num+1][w]=dp[num][w]
                prev_w[num+1][w]=w
                real_w[num+1][w]=w
     w range(N+1) range N+1 N 1 if w>=l[num]:
                if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
             w>=l[num] w l[num] l num if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
             dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w dp[num+1][w]<dp[num+1][w-l[num]]+1 dp[num+1][w] [num+1] dp num+1 num 1 w dp[num+1][w-l[num]]+1 dp[num+1][w-l[num]] [num+1] dp num+1 num 1 w-l[num] w l[num] l num 1 real_w[num+1][w-l[num]]+l[num]==w real_w[num+1][w-l[num]]+l[num] real_w[num+1][w-l[num]] [num+1] real_w num+1 num 1 w-l[num] w l[num] l num l[num] l num w dp[num+1][w]=dp[num+1][w-l[num]]+1 dp[num+1][w] [num+1] dp num+1 num 1 w dp[num+1][w-l[num]]+1 dp[num+1][w-l[num]] [num+1] dp num+1 num 1 w-l[num] w l[num] l num 1 prev_w[num+1][w]=w-l[num] prev_w[num+1][w] [num+1] prev_w num+1 num 1 w w-l[num] w l[num] l num real_w[num+1][w]=w real_w[num+1][w] [num+1] real_w num+1 num 1 w w if dp[num+1][w]<dp[num][w]:
                dp[num+1][w]=dp[num][w]
                prev_w[num+1][w]=w
                real_w[num+1][w]=w
     dp[num+1][w]<dp[num][w] dp[num+1][w] [num+1] dp num+1 num 1 w dp[num][w] [num] dp num w dp[num+1][w]=dp[num][w] dp[num+1][w] [num+1] dp num+1 num 1 w dp[num][w] [num] dp num w prev_w[num+1][w]=w prev_w[num+1][w] [num+1] prev_w num+1 num 1 w w real_w[num+1][w]=w real_w[num+1][w] [num+1] real_w num+1 num 1 w w l2=[] l2 [] cur_w=N cur_w N for num in range(len(l)-1,-1,-1):
        while True:
            if prev_w[num+1][cur_w]==cur_w-l[num]:
                l2.append(str(A[num]))
                cur_w=prev_w[num+1][cur_w]
            else:
                cur_w=prev_w[num+1][cur_w]
                break
     num range(len(l)-1,-1,-1) range len(l)-1 len(l) len l 1 -1 -1 while True:
            if prev_w[num+1][cur_w]==cur_w-l[num]:
                l2.append(str(A[num]))
                cur_w=prev_w[num+1][cur_w]
            else:
                cur_w=prev_w[num+1][cur_w]
                break
     True if prev_w[num+1][cur_w]==cur_w-l[num]:
                l2.append(str(A[num]))
                cur_w=prev_w[num+1][cur_w]
            else:
                cur_w=prev_w[num+1][cur_w]
                break
     prev_w[num+1][cur_w]==cur_w-l[num] prev_w[num+1][cur_w] [num+1] prev_w num+1 num 1 cur_w cur_w-l[num] cur_w l[num] l num l2.append(str(A[num])) l2.append l2 append str(A[num]) str A[num] A num cur_w=prev_w[num+1][cur_w] cur_w prev_w[num+1][cur_w] [num+1] prev_w num+1 num 1 cur_w cur_w=prev_w[num+1][cur_w] cur_w prev_w[num+1][cur_w] [num+1] prev_w num+1 num 1 cur_w break print(''.join(l2)) print ''.join(l2) ''.join '' join l2 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main INF=10**18 10**18 INF MOD=10**9+7 10**9+7 MOD def input():
    return sys.stdin.readline().rstrip() def input():
    return sys.stdin.readline().rstrip() input def main():
    N,M=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    for a in A:
        if a==3:
            try:
                A.remove(2)
            except:
                pass
        elif a==5:
            try:
                A.remove(2)
            except:
                pass
            try:
                A.remove(3)
            except:
                pass
        elif a==9:
            try:
                A.remove(6)
            except:
                pass
    d={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
    l=[d[x] for x in A]
    dp=[[0]*(N+1) for _ in range(len(l)+1)]
    prev_w=[[0]*(N+1) for _ in range(len(l)+1)]
    real_w=[[0]*(N+1) for _ in range(len(l)+1)]
    for num in range(len(l)):
        for w in range(N+1):
            if w>=l[num]:
                if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
            if dp[num+1][w]<dp[num][w]:
                dp[num+1][w]=dp[num][w]
                prev_w[num+1][w]=w
                real_w[num+1][w]=w
    l2=[]
    cur_w=N
    for num in range(len(l)-1,-1,-1):
        while True:
            if prev_w[num+1][cur_w]==cur_w-l[num]:
                l2.append(str(A[num]))
                cur_w=prev_w[num+1][cur_w]
            else:
                cur_w=prev_w[num+1][cur_w]
                break
    print(''.join(l2)) def main():
    N,M=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    for a in A:
        if a==3:
            try:
                A.remove(2)
            except:
                pass
        elif a==5:
            try:
                A.remove(2)
            except:
                pass
            try:
                A.remove(3)
            except:
                pass
        elif a==9:
            try:
                A.remove(6)
            except:
                pass
    d={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
    l=[d[x] for x in A]
    dp=[[0]*(N+1) for _ in range(len(l)+1)]
    prev_w=[[0]*(N+1) for _ in range(len(l)+1)]
    real_w=[[0]*(N+1) for _ in range(len(l)+1)]
    for num in range(len(l)):
        for w in range(N+1):
            if w>=l[num]:
                if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
            if dp[num+1][w]<dp[num][w]:
                dp[num+1][w]=dp[num][w]
                prev_w[num+1][w]=w
                real_w[num+1][w]=w
    l2=[]
    cur_w=N
    for num in range(len(l)-1,-1,-1):
        while True:
            if prev_w[num+1][cur_w]==cur_w-l[num]:
                l2.append(str(A[num]))
                cur_w=prev_w[num+1][cur_w]
            else:
                cur_w=prev_w[num+1][cur_w]
                break
    print(''.join(l2)) main
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,M=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    for a in A:
        if a==3:
            try:
                A.remove(2)
            except:
                pass
        elif a==5:
            try:
                A.remove(2)
            except:
                pass
            try:
                A.remove(3)
            except:
                pass
        elif a==9:
            try:
                A.remove(6)
            except:
                pass
    d={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
    l=[d[x] for x in A]
    dp=[[0]*(N+1) for _ in range(len(l)+1)]
    prev_w=[[0]*(N+1) for _ in range(len(l)+1)]
    real_w=[[0]*(N+1) for _ in range(len(l)+1)]
    for num in range(len(l)):
        for w in range(N+1):
            if w>=l[num]:
                if dp[num+1][w]<dp[num+1][w-l[num]]+1 and real_w[num+1][w-l[num]]+l[num]==w:
                    dp[num+1][w]=dp[num+1][w-l[num]]+1
                    prev_w[num+1][w]=w-l[num]
                    real_w[num+1][w]=w
            if dp[num+1][w]<dp[num][w]:
                dp[num+1][w]=dp[num][w]
                prev_w[num+1][w]=w
                real_w[num+1][w]=w
    l2=[]
    cur_w=N
    for num in range(len(l)-1,-1,-1):
        while True:
            if prev_w[num+1][cur_w]==cur_w-l[num]:
                l2.append(str(A[num]))
                cur_w=prev_w[num+1][cur_w]
            else:
                cur_w=prev_w[num+1][cur_w]
                break
    print(''.join(l2))

if __name__ == '__main__':
    main()
