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) }

def f a,n
	return a if n == 0
	A.each{|d|
		next unless D[d] <= n
		a.push d
		return a if f a,n-D[d]
		a.pop
	}
	return nil
end

puts f([],N).sort.reverse.join('')
