/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.c
 * Author: onlyi
 *
 * Created on January 24, 2019, 1:17 PM
 */

#include <stdio.h>
#include <stdlib.h>

typedef int bool;
#define true 1
#define false 0

static int parseint(void) {
    long c, n;

    n = getchar() - '0';
    while ((c = getchar()) >= '0')
        n = 10 * n + c - '0';

    return n;
}

int main(int argc, char** argv) {

    // TODO code application logic here  

    long long i = 0;
    long long n, t, k;
    long long ck, p1 = 0;    

    n = parseint();
    t = parseint();
    k = parseint();

    long long a[n];
    long long b[n];
    long long c[n];

    for (i = 0; i < n; i++)
    {
        a[i] = parseint();
        b[i] = parseint();
        c[i] = parseint();

        if (a[i] > 0)
            p1++;
    }

    ck = k;
    bool needTrain = true; // Check whether next hour need train
    if (p1 == 0)
    {
        needTrain = false;
        ck = 0;
    }

    long long numberOfTrain = 0; // The number of trains used 
    long long calcTrain = 0;

    i = 0;
    while (i < t)
    {
        p1 = 0;
        calcTrain = 0;
        long long p2[n]; //P2 is # passanger that will take the train            

        long long j, sum = 0;
        
        if (i == (t - 1))
        {
            long long sumA = 0, sumB = 0, sumC = 0;
            
            for (j = 0; j < n; j++)
            {
                sumA += a[j];
                sumB += b[j];
                sumC += c[j];
            }
            sum = sumA + sumB - sumC;
        }
        
        j = 0;
        while (j < n)
        {

            p2[j] = 0;

            if (needTrain)
            {
                if (p1 + a[j] <= ck)
                    p2[j] = a[j];
                else
                    p2[j] = ck - p1;

                p1 = p1 + p2[j];
            }

            a[j] = a[j] + b[j] - p2[j];
            if (a[j] + b[j] > c[j])
            {
                calcTrain++;
            }

            if (a[j] > c[j])
            {
                long long x = a[j] - c[j];

                if ((x % k) == 0)
                    x = x / k;
                else
                    x = x / k + 1;

                ck += k*x;
                p1 = 0;
                calcTrain = 0;

                for (x = 0; x <= j; x++)
                {
                    a[x] = a[x] - b[x] + p2[x];
                }

                j = 0;
                continue;
            }
            
            j++;
        }

        if (i == (t - 1) && i > 0)
        {
            if ((sum > 0) && (n <= 2))
            {
                if (ck > sum)
                    ck = sum;
            }
            //else 
            //    ck = k;
        }

        if (i < t)
        {
            numberOfTrain = numberOfTrain + ck / k;
        }

        ck = k;
        if (calcTrain > 0)
            needTrain = true;
        else
        {
            needTrain = false;
            ck = 0;
        }

        i++;
    }

    printf("%lld\n", numberOfTrain);
    return (EXIT_SUCCESS);
}
