#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t, k;
  cin >> n >> t >> k;
  unsigned long** d = new unsigned long*[n];
  for (int i = 0; i < n; i++) {
    d[i] = new unsigned long[3];
    for (int j = 0; j < 3; j++) cin >> d[i][j];
  }
  unsigned long p = 1, a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    a += d[i][0];
    b += d[i][1];
  }
  for (int i = 0; i < n; i++) {
    delete[] d[i];
  }
  delete[] d;
  cout << max(a, b) - (a + b) % k + 1 << endl;
  return 0;
}
