#include <bits/stdc++.h>
using namespace std;
const long long maxV = 114514;
struct edge {
  long long end;
  long long cost;
};
long long V;
vector<long long> d;
vector<edge> G[maxV];
void dijkstra(long long s) {
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      que;
  for (long long i = 0; i < maxV; i++) {
    d.push_back(1145141919810893364);
  }
  d[s] = 0;
  que.push(pair<long long, long long>(0, s));
  while (!que.empty()) {
    pair<long long, long long> p = que.top();
    que.pop();
    long long v = p.second;
    if (d[v] < p.first) continue;
    for (long long i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.end] > d[v] + e.cost) {
        d[e.end] = d[v] + e.cost;
        que.push(pair<long long, long long>(d[e.end], e.end));
      }
    }
  }
}
signed main(void) {
  long long N, M;
  long long L[114514], R[114514], C[114514];
  long long imos[114514] = {};
  cin >> N >> M;
  for (long long i = 0; i < (N); ++i) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
    R[i]--;
  }
  edge h;
  for (long long i = 0; i < (M); ++i) {
    h.end = R[i];
    h.cost = C[i];
    G[L[i]].push_back(h);
    imos[L[i]] += 1;
    imos[R[i]] -= 1;
  }
  for (long long i = 0; i < (N); ++i) {
    imos[i + 1] += imos[i];
  }
  for (long long i = 0; i < (N); ++i) {
    if (imos[i] > 0) {
      h.end = i;
      h.cost = 0;
      G[i + 1].push_back(h);
    }
  }
  dijkstra(0);
  long long Ans = d[N - 1];
  if (Ans == 1145141919810893364) Ans = -1;
  cout << Ans << endl;
  return 0;
}
