#include <bits/stdc++.h>
using namespace std;
long long int inf = 1e12;
typedef struct ST {
  vector<long long int> a;
  int n;
  void init(vector<long long int> &b) {
    int size = b.size();
    n = 1;
    while (n < size) n *= 2;
    a = vector<long long int>(2 * n - 1, inf);
    for (int i = 0; i < size; i++) a[i + n - 1] = b[i];
    for (int i = n - 2; i >= 0; i--) a[i] = min(a[i * 2 + 1], a[i * 2 + 2]);
  }
  void update(int k, long long int val) {
    k += n - 1;
    a[k] = val;
    while (k > 0) {
      k = (k - 1) / 2;
      a[k] = min(a[k * 2 + 1], a[k * 2 + 2]);
    }
  }
  long long int get(int aa, int b, int k, int l, int r) {
    if (aa <= l && r <= b) return a[k];
    if (r <= aa || b <= l) return inf;
    long long int vl = get(aa, b, k * 2 + 1, l, (l + r) / 2);
    long long int vr = get(aa, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  long long int find(int aa, int b) { return get(aa, b, 0, 0, n); }
} ST;
int main() {
  int n, m;
  cin >> n >> m;
  vector<long long int> dp(n + 1, inf);
  vector<vector<pair<long long int, long long int> > > g(n + 1);
  vector<vector<pair<long long int, long long int> > > rg(n + 1);
  for (int i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    g[l].push_back(pair<long long int, long long int>(r, c));
    rg[r].push_back(pair<long long int, long long int>(l, c));
  }
  dp[0] = dp[1] = 0;
  ST tree;
  tree.init(dp);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < rg[i].size(); j++) {
      long long int mini = tree.find(rg[i][j].first, i);
      tree.update(i, min(tree.find(i, i + 1), mini + rg[i][j].second));
    }
  }
  long long int res = tree.find(n, n + 1);
  if (res == inf) {
    cout << -1 << endl;
  } else
    cout << res << endl;
  return 0;
}
