/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 2;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = m[j].x.y - m[j].x.x;
      if (reload * k + r < need) {
        c = oo;
      } else {
        reload = max(0ll, (need - r + k - 1) / k);
        c += reload;
        r += k * reload;
        r -= need;
      }
      cost[i][j] = c;
      rest[i][j] = r;
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo)
        continue;
      // if (j && m[j - 1].x.y == m[j].x.x)
      //   continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 2;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = m[j].x.y - m[j].x.x;
      if (reload * k + r < need) {
        c = oo;
      } else {
        reload = max(0ll, (need - r + k - 1) / k);
        c += reload;
        r += k * reload;
        r -= need;
      }
      cmin(c, oo);
      cost[i][j] = c;
      rest[i][j] = r;
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo || dp[j] >= oo)
        continue;
      if (j && m[j - 1].x.y == m[j].x.x)
        continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 2;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    bool d = false;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = max(0ll, (need - r + k - 1) / k);
      if (reload > m[j].x.y - m[j].x.x) {
        if (d) {
          if (reload > m[j].x.y - m[j].x.x + 1 && m[j - 1].x.y == m[j].x.x) {
            c = oo;
          } else {
            c += reload;
            r += k * reload;
            r -= need;
          }
        } else {
          c = oo;
        }
      } else {
        c += reload;
        r += k * reload;
        r -= need;
      }
      if (c >= oo) {
        c = oo, r = -oo;
      }
      cost[i][j] = c;
      rest[i][j] = r;
      if (reload < m[j].x.y - m[j].x.x)
        d = true;
      else
        d = false;
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo || dp[j] >= oo)
        continue;
      if (j && m[j - 1].x.y == m[j].x.x)
        continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 2;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = m[j].x.y - m[j].x.x;
      if (reload * k + r < need) {
        c = oo;
      } else {
        reload = max(0ll, (need - r + k - 1) / k);
        c += reload;
        r += k * reload;
        r -= need;
      }
      cost[i][j] = c;
      rest[i][j] = r;
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo)
        continue;
      if (j && m[j - 1].x.y == m[j].x.x)
        continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }


/*
 * Package: StandardCodeLibrary.Core
 * */
//引进常用的头文件并使用std名字空间;
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

//用于减少代码量的宏;
#define lp for (;;)
#define repf(i, a, b) for (int i = (a); i < (b); ++i)
#define ft(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdt(i, a, b) for (int i = (a); i >= (b); --i)
#define rrepf(i, a, b) fdt(i, (a)-1, b)
#define rep(i, n) repf(i, 0, n)
#define rrep(i, n) rrepf(i, n, 0)
#define for_each(e, s)                                                         \
  for (__typeof__((s).begin()) e = (s).begin(); e != (s).end(); ++e)
#define for_nonempty_subsets(subset, set)                                      \
  for (int subset = set; subset; subset = (subset - 1) & (set))
#define for_in_charset(i, charset) for (cstr i = (charset); *i; i++)
#define whl(x) while (x)
#define rtn return
#define fl(x, y) memset((x), char(y), sizeof(x))
#define clr(x) fl(x, char(0))
#define cpy(x, y) memcpy(x, y, sizeof(x))
#define sf scanf
#define pf printf
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ins insert
#define ers erase
#define lb lower_bound
#define ub upper_bound
#define rnk order_of_key
#define sel find_by_order
#define ctz __builtin_ctz
#define clz __builtin_clz
#define bc __builtin_popcount
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define srt(x) sort(all(x))
#define uniq(x) srt(x), (x).erase(unique(all(x)), (x).end())
#define rev(x) reverse(all(x))
#define shf(x) random_shuffle(all(x))
#define nxtp(x) next_permutation(all(x))

//调试相关的宏;
#ifndef DEBUG
#define prt(x) (cerr)
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      exit(0);                                                                 \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1);                                                                  \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(new int);                                                            \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      whl(1) puts("OLE");                                                      \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      *(int *)0 = 0;                                                           \
  whl(0)
#define runtime() (cerr)
#define input(in) freopen(in, "r", stdin)
#define output(out) freopen(out, "w", stdout)
#else
#define prt(x) cerr << "第" << __LINE__ << "行\t: " << #x "\t=" << (x) << endl
#define asrtWA(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtTLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtMLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtOLE(s)                                                             \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define asrtRE(s)                                                              \
  do                                                                           \
    if (!(s))                                                                  \
      cerr << "assert(" #s ")" << endl;                                        \
  whl(0)
#define runtime()                                                              \
  cerr << "Used: " << db(clock()) / CLOCKS_PER_SEC << "s" << endl
#define input(in)
#define output(out)
#endif

//常用数据类型;
typedef long long int lli;
typedef double db;
typedef const char *cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<lli> vl;
typedef vec<vl> vvl;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<char> vc;
typedef vec<vc> vvc;
typedef vec<str> vs;
typedef pr<int, int> pii;
typedef pr<lli, lli> pll;
typedef pr<db, db> pdd;
typedef vec<pii> vpii;
typedef vec<pll> vpll;
typedef vec<pdd> vpdd;
typedef map<int, int> mii;
typedef map<str, int> msi;
typedef map<char, int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;

//常用常量:int的最大值;lli的最大值;db的误差相关常数;欧拉常数;圆周率;移动向量;取模使用的除数;
int oo = (~0u) >> 1;
lli ooll = (~0ull) >> 1;
db inf = 1e+10;
db eps = 1e-10;
db gam = 0.5772156649015328606;
db pi = acos(-1.0);
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int MOD = 1000000007;

