n, m = gets.split.map(&:to_i)
a = gets.split
num = {}
[0, 2, 5, 5, 4, 5, 6, 3, 7, 6].each_with_index{|i, idx| num[idx.to_s] = i }
dp = Array.new(n+1, "")
use = Array.new(n+1, 0)
for i in 1..n
    maxval = ""
    for j in 0..m-1
        num_aj = num[a[j]]

        next if i - num_aj < 0
        next if use[i - num_aj] + num_aj != i

        s = dp[i - num_aj] + a[j]

        if s.length > maxval.length || s.length == maxval.length && s > maxval
            maxval = s
            use[i] = use[i - num_aj] + num_aj
        end
    end
    dp[i] = maxval
end
puts dp.last
