import sys

sys.setrecursionlimit(1000000)



ma_map = [ -1, 2, 5, 5, 4, 5, 6, 3, 7, 6 ]

N, M = (int(i) for i in input().split())

ma = list(int(i) for i in input().split())

memo = [-1] * 100000



def dfs(num):

	if memo[num] != -1: return memo[num]

	if num == 0: return 0



	ret = -1

	for p in ma:

		if 0 <= num - ma_map[p]:

			d = dfs(num - ma_map[p])

			if d == -1: continue

			ret = max(dfs(num - ma_map[p]) * 10 + p, ret)



	memo[num] = ret

	return ret



ma.sort()

print(("{}".format(dfs(N))))
import sys

sys.setrecursionlimit(1000000)



ma_map = [ -1, 2, 5, 5, 4, 5, 6, 3, 7, 6 ]

N, M = (int(i) for i in input().split())

ma = list(int(i) for i in input().split())

memo = [-1] * 100000



def dfs(num):

	if memo[num] != -1: return memo[num]

	if num == 0: return 0



	ret = -1

	for p in ma:

		if 0 <= num - ma_map[p]:

			ret = max(dfs(num - ma_map[p]) * 10 + p, ret)



	memo[num] = ret

	return ret



ma.sort()

print((dfs(N)))
import sys

sys.setrecursionlimit(1000000)
ma_map = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = (int(i) for i in input().split())
ma = list(int(i) for i in input().split())
memo = [-1] * 100000


def dfs(num):
    if memo[num] != -1:
        return memo[num]
    if num == 0:
        return 0
    ret = -1
    for p in ma:
        if 0 <= num - ma_map[p]:
            d = dfs(num - ma_map[p])
            if d == -1:
                continue
            ret = max(dfs(num - ma_map[p]) * 10 + p, ret)
    memo[num] = ret
    return ret


ma.sort()
print(("{}".format(dfs(N))))

import sys

sys.setrecursionlimit(1000000)
ma_map = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = (int(i) for i in input().split())
ma = list(int(i) for i in input().split())
memo = [-1] * 100000


def dfs(num):
    if memo[num] != -1:
        return memo[num]
    if num == 0:
        return 0
    ret = -1
    for p in ma:
        if 0 <= num - ma_map[p]:
            ret = max(dfs(num - ma_map[p]) * 10 + p, ret)
    memo[num] = ret
    return ret


ma.sort()
print((dfs(N)))

-            d = dfs(num - ma_map[p]) -            if d == -1: -                continue -print(("{}".format(dfs(N)))) +print((dfs(N)))