//常用函数:最大最小值更新;数学相关函数;输入和输出;树状数组;并查集;可合并堆;
template <typename type> inline bool cmax(type &a, const type &b) {
  rtn a < b ? a = b, true : false;
}
template <typename type> inline bool cmin(type &a, const type &b) {
  rtn b < a ? a = b, true : false;
}
template <typename type> inline type sqr(const type &x) { rtn x *x; }
template <typename type> inline type mod(const type &x) { rtn x % MOD; }
inline int sgn(const db &x) { rtn(x > +eps) - (x < -eps); }
inline int dbcmp(const db &a, const db &b) { rtn sgn(a - b); }
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &x) {
  rtn mp(-x.x, -x.y);
}
template <typename type>
inline pr<type, type> operator+(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x + b.x, a.y + b.y);
}
template <typename type>
inline pr<type, type> operator-(const pr<type, type> &a,
                                const pr<type, type> &b) {
  rtn mp(a.x - b.x, a.y - b.y);
}
template <typename type>
inline pr<type, type> operator*(const pr<type, type> &a, const type &b) {
  rtn mp(a.x * b, a.y * b);
}
template <typename type>
inline pr<type, type> operator/(const pr<type, type> &a, const type &b) {
  rtn mp(a.x / b, a.y / b);
}
template <typename type>
inline pr<type, type> &operator-=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a - b;
}
template <typename type>
inline pr<type, type> &operator+=(pr<type, type> &a, const pr<type, type> &b) {
  rtn a = a + b;
}
template <typename type>
inline pr<type, type> &operator*=(pr<type, type> &a, const type &b) {
  rtn a = a * b;
}
template <typename type>
inline pr<type, type> &operator/=(pr<type, type> &a, const type &b) {
  rtn a = a / b;
}
template <typename type>
inline type cross(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.y - a.y *b.x;
}
template <typename type>
inline type dot(const pr<type, type> &a, const pr<type, type> &b) {
  rtn a.x *b.x + a.y *b.y;
}
template <typename type> inline type gcd(type a, type b) {
  if (b)
    whl((a %= b) && (b %= a));
  rtn a + b;
}
template <typename type> inline type lcm(type a, type b) {
  rtn a *b / gcd(a, b);
}
inline lli bin_pow(lli x, lli y) {
  lli z = 1;
  whl(y) {
    if (y & 1)
      z = mod(z * x);
    x = mod(sqr(x)), y >>= 1;
  }
  rtn z;
}
template <typename istream, typename first_type, typename second_type>
inline istream &operator>>(istream &cin, pr<first_type, second_type> &x) {
  rtn cin >> x.x >> x.y;
}
template <typename ostream, typename first_type, typename second_type>
inline ostream &operator<<(ostream &cout,
                           const pr<first_type, second_type> &x) {
  rtn cout << x.x << " " << x.y;
}
template <typename istream, typename type>
inline istream &operator>>(istream &cin, vec<type> &x) {
  rep(i, sz(x)) cin >> x[i];
  rtn cin;
}
template <typename ostream, typename type>
inline ostream &operator<<(ostream &cout, const vec<type> &x) {
  rep(i, sz(x)) cout << x[i] << (i + 1 == sz(x) ? "" : " ");
  rtn cout;
}
inline ostream &pdb(int prcs, db x) {
  rtn cout << setprecision(prcs) << fixed << (sgn(x) ? (x) : 0);
}
template <typename type> inline void bit_inc(vec<type> &st, int x, type inc) {
  whl(x < sz(st)) st[x] += inc, x |= x + 1;
}
template <typename type> inline type bit_sum(const vec<type> &st, int x) {
  type s = 0;
  whl(x >= 0) s += st[x], x = (x & (x + 1)) - 1;
  rtn s;
}
template <typename type> inline type bit_kth(const vec<type> &st, int k) {
  int x = 0, y = 0, z = 0;
  whl((1 << (++y)) <= sz(st));
  rrep(i, y) {
    if ((x += 1 << i) > sz(st) || z + st[x - 1] > k)
      x -= 1 << i;
    else
      z += st[x - 1];
  }
  rtn x;
}
inline void make_set(vi &st) { rep(i, sz(st)) st[i] = i; }
inline int find_set(vi &st, int x) {
  int y = x, z;
  whl(y != st[y]) y = st[y];
  whl(x != st[x]) z = st[x], st[x] = y, x = z;
  rtn y;
}
inline bool union_set(vi &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? st[a] = b, true : false;
}
inline void make_set(vpii &st) { rep(i, sz(st)) st[i] = mp(i, 1); }
inline int find_set(vpii &st, int x) {
  int y = x, z;
  whl(y != st[y].x) y = st[y].x;
  whl(x != st[x].x) z = st[x].x, st[x].x = y, x = z;
  rtn y;
}
inline bool union_set(vpii &st, int a, int b) {
  a = find_set(st, a), b = find_set(st, b);
  rtn a != b ? (st[a].y > st[b].y ? st[a].x = b,
                st[a].y += st[b].y : st[b].x = a, st[b].y += st[a].y),
      true : false;
}
template <typename type> inline void merge(type &a, type &b) {
  if (sz(a) < sz(b))
    swap(a, b);
  whl(sz(b)) a.ins(*b.begin()), b.ers(b.begin());
}

//初始化;
struct Initializer {
#ifndef DEBUG
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
#else
  ~Initializer() { runtime(); }
#endif
} initializer;

//非标准;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
typedef __gnu_cxx::rope<char> rope;
template <typename key, typename value>
class ext_map
    : public __gnu_pbds::tree<key, value, less<key>, __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};
