n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)

count = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
use = []
a.map{ |i| count[i] }.uniq.sort.each do |k|
    9.downto(1).each do |i|
        if k == count[i] && a.include?(i)
            use << i
            break
        end
    end
end

f = ['!'] * (n + 1)
f[0] = ''
(n+1).times do |j|
    use.each do |i|
        len = count[i]
        break if j + len > n
        if f[j] != '!'
            s = f[j] + i.to_s
            if f[j + len].size < s.size || f[j + len].size == s.size &&  f[j + len] < s
                f[j + len] = s
            end
        end
    end
end

puts f[n]

