#include <bits/stdc++.h>
using namespace std;
template <typename T, T F(T, T), T INIT>
struct SegmentTree {
  T n;
  vector<T> v;
  SegmentTree(T n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    v.resize(2 * n - 1);
    fill(v.begin(), v.end(), INIT);
  }
  void update(long long k, T a) {
    k += n - 1;
    v[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      v[k] = F(v[k * 2 + 1], v[k * 2 + 2]);
    }
  }
  T query(long long a, long long b) const {
    return query_internal(a, b, 0, 0, n);
  }
  T get(long long a) const { return query(a, a + 1); }
  T query_internal(long long a, long long b, long long k, long long l,
                   long long r) const {
    if (r <= a || b <= l) {
      return INIT;
    }
    if (a <= l && r <= b) {
      return v[k];
    } else {
      T v1 = query_internal(a, b, k * 2 + 1, l, (l + r) / 2);
      T v2 = query_internal(a, b, k * 2 + 2, (l + r) / 2, r);
      return F(v1, v2);
    }
  }
};
long long op(long long a, long long b) { return min(a, b); }
int main() {
  long long n, m;
  cin >> n >> m;
  vector<array<long long, 3>> lrc(m);
  for (long long i = 0; i < (m); ++i) {
    cin >> lrc[i][0] >> lrc[i][1] >> lrc[i][2];
    lrc[i][0]--;
    lrc[i][1]--;
  }
  sort(lrc.begin(), lrc.end());
  SegmentTree<long long, op, numeric_limits<long long>::max()> st(n);
  st.update(0, 0);
  for (long long i = 0; i < (m); ++i) {
    long long mval = st.query(lrc[i][0], lrc[i][1]);
    if (mval == numeric_limits<long long>::max()) {
      cout << -1 << endl;
      return 0;
    }
    long long newval = mval + lrc[i][2];
    st.update(lrc[i][1], op(newval, st.get(lrc[i][1])));
  }
  if (st.get(n - 1) == numeric_limits<long long>::max()) {
    cout << -1 << endl;
  } else {
    cout << st.get(n - 1) << endl;
  }
  return 0;
}