template <typename key>
class ext_set
    : public __gnu_pbds::tree<key, __gnu_pbds::null_type, less<key>,
                              __gnu_pbds::rb_tree_tag,
                              __gnu_pbds::tree_order_statistics_node_update> {};

void solve() {
  lli oo = ooll / 2;
  lli n, k;
  cin >> n >> k;
  vec<pr<pll, lli>> m(n);
  cin >> m;
  // int s = 0;
  // rep(i, n) {
  //   if (s > 0 && m[s - 1].x.y == m[i].x.x) {
  //     m[s - 1].x.y = m[i].x.y;
  //     m[s - 1].y += m[i].y;
  //     // wrong
  //   } else {
  //     m[s++] = m[i];
  //   }
  // }
  // n = s;
  // m.resize(n);
  vvl cost(n, vl(n));
  vvl rest(n, vl(n));
  vvi dd(n, vi(n));
  rep(i, n) {
    lli c = 0;
    lli r = k;
    bool d = false;
    repf(j, i, n) {
      lli need = m[j].y;
      lli reload = max(0ll, (need - r + k - 1) / k);
      if (reload > m[j].x.y - m[j].x.x) {
        if (d) {
          r = k;
          reload = max(0ll, (need - r + k - 1) / k);
          if (reload > m[j].x.y - m[j].x.x || m[j - 1].x.y != m[j].x.x) {
            c = oo;
          } else {
            c += reload + 1;
            r += k * reload;
            r -= need;
            reload -= 1;
          }
        } else {
          c = oo;
        }
      } else {
        c += reload;
        r += k * reload;
        r -= need;
      }
      if (c >= oo) {
        c = oo, r = -oo;
      }
      if (reload < m[j].x.y - m[j].x.x)
        d = true;
      else
        d = false;
      dd[i][j] = d;
      if (j != n - 1) {
        cost[i][j] = c;
        if (!d && m[j].x.y == m[j + 1].x.x) {
          cost[i][j] = oo;
        }
        rest[i][j] = r;
      } else {
        cost[i][j] = c;
        rest[i][j] = r;
      }
      prt(mp(i, j));
      prt(mp(c, r));
    }
  }
  vl dp(n + 1, oo);
  lli ans = oo;
  dp[0] = 0;
  ft(i, 1, n) {
    rep(j, i) {
      if (cost[j][i - 1] >= oo || dp[j] >= oo)
        continue;
      // if (j && m[j - 1].x.y == m[j].x.x)
      //   continue;
      cmin(dp[i], dp[j] + cost[j][i - 1] + 1);
      if (i == n) {
        if (cmin(ans, (dp[j] + cost[j][i - 1] + 1) * k - rest[j][i - 1])) {
        }
      }
    }
    prt(dp[i]);
  }
  if (ans >= oo) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}

int main() { solve(); }


#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,k;
const int N=2005;
int a[N],l[N],r[N];
const int INF=1e17;

