import sys
input = sys.stdin.readline

#import fractions #fractions.gcd() python~3.4
sys.setrecursionlimit(1000000)

N, M = map(int,input().split())
A = list(map(int, input().split()))
INF = 1e+10
def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7 
    if x == 9:
        return 6

A.sort(reverse=True)

dp=[-INF]*(N+1)
dp[0] = 0
for i in range(1,N+1):
    for j in range(M):
        if i-f(A[j]) >= 0:
            dp[i] = max(dp[i],dp[i-f(A[j])]+1)
 
ans = ""
remain = dp[N]
match = N

while match > 0:
    for i in range(M):
        if match - f(A[i]) >=0 and dp[match - f(A[i])] == remain - 1:
            ans += str(A[i])
            match -= f(A[i])
            remain -= 1
            
            break
            
print(ans)
 import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline sys.setrecursionlimit(1000000) sys.setrecursionlimit sys setrecursionlimit 1000000 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 INF = 1e+10 INF 1e+10 def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7 
    if x == 9:
        return 6 f if x == 1:
        return 2
     x == 1 x 1 return 2 2 if x == 2:
        return 5
     x == 2 x 2 return 5 5 if x == 3:
        return 5
     x == 3 x 3 return 5 5 if x == 4:
        return 4
     x == 4 x 4 return 4 4 if x == 5:
        return 5
     x == 5 x 5 return 5 5 if x == 6:
        return 6
     x == 6 x 6 return 6 6 if x == 7:
        return 3
     x == 7 x 7 return 3 3 if x == 8:
        return 7 
     x == 8 x 8 return 7 7 if x == 9:
        return 6 x == 9 x 9 return 6 6 x x A.sort(reverse=True) A.sort A sort reverse=True reverse True dp=[-INF]*(N+1) dp [-INF]*(N+1) [-INF] -INF INF N+1 N 1 dp[0] = 0 dp[0] dp 0 0 for i in range(1,N+1):
    for j in range(M):
        if i-f(A[j]) >= 0:
            dp[i] = max(dp[i],dp[i-f(A[j])]+1)
  i range(1,N+1) range 1 N+1 N 1 for j in range(M):
        if i-f(A[j]) >= 0:
            dp[i] = max(dp[i],dp[i-f(A[j])]+1)
  j range(M) range M if i-f(A[j]) >= 0:
            dp[i] = max(dp[i],dp[i-f(A[j])]+1)
  i-f(A[j]) >= 0 i-f(A[j]) i f(A[j]) f A[j] A j 0 dp[i] = max(dp[i],dp[i-f(A[j])]+1) dp[i] dp i max(dp[i],dp[i-f(A[j])]+1) max dp[i] dp i dp[i-f(A[j])]+1 dp[i-f(A[j])] dp i-f(A[j]) i f(A[j]) f A[j] A j 1 ans = "" ans "" remain = dp[N] remain dp[N] dp N match = N match N while match > 0:
    for i in range(M):
        if match - f(A[i]) >=0 and dp[match - f(A[i])] == remain - 1:
            ans += str(A[i])
            match -= f(A[i])
            remain -= 1
            
            break
             match > 0 match 0 for i in range(M):
        if match - f(A[i]) >=0 and dp[match - f(A[i])] == remain - 1:
            ans += str(A[i])
            match -= f(A[i])
            remain -= 1
            
            break
             i range(M) range M if match - f(A[i]) >=0 and dp[match - f(A[i])] == remain - 1:
            ans += str(A[i])
            match -= f(A[i])
            remain -= 1
            
            break
             match - f(A[i]) >=0 and dp[match - f(A[i])] == remain - 1 match - f(A[i]) >=0 match - f(A[i]) match f(A[i]) f A[i] A i 0 dp[match - f(A[i])] == remain - 1 dp[match - f(A[i])] dp match - f(A[i]) match f(A[i]) f A[i] A i remain - 1 remain 1 ans += str(A[i]) ans str(A[i]) str A[i] A i match -= f(A[i]) match f(A[i]) f A[i] A i remain -= 1 remain 1 break print(ans) print ans def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7 
    if x == 9:
        return 6 def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7 
    if x == 9:
        return 6 f match -= f(A[i]) f(A[i]) match ans += str(A[i]) str(A[i]) ans input = sys.stdin.readline sys.stdin.readline input A = list(map(int, input().split())) list(map(int, input().split())) A dp=[-INF]*(N+1) [-INF]*(N+1) dp ans = "" "" ans N, M = map(int,input().split()) map(int,input().split()) N INF = 1e+10 1e+10 INF remain -= 1 1 remain remain = dp[N] dp[N] remain match = N N match M = map(int,input().split()) map(int,input().split()) M
import sys
input = sys.stdin.readline

#import fractions #fractions.gcd() python~3.4
sys.setrecursionlimit(1000000)

N, M = map(int,input().split())
A = list(map(int, input().split()))
INF = 1e+10
def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7 
    if x == 9:
        return 6

A.sort(reverse=True)

dp=[-INF]*(N+1)
dp[0] = 0
for i in range(1,N+1):
    for j in range(M):
        if i-f(A[j]) >= 0:
            dp[i] = max(dp[i],dp[i-f(A[j])]+1)
 
ans = ""
remain = dp[N]
match = N

while match > 0:
    for i in range(M):
        if match - f(A[i]) >=0 and dp[match - f(A[i])] == remain - 1:
            ans += str(A[i])
            match -= f(A[i])
            remain -= 1
            
            break
            
print(ans)

