N, M = gets.split.map &:to_i
A = gets.split.map &:to_i
c = [0,2,5,5,4,5,6,3,7,6]
p = -1
dp = Array.new(10 ** 4+10, p)
dp[0] = [0]
N.times{|i|
    A.each{|e|
        if dp[i] != p
            dp[i + c[e]] = [dp[i + c[e]] , (e.to_s + dp[i].to_s).to_i ].max
        end
    }
}
p dp[N]
