n,m=map(int,input().split())
a=list(map(int,input().split()))
D=[2,5,5,4,5,6,3,7,6]
dp=[]
dp.append([0,0,0,0,0,0,0,0,0,0])
for i in range(n):
  dp.append([-1,-1,-1,-1,-1,-1,-1,-1,-1,-1])
allowed=[]
for i in a:
  allowed.append([i,D[i-1]])
for i in range(n):
  for j in allowed:
    temp=i+j[1]
    if temp<n+1 and dp[i][0]!=-1:
      if dp[temp][9]<dp[i][9]+1:
        for k in range(10):
          dp[temp][k]=dp[i][k]
        dp[temp][9]+=1
        dp[temp][j[0]-1]+=1
      elif dp[temp][9]==dp[i][9]+1:
        change=0
        for k in range(8,-1,-1):
          if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
          if breakflag==1:
            break
        if change==1:
          for k in range(10):
            dp[temp][k]=dp[i][k]
          dp[temp][9]+=1
          dp[temp][j[0]-1]+=1
ans=[]
for i in range(8,-1,-1):
  for j in range(dp[n][i]):
    ans.append(str(i+1))
print(''.join(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 D=[2,5,5,4,5,6,3,7,6] D [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 dp=[] dp [] dp.append([0,0,0,0,0,0,0,0,0,0]) dp.append dp append [0,0,0,0,0,0,0,0,0,0] 0 0 0 0 0 0 0 0 0 0 for i in range(n):
  dp.append([-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]) i range(n) range n dp.append([-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]) dp.append dp append [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 allowed=[] allowed [] for i in a:
  allowed.append([i,D[i-1]]) i a allowed.append([i,D[i-1]]) allowed.append allowed append [i,D[i-1]] i D[i-1] D i-1 i 1 for i in range(n):
  for j in allowed:
    temp=i+j[1]
    if temp<n+1 and dp[i][0]!=-1:
      if dp[temp][9]<dp[i][9]+1:
        for k in range(10):
          dp[temp][k]=dp[i][k]
        dp[temp][9]+=1
        dp[temp][j[0]-1]+=1
      elif dp[temp][9]==dp[i][9]+1:
        change=0
        for k in range(8,-1,-1):
          if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
          if breakflag==1:
            break
        if change==1:
          for k in range(10):
            dp[temp][k]=dp[i][k]
          dp[temp][9]+=1
          dp[temp][j[0]-1]+=1 i range(n) range n for j in allowed:
    temp=i+j[1]
    if temp<n+1 and dp[i][0]!=-1:
      if dp[temp][9]<dp[i][9]+1:
        for k in range(10):
          dp[temp][k]=dp[i][k]
        dp[temp][9]+=1
        dp[temp][j[0]-1]+=1
      elif dp[temp][9]==dp[i][9]+1:
        change=0
        for k in range(8,-1,-1):
          if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
          if breakflag==1:
            break
        if change==1:
          for k in range(10):
            dp[temp][k]=dp[i][k]
          dp[temp][9]+=1
          dp[temp][j[0]-1]+=1 j allowed temp=i+j[1] temp i+j[1] i j[1] j 1 if temp<n+1 and dp[i][0]!=-1:
      if dp[temp][9]<dp[i][9]+1:
        for k in range(10):
          dp[temp][k]=dp[i][k]
        dp[temp][9]+=1
        dp[temp][j[0]-1]+=1
      elif dp[temp][9]==dp[i][9]+1:
        change=0
        for k in range(8,-1,-1):
          if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
          if breakflag==1:
            break
        if change==1:
          for k in range(10):
            dp[temp][k]=dp[i][k]
          dp[temp][9]+=1
          dp[temp][j[0]-1]+=1 temp<n+1 and dp[i][0]!=-1 temp<n+1 temp n+1 n 1 dp[i][0]!=-1 dp[i][0] [i] dp i 0 -1 if dp[temp][9]<dp[i][9]+1:
        for k in range(10):
          dp[temp][k]=dp[i][k]
        dp[temp][9]+=1
        dp[temp][j[0]-1]+=1
      elif dp[temp][9]==dp[i][9]+1:
        change=0
        for k in range(8,-1,-1):
          if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
          if breakflag==1:
            break
        if change==1:
          for k in range(10):
            dp[temp][k]=dp[i][k]
          dp[temp][9]+=1
          dp[temp][j[0]-1]+=1 dp[temp][9]<dp[i][9]+1 dp[temp][9] [temp] dp temp 9 dp[i][9]+1 dp[i][9] [i] dp i 9 1 for k in range(10):
          dp[temp][k]=dp[i][k]
         k range(10) range 10 dp[temp][k]=dp[i][k] dp[temp][k] [temp] dp temp k dp[i][k] [i] dp i k dp[temp][9]+=1 dp[temp][9] [temp] dp temp 9 1 dp[temp][j[0]-1]+=1 dp[temp][j[0]-1] [temp] dp temp j[0]-1 j[0] j 0 1 1 elif dp[temp][9]==dp[i][9]+1:
        change=0
        for k in range(8,-1,-1):
          if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
          if breakflag==1:
            break
        if change==1:
          for k in range(10):
            dp[temp][k]=dp[i][k]
          dp[temp][9]+=1
          dp[temp][j[0]-1]+=1 dp[temp][9]==dp[i][9]+1 dp[temp][9] [temp] dp temp 9 dp[i][9]+1 dp[i][9] [i] dp i 9 1 change=0 change 0 for k in range(8,-1,-1):
          if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
          if breakflag==1:
            break
         k range(8,-1,-1) range 8 -1 -1 if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
           k!=j[0]-1 k j[0]-1 j[0] j 0 1 breakflag=0 breakflag 0 if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
             dp[temp][k]>dp[i][k] dp[temp][k] [temp] dp temp k dp[i][k] [i] dp i k change=-1 change -1 breakflag=1 breakflag 1 if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
           dp[temp][k]<dp[i][k] dp[temp][k] [temp] dp temp k dp[i][k] [i] dp i k change=1 change 1 breakflag=1 breakflag 1 breakflag=0 breakflag 0 if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
             dp[temp][k]>dp[i][k]+1 dp[temp][k] [temp] dp temp k dp[i][k]+1 dp[i][k] [i] dp i k 1 change=-1 change -1 breakflag=1 breakflag 1 if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
           dp[temp][k]<dp[i][k]+1 dp[temp][k] [temp] dp temp k dp[i][k]+1 dp[i][k] [i] dp i k 1 change=1 change 1 breakflag=1 breakflag 1 if breakflag==1:
            break
         breakflag==1 breakflag 1 break if change==1:
          for k in range(10):
            dp[temp][k]=dp[i][k]
          dp[temp][9]+=1
          dp[temp][j[0]-1]+=1 change==1 change 1 for k in range(10):
            dp[temp][k]=dp[i][k]
           k range(10) range 10 dp[temp][k]=dp[i][k] dp[temp][k] [temp] dp temp k dp[i][k] [i] dp i k dp[temp][9]+=1 dp[temp][9] [temp] dp temp 9 1 dp[temp][j[0]-1]+=1 dp[temp][j[0]-1] [temp] dp temp j[0]-1 j[0] j 0 1 1 ans=[] ans [] for i in range(8,-1,-1):
  for j in range(dp[n][i]):
    ans.append(str(i+1)) i range(8,-1,-1) range 8 -1 -1 for j in range(dp[n][i]):
    ans.append(str(i+1)) j range(dp[n][i]) range dp[n][i] [n] dp n i ans.append(str(i+1)) ans.append ans append str(i+1) str i+1 i 1 print(''.join(ans)) print ''.join(ans) ''.join '' join ans dp=[] [] dp allowed=[] [] allowed breakflag=0 0 breakflag ans=[] [] ans temp=i+j[1] i+j[1] temp breakflag=1 1 breakflag m=map(int,input().split()) map(int,input().split()) m breakflag=0 0 breakflag a=list(map(int,input().split())) list(map(int,input().split())) a n,m=map(int,input().split()) map(int,input().split()) n breakflag=1 1 breakflag breakflag=1 1 breakflag change=-1 -1 change breakflag=1 1 breakflag D=[2,5,5,4,5,6,3,7,6] [2,5,5,4,5,6,3,7,6] D change=0 0 change change=-1 -1 change change=1 1 change change=1 1 change
n,m=map(int,input().split())
a=list(map(int,input().split()))
D=[2,5,5,4,5,6,3,7,6]
dp=[]
dp.append([0,0,0,0,0,0,0,0,0,0])
for i in range(n):
  dp.append([-1,-1,-1,-1,-1,-1,-1,-1,-1,-1])
allowed=[]
for i in a:
  allowed.append([i,D[i-1]])
for i in range(n):
  for j in allowed:
    temp=i+j[1]
    if temp<n+1 and dp[i][0]!=-1:
      if dp[temp][9]<dp[i][9]+1:
        for k in range(10):
          dp[temp][k]=dp[i][k]
        dp[temp][9]+=1
        dp[temp][j[0]-1]+=1
      elif dp[temp][9]==dp[i][9]+1:
        change=0
        for k in range(8,-1,-1):
          if k!=j[0]-1:
            breakflag=0
            if dp[temp][k]>dp[i][k]:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]:
              change=1
              breakflag=1
          else:
            breakflag=0
            if dp[temp][k]>dp[i][k]+1:
              change=-1
              breakflag=1
            if dp[temp][k]<dp[i][k]+1:
              change=1
              breakflag=1
          if breakflag==1:
            break
        if change==1:
          for k in range(10):
            dp[temp][k]=dp[i][k]
          dp[temp][9]+=1
          dp[temp][j[0]-1]+=1
ans=[]
for i in range(8,-1,-1):
  for j in range(dp[n][i]):
    ans.append(str(i+1))
print(''.join(ans))