# coding: euc-jp N,L = $stdin.gets.chomp.split(" ").map{ |a| a.to_i } Signals = (0...N).map{ x,w,t = $stdin.gets.chomp.split(" ").map{ |a| a.to_i } { x: x, w: w, t: t } } x = 0 t = 0 Signals.each_with_index{ |s,i| t += s[:x] - x x = s[:x] isBlue = (t / s[:t]) % 2 == 0 restTime = s[:t] - (t % s[:t]) #p [i,x,t, isBlue, restTime] if isBlue if restTime >= s[:w] # 十分に余裕のある青信号 -> そのまま渡る #puts "B1" t += s[:w] else # 余裕のない青信号 -> 次の青まで待つ #puts "B2" t += restTime + s[:t] + s[:w] end # 赤信号 -> 次の青まで待つ else #puts "R" t += restTime + s[:w] end x = s[:x] + s[:w] } t += L - x puts t