#include <bits/stdc++.h>
using namespace std;
long long n;
struct ST {
  long long a[400010], lazy[400010];
  void build(long long n, long long l, long long r) {
    long long lc = n * 2, rc = n * 2 + 1, m = (l + r) / 2;
    lazy[n] = 100010;
    if (l == r) {
      a[n] = 100010;
      return;
    }
    build(lc, l, m);
    build(rc, m + 1, r);
    a[n] = min(a[lc], a[rc]);
  }
  void down(long long n, long long l, long long r) {
    long long lc = n * 2, rc = n * 2 + 1, m = (l + r) / 2;
    if (l < r) {
      lazy[lc] = min(lazy[lc], lazy[n]);
      lazy[rc] = min(lazy[rc], lazy[n]);
    }
    a[n] = min(a[n], lazy[n]);
    lazy[n] = 100010;
  }
  void update(long long n, long long l, long long r, long long x, long long y,
              long long del) {
    long long lc = n * 2, rc = n * 2 + 1, m = (l + r) / 2;
    x = max(x, l);
    y = min(y, r);
    if (x > y) {
      down(n, l, r);
      return;
    }
    if (x == l && y == r) {
      lazy[n] = min(lazy[n], del);
      down(n, l, r);
      return;
    }
    down(n, l, r);
    update(lc, l, m, x, y, del);
    update(rc, m + 1, r, x, y, del);
    a[n] = min(a[lc], a[rc]);
  }
  long long query(long long n, long long l, long long r, long long x,
                  long long y) {
    long long lc = n * 2, rc = n * 2 + 1, m = (l + r) / 2;
    x = max(x, l);
    y = min(y, r);
    if (x > y) return 100010;
    down(n, l, r);
    if (x == l && y == r) return a[n];
    return min(query(lc, l, m, x, y), query(rc, m + 1, r, x, y));
  }
} st;
struct node {
  long long l, r, w;
  bool operator<(node B) const { return l < B.l; }
};
node a[100010];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long m;
  cin >> n >> m;
  for (long long i = (0), i__end = (m); i < i__end; i++)
    cin >> a[i].l >> a[i].r >> a[i].w;
  sort(a, a + m);
  st.build(1, 1, n);
  st.update(1, 1, n, 1, 1, 0);
  for (long long i = (0), i__end = (m); i < i__end; i++) {
    long long s = st.query(1, 1, n, a[i].l, a[i].l);
    st.update(1, 1, n, 1, a[i].r, s + a[i].w);
  }
  long long s = st.query(1, 1, n, n, n);
  if (s == 100010)
    cout << -1 << endl;
  else
    cout << s << endl;
  return 0;
}
