#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<ll, ll>;
template <class T>
inline bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T>
inline bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
using T = tuple<ll, int, int>;
const ll inf = 1e18;
void answer() {
  int n, m;
  cin >> n >> m;
  vector<set<T> > mp(n + 1);
  for (int i = (int)(0); i < (int)((n)); i++) {
    int l, r, c;
    cin >> l >> r >> c;
    mp[l].emplace(c, l, r);
  }
  vector<ll> dist(n + 1, inf);
  int cur = 0;
  dist[cur] = 0;
  set<T> s;
  s.emplace(0, 0, 1);
  while (!s.empty()) {
    auto [c, l, r] = *(s.begin());
    s.erase(s.begin());
    for (int i = (int)(cur + 1); i <= (int)(r); i++) {
      chmin(dist[i], c);
      for (auto [c2, l2, r2] : mp[i]) {
        s.emplace(dist[l2] + c2, l2, r2);
      }
    }
    chmax(cur, r);
  }
  ll ans = dist[n];
  if (ans == inf) ans = -1;
  cout << ans << '\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}
