def larger(x, y):
	if len(x) > len(y):
		return x
	elif len(x) < len(y):
		return y
	else:
		if x > y:
			return x
		return y

matchuse = [5000000000000000, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = map(int, input().split())
A = list(map(int, input().split()))
dp = [[]]
for i in range(0, 10):
	dp[0].append('')
made = []
for i in range(0, N+1):
	lst = []
	for j in range(0, M):
		lst.append(False)
	made.append(lst)
made[0][0] = True
for i in range(1, N+1):
	lst = []
	for j in range(0, M):
		next = ''
		if i - matchuse[A[j]] >= 0:
			index = i - matchuse[A[j]]
			for k in range(0, M):
				if made[index][k]:
					next = larger(next, dp[index][k]+str(A[j]))
		lst.append(next)
		if next != '':
			made[i][j] = True
	dp.append(lst)
ans = ''
for i in range(0, M):
	ans = larger(ans, dp[N][i])
print(ans) def larger(x, y):
	if len(x) > len(y):
		return x
	elif len(x) < len(y):
		return y
	else:
		if x > y:
			return x
		return y larger if len(x) > len(y):
		return x
	elif len(x) < len(y):
		return y
	else:
		if x > y:
			return x
		return y len(x) > len(y) len(x) len x len(y) len y return x x elif len(x) < len(y):
		return y
	 len(x) < len(y) len(x) len x len(y) len y return y y if x > y:
			return x
		 x > y x y return x x return y y x x y y matchuse = [5000000000000000, 2, 5, 5, 4, 5, 6, 3, 7, 6] matchuse [5000000000000000, 2, 5, 5, 4, 5, 6, 3, 7, 6] 5000000000000000 2 5 5 4 5 6 3 7 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 dp = [[]] dp [[]] [] for i in range(0, 10):
	dp[0].append('') i range(0, 10) range 0 10 dp[0].append('') [0].append [0] dp 0 append '' made = [] made [] for i in range(0, N+1):
	lst = []
	for j in range(0, M):
		lst.append(False)
	made.append(lst) i range(0, N+1) range 0 N+1 N 1 lst = [] lst [] for j in range(0, M):
		lst.append(False)
	 j range(0, M) range 0 M lst.append(False) lst.append lst append False made.append(lst) made.append made append lst made[0][0] = True made[0][0] [0] made 0 0 True for i in range(1, N+1):
	lst = []
	for j in range(0, M):
		next = ''
		if i - matchuse[A[j]] >= 0:
			index = i - matchuse[A[j]]
			for k in range(0, M):
				if made[index][k]:
					next = larger(next, dp[index][k]+str(A[j]))
		lst.append(next)
		if next != '':
			made[i][j] = True
	dp.append(lst) i range(1, N+1) range 1 N+1 N 1 lst = [] lst [] for j in range(0, M):
		next = ''
		if i - matchuse[A[j]] >= 0:
			index = i - matchuse[A[j]]
			for k in range(0, M):
				if made[index][k]:
					next = larger(next, dp[index][k]+str(A[j]))
		lst.append(next)
		if next != '':
			made[i][j] = True
	 j range(0, M) range 0 M next = '' next '' if i - matchuse[A[j]] >= 0:
			index = i - matchuse[A[j]]
			for k in range(0, M):
				if made[index][k]:
					next = larger(next, dp[index][k]+str(A[j]))
		 i - matchuse[A[j]] >= 0 i - matchuse[A[j]] i matchuse[A[j]] matchuse A[j] A j 0 index = i - matchuse[A[j]] index i - matchuse[A[j]] i matchuse[A[j]] matchuse A[j] A j for k in range(0, M):
				if made[index][k]:
					next = larger(next, dp[index][k]+str(A[j]))
		 k range(0, M) range 0 M if made[index][k]:
					next = larger(next, dp[index][k]+str(A[j]))
		 made[index][k] [index] made index k next = larger(next, dp[index][k]+str(A[j])) next larger(next, dp[index][k]+str(A[j])) larger next dp[index][k]+str(A[j]) dp[index][k] [index] dp index k str(A[j]) str A[j] A j lst.append(next) lst.append lst append next if next != '':
			made[i][j] = True
	 next != '' next '' made[i][j] = True made[i][j] [i] made i j True dp.append(lst) dp.append dp append lst ans = '' ans '' for i in range(0, M):
	ans = larger(ans, dp[N][i]) i range(0, M) range 0 M ans = larger(ans, dp[N][i]) ans larger(ans, dp[N][i]) larger ans dp[N][i] [N] dp N i print(ans) print ans next = '' '' next dp = [[]] [[]] dp made = [] [] made lst = [] [] lst matchuse = [5000000000000000, 2, 5, 5, 4, 5, 6, 3, 7, 6] [5000000000000000, 2, 5, 5, 4, 5, 6, 3, 7, 6] matchuse N, M = map(int, input().split()) map(int, input().split()) N ans = '' '' ans ans = larger(ans, dp[N][i]) larger(ans, dp[N][i]) ans def larger(x, y):
	if len(x) > len(y):
		return x
	elif len(x) < len(y):
		return y
	else:
		if x > y:
			return x
		return y def larger(x, y):
	if len(x) > len(y):
		return x
	elif len(x) < len(y):
		return y
	else:
		if x > y:
			return x
		return y larger A = list(map(int, input().split())) list(map(int, input().split())) A lst = [] [] lst M = map(int, input().split()) map(int, input().split()) M index = i - matchuse[A[j]] i - matchuse[A[j]] index next = larger(next, dp[index][k]+str(A[j])) larger(next, dp[index][k]+str(A[j])) next
def larger(x, y):
	if len(x) > len(y):
		return x
	elif len(x) < len(y):
		return y
	else:
		if x > y:
			return x
		return y

matchuse = [5000000000000000, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = map(int, input().split())
A = list(map(int, input().split()))
dp = [[]]
for i in range(0, 10):
	dp[0].append('')
made = []
for i in range(0, N+1):
	lst = []
	for j in range(0, M):
		lst.append(False)
	made.append(lst)
made[0][0] = True
for i in range(1, N+1):
	lst = []
	for j in range(0, M):
		next = ''
		if i - matchuse[A[j]] >= 0:
			index = i - matchuse[A[j]]
			for k in range(0, M):
				if made[index][k]:
					next = larger(next, dp[index][k]+str(A[j]))
		lst.append(next)
		if next != '':
			made[i][j] = True
	dp.append(lst)
ans = ''
for i in range(0, M):
	ans = larger(ans, dp[N][i])
print(ans)