N, M = gets.split.map(&:to_i)
a_list = gets.split.map(&:to_i)
m_table = {
  1 => 2,
  2 => 5,
  3 => 5,
  4 => 4,
  5 => 5,
  6 => 6,
  7 => 3,
  8 => 7,
  9 => 6
}
dp = Array.new(N + 10) { -1 }
dp[0] = 0
(0...N).each do |i|
  n = dp[i]
  next if n == -1
  a_list.each do |a|
    max = n * 10 + a
    m = m_table[a]
    dp[i + m] = max if dp[i + m] < max
  end
end
puts dp[N]
