import std.algorithm, std.array, std.container, std.range; import std.string, std.conv; import std.math, std.bigint, std.bitmanip, std.random; import std.stdio, std.typecons; alias Tuple!(int, "x", int, "w", int, "t") cross; void main() { auto rd = readln.chomp.split.map!(to!int); auto n = rd[0], l = rd[1]; auto ci = iota(n) .map!(_ => readln.split.map!(to!int)) .map!(rd => cross(rd[0], rd[1], rd[2])); auto x = 0; auto t = 0; foreach (c; ci) { t += c.x - x; x = c.x; if (signal(t, c) && next(t, c) - t < c.w) t = next(t, c); if (!signal(t, c)) t = (t / c.t + 1) * c.t; x += c.w; t += c.w; } t += l - x; writeln(t); } bool signal(int t, cross c) { return (t / c.t) % 2 == 0; } int next(int t, cross c) { return (t / c.t + 1) * c.t; }