import sys

def get():
	return [int(x) for x in sys.stdin.readline().rstrip().split(" ")]

N, M = get()
A = get()

tukaukazu = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

memo = [0 for x in range(N + 1)]

def search(n):
	if n < 0:
		return None
	else:
		return memo[n]

for n in range(1, N + 1):
	answer = None
	for d in A:
		senzinnnotie = search(n - tukaukazu[d])
		if senzinnnotie is not None:
			candidate = senzinnnotie * 10 + d
			if (answer is None) or (candidate > answer):
				answer = candidate
	memo[n] = answer

print(search(n)) import sys sys def get():
	return [int(x) for x in sys.stdin.readline().rstrip().split(" ")] get int(x) for x in sys.stdin.readline().rstrip().split(" ") for x in sys.stdin.readline().rstrip().split(" ") x sys.stdin.readline().rstrip().split(" ") sys.stdin.readline().rstrip().split sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip split " " for x in sys.stdin.readline().rstrip().split(" ") int(x) int x return [int(x) for x in sys.stdin.readline().rstrip().split(" ")] [int(x) for x in sys.stdin.readline().rstrip().split(" ")] N, M = get() N get() get M A = get() A get() get tukaukazu = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] tukaukazu [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 0 for x in range(N + 1) for x in range(N + 1) x range(N + 1) range N + 1 N 1 for x in range(N + 1) 0 memo = [0 for x in range(N + 1)] memo [0 for x in range(N + 1)] def search(n):
	if n < 0:
		return None
	else:
		return memo[n] search if n < 0:
		return None
	else:
		return memo[n] n < 0 n 0 return None None return memo[n] memo[n] memo n n n for n in range(1, N + 1):
	answer = None
	for d in A:
		senzinnnotie = search(n - tukaukazu[d])
		if senzinnnotie is not None:
			candidate = senzinnnotie * 10 + d
			if (answer is None) or (candidate > answer):
				answer = candidate
	memo[n] = answer n range(1, N + 1) range 1 N + 1 N 1 answer = None answer None for d in A:
		senzinnnotie = search(n - tukaukazu[d])
		if senzinnnotie is not None:
			candidate = senzinnnotie * 10 + d
			if (answer is None) or (candidate > answer):
				answer = candidate
	 d A senzinnnotie = search(n - tukaukazu[d]) senzinnnotie search(n - tukaukazu[d]) search n - tukaukazu[d] n tukaukazu[d] tukaukazu d if senzinnnotie is not None:
			candidate = senzinnnotie * 10 + d
			if (answer is None) or (candidate > answer):
				answer = candidate
	 senzinnnotie is not None senzinnnotie None candidate = senzinnnotie * 10 + d candidate senzinnnotie * 10 + d senzinnnotie * 10 senzinnnotie 10 d if (answer is None) or (candidate > answer):
				answer = candidate
	 (answer is None) or (candidate > answer) answer is None answer None candidate > answer candidate answer answer = candidate answer candidate memo[n] = answer memo[n] memo n answer print(search(n)) print search(n) search n memo = [0 for x in range(N + 1)] [0 for x in range(N + 1)] memo senzinnnotie = search(n - tukaukazu[d]) search(n - tukaukazu[d]) senzinnnotie def get():
	return [int(x) for x in sys.stdin.readline().rstrip().split(" ")] def get():
	return [int(x) for x in sys.stdin.readline().rstrip().split(" ")] get answer = candidate candidate answer candidate = senzinnnotie * 10 + d senzinnnotie * 10 + d candidate answer = None None answer M = get() get() M def search(n):
	if n < 0:
		return None
	else:
		return memo[n] def search(n):
	if n < 0:
		return None
	else:
		return memo[n] search N, M = get() get() N A = get() get() A tukaukazu = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] tukaukazu
import sys

def get():
	return [int(x) for x in sys.stdin.readline().rstrip().split(" ")]

N, M = get()
A = get()

tukaukazu = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

memo = [0 for x in range(N + 1)]

def search(n):
	if n < 0:
		return None
	else:
		return memo[n]

for n in range(1, N + 1):
	answer = None
	for d in A:
		senzinnnotie = search(n - tukaukazu[d])
		if senzinnnotie is not None:
			candidate = senzinnnotie * 10 + d
			if (answer is None) or (candidate > answer):
				answer = candidate
	memo[n] = answer

print(search(n))
