#include <bits/stdc++.h>
using namespace std;
long long tt[(1 << 17) + (1 << 17)];
long long mymin(int l, int r) {
  long long ret = (1LL << 61);
  l += (1 << 17);
  r += (1 << 17);
  while (l <= r) {
    ret = min(ret, tt[l]);
    ret = min(ret, tt[r]);
    l++;
    l >>= 1;
    r--;
    r >>= 1;
  }
  return ret;
}
void myup(int pos, long long val) {
  pos += (1 << 17);
  while (pos) {
    tt[pos] = min(tt[pos], val);
    pos >>= 1;
  }
}
struct edge {
  int l, r, c;
  edge(int tl = 0, int tr = 0, int tc = 0) {
    l = tl;
    r = tr;
    c = tc;
  }
  void read() {
    cin >> l >> r >> c;
    l--;
    r--;
  }
  bool operator<(const edge& e) const { return (l < e.l); }
  void print() { cerr << l << " " << r << " " << c << endl; }
};
int n, m;
edge a[(400410)];
long long ans[(400410)];
int main() {
  for (int i = 0; i < (400410); i++) {
    tt[i] = (1LL << 61);
    ans[i] = (1LL << 61);
  }
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    a[i].read();
  }
  sort(a, a + m);
  ans[0] = 0;
  myup(0, 0);
  int curedge = 0;
  for (int i = 0; i < n; i++) {
    long long& curdist = ans[i];
    curdist = mymin(i, n - 1);
    if ((curedge >= m) || (a[curedge].l > i)) {
      continue;
    }
    while ((curedge < m) && (a[curedge].l == i)) {
      myup(a[curedge].r, curdist + a[curedge].c);
      curedge++;
    }
  }
  long long ret = ans[n - 1];
  if (ret == (1LL << 61)) {
    cout << -1 << endl;
  } else {
    cout << ret << endl;
  }
  return 0;
}
