N,M=gets.split.map(&:to_i)
A=gets.split.map(&:to_i)
ma=[100,2,5,5,4,5,6,3,7,6]
dp=Array.new(N+1,-1)
A.each{|e|dp[ma[e]]=[dp[ma[e]],e].max}
for i in 1..N
	cand=[dp[i],-1].max
	A.each{|e|
		hoge=dp[i-ma[e]]
		if hoge!=-1
			cand=[(e.to_s+hoge.to_s).to_i,cand].max
		end
	}
	dp[i]=cand
end
puts dp[N]