#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
ll INF = 1e9;
const int MAX = 2000000;
class SGT {
 public:
  int n;
  ll d = INF;
  ll num[MAX];
  void init(vector<ll> &a) {
    n = 1;
    while (a.size() > n) n *= 2;
    for (int i = 0; i < n; i++) {
      if (i < a.size())
        num[n - 1 + i] = a.at(i);
      else
        num[n - 1 + i] = d;
    }
    for (int i = n - 2; i >= 0; i--) {
      num[i] = min(num[i * 2 + 1], num[i * 2 + 2]);
    }
  }
  void update(int i, ll a) {
    i += n - 1;
    num[i] = a;
    while (i > 0) {
      i = (i - 1) / 2;
      num[i] = min(num[i * 2 + 1], num[i * 2 + 2]);
    }
  }
  ll query(int a, int b, int l, int r, int now) {
    if (r <= a || b <= l) return d;
    if (a <= l && r <= b) return num[now];
    ll r1 = query(a, b, l, (l + r) / 2, now * 2 + 1);
    ll r2 = query(a, b, (l + r) / 2, r, now * 2 + 2);
    return min(r1, r2);
  }
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<pair<pair<ll, ll>, ll>> q(m);
  for (int i = 0; i < m; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    q.at(i).first.first = r;
    q.at(i).first.second = l;
    q.at(i).second = c;
  }
  sort(q.begin(), q.end());
  static SGT sgt;
  vll def(n, INF);
  def.at(0) = 0;
  sgt.init(def);
  int pos = 0;
  for (int i = 1; i < n; i++) {
    ll mn = INF;
    while (pos < m && q.at(pos).first.first == i) {
      mn = min(mn, sgt.query(q.at(pos).first.second, q.at(pos).first.first + 1,
                             0, sgt.n, 0) +
                       q.at(pos).second);
      pos++;
    }
    sgt.update(i, mn);
  }
  cout << (sgt.num[n - 1 + sgt.n - 1] == INF ? -1 : sgt.num[n - 1 + sgt.n - 1])
       << endl;
}
