from collections import defaultdict
n,m=map(int,input().split())
A=list(map(int,input().split()))
hon2=[1]
hon5=[2,3,5]
hon4=[4]
hon3=[7]
hon6=[6,9]
hon7=[8]
usenum=[]
usematch=defaultdict(int)
for a in A:
    if a in hon2:
        usenum.append(2)
        usematch[2]=max(a, usematch[2])
    if a in hon5:
        usenum.append(5)
        usematch[5]=max(a, usematch[5])
    if a in hon4:
        usenum.append(4)
        usematch[4]=max(a, usematch[4])
    if a in hon3:
        usenum.append(3)
        usematch[3]=max(a, usematch[3])
    if a in hon6:
        usenum.append(6)
        usematch[6]=max(a, usematch[6])
    if a in hon7:
        usenum.append(7)
        usematch[7]=max(a, usematch[7])
usenum=list(set(usenum))
usenum.sort()
dp=[-1]*(n+11)
dp[0]=0
for i in range(n+1):
    if dp[i]==-1:
        continue
    for num in usenum:
        dp[i+num]=max(10*dp[i]+usematch[num], dp[i+num])
print(dp[n]) from collections import defaultdict 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 hon2=[1] hon2 [1] 1 hon5=[2,3,5] hon5 [2,3,5] 2 3 5 hon4=[4] hon4 [4] 4 hon3=[7] hon3 [7] 7 hon6=[6,9] hon6 [6,9] 6 9 hon7=[8] hon7 [8] 8 usenum=[] usenum [] usematch=defaultdict(int) usematch defaultdict(int) defaultdict int for a in A:
    if a in hon2:
        usenum.append(2)
        usematch[2]=max(a, usematch[2])
    if a in hon5:
        usenum.append(5)
        usematch[5]=max(a, usematch[5])
    if a in hon4:
        usenum.append(4)
        usematch[4]=max(a, usematch[4])
    if a in hon3:
        usenum.append(3)
        usematch[3]=max(a, usematch[3])
    if a in hon6:
        usenum.append(6)
        usematch[6]=max(a, usematch[6])
    if a in hon7:
        usenum.append(7)
        usematch[7]=max(a, usematch[7]) a A if a in hon2:
        usenum.append(2)
        usematch[2]=max(a, usematch[2])
     a in hon2 a hon2 usenum.append(2) usenum.append usenum append 2 usematch[2]=max(a, usematch[2]) usematch[2] usematch 2 max(a, usematch[2]) max a usematch[2] usematch 2 if a in hon5:
        usenum.append(5)
        usematch[5]=max(a, usematch[5])
     a in hon5 a hon5 usenum.append(5) usenum.append usenum append 5 usematch[5]=max(a, usematch[5]) usematch[5] usematch 5 max(a, usematch[5]) max a usematch[5] usematch 5 if a in hon4:
        usenum.append(4)
        usematch[4]=max(a, usematch[4])
     a in hon4 a hon4 usenum.append(4) usenum.append usenum append 4 usematch[4]=max(a, usematch[4]) usematch[4] usematch 4 max(a, usematch[4]) max a usematch[4] usematch 4 if a in hon3:
        usenum.append(3)
        usematch[3]=max(a, usematch[3])
     a in hon3 a hon3 usenum.append(3) usenum.append usenum append 3 usematch[3]=max(a, usematch[3]) usematch[3] usematch 3 max(a, usematch[3]) max a usematch[3] usematch 3 if a in hon6:
        usenum.append(6)
        usematch[6]=max(a, usematch[6])
     a in hon6 a hon6 usenum.append(6) usenum.append usenum append 6 usematch[6]=max(a, usematch[6]) usematch[6] usematch 6 max(a, usematch[6]) max a usematch[6] usematch 6 if a in hon7:
        usenum.append(7)
        usematch[7]=max(a, usematch[7]) a in hon7 a hon7 usenum.append(7) usenum.append usenum append 7 usematch[7]=max(a, usematch[7]) usematch[7] usematch 7 max(a, usematch[7]) max a usematch[7] usematch 7 usenum=list(set(usenum)) usenum list(set(usenum)) list set(usenum) set usenum usenum.sort() usenum.sort usenum sort dp=[-1]*(n+11) dp [-1]*(n+11) [-1] -1 n+11 n 11 dp[0]=0 dp[0] dp 0 0 for i in range(n+1):
    if dp[i]==-1:
        continue
    for num in usenum:
        dp[i+num]=max(10*dp[i]+usematch[num], dp[i+num]) i range(n+1) range n+1 n 1 if dp[i]==-1:
        continue
     dp[i]==-1 dp[i] dp i -1 continue for num in usenum:
        dp[i+num]=max(10*dp[i]+usematch[num], dp[i+num]) num usenum dp[i+num]=max(10*dp[i]+usematch[num], dp[i+num]) dp[i+num] dp i+num i num max(10*dp[i]+usematch[num], dp[i+num]) max 10*dp[i]+usematch[num] 10*dp[i] 10 dp[i] dp i usematch[num] usematch num dp[i+num] dp i+num i num print(dp[n]) print dp[n] dp n usenum=list(set(usenum)) list(set(usenum)) usenum hon4=[4] [4] hon4 usematch=defaultdict(int) defaultdict(int) usematch n,m=map(int,input().split()) map(int,input().split()) n A=list(map(int,input().split())) list(map(int,input().split())) A m=map(int,input().split()) map(int,input().split()) m hon3=[7] [7] hon3 usenum=[] [] usenum hon2=[1] [1] hon2 hon7=[8] [8] hon7 hon6=[6,9] [6,9] hon6 dp=[-1]*(n+11) [-1]*(n+11) dp hon5=[2,3,5] [2,3,5] hon5
from collections import defaultdict
n,m=map(int,input().split())
A=list(map(int,input().split()))
hon2=[1]
hon5=[2,3,5]
hon4=[4]
hon3=[7]
hon6=[6,9]
hon7=[8]
usenum=[]
usematch=defaultdict(int)
for a in A:
    if a in hon2:
        usenum.append(2)
        usematch[2]=max(a, usematch[2])
    if a in hon5:
        usenum.append(5)
        usematch[5]=max(a, usematch[5])
    if a in hon4:
        usenum.append(4)
        usematch[4]=max(a, usematch[4])
    if a in hon3:
        usenum.append(3)
        usematch[3]=max(a, usematch[3])
    if a in hon6:
        usenum.append(6)
        usematch[6]=max(a, usematch[6])
    if a in hon7:
        usenum.append(7)
        usematch[7]=max(a, usematch[7])
usenum=list(set(usenum))
usenum.sort()
dp=[-1]*(n+11)
dp[0]=0
for i in range(n+1):
    if dp[i]==-1:
        continue
    for num in usenum:
        dp[i+num]=max(10*dp[i]+usematch[num], dp[i+num])
print(dp[n])