def num_counts_to_make(sum, arr)
    return nil if arr.size == 0
    count = sum / arr[0]
    left = sum % arr[0]
    if left == 0
        return [count]
    end
    while count >= 0
        tail = num_counts_to_make(left, arr.drop(1))
        if tail != nil
            return [count] + tail
        end
        count -= 1
        left += arr[0]
    end
    return nil
end


n, m = gets.split.map(&:to_i)
costs = [nil,2,5,5,4,5,6,3,7,6]
digit_of_cost = {}
gets.split.map(&:to_i).sort.each{|num|
    digit_of_cost[costs[num]] = num
}

costs = digit_of_cost.keys.sort

counts_of_costs = num_counts_to_make(n,costs)

ans_costs = []
counts_of_costs.each_with_index{|count, i|
    count.times{|it| ans_costs.push(costs[i])}
}
puts ans_costs.map{|it| digit_of_cost[it]}.sort.reverse.inject(0){|acc, num| acc * 10 + num}
