class Group
  attr_accessor :arrival, :number, :sojourn
  attr_accessor :in, :out
  def initialize(arrival, number, sojourn)
    @arrival, @number, @sojourn = arrival, number, sojourn
  end
  def sit(time)
    @in = time
    @out = @in + sojourn
  end
  def show
    p [@arrival, @number, @sojourn]
  end
end

group = 100.times.map{|i| Group.new(5 * i, (i % 5 == 1 ? 5 : 2), (17 * (i % 2) + 3 * (i % 3) + 19))}
seat = Array.new(17)

time = 0
head = 0
until time == 3047 || head == 100
  # out
  seat.map! do |s|
    if s
      group[s].out <= time ? nil : s
    else
      nil
    end
  end

  # in
  loop do
    break if head == 100
    break if group[head].arrival > time

    ok = false
    n = group[head].number
    (17 - n).times do |i|
      next unless seat[i, n].all?(&:nil?)
      group[head].sit(time)
      seat[i, n] = n.times.map{head}
      head += 1
      ok = true
      break
    end
    break unless ok
  end

  # update
  time += 1
end

while gets
  n = $_.to_i
  p (group[n].in - group[n].arrival)
end