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

N, M = gets.split.map(&:to_i)
Digits = gets.split.map(&:to_i)
D1 = Digits.sort_by{|d| CostOf[d]*10 + (9-d) }
D2 = Digits.sort_by{|d| (CostOf.max-CostOf[d])*10 + (9-d) }

number = [D1[0]] * (N / CostOf[D1[0]])
remain = N % CostOf[D1[0]]

until remain == 0
	D1.each{|d|
		0.upto(number.size-1){|i| n = number[i]
			if n < d && CostOf[d] <= remain + CostOf[n]
				number[i] = d
				remain += CostOf[n] - CostOf[d]
			end
		}
	}
	D2.each{|d|
		(number.size-1).downto(0){|i| n = number[i]
			if CostOf[n] < CostOf[d] && CostOf[d] - CostOf[n] <= remain
				number[i] = d
				remain += CostOf[n] - CostOf[d]
			end
		}
	}
end

puts number.join('')
