#include <bits/stdc++.h>
using namespace std;
inline bool upd(int k, long long v, map<int, long long>& dp) {
  if (!dp.count(k) || v < dp[k]) {
    dp[k] = v;
    return true;
  }
  return false;
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<pair<pair<int, int>, int> > v;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    v.push_back({pair<int, int>(a, b), c});
  }
  sort(v.begin(), v.end());
  map<int, long long> dp;
  dp[1] = 0;
  int last = 1;
  int idx = 0;
  while (idx < (int)(v).size()) {
    if (last < v[idx].first.first) {
      if (dp.upper_bound(last) == dp.end()) break;
      last = dp.upper_bound(last)->first;
      continue;
    }
    assert(last < v[idx].first.second);
    upd(v[idx].first.second, dp[last] + v[idx].second, dp);
    while (dp.upper_bound(last) != dp.end()) {
      int cand = dp.upper_bound(last)->first;
      if (cand >= v[idx].first.first && cand < v[idx].first.second &&
          upd(v[idx].first.second, dp[cand] + v[idx].second, dp)) {
        last = cand;
      } else {
        break;
      }
    }
    idx++;
  }
  if (!dp.count(n))
    cout << "-1\n";
  else
    cout << dp[n] << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
