n, m = [ int(v) for v in input().split() ]
num_list = [ int(v) for v in input().split() ]
match_table = [2,5,5,4,5,6,3,7,6]
match_table_2 = [ [] for i in range(8) ]
for i in num_list:
    match_table_2[match_table[i-1]].append(i)

match_table_2 = [ (max(match_table_2[i]), i) for i in range(8) if match_table_2[i] != [] ]
match_list, figure_list = zip(*match_table_2)
k = len(match_list)

dp_list = [ 0 for i in range(n+1) ]

for i in range(n+1):
    index_list = [ i - j for j in figure_list ]
    candidate_list = []
    for j in range(k):
        if index_list[j] > 0 and dp_list[index_list[j]] > 0:
            candidate_list.append(dp_list[index_list[j]] * 10 + match_list[j])
        elif index_list[j] == 0:
            candidate_list.append(match_list[j])
    if candidate_list != []:
        dp_list[i] = max(candidate_list)

print(dp_list[-1]) int(v) for v in input().split() for v in input().split() v input().split() ().split () input split for v in input().split() int(v) int v n, m = [ int(v) for v in input().split() ] n [ int(v) for v in input().split() ] m int(v) for v in input().split() for v in input().split() v input().split() ().split () input split for v in input().split() int(v) int v num_list = [ int(v) for v in input().split() ] num_list [ int(v) for v in input().split() ] match_table = [2,5,5,4,5,6,3,7,6] match_table [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 [] for i in range(8) for i in range(8) i range(8) range 8 for i in range(8) [] match_table_2 = [ [] for i in range(8) ] match_table_2 [ [] for i in range(8) ] for i in num_list:
    match_table_2[match_table[i-1]].append(i) i num_list match_table_2[match_table[i-1]].append(i) [match_table[i-1]].append [match_table[i-1]] match_table_2 match_table[i-1] match_table i-1 i 1 append i (max(match_table_2[i]), i) for i in range(8) if match_table_2[i] != [] for i in range(8) if match_table_2[i] != [] i range(8) range 8 match_table_2[i] != [] match_table_2[i] match_table_2 i [] if match_table_2[i] != [] (max(match_table_2[i]), i) max(match_table_2[i]) max match_table_2[i] match_table_2 i i match_table_2 = [ (max(match_table_2[i]), i) for i in range(8) if match_table_2[i] != [] ] match_table_2 [ (max(match_table_2[i]), i) for i in range(8) if match_table_2[i] != [] ] match_list, figure_list = zip(*match_table_2) match_list zip(*match_table_2) zip *match_table_2 match_table_2 figure_list k = len(match_list) k len(match_list) len match_list 0 for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) 0 dp_list = [ 0 for i in range(n+1) ] dp_list [ 0 for i in range(n+1) ] for i in range(n+1):
    index_list = [ i - j for j in figure_list ]
    candidate_list = []
    for j in range(k):
        if index_list[j] > 0 and dp_list[index_list[j]] > 0:
            candidate_list.append(dp_list[index_list[j]] * 10 + match_list[j])
        elif index_list[j] == 0:
            candidate_list.append(match_list[j])
    if candidate_list != []:
        dp_list[i] = max(candidate_list) i range(n+1) range n+1 n 1 i - j for j in figure_list for j in figure_list j figure_list for j in figure_list i - j i j index_list = [ i - j for j in figure_list ] index_list [ i - j for j in figure_list ] candidate_list = [] candidate_list [] for j in range(k):
        if index_list[j] > 0 and dp_list[index_list[j]] > 0:
            candidate_list.append(dp_list[index_list[j]] * 10 + match_list[j])
        elif index_list[j] == 0:
            candidate_list.append(match_list[j])
     j range(k) range k if index_list[j] > 0 and dp_list[index_list[j]] > 0:
            candidate_list.append(dp_list[index_list[j]] * 10 + match_list[j])
        elif index_list[j] == 0:
            candidate_list.append(match_list[j])
     index_list[j] > 0 and dp_list[index_list[j]] > 0 index_list[j] > 0 index_list[j] index_list j 0 dp_list[index_list[j]] > 0 dp_list[index_list[j]] dp_list index_list[j] index_list j 0 candidate_list.append(dp_list[index_list[j]] * 10 + match_list[j]) candidate_list.append candidate_list append dp_list[index_list[j]] * 10 + match_list[j] dp_list[index_list[j]] * 10 dp_list[index_list[j]] dp_list index_list[j] index_list j 10 match_list[j] match_list j elif index_list[j] == 0:
            candidate_list.append(match_list[j])
     index_list[j] == 0 index_list[j] index_list j 0 candidate_list.append(match_list[j]) candidate_list.append candidate_list append match_list[j] match_list j if candidate_list != []:
        dp_list[i] = max(candidate_list) candidate_list != [] candidate_list [] dp_list[i] = max(candidate_list) dp_list[i] dp_list i max(candidate_list) max candidate_list print(dp_list[-1]) print dp_list[-1] dp_list -1 n, m = [ int(v) for v in input().split() ] [ int(v) for v in input().split() ] n match_table = [2,5,5,4,5,6,3,7,6] [2,5,5,4,5,6,3,7,6] match_table candidate_list = [] [] candidate_list match_table_2 = [ [] for i in range(8) ] [ [] for i in range(8) ] match_table_2 match_table_2 = [ (max(match_table_2[i]), i) for i in range(8) if match_table_2[i] != [] ] [ (max(match_table_2[i]), i) for i in range(8) if match_table_2[i] != [] ] match_table_2 dp_list = [ 0 for i in range(n+1) ] [ 0 for i in range(n+1) ] dp_list figure_list = zip(*match_table_2) zip(*match_table_2) figure_list num_list = [ int(v) for v in input().split() ] [ int(v) for v in input().split() ] num_list k = len(match_list) len(match_list) k m = [ int(v) for v in input().split() ] [ int(v) for v in input().split() ] m index_list = [ i - j for j in figure_list ] [ i - j for j in figure_list ] index_list match_list, figure_list = zip(*match_table_2) zip(*match_table_2) match_list
n, m = [ int(v) for v in input().split() ]
num_list = [ int(v) for v in input().split() ]
match_table = [2,5,5,4,5,6,3,7,6]
match_table_2 = [ [] for i in range(8) ]
for i in num_list:
    match_table_2[match_table[i-1]].append(i)

match_table_2 = [ (max(match_table_2[i]), i) for i in range(8) if match_table_2[i] != [] ]
match_list, figure_list = zip(*match_table_2)
k = len(match_list)

dp_list = [ 0 for i in range(n+1) ]

for i in range(n+1):
    index_list = [ i - j for j in figure_list ]
    candidate_list = []
    for j in range(k):
        if index_list[j] > 0 and dp_list[index_list[j]] > 0:
            candidate_list.append(dp_list[index_list[j]] * 10 + match_list[j])
        elif index_list[j] == 0:
            candidate_list.append(match_list[j])
    if candidate_list != []:
        dp_list[i] = max(candidate_list)

print(dp_list[-1])