import sys
input = lambda: sys.stdin.readline().rstrip()

match_dict = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

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

def greater(a, b):
    if len(a) == len(b):
        return max(a, b)
    elif len(a) > len(b):
        return a
    else:
        return b        

dp = ["0"] * 11000

dp[0] = "" 

for i in range(N+1):
    
    for a in A:
        if i - match_dict[a] >= 0 and dp[i-match_dict[a]] != '0':
            dp[i] = greater(dp[i - match_dict[a]] + str(a), dp[i])

print(dp[N]) import sys sys input = lambda: sys.stdin.readline().rstrip() input lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip match_dict = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} match_dict {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 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 def greater(a, b):
    if len(a) == len(b):
        return max(a, b)
    elif len(a) > len(b):
        return a
    else:
        return b         greater if len(a) == len(b):
        return max(a, b)
    elif len(a) > len(b):
        return a
    else:
        return b         len(a) == len(b) len(a) len a len(b) len b return max(a, b) max(a, b) max a b elif len(a) > len(b):
        return a
     len(a) > len(b) len(a) len a len(b) len b return a a return b b a a b b dp = ["0"] * 11000 dp ["0"] * 11000 ["0"] "0" 11000 dp[0] = "" dp[0] dp 0 "" for i in range(N+1):
    
    for a in A:
        if i - match_dict[a] >= 0 and dp[i-match_dict[a]] != '0':
            dp[i] = greater(dp[i - match_dict[a]] + str(a), dp[i]) i range(N+1) range N+1 N 1 for a in A:
        if i - match_dict[a] >= 0 and dp[i-match_dict[a]] != '0':
            dp[i] = greater(dp[i - match_dict[a]] + str(a), dp[i]) a A if i - match_dict[a] >= 0 and dp[i-match_dict[a]] != '0':
            dp[i] = greater(dp[i - match_dict[a]] + str(a), dp[i]) i - match_dict[a] >= 0 and dp[i-match_dict[a]] != '0' i - match_dict[a] >= 0 i - match_dict[a] i match_dict[a] match_dict a 0 dp[i-match_dict[a]] != '0' dp[i-match_dict[a]] dp i-match_dict[a] i match_dict[a] match_dict a '0' dp[i] = greater(dp[i - match_dict[a]] + str(a), dp[i]) dp[i] dp i greater(dp[i - match_dict[a]] + str(a), dp[i]) greater dp[i - match_dict[a]] + str(a) dp[i - match_dict[a]] dp i - match_dict[a] i match_dict[a] match_dict a str(a) str a dp[i] dp i print(dp[N]) print dp[N] dp N input = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() input M = map(int, input().split()) map(int, input().split()) M dp = ["0"] * 11000 ["0"] * 11000 dp A = list(map(int, input().split())) list(map(int, input().split())) A def greater(a, b):
    if len(a) == len(b):
        return max(a, b)
    elif len(a) > len(b):
        return a
    else:
        return b         def greater(a, b):
    if len(a) == len(b):
        return max(a, b)
    elif len(a) > len(b):
        return a
    else:
        return b         greater match_dict = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} match_dict N,M = map(int, input().split()) map(int, input().split()) N
import sys
input = lambda: sys.stdin.readline().rstrip()

match_dict = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

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

def greater(a, b):
    if len(a) == len(b):
        return max(a, b)
    elif len(a) > len(b):
        return a
    else:
        return b        

dp = ["0"] * 11000

dp[0] = "" 

for i in range(N+1):
    
    for a in A:
        if i - match_dict[a] >= 0 and dp[i-match_dict[a]] != '0':
            dp[i] = greater(dp[i - match_dict[a]] + str(a), dp[i])

print(dp[N])