#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T updmax(T& a, T b) {
  return a = max(a, b);
}
template <class T>
inline T updmin(T& a, T b) {
  return a = min(a, b);
}
template <class T>
class SegmentTree {
  int size;
  T def;
  vector<T> data;
  function<T(T, T)> f;

 public:
  SegmentTree<T>(int _size, T _def, function<T(T, T)> _f)
      : f(_f), def(_def), size(1) {
    while (size < _size) size <<= 1;
    data = vector<T>(size * 2, def);
  }
  T query(int l, int r) {
    T L = def, R = def;
    for (l += size, r += size; l < r; l >>= 1, r >>= 1) {
      if (l & 1) L = f(L, data[l++]);
      if (r & 1) R = f(data[--r], R);
    }
    return f(L, R);
  }
  void update(int idx, T val) {
    idx += size;
    data[idx] = val;
    while (idx > 1) {
      idx >>= 1;
      data[idx] = f(data[2 * idx], data[2 * idx + 1]);
    }
  }
  T operator[](int idx) { return data[idx + size]; }
};
class Solution {
 public:
  void solve() {
    int n, m;
    cin >> n >> m;
    vector<tuple<int, int, long long>> vlrc(m);
    for (int i = 0; i < m; i++) {
      int l, r;
      long long c;
      cin >> l >> r >> c;
      vlrc[i] = make_tuple(l - 1, r, c);
    }
    sort(vlrc.begin(), vlrc.end());
    SegmentTree<long long> st(
        n, 0x3f3f3f3f3f3f3f3f,
        [](long long a, long long b) { return min(a, b); });
    st.update(0, 0);
    for (int i = 0; i < m; i++) {
      int l, r;
      long long c;
      tie(l, r, c) = vlrc[i];
      long long d = st.query(l, r);
      long long x = st.query(r - 1, r);
      if (d + c < x) st.update(r - 1, d + c);
    }
    if (st[n - 1] == 0x3f3f3f3f3f3f3f3f) cout << -1 << endl;
    cout << st[n - 1] << endl;
    return;
  };
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}
