#include <bits/stdc++.h>
using namespace std;
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());
  priority_queue<pair<int, int> > q;
  map<int, long long> dp;
  q.push({0, 1});
  int lastSeen = 0;
  int idx = 0;
  while (!q.empty()) {
    pair<int, int> curr = q.top();
    q.pop();
    if (curr.second <= lastSeen || dp[curr.second] != -curr.first) continue;
    lastSeen = curr.second;
    while (idx < (int)(v).size() && v[idx].first.first <= curr.second) {
      long long candD = dp[curr.second] + v[idx].second;
      if (!dp.count(v[idx].first.second) || candD < dp[v[idx].first.second]) {
        dp[v[idx].first.second] = candD;
        q.push({-candD, v[idx].first.second});
      }
      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();
}
