#!/usr/bin/env python3 import collections Intersection = collections.namedtuple("Intersection", "start width delta") def compute_time(num_is, intersections): t = intersections[0].start for i in range(num_is): inti = intersections[i] if t % (2 * inti.delta) > inti.delta - inti.width: t += 2 * inti.delta - (t % (2 * inti.delta)) t += intersections[i + 1].start - inti.start return t def main(): num_is, dist = map(int, input().split()) intersections = [Intersection(*map(int, input().split())) for _ in range(num_is)] intersections.append(Intersection(dist, 0, 0)) print(compute_time(num_is, intersections)) if __name__ == '__main__': main()