class Calc0139 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @n, @l = args.shift.map(&:to_i) @xs, @ws, @ts = args.map { |l| l.map(&:to_i) }.transpose end def run cx, ct = 0, 0 @xs.zip(@ws, @ts) do |x, w, t| ct += x - cx sig = (ct / t) % 2 if sig == 1 ct = ((ct / t) + 1) * t else rest = t - (ct % t) if rest < w ct = ((ct / t) + 2) * t end end cx = x + w ct += w end ct + (@l - cx) end end puts Calc0139.new(STDIN.readlines).run if __FILE__ == $0