N,M = map(int,input().split())
A = list(map(int,input().split()))
C = [0,1,4,4,3,4,5,2,6,5]
dp = N*[0]+9*[-1]

for n in range(N):
  dp[n+1] = max(a+10*dp[n-C[a]] for a in A)

print(dp[N]) 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 C = [0,1,4,4,3,4,5,2,6,5] C [0,1,4,4,3,4,5,2,6,5] 0 1 4 4 3 4 5 2 6 5 dp = N*[0]+9*[-1] dp N*[0]+9*[-1] N*[0] N [0] 0 9*[-1] 9 [-1] -1 for n in range(N):
  dp[n+1] = max(a+10*dp[n-C[a]] for a in A) n range(N) range N dp[n+1] = max(a+10*dp[n-C[a]] for a in A) dp[n+1] dp n+1 n 1 max(a+10*dp[n-C[a]] for a in A) max a+10*dp[n-C[a]] a 10*dp[n-C[a]] 10 dp[n-C[a]] dp n-C[a] n C[a] C a print(dp[N]) print dp[N] dp N A = list(map(int,input().split())) list(map(int,input().split())) A M = map(int,input().split()) map(int,input().split()) M N,M = map(int,input().split()) map(int,input().split()) N dp = N*[0]+9*[-1] N*[0]+9*[-1] dp C = [0,1,4,4,3,4,5,2,6,5] [0,1,4,4,3,4,5,2,6,5] C
N,M = map(int,input().split())
A = list(map(int,input().split()))
C = [0,1,4,4,3,4,5,2,6,5]
dp = N*[0]+9*[-1]

for n in range(N):
  dp[n+1] = max(a+10*dp[n-C[a]] for a in A)

print(dp[N])