struct HASH{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

// unordered_map<int,int>dp[N];

int dp[N][N];

int func(int i,int lastReloadIndex,int remain){
    if(i==n){
        return -remain;
    }

    if(dp[i][lastReloadIndex]!=-1)
        return dp[i][lastReloadIndex];

    int time=r[i]-l[i];
    int req=(max(0ll,(a[i]-remain))+k-1)/k;
    if(req>time){
        return INF;
    }

    int ans=INF;

    //On-Demand..
    int newRemain=remain+req*k-a[i];
    ans=min(ans,req*k+func(i+1,lastReloadIndex,newRemain));

    // Extra Reload.
    int finishTime=l[i]+req;

    if(i+1<n && (finishTime+1<=r[i] || r[i]<l[i+1])){
        ans=min(ans,(req+1)*k+func(i+1,i,k));
    }
    return dp[i][lastReloadIndex]=ans;
}

void solve(){
    // for(int i=0;i<N;i++){
    //     dp[i].clear();
    // }
    memset(dp,-1,sizeof(dp));
    cin>>n>>k;
    for(int i=0;i<n;i++)    cin>>l[i]>>r[i]>>a[i];
    int ans=k+func(0ll,0ll,k);
    if(ans>INF) cout<<-1<<"\n";
    else        cout<<ans<<"\n";
}

main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    solve();
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,k;
const int N=2005;
int a[N],l[N],r[N];
const int INF=1e17;

map<pair<int,int>,int>dp;

int func(int i,int remain){
    if(i==n){
        return -remain;
    }

    if(dp.count({i,remain}))
        return dp[{i,remain}];

    int time=r[i]-l[i];
    int req=(max(0ll,(a[i]-remain))+k-1)/k;
    if(req>time){
        return INF;
    }

    int ans=INF;

    //On-Demand..
    int newRemain=remain+req*k-a[i];
    ans=min(ans,req*k+func(i+1,newRemain));

    // Extra Reload.
    int finishTime=l[i]+req;

    if(i+1<n && (finishTime+1<=r[i] || r[i]<l[i+1])){
        ans=min(ans,(req+1)*k+func(i+1,k));
    }
    return dp[{i,remain}]=ans;
}

void solve(){
    dp.clear();
    cin>>n>>k;
    for(int i=0;i<n;i++)    cin>>l[i]>>r[i]>>a[i];
    int ans=k+func(0ll,k);
    if(ans>INF) cout<<-1<<"\n";
    else        cout<<ans<<"\n";
}

main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    solve();
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,k;
const int N=2005;
int a[N],l[N],r[N];
const int INF=1e17;

struct HASH{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

unordered_map<int,int>dp[N];

int func(int i,int remain){
    if(i==n){
        return -remain;
    }

    if(dp[i].count(remain))
        return dp[i][remain];

    int time=r[i]-l[i];
    int req=(max(0ll,(a[i]-remain))+k-1)/k;
    if(req>time){
        return INF;
    }

    int ans=INF;

    //On-Demand..
    int newRemain=remain+req*k-a[i];
    ans=min(ans,req*k+func(i+1,newRemain));

    // Extra Reload.
    int finishTime=l[i]+req;

    if(i+1<n && (finishTime+1<=r[i] || r[i]<l[i+1])){
        ans=min(ans,(req+1)*k+func(i+1,k));
    }
    return dp[i][remain]=ans;
}

void solve(){
    for(int i=0;i<N;i++){
        dp[i].clear();
    }
    cin>>n>>k;
    for(int i=0;i<n;i++)    cin>>l[i]>>r[i]>>a[i];
    int ans=k+func(0ll,k);
    if(ans>INF) cout<<-1<<"\n";
    else        cout<<ans<<"\n";
}

main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    solve();
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,k;
const int N=2005;
int a[N],l[N],r[N];
const int INF=1e17;

map<pair<int,int>,int>dp;

int func(int i,int remain){
    if(i==n){
        return -remain;
    }

    if(dp.count({i,remain}))
        return dp[{i,remain}];

    int time=r[i]-l[i];
    int req=(max(0ll,(a[i]-remain))+k-1)/k;
    if(req>time){
        return INF;
    }

    int ans=INF;

    //On-Demand..
    int newRemain=remain+req*k-a[i];
    ans=min(ans,req*k+func(i+1,newRemain));

    // Extra Reload.
    if(i+1<n && r[i]<l[i+1]){
        ans=min(ans,(req+1)*k+func(i+1,k));
    }
    return dp[{i,remain}]=ans;
}

void solve(){
    dp.clear();
    cin>>n>>k;
    for(int i=0;i<n;i++)    cin>>l[i]>>r[i]>>a[i];
    int ans=k+func(0ll,k);
    if(ans>INF) cout<<-1<<"\n";
    else        cout<<ans<<"\n";
}

main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    solve();
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,k;
const int N=2005;
int a[N],l[N],r[N];
const int INF=1e17;

struct HASH{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

unordered_map<pair<int,int>,int,HASH>dp;

int func(int i,int remain){
    if(i==n){
        return -remain;
    }

    if(dp.count({i,remain}))
        return dp[{i,remain}];

    int time=r[i]-l[i];
    int req=(max(0ll,(a[i]-remain))+k-1)/k;
    if(req>time){
        return INF;
    }

    int ans=INF;

    //On-Demand..
    int newRemain=remain+req*k-a[i];
    ans=min(ans,req*k+func(i+1,newRemain));

    // Extra Reload.
    int finishTime=l[i]+req;

    if(i+1<n && (finishTime+1<=r[i] || r[i]<l[i+1])){
        ans=min(ans,(req+1)*k+func(i+1,k));
    }
    return dp[{i,remain}]=ans;
}

void solve(){
    dp.clear();
    cin>>n>>k;
    for(int i=0;i<n;i++)    cin>>l[i]>>r[i]>>a[i];
    int ans=k+func(0ll,k);
    if(ans>INF) cout<<-1<<"\n";
    else        cout<<ans<<"\n";
}

main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    solve();
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,k;
const int N=2005;
int a[N],l[N],r[N];
const int INF=1e17;

struct HASH{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};
unordered_map<pair<int,int>,int,HASH>dp;

int func(int i,int remain){
    if(i==n){
        return -remain;
    }

    if(dp.count({i,remain}))
        return dp[{i,remain}];

    int time=r[i]-l[i];
    int req=(max(0ll,(a[i]-remain))+k-1)/k;
    if(req>time){
        return INF;
    }

    int ans=INF;

    //On-Demand..
    int newRemain=remain+req*k-a[i];
    ans=min(ans,req*k+func(i+1,newRemain));

    // Extra Reload.
    int finishTime=l[i]+req;

    if(i+1<n && (finishTime+1<=r[i] || r[i]<l[i+1])){
        ans=min(ans,(req+1)*k+func(i+1,k));
    }
    return dp[{i,remain}]=ans;
}

void solve(){
    dp.clear();
    cin>>n>>k;
    for(int i=0;i<n;i++)    cin>>l[i]>>r[i]>>a[i];
    int ans=k+func(0ll,k);
    if(ans>INF) cout<<-1<<"\n";
    else        cout<<ans<<"\n";
}

main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    solve();
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,k;
const int N=2005;
int a[N],l[N],r[N];
const int INF=1e17;

struct HASH{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

// unordered_map<int,int>dp[N];

int dp[N][N];

int func(int i,int lastReloadIndex,int remain){
    // cout<<i<<" "<<lastReloadIndex<<"\n";
    if(i==n){
        return -remain;
    }

    if(dp[i][lastReloadIndex]!=-INF)
        return dp[i][lastReloadIndex];

    int time=r[i]-l[i];
    int req=(max(0ll,(a[i]-remain))+k-1)/k;
    if(req>time){
        return INF;
    }

    int ans=INF;

    //On-Demand..
    int newRemain=remain+req*k-a[i];
    ans=min(ans,req*k+func(i+1,lastReloadIndex,newRemain));

    // Extra Reload.
    int finishTime=l[i]+req;

    if(i+1<n && (finishTime+1<=r[i] || r[i]<l[i+1])){
        ans=min(ans,(req+1)*k+func(i+1,i+1,k));
    }
    return dp[i][lastReloadIndex]=ans;
}

void solve(){
    // for(int i=0;i<N;i++){
    //     dp[i].clear();
    // }
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            dp[i][j]=-INF;
        }
    }
    // memset(dp,-INF,sizeof(dp));
    cin>>n>>k;
    for(int i=0;i<n;i++)    cin>>l[i]>>r[i]>>a[i];
    int ans=k+func(0ll,0ll,k);
    // cout<<ans<<"\n";
    if(ans>INF) cout<<-1<<"\n";
    else        cout<<ans<<"\n";
}

main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    solve();
}


#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

#define f first
#define s second

const int MAXN = 2e3 + 3;
const long long INF = 1e18 + 3;

int l[MAXN];
int r[MAXN];
int w[MAXN];
map<int, long long> dp, temp;

void ust(int ind, long long war) {
    if (temp.count(ind))
        temp[ind] = min(temp[ind], war);
    else
        temp[ind] = war;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k, kon, pocz;
    int time;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> l[i] >> r[i] >> w[i];
    l[n + 1] = r[n];
    dp[k] = 0;
    for (int i = 1; i <= n; i++) {
        if (dp.empty()) {
            cout << "-1";
            return 0;
        }
        kon = w[i] / k;
        pocz = w[i] % k;
        if (w[i] % k == 0) {
            kon--;
            pocz = k;
        }
        for (auto &it:dp) {
            if (it.f >= pocz) {
                time = kon;
                if (r[i] - l[i] >= time)
                    ust(it.f - pocz, it.s + time);
                if (l[i + 1] >= l[i] + time + 1)
                    ust(k, it.s + time + 1);
            }
            else {
                time = kon + 1;
                if (r[i] - l[i] >= time)
                    ust(k - pocz + it.f, it.s + time);
                if (l[i + 1] >= l[i] + time + 1)
                    ust(k, it.s + time + 1);
            }
        }
        swap(dp, temp);
        temp.clear();
    }
    if (dp.empty()) {
        cout << "-1";
        return 0;
    }
    long long ans = INF;
    for (auto &it:dp)
        ans = min(ans, it.s * (long long)(k) + (long long)(k - it.f));
    cout << ans;
    return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdint>
#include <map>

using namespace std;

#define f first
#define s second
#define int long long

const int MAXN = 2e3 + 3;
const long long INF = 1e18 + 3;

int l[MAXN];
int r[MAXN];
int w[MAXN];
map<int, long long> dp, temp;

void ust(int ind, long long war) {
    if (temp.count(ind))
        temp[ind] = min(temp[ind], war);
    else
        temp[ind] = war;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k, kon, pocz;
    int time;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> l[i] >> r[i] >> w[i];
    l[n + 1] = r[n] + 3;
    dp[k] = 0;
    for (int i = 1; i <= n; i++) {
        if (l[i] == r[i] && l[i + 1] == l[i] && l[i + 1] == r[i + 1]) {
            w[i + 1] += w[i];
            continue;
        }

        if (dp.empty()) {
            cout << "-1";
            return 0;
        }
        kon = w[i] / k;
        pocz = w[i] % k;
        if (w[i] % k == 0) {
            kon--;
            pocz = k;
        }
        for (auto &it:dp) {
            if (it.f >= pocz) {
                time = kon;
                if (r[i] - l[i] >= time)
                    ust(it.f - pocz, it.s + time);
                if (r[i] - l[i] >= time && l[i + 1] >= l[i] + time + 1)
                    ust(k, it.s + time + 1);
            }
            else {
                time = kon + 1;
                if (r[i] - l[i] >= time)
                    ust(k - pocz + it.f, it.s + time);
                if (r[i] - l[i] >= time && l[i + 1] >= l[i] + time + 1)
                    ust(k, it.s + time + 1);
            }
        }
        swap(dp, temp);
        temp.clear();
    }
    if (dp.empty()) {
        cout << "-1";
        return 0;
    }
    long long ans = INF;
    for (auto &it:dp)
        ans = min(ans, it.s * (long long)(k) + (long long)(k - it.f));
    cout << ans;
    return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdint>
#include <map>

using namespace std;

#define f first
#define s second
#define int long long

const int MAXN = 2e3 + 3;
const long long INF = 1e18 + 3;

int l[MAXN];
int r[MAXN];
int w[MAXN];
map<int, long long> dp, temp;

void ust(int ind, long long war) {
    if (temp.count(ind))
        temp[ind] = min(temp[ind], war);
    else
        temp[ind] = war;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k, kon, pocz;
    int time;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> l[i] >> r[i] >> w[i];
    l[n + 1] = r[n] + 3;
    dp[k] = 0;
    for (int i = 1; i <= n; i++) {
        if (dp.empty()) {
            cout << "-1";
            return 0;
        }
        kon = w[i] / k;
        pocz = w[i] % k;
        if (w[i] % k == 0) {
            kon--;
            pocz = k;
        }
        for (auto &it:dp) {
            if (it.f >= pocz) {
                time = kon;
                if (r[i] - l[i] >= time)
                    ust(it.f - pocz, it.s + time);
                if (l[i + 1] >= l[i] + time + 1)
                    ust(k, it.s + time + 1);
            }
            else {
                time = kon + 1;
                if (r[i] - l[i] >= time)
                    ust(k - pocz + it.f, it.s + time);
                if (l[i + 1] >= l[i] + time + 1)
                    ust(k, it.s + time + 1);
            }
        }
        swap(dp, temp);
        temp.clear();
    }
    if (dp.empty()) {
        cout << "-1";
        return 0;
    }
    long long ans = INF;
    for (auto &it:dp)
        ans = min(ans, it.s * (long long)(k) + (long long)(k - it.f));
    cout << ans;
    return 0;
}


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
#define int LL
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
int t[2300],a[2400];
LL pre[2002][2002];//消耗
int rest[2002][2002];
bool can[2002][2002];
LL dp[2002];
int ll[2002],rr[2002];
signed main(){
	fastio;
	int k;
	cin>>n>>k;
	rb(i,1,n){
		int l,r;
		cin>>l>>r>>a[i];
		t[i]=r-l+1;
		ll[i]=l;
		rr[i]=r;
	}
	rb(i,1,n){
		int needt=0;
		int last=0;
		rb(j,i,n){
			int res=(j==i? k:rest[i][j-1]);
//			cout<<i<<' '<<j<<' '<<res<<' '<<last<<'\n';
			if(j==i){
				last=ll[i]-1;
			}
			check_max(last,(ll[i]-1));
			int needt=0;
			if(a[j]<res){
				last++;
				if(last>rr[j]) break;
				can[i][j]=1;
				rest[i][j]=res-a[j];
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			if(a[j]==res){
				last++;
				if(last>rr[j]) break;
				can[i][j]=1;
				rest[i][j]=k;
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			last++;
			int aj=a[j]-res;
			needt+=(aj+k-1)/k;
			if(needt+last>rr[i]){
				break;
			}
			last+=needt;
			can[i][j]=1;
			pre[i][j]=pre[i][j-1]+a[j];
			rest[i][j]=(aj+k-1)/k*k-aj;
			if(rest[i][j]==0) rest[i][j]=k;
		}
	}
	memset(dp,127,sizeof(dp));
	dp[0]=0;
	rb(i,1,n){
		rb(j,1,i){
			if(dp[j-1]!=INF)
				if(j==1||rr[j-1]!=ll[j])
					if(can[j][i]){
						check_min(dp[i],dp[j-1]+rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]);
//						if(i==n){
//							cout<<j<<" "<<pre[j][i]<<" "<<dp[j-1]<<' '<<can[j][i]<<endl;
//						}
					}
		}
	}
//	cout<<rest[1][1]<<endl;
	if(dp[n]==dp[n+1]) dp[n]=-1;
	cout<<dp[n]<<endl;
	return 0;
}


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
#define int LL
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
int t[2300],a[2400];
LL pre[2002][2002];//消耗
int rest[2002][2002];
bool can[2002][2002];
LL dp[2002];
int ll[2002],rr[2002];
bool used_las[2002][2002];
signed main(){
	fastio;
	int k;
	cin>>n>>k;
	rb(i,1,n){
		int l,r;
		cin>>l>>r>>a[i];
		t[i]=r-l+1;
		ll[i]=l;
		rr[i]=r;
	}
	rb(i,1,n){
		int needt=0;
		int last=0;
		rb(j,i,n){
 			int res=(j==i? k:rest[i][j-1]);
//			cout<<i<<" "<<j<<" "<<res<<endl;
			if(j==i){
				last=ll[i]-1;
			}
			check_max(last,(ll[j]));
			int needt=0;
			if(a[j]<res){
				used_las[i][j]=((last)==rr[j]);
				can[i][j]=1;
				rest[i][j]=res-a[j];
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			if(a[j]==res){
				last++;
				if(last>rr[j]+1) break;
				used_las[i][j]=((last-1)==rr[j]);
				can[i][j]=1;
				rest[i][j]=k;
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			last++;
			int aj=a[j]-res;
			needt+=(aj+k-1)/k;
			if(needt+last>rr[j]+1){
				break;
			}
			used_las[i][j]=((needt+last)==(rr[j]+1));
			last+=needt;
			can[i][j]=1;
			pre[i][j]=pre[i][j-1]+a[j];
			rest[i][j]=(aj+k-1)/k*k-aj;
			if(rest[i][j]==0) rest[i][j]=k;
			else last--;
		}
	}
	memset(dp,127,sizeof(dp));
	dp[0]=0;
	rb(i,1,n){
		rb(j,1,i){
			if(dp[j-1]!=dp[n+1])
				if(can[j][i]){
					if(i==n||!used_las[j][i]||ll[i+1]!=rr[i]){
						check_min(dp[i],dp[j-1]+rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]);
					}
				}
		}
	}
	if(dp[n]==dp[n+1]) dp[n]=-1;
	cout<<dp[n]<<endl;
	return 0;
}
/*
10 85
3 5 57
6 8 86
9 10 46
11 11 19
11 12 37
12 12 62
14 14 60
15 15 78
16 16 69
19 20 50
*/


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
#define int LL
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
int t[2300],a[2400];
LL pre[2002][2002];//消耗
int rest[2002][2002];
bool can[2002][2002];
LL dp[2002];
int ll[2002],rr[2002];
bool used_las[2002][2002];
signed main(){
	fastio;
	int k;
	cin>>n>>k;
	rb(i,1,n){
		int l,r;
		cin>>l>>r>>a[i];
		t[i]=r-l+1;
		ll[i]=l;
		rr[i]=r;
	}
	rb(i,1,n){
		int needt=0;
		int last=0;
		rb(j,i,n){
 			int res=(j==i? k:rest[i][j-1]);
//			cout<<i<<" "<<j<<" "<<res<<endl;
			check_max(last,(ll[j]));
//			if(i==1){
//				cout<<j<<' '<<last<<endl;
//			}
			if(last>rr[j]) break;
			int needt=0;
			if(a[j]<res){
				used_las[i][j]=((last)==rr[j]);
				can[i][j]=1;
				rest[i][j]=res-a[j];
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			if(a[j]==res){
				last++;
				if(last>rr[j]+1) break;
				used_las[i][j]=((last-1)==rr[j]);
				can[i][j]=1;
				rest[i][j]=k;
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			last++;
			int aj=a[j]-res;
			needt+=(aj+k-1)/k;
			if(needt+last>rr[j]+1){
				break;
			}
			used_las[i][j]=((needt+last)==(rr[j]+1));
			last+=needt;
			can[i][j]=1;
			pre[i][j]=pre[i][j-1]+a[j];
			rest[i][j]=(aj+k-1)/k*k-aj;
			if(rest[i][j]==0) rest[i][j]=k;
			else last--;
		}
	}
	memset(dp,127,sizeof(dp));
	dp[0]=0;
	rb(i,1,n){
		rb(j,1,i){
			if(dp[j-1]!=dp[n+1])
				if(can[j][i]){
					if(i==n||!used_las[j][i]||ll[i+1]!=rr[i]){
						check_min(dp[i],dp[j-1]+rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]);
					}
				}
		}
	}
	if(dp[n]==dp[n+1]) dp[n]=-1;
	cout<<dp[n]<<endl;
	return 0;
}
/*
10 85
3 5 57
6 8 86
9 10 46
11 11 19
11 12 37
12 12 62
14 14 60
15 15 78
16 16 69
19 20 50
*/


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
#define int LL
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
int t[2300],a[2400];
LL pre[2002][2002];//消耗
int rest[2002][2002];
bool can[2002][2002];
LL dp[2002];
int ll[2002],rr[2002];
signed main(){
	fastio;
	int k;
	cin>>n>>k;
	rb(i,1,n){
		int l,r;
		cin>>l>>r>>a[i];
		t[i]=r-l+1;
		ll[i]=l;
		rr[i]=r;
	}
	rb(i,1,n){
		int needt=0;
		int last=0;
		rb(j,i,n){
			int res=(j==i? k:rest[i][j-1]);
			if(j==i){
				last=ll[i]-1;
			}
			check_max(last,(ll[i]-1));
			int needt=0;
			if(a[j]<res){
				can[i][j]=1;
				rest[i][j]=res-a[j];
				last++;
				continue;
			}
			if(a[j]==res){
				can[i][j]=1;
				rest[i][j]=k;
				last++;
				continue;
			}
			last++;
			needt++;
			int aj=a[j]-res;
			needt+=(aj+k-1)/k;
			if(needt+last>rr[i]){
				break;
			}
			last+=needt;
			can[i][j]=1;
			pre[i][j]=pre[i][j-1]+a[j];
			rest[i][j]=(aj+k-1)/k*k-aj;
			if(rest[i][j]==0) rest[i][j]=k;
		}
	}
	memset(dp,127,sizeof(dp));
	dp[0]=0;
	rb(i,1,n){
		rb(j,1,i){
			if(dp[j-1]!=INF)
				if(j==1||rr[j-1]!=ll[j])
					if(can[j][i]){
						check_min(dp[i],dp[j-1]+rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]);
//						if(i==n){
//							cout<<j<<" "<<pre[j][i]<<" "<<dp[j-1]<<' '<<can[j][i]<<endl;
//						}
					}
		}
	}
//	cout<<rest[1][1]<<endl;
	if(dp[n]==dp[n+1]) dp[n]=-1;
	cout<<dp[n]<<endl;
	return 0;
}


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
#define int LL
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
int t[2300],a[2400];
LL pre[2002][2002];//消耗
int rest[2002][2002];
bool can[2002][2002];
LL dp[2002];
int ll[2002],rr[2002];
bool used_las[2002][2002];
signed main(){
	fastio;
	int k;
	cin>>n>>k;
	rb(i,1,n){
		int l,r;
		cin>>l>>r>>a[i];
		t[i]=r-l+1;
		ll[i]=l;
		rr[i]=r;
	}
	rb(i,1,n){
		int needt=0;
		int last=0;
		rb(j,i,n){
 			int res=(j==i? k:rest[i][j-1]);
//			cout<<i<<" "<<j<<" "<<res<<endl;
			if(j==i){
				last=ll[i]-1;
			}
			check_max(last,(ll[j]-1));
			int needt=0;
			if(a[j]<res){
				check_max(last,ll[j]);
				if(last>rr[j]) break;
				used_las[i][j]=(last==rr[j]);
				can[i][j]=1;
				rest[i][j]=res-a[j];
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			if(a[j]==res){
				check_max(last,ll[j]);
				if(last>rr[j]) break;
				used_las[i][j]=(last==rr[j]);
				can[i][j]=1;
				rest[i][j]=k;
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			last++;
			int aj=a[j]-res;
			needt+=(aj+k-1)/k;
			if(needt+last>rr[j]){
				break;
			}
			used_las[i][j]=((needt+last)==rr[j]);
			last+=needt;
			can[i][j]=1;
			pre[i][j]=pre[i][j-1]+a[j];
			rest[i][j]=(aj+k-1)/k*k-aj;
			if(rest[i][j]==0) rest[i][j]=k;
		}
	}
	memset(dp,127,sizeof(dp));
	dp[0]=0;
	rb(i,1,n){
		rb(j,1,i){
			if(dp[j-1]!=dp[n+1])
				if(can[j][i]){
					if(i==n||!used_las[j][i]||ll[i+1]!=rr[i]){
//						cout<<i<<'-'<<j<<' '<<dp[j-1]<<' '<<rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]<<endl;
						check_min(dp[i],dp[j-1]+rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]);
					}
				}
		}
	}
//	rb(i,1,n){
//		cout<<(dp[i]==dp[n+1]? -1:dp[i])<<endl;
//	}
	if(dp[n]==dp[n+1]) dp[n]=-1;
	cout<<dp[n]<<endl;
	return 0;
}
/*
10 85
3 5 57
6 8 86
9 10 46
11 11 19
11 12 37
12 12 62
14 14 60
15 15 78
16 16 69
19 20 50
*/


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
#define int LL
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
int t[2300],a[2400];
LL pre[2002][2002];//消耗
int rest[2002][2002];
bool can[2002][2002];
LL dp[2002];
int ll[2002],rr[2002];
bool used_las[2002][2002];
signed main(){
	fastio;
	int k;
	cin>>n>>k;
	rb(i,1,n){
		int l,r;
		cin>>l>>r>>a[i];
		t[i]=r-l+1;
		ll[i]=l;
		rr[i]=r;
	}
	rb(i,1,n){
		int needt=0;
		int last=0;
		rb(j,i,n){
 			int res=(j==i? k:rest[i][j-1]);
//			cout<<i<<" "<<j<<" "<<res<<endl;
			if(j==i){
				last=ll[i]-1;
			}
			check_max(last,(ll[j]-1));
			int needt=0;
			if(a[j]<res){
				last++;
				if(last>rr[j]) break;
				used_las[i][j]=(last==rr[j]);
				can[i][j]=1;
				rest[i][j]=res-a[j];
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			if(a[j]==res){
				last++;
				if(last>rr[j]) break;
				used_las[i][j]=(last==rr[j]);
				can[i][j]=1;
				rest[i][j]=k;
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			last++;
			int aj=a[j]-res;
			needt+=(aj+k-1)/k;
			if(needt+last>rr[j]){
				break;
			}
			used_las[i][j]=((needt+last)==rr[j]);
			last+=needt;
			can[i][j]=1;
			pre[i][j]=pre[i][j-1]+a[j];
			rest[i][j]=(aj+k-1)/k*k-aj;
			if(rest[i][j]==0) rest[i][j]=k;
		}
	}
	memset(dp,127,sizeof(dp));
	dp[0]=0;
	rb(i,1,n){
		rb(j,1,i){
			if(dp[j-1]!=INF)
				if(can[j][i]){
					if(i==n||!used_las[j][i]||ll[i+1]!=rr[i]){
//						cout<<i<<'-'<<j<<' '<<dp[j-1]+rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]<<endl;
						check_min(dp[i],dp[j-1]+rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]);
					}
				}
		}
	}
//	rb(i,1,n){
//		cout<<(dp[i]==dp[n+1]? -1:dp[i])<<endl;
//	}
	if(dp[n]==dp[n+1]) dp[n]=-1;
	cout<<dp[n]<<endl;
	return 0;
}
/*
2 89
1 2 82
3 4 63

145
*/


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
#define int LL
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
int t[2300],a[2400];
LL pre[2002][2002];//消耗
int rest[2002][2002];
bool can[2002][2002];
LL dp[2002];
int ll[2002],rr[2002];
bool used_las[2002][2002];
signed main(){
	fastio;
	int k;
	cin>>n>>k;
	rb(i,1,n){
		int l,r;
		cin>>l>>r>>a[i];
		t[i]=r-l+1;
		ll[i]=l;
		rr[i]=r;
	}
	rb(i,1,n){
		int needt=0;
		int last=0;
		rb(j,i,n){
 			int res=(j==i? k:rest[i][j-1]);
//			cout<<i<<" "<<j<<" "<<res<<endl;
			if(j==i){
				last=ll[i]-1;
			}
			check_max(last,(ll[j]));
			int needt=0;
			if(a[j]<res){
				check_max(last,ll[j]);
				if(last>rr[j]) break;
				used_las[i][j]=((last-1)==rr[j]);
				can[i][j]=1;
				rest[i][j]=res-a[j];
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			if(a[j]==res){
				last++;
				if(last>rr[j]+1) break;
				used_las[i][j]=((last-1)==rr[j]);
				can[i][j]=1;
				rest[i][j]=k;
				pre[i][j]=pre[i][j-1]+a[j];
				continue;
			}
			last++;
			int aj=a[j]-res;
			needt+=(aj+k-1)/k;
			if(needt+last>rr[j]+1){
				break;
			}
			used_las[i][j]=((needt+last)==(rr[j]+1));
			last+=needt;
			can[i][j]=1;
			pre[i][j]=pre[i][j-1]+a[j];
			rest[i][j]=(aj+k-1)/k*k-aj;
			if(rest[i][j]==0) rest[i][j]=k;
		}
	}
	memset(dp,127,sizeof(dp));
	dp[0]=0;
	rb(i,1,n){
		rb(j,1,i){
			if(dp[j-1]!=dp[n+1])
				if(can[j][i]){
					if(i==n||!used_las[j][i]||ll[i+1]!=rr[i]){
//						cout<<i<<' '<<j<<' '<<used_las[j][i]<<'\n';
						check_min(dp[i],dp[j-1]+rest[j][i]-(i==n? rest[j][i]:0)+pre[j][i]);
					}
				}
		}
	}
//	rb(i,1,n){
//		cout<<(dp[i]==dp[n+1]? -1:dp[i])<<endl;
//	}
	if(dp[n]==dp[n+1]) dp[n]=-1;
	cout<<dp[n]<<endl;
	return 0;
}
/*
10 85
3 5 57
6 8 86
9 10 46
11 11 19
11 12 37
12 12 62
14 14 60
15 15 78
16 16 69
19 20 50
*/