package main import ( "bufio" "fmt" "os" "strconv" ) var sc = bufio.NewScanner(os.Stdin) var rdr = bufio.NewReaderSize(os.Stdin, 1000000) func main() { sc.Split(bufio.ScanWords) n, l := nextInt(), nextInt() ns := make([][3]int, n) for i := 0; i < n; i++ { ns[i] = [3]int{nextInt(), nextInt(), nextInt()} } t, r := 0, 0 for _, v := range ns { x, w, c := v[0], v[1], v[2] t += x - r r += x - r div, mod := t/c, t%c if div%2 == 1 { t += c - mod } else if w > c-mod { t += c - mod + c } t += w r += w } fmt.Println(t + l - r) } func nextLine() string { sc.Scan() return sc.Text() } func nextInt() int { i, _ := strconv.Atoi(nextLine()) return i }