#include <bits/stdc++.h>
using ull = unsigned long long;
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return 1;
  } else
    return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return 0;
}
void Yes(bool flag = true) {
  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void No(bool flag = true) { Yes(!flag); }
void YES(bool flag = true) {
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void NO(bool flag = true) { YES(!flag); }
bool dbg = false;
long long N, M;
struct S {
  long long L, R, C;
};
vector<S> V;
template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
  long long sz, height;
  vector<Monoid> data;
  vector<OperatorMonoid> lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;
  LazySegmentTree(long long n, const F f, const G g, const H h,
                  const Monoid& M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while (sz < n) sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }
  void set(long long k, const Monoid& x) { data[k + sz] = x; }
  void build() {
    for (long long k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }
  inline void propagate(long long k) {
    if (lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }
  inline Monoid reflect(long long k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }
  inline void recalc(long long k) {
    while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }
  inline void thrust(long long k) {
    for (long long i = height; i > 0; i--) propagate(k >> i);
  }
  void update(long long a, long long b, const OperatorMonoid& x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for (long long l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) lazy[l] = h(lazy[l], x), ++l;
      if (r & 1) --r, lazy[r] = h(lazy[r], x);
    }
    recalc(a);
    recalc(b);
  }
  Monoid query(long long a, long long b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for (long long l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) L = f(L, reflect(l++));
      if (r & 1) R = f(reflect(--r), R);
    }
    return f(L, R);
  }
  Monoid operator[](const long long& k) { return query(k, k + 1); }
  template <typename C>
  long long find_subtree(long long a, const C& check, Monoid& M, bool type) {
    while (a < sz) {
      propagate(a);
      Monoid nxt =
          type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
      if (check(nxt))
        a = 2 * a + type;
      else
        M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }
  template <typename C>
  long long find_first(long long a, const C& check) {
    Monoid L = M1;
    if (a <= 0) {
      if (check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
      return -1;
    }
    thrust(a + sz);
    long long b = sz;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1) {
        Monoid nxt = f(L, reflect(a));
        if (check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }
  template <typename C>
  long long find_last(long long b, const C& check) {
    Monoid R = M1;
    if (b >= sz) {
      if (check(f(reflect(1), R))) return find_subtree(1, check, R, true);
      return -1;
    }
    thrust(b + sz - 1);
    long long a = sz;
    for (b += sz; a < b; a >>= 1, b >>= 1) {
      if (b & 1) {
        Monoid nxt = f(reflect(--b), R);
        if (check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};
void solve() {
  sort((V).begin(), (V).end(), [](S a, S b) { return a.L < b.L; });
  LazySegmentTree<long long, long long> seg(
      N + 10, [](long long a, long long b) { return min(a, b); },
      [](long long a, long long x) { return min(a, x); },
      [](long long x, long long y) { return min(x, y); }, INF, INF);
  seg.build();
  seg.update(1, 2, 0);
  for (long long i = 0; i < (M); ++i) {
    S s = V[i];
    seg.update(s.L, s.R + 1, seg.query(s.L, s.R + 1) + s.C);
  }
  if (seg.query(N, N + 1) != INF)
    cout << seg.query(N, N + 1) << endl;
  else
    cout << -1 << endl;
}
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (long long i = 0; i < (M); ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    V.emplace_back(S{l, r, c});
  }
  solve();
  return 0;
}
