#include <bits/stdc++.h>
using namespace std;
long long const INF = 1LL << 60;
struct LazySegmentTree {
 private:
  int n;
  vector<long long> node, lazy;
  vector<bool> upd;

 public:
  LazySegmentTree(vector<long long> v) {
    int sz = v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);
    upd.resize(2 * n - 1, false);
    for (int i = 0; i < sz; ++i) node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; --i)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void eval(int k, int l, int r) {
    if (upd[k]) {
      if (node[k] > lazy[k]) node[k] = lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] = lazy[2 * k + 2] = lazy[k];
        upd[2 * k + 1] = upd[2 * k + 2] = true;
      }
      lazy[k] = 0;
      upd[k] = false;
    }
  }
  void update(int a, int b, long long x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      if (upd[k]) {
        if (lazy[k] > x) lazy[k] = x;
      } else {
        lazy[k] = x;
        upd[k] = true;
      }
      eval(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  long long getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (b <= l || r <= a) return INT64_MAX;
    eval(k, l, r);
    if (a <= l && r <= b) return node[k];
    long long vl = getmin(a, b, k * 2 + 1, l, (l + r) / 2);
    long long vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
struct interval {
  int l, r, c;
  bool operator<(const interval& a) const { return l < a.l; }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<interval> a(m);
  for (int i = 0; i < m; ++i) cin >> a[i].l >> a[i].r >> a[i].c;
  LazySegmentTree seg(vector<long long>(n + 10, INF));
  seg.update(1, 2, 0);
  sort(a.begin(), a.end());
  for (int i = 0; i < m; ++i) {
    long long mn = seg.getmin(a[i].l, a[i].r + 1);
    seg.update(a[i].l, a[i].r + 1, mn + a[i].c);
  }
  long long ans = seg.getmin(n, n + 1);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
