n,m = gets.split.map(&:to_i)
b = [0,2,5,5,4,5,6,3,7,6]
a = gets.split.map(&:to_i).sort_by{|i|b[i]}
ans = []
i = 0
while n > 0
    if i == m
        i = a.index(ans[-1])
        ans.pop
        n += b[a[i]]
        i += 1
    end
    if b[a[i]] <= n
        ans << a[i]
        n -= b[a[i]]
    else
        n += b[ans[-1]]
        ans.pop
        i += 1
    end
end
puts ans.sort.reverse.join("")