#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
T gcd(T a, T b) {
  if (a < b) std::swap(a, b);
  while (b) std::swap(a %= b, b);
  return a;
}
template <class T>
T lcm(const T a, const T b) {
  return a / gcd(a, b) * b;
}
using ll = long signed long;
using ull = long unsigned long;
using cll = long long const;
using ld = long double;
using namespace std;
class segtree {
  using T = ll;
  using U = ll;
  const T IT = 0x7FFFFFFFFFFFFFFFLL;
  const U IU = 0x7FFFFFFFFFFFFFFFLL;
  T join_t(T x, T y) const { return x < y ? x : y; }
  T apply_u(T x, U y) const { return x < y ? x : y; }
  U join_u(U x, U y) const { return x < y ? x : y; }
  U multiply_u(U x, ll n) const { return x; }
  U half_u(U x) const { return x; }
  ll n;
  vector<T> node;
  vector<U> lazy;
  vector<bool> fall;
  inline void eval(ll k, ll l, ll r) {
    if (!fall[k]) return;
    node[k] = apply_u(node[k], lazy[k]);
    if (r - l > 1) {
      lazy[k * 2 + 1] = join_u(lazy[k * 2 + 1], half_u(lazy[k]));
      lazy[k * 2 + 2] = join_u(lazy[k * 2 + 2], half_u(lazy[k]));
      fall[k * 2 + 1] = fall[k * 2 + 2] = true;
    }
    lazy[k] = IU;
    fall[k] = false;
  }

 public:
  segtree(const vector<T> &v) {
    ll sz = (ll)v.size();
    n = 1;
    while (n < sz) n *= 2;
    this->node = vector<T>(n * 2 - 1, IT);
    this->lazy = vector<T>(n * 2 - 1, IU);
    this->fall = vector<bool>(n * 2 - 1, false);
    for (ll i = 0; i < sz; i++) node[n - 1 + i] = v[i];
    for (ll i = n - 1; i--;) node[i] = join_t(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void apply(ll a, ll b, U x, ll k = 0, ll l = 0, ll r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] = join_u(lazy[k], multiply_u(x, r - l));
      fall[k] = true;
      eval(k, l, r);
    } else {
      apply(a, b, x, k * 2 + 1, l, (l + r) / 2);
      apply(a, b, x, k * 2 + 2, (l + r) / 2, r);
      node[k] = join_t(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  T fetch(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) {
    if (r < 0) r = n;
    if (b <= l || r <= a) return IT;
    eval(k, l, r);
    if (a <= l && r <= b) return node[k];
    T vl = fetch(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = fetch(a, b, k * 2 + 2, (l + r) / 2, r);
    return join_t(vl, vr);
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, m;
  cin >> n >> m;
  vector<ll> dp(n, 0x7FFFFFFFFFFFFFFFLL);
  dp[0] = 0;
  segtree sgt(dp);
  vector<tuple<ll, ll, ll>> e(m);
  for (long long i = 0, i_len = (m); i < i_len; i++) {
    ll li, ri, ci;
    cin >> li >> ri >> ci;
    li--;
    ri--;
    e[i] = make_tuple(li, ri, ci);
  }
  sort((e).begin(), (e).end());
  for (long long i = 0, i_len = (m); i < i_len; i++) {
    ll l, r, c;
    tie(l, r, c) = e[i];
    ll x = sgt.fetch(l, r + 1);
    sgt.apply(l, r + 1, x + c);
  }
  ll ans = sgt.fetch(n - 1, n);
  cout << (ans == 0x7FFFFFFFFFFFFFFFLL ? -1 : ans) << endl;
}
