#include <bits/stdc++.h>
using namespace std;
struct E {
  int l, r;
  long long c;
};
bool cmpL(const E& e1, const E& e2) { return e1.l < e2.l; }
struct K {
  int l, r;
  long long c;
};
bool operator>(const K& e1, const K& e2) { return e1.c > e2.c; }
E es[100000 + 10];
long long dist[100000 + 10];
const long long INF = 1e18;
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(m); ++i) {
    scanf("%d%d%lld", &es[i].l, &es[i].r, &es[i].c);
    es[i].l--;
    es[i].r--;
  }
  for (int i = 0; i < (int)(n); ++i) {
    dist[i] = INF;
  }
  sort(es, es + m, cmpL);
  priority_queue<K, vector<K>, greater<K> > q;
  int ii = 0;
  for (; ii < m && es[ii].l == 0; ++ii) {
    q.push((K){es[ii].l, es[ii].r, es[ii].c});
  }
  dist[0] = 0;
  int iii = 1;
  while (!q.empty()) {
    K cur = q.top();
    q.pop();
    if (cur.r < iii) {
      continue;
    }
    while (iii <= cur.r) {
      dist[iii] = dist[cur.l] + cur.c;
      ++iii;
    }
    for (; ii < m && es[ii].l < iii; ++ii) {
      q.push((K){es[ii].l, es[ii].r, es[ii].c});
    }
  }
  cout << (dist[n - 1] == INF ? -1 : dist[n - 1]) << endl;
  return 0;
}
