def is_blue(current: int, interval: int) -> bool: return (current // interval) % 2 == 0 def main(): N, L = map(int, input().split()) route = [list(map(int, input().split())) for _ in range(N)] current_x = 0 current_t = 0 for cross_x, cross_w, interval in route: current_t += cross_x - current_x current_x = cross_x if not is_blue(current_t, interval): current_t += (interval - (current_t % interval)) elif not is_blue(current_t + cross_w, interval): current_t += (2 * interval - (current_t % interval)) current_t += cross_w current_x += cross_w current_t += L - current_x current_x = L print(current_t) if __name__ == "__main__": main()