priority = [1,7,4,5,3,2,9,6,8]
cost = [2,3,4,5,5,5,6,6,7]
n,m = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i)

choise = []

priority.each_with_index do |pri,i|
    if a.include?(pri) && !choise.include?(cost[i])
        choise.push([cost[i],pri])
    end
end

def seek_combination(cur,choise,crumb,n)
    choise.each do |c|
        if (cur+c[0]) == n
            cur += c[0]
            crumb.push(c)
            return crumb
        elsif (cur+c[0]) > n
            next
        else
            cur += c[0]
            crumb.push(c)
            res = seek_combination(cur,choise,crumb,n)
            if res
                return res
            end
            cur -= c[0]
            crumb.pop
        end
    end
    return nil
end

crumb = []
crumb = seek_combination(0,choise,crumb,n)
puts crumb.map{|i| i[1]}.sort.reverse.join('')