
"""
個数無制限ナップザック
大小判定は和が大きいほう→同じなら桁で比較していって先に大きくなった方
"""

N,M = map(int,input().split())

A = list(map(int,input().split()))

lis = [[0] * 10 for i in range(N+1)]
able = [False] * (N+1)
able[0] = True

nums = [0,2,5,5,4,5,6,3,7,6]

for i in A:

    for j in range(N):

        if able[j] and j+nums[i] <= N:

            if not able[j+nums[i]]:
                able[j+nums[i]] = True
                
                for k in range(10):
                    lis[j+nums[i]][k] = lis[j][k]

                lis[j+nums[i]][i] += 1

            else:

                flag = False

                if sum(lis[j+nums[i]]) < sum(lis[j]) + 1:
                    flag = True

                elif sum(lis[j+nums[i]]) == sum(lis[j]) + 1:

                    cp = lis[j].copy()
                    cp[i] += 1

                    for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                if flag:
                    for k in range(10):
                        lis[j+nums[i]][k] = lis[j][k]
                    lis[j+nums[i]][i] += 1


ans = []

for i in range(9,0,-1):

    for j in range(lis[-1][i]):
        ans.append(i)

print ("".join(map(str,ans))) """
個数無制限ナップザック
大小判定は和が大きいほう→同じなら桁で比較していって先に大きくなった方
""" 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 [0] * 10 for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) [0] * 10 [0] 0 10 lis = [[0] * 10 for i in range(N+1)] lis [[0] * 10 for i in range(N+1)] able = [False] * (N+1) able [False] * (N+1) [False] False N+1 N 1 able[0] = True able[0] able 0 True nums = [0,2,5,5,4,5,6,3,7,6] nums [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 for i in A:

    for j in range(N):

        if able[j] and j+nums[i] <= N:

            if not able[j+nums[i]]:
                able[j+nums[i]] = True
                
                for k in range(10):
                    lis[j+nums[i]][k] = lis[j][k]

                lis[j+nums[i]][i] += 1

            else:

                flag = False

                if sum(lis[j+nums[i]]) < sum(lis[j]) + 1:
                    flag = True

                elif sum(lis[j+nums[i]]) == sum(lis[j]) + 1:

                    cp = lis[j].copy()
                    cp[i] += 1

                    for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                if flag:
                    for k in range(10):
                        lis[j+nums[i]][k] = lis[j][k]
                    lis[j+nums[i]][i] += 1 i A for j in range(N):

        if able[j] and j+nums[i] <= N:

            if not able[j+nums[i]]:
                able[j+nums[i]] = True
                
                for k in range(10):
                    lis[j+nums[i]][k] = lis[j][k]

                lis[j+nums[i]][i] += 1

            else:

                flag = False

                if sum(lis[j+nums[i]]) < sum(lis[j]) + 1:
                    flag = True

                elif sum(lis[j+nums[i]]) == sum(lis[j]) + 1:

                    cp = lis[j].copy()
                    cp[i] += 1

                    for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                if flag:
                    for k in range(10):
                        lis[j+nums[i]][k] = lis[j][k]
                    lis[j+nums[i]][i] += 1 j range(N) range N if able[j] and j+nums[i] <= N:

            if not able[j+nums[i]]:
                able[j+nums[i]] = True
                
                for k in range(10):
                    lis[j+nums[i]][k] = lis[j][k]

                lis[j+nums[i]][i] += 1

            else:

                flag = False

                if sum(lis[j+nums[i]]) < sum(lis[j]) + 1:
                    flag = True

                elif sum(lis[j+nums[i]]) == sum(lis[j]) + 1:

                    cp = lis[j].copy()
                    cp[i] += 1

                    for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                if flag:
                    for k in range(10):
                        lis[j+nums[i]][k] = lis[j][k]
                    lis[j+nums[i]][i] += 1 able[j] and j+nums[i] <= N able[j] able j j+nums[i] <= N j+nums[i] j nums[i] nums i N if not able[j+nums[i]]:
                able[j+nums[i]] = True
                
                for k in range(10):
                    lis[j+nums[i]][k] = lis[j][k]

                lis[j+nums[i]][i] += 1

            else:

                flag = False

                if sum(lis[j+nums[i]]) < sum(lis[j]) + 1:
                    flag = True

                elif sum(lis[j+nums[i]]) == sum(lis[j]) + 1:

                    cp = lis[j].copy()
                    cp[i] += 1

                    for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                if flag:
                    for k in range(10):
                        lis[j+nums[i]][k] = lis[j][k]
                    lis[j+nums[i]][i] += 1 not able[j+nums[i]] able[j+nums[i]] able j+nums[i] j nums[i] nums i able[j+nums[i]] = True able[j+nums[i]] able j+nums[i] j nums[i] nums i True for k in range(10):
                    lis[j+nums[i]][k] = lis[j][k]

                 k range(10) range 10 lis[j+nums[i]][k] = lis[j][k] lis[j+nums[i]][k] [j+nums[i]] lis j+nums[i] j nums[i] nums i k lis[j][k] [j] lis j k lis[j+nums[i]][i] += 1 lis[j+nums[i]][i] [j+nums[i]] lis j+nums[i] j nums[i] nums i i 1 flag = False flag False if sum(lis[j+nums[i]]) < sum(lis[j]) + 1:
                    flag = True

                elif sum(lis[j+nums[i]]) == sum(lis[j]) + 1:

                    cp = lis[j].copy()
                    cp[i] += 1

                    for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                 sum(lis[j+nums[i]]) < sum(lis[j]) + 1 sum(lis[j+nums[i]]) sum lis[j+nums[i]] lis j+nums[i] j nums[i] nums i sum(lis[j]) + 1 sum(lis[j]) sum lis[j] lis j 1 flag = True flag True elif sum(lis[j+nums[i]]) == sum(lis[j]) + 1:

                    cp = lis[j].copy()
                    cp[i] += 1

                    for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                 sum(lis[j+nums[i]]) == sum(lis[j]) + 1 sum(lis[j+nums[i]]) sum lis[j+nums[i]] lis j+nums[i] j nums[i] nums i sum(lis[j]) + 1 sum(lis[j]) sum lis[j] lis j 1 cp = lis[j].copy() cp lis[j].copy() [j].copy [j] lis j copy cp[i] += 1 cp[i] cp i 1 for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                 k range(9,0,-1) range 9 0 -1 if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                 lis[j+nums[i]][k] < cp[k] lis[j+nums[i]][k] [j+nums[i]] lis j+nums[i] j nums[i] nums i k cp[k] cp k flag = True flag True break elif lis[j+nums[i]][k] > cp[k]:
                            break


                 lis[j+nums[i]][k] > cp[k] lis[j+nums[i]][k] [j+nums[i]] lis j+nums[i] j nums[i] nums i k cp[k] cp k break if flag:
                    for k in range(10):
                        lis[j+nums[i]][k] = lis[j][k]
                    lis[j+nums[i]][i] += 1 flag for k in range(10):
                        lis[j+nums[i]][k] = lis[j][k]
                     k range(10) range 10 lis[j+nums[i]][k] = lis[j][k] lis[j+nums[i]][k] [j+nums[i]] lis j+nums[i] j nums[i] nums i k lis[j][k] [j] lis j k lis[j+nums[i]][i] += 1 lis[j+nums[i]][i] [j+nums[i]] lis j+nums[i] j nums[i] nums i i 1 ans = [] ans [] for i in range(9,0,-1):

    for j in range(lis[-1][i]):
        ans.append(i) i range(9,0,-1) range 9 0 -1 for j in range(lis[-1][i]):
        ans.append(i) j range(lis[-1][i]) range lis[-1][i] [-1] lis -1 i ans.append(i) ans.append ans append i print ("".join(map(str,ans))) print "".join(map(str,ans)) "".join "" join map(str,ans) map str ans ans = [] [] ans A = list(map(int,input().split())) list(map(int,input().split())) A flag = False False flag cp = lis[j].copy() lis[j].copy() cp N,M = map(int,input().split()) map(int,input().split()) N flag = True True flag M = map(int,input().split()) map(int,input().split()) M nums = [0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] nums flag = True True flag lis = [[0] * 10 for i in range(N+1)] [[0] * 10 for i in range(N+1)] lis able = [False] * (N+1) [False] * (N+1) able

"""
個数無制限ナップザック
大小判定は和が大きいほう→同じなら桁で比較していって先に大きくなった方
"""

N,M = map(int,input().split())

A = list(map(int,input().split()))

lis = [[0] * 10 for i in range(N+1)]
able = [False] * (N+1)
able[0] = True

nums = [0,2,5,5,4,5,6,3,7,6]

for i in A:

    for j in range(N):

        if able[j] and j+nums[i] <= N:

            if not able[j+nums[i]]:
                able[j+nums[i]] = True
                
                for k in range(10):
                    lis[j+nums[i]][k] = lis[j][k]

                lis[j+nums[i]][i] += 1

            else:

                flag = False

                if sum(lis[j+nums[i]]) < sum(lis[j]) + 1:
                    flag = True

                elif sum(lis[j+nums[i]]) == sum(lis[j]) + 1:

                    cp = lis[j].copy()
                    cp[i] += 1

                    for k in range(9,0,-1):

                        if lis[j+nums[i]][k] < cp[k]:
                            flag = True
                            break

                        elif lis[j+nums[i]][k] > cp[k]:
                            break


                if flag:
                    for k in range(10):
                        lis[j+nums[i]][k] = lis[j][k]
                    lis[j+nums[i]][i] += 1


ans = []

for i in range(9,0,-1):

    for j in range(lis[-1][i]):
        ans.append(i)

print ("".join(map(str,ans)))
