def safe?(t,iv) t % (iv*2) < iv end n,l = gets.split.map(&:to_i) lx = 0; lt = 0 n.times{ x,w,t = gets.split.map(&:to_i) lt += x-lx if !safe?(lt,t) || !safe?(lt+w-1,t) lt = (lt+t*2-1)/(t*2)*t*2 end lx = x } lt += l-lx p lt