#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
class SegTree {
  int n;
  vector<T> data;
  T def;
  function<T(T, T)> operation;
  function<T(T, T)> update;
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return def;
    if (a <= l && r <= b)
      return data[k];
    else {
      T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
      T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
      return operation(c1, c2);
    }
  }

 public:
  SegTree(size_t _n, T _def, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    data = vector<T>(2 * n - 1, def);
  }
  void change(int i, T x) {
    i += n - 1;
    data[i] = update(data[i], x);
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }
  T query(int a, int b) { return _query(a, b, 0, 0, n); }
  T operator[](int i) { return data[i + n - 1]; }
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<pair<pl, ll>> e(m);
  for (ll i = 0; i < m; i++) {
    cin >> e[i].first.first >> e[i].first.second >> e[i].second;
    e[i].first.first--;
    e[i].first.second--;
  }
  sort((e).begin(), (e).end());
  SegTree<ll> st(
      n, inf, [](ll a, ll b) { return min(a, b); },
      [](ll a, ll b) { return min(a, b); });
  st.change(0, 0);
  for (ll i = 0; i < m; i++) {
    ll cost = st.query(e[i].first.first, e[i].first.second + 1);
    st.change(e[i].first.second, cost + e[i].second);
  }
  if (st[n - 1] == inf)
    cout << -1 << endl;
  else
    cout << st[n - 1] << endl;
}
