table = [4, 1, 4, 1, 2, 1, 2, 1]

$<.readlines.map {|l| l.split.map(&:to_i)}.each do |persons|
  result = (0..7).map do |i|
    order = table.rotate(i)
    left = persons.map.with_index do |n, j|
      a = n - order[j]
      a < 0 ? 0 : a
    end.sum
    [left, order]
  end.sort_by {|d| d.first}
  min = result.first.first
  result = result.select {|d| d.first == min}.sort_by {|d| d.last.join.to_i}.first
  puts result.last.join(" ")
end
