n, m = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)

STICKS = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = Array.new(n + 1, -1)
dp[0] = 0

0.upto(n) do |i|
  as.each do |a|
    if i - STICKS[a] >= 0 && dp[i - STICKS[a]] != -1
      dp[i] = [dp[i], dp[i - STICKS[a]] * 10 + a].max
    end
  end
end

p dp[n]
