#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
class segtree {
 public:
  int depth;
  int size;
  vector<long long> node;
  segtree(int n) {
    depth = 0;
    size = 1;
    while (n > size) {
      size *= 2;
      depth++;
    }
    size *= 2;
    node.resize(size, 1e14 - 1);
  }
  void update(int k, long long a) {
    k += (1 << depth);
    node[k] = a;
    while (k > 1) {
      k /= 2;
      node[k] = min(node[k * 2], node[k * 2 + 1]);
    }
    return;
  }
  long long get(int a, int b) {
    a += (1 << depth);
    b += (1 << depth);
    if (a == b) return node[a];
    long long nowa = node[a];
    long long nowb = node[b];
    while (a / 2 < b / 2) {
      if ((a & 1) == 0) nowa = min(nowa, node[a + 1]);
      if ((b & 1) == 1) nowb = min(nowb, node[b - 1]);
      a /= 2;
      b /= 2;
    }
    long long ret = min(nowa, nowb);
    return ret;
  }
};
pair<pair<int, int>, long long> p[100000];
int main() {
  int n, m;
  long long ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    l--;
    r--;
    p[i] = pair<pair<int, int>, long long>(make_pair(l, r), c);
  }
  sort(p, p + m);
  segtree t = segtree(n);
  t.update(0, 0);
  for (int i = 0; i < m; i++) {
    long long s = t.get(p[i].first.first, p[i].first.second);
    if (s == 1e14 - 1) continue;
    t.update(p[i].first.second, s + p[i].second);
  }
  ans = t.get(n - 1, n - 1);
  if (ans == 1e14 - 1) ans = -1;
  cout << ans << endl;
  return 0;
}
