#include <bits/stdc++.h>
using namespace std;
const long long INF = 1001001001001001001ll;
int size;
vector<long long> val;
vector<long long> lazy;
void lazy_propagate(int p) {
  if (lazy[p] != -1) {
    val[p] = min(val[p], lazy[p]);
    if (p < size - 1) {
      lazy[p * 2 + 1] = lazy[p];
      lazy[p * 2 + 2] = lazy[p];
    }
    lazy[p] = -1;
  }
}
void update(int wishl, int wishr, int watchl, int watchr, int k,
            long long int x) {
  if (wishr <= watchl || watchr <= wishl) {
    lazy_propagate(k);
    return;
  }
  if (wishl <= watchl && watchr <= wishr) {
    lazy[k] = x;
    lazy_propagate(k);
    return;
  }
  int mid = (watchl + watchr) / 2;
  lazy_propagate(k);
  update(wishl, wishr, watchl, mid, k * 2 + 1, x);
  update(wishl, wishr, mid, watchr, k * 2 + 2, x);
  val[k] = min(val[k * 2 + 1], val[k * 2 + 2]);
}
void update(int wishl, int wishr, long long int x) {
  update(wishl, wishr, 0, size, 0, x);
}
long long getmin(int wishl, int wishr, int watchl, int watchr, int k) {
  if (wishr <= watchl || watchr <= wishl) {
    return INF;
  }
  if (wishl <= watchl && watchr <= wishr) {
    lazy_propagate(k);
    return val[k];
  }
  int mid = (watchl + watchr) / 2;
  lazy_propagate(k);
  long long ml = getmin(wishl, wishr, watchl, mid, k * 2 + 1);
  long long mr = getmin(wishl, wishr, mid, watchr, k * 2 + 2);
  return min(ml, mr);
}
long long getmin(int wishl, int wishr) {
  return getmin(wishl, wishr, 0, size, 0);
}
int main() {
  int n, m;
  cin >> n >> m;
  size = 1;
  while (size < n) size <<= 1;
  val = vector<long long>(size * 2, INF);
  lazy = vector<long long>(size * 2, -1);
  vector<pair<pair<int, int>, long long int> > v;
  for (int i = 0; i < m; i++) {
    int l, r;
    long long int c;
    cin >> l >> r >> c;
    l--;
    v.push_back(make_pair(make_pair(r, l), c));
  }
  sort(v.begin(), v.end());
  update(n - 1, n, 0);
  for (int i = m - 1; i >= 0; i--) {
    int r = (v[i].first).first;
    int l = (v[i].first).second;
    long long int c = v[i].second;
    long long int tmp = getmin(l, r);
    update(l, r, c + tmp);
  }
  long long int ans = getmin(0, 1);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
