D = [0,2,5,5,4,5,6,3,7,6]
N,M,*A = $<.read.split.map(&:to_i)
A.sort_by!{|d| (D[d]<<4) + (9-d) }

n,a,popped = N,[],false
until n == 0
	if ! popped and d = A.find{|d| D[d] <= n }
		a << d
		n -= D[d]
		popped = false
	else
		n += D[a[-1]]
		if d = A[A.index(a[-1])+1,A.size].find{|d| D[d] <= n }
			a[-1] = d
			n -= D[d]
			popped = false
		else
			a.pop
			popped = true
		end
	end
end

puts a.sort.reverse.join('')
