n,m = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
uses = [nil,2,5,5,4,5,6,3,7,6]

memo = [{}] # memo[桁][使用本数] = 値
as.each do |a|
  memo[0][uses[a]] = a
end

result = -1
1.upto(10e4) do |digit|
  memo[digit] = {}
  memo[digit-1].each do |cost, value|
    next if value.nil?
    next if cost > n
    if cost == n
      result = [result, value].max
      next
    end
    as.each do |a|
      next_value = (a * 10 ** (digit)) + value
      next_cost = uses[a] + cost
      if memo[digit][next_cost].nil? || next_value > memo[digit][next_cost]
        memo[digit][next_cost] = next_value
      end
    end
  end
  break if memo[digit].size == 0
end
puts result
