#include <bits/stdc++.h>
using namespace std;
long long int INF = 3e18;
const long long fact_table = 5000000;
double Pi = 3.1415926535897932384626;
vector<pair<long long, long long> > tree[500010];
priority_queue<long long> pql;
priority_queue<pair<long long, long long> > pqp;
priority_queue<long long, vector<long long>, greater<long long> > pqls;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    pqps;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
char dir[] = "DRUL";
long long mod = 998244353;
long long rui(long long number1, long long number2) {
  if (number2 == 0) {
    return 1;
  } else {
    long long number3 = rui(number1, number2 / 2);
    number3 *= number3;
    number3 %= mod;
    if (number2 % 2 == 1) {
      number3 *= number1;
      number3 %= mod;
    }
    return number3;
  }
}
long long gcd(long long number1, long long number2) {
  if (number1 > number2) {
    swap(number1, number2);
  }
  if (number1 == 0 || number1 == number2) {
    return number2;
  } else {
    return gcd(number2 % number1, number1);
  }
}
void YES(bool condition) {
  if (condition) {
    cout << "YES"
         << "\n";
    ;
  } else {
    cout << "NO"
         << "\n";
    ;
  }
  return;
}
void Yes(bool condition) {
  if (condition) {
    cout << "Yes"
         << "\n";
    ;
  } else {
    cout << "No"
         << "\n";
    ;
  }
  return;
}
long long n, m, num, sum, a, b, c, d, e, g, h, w, i, j, q, r, l;
long long k, ans;
long long x[500005], y[500005], z[500005];
char s[500005];
long long idx[500005];
pair<long long, long long> table[500005];
struct LazySegmentTree {
 private:
  int n;
  vector<long long> node, lazy;
  vector<bool> lazyFlag;

 public:
  LazySegmentTree(vector<long long> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, INF);
    lazyFlag.resize(2 * n - 1, false);
    for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void lazyEvaluate(int k, int l, int r) {
    if (lazyFlag[k]) {
      node[k] = lazy[k];
      if (r - l > 1) {
        lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
        lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
      }
      lazyFlag[k] = false;
    }
  }
  void update(int a, int b, long long x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] = min(lazy[k], x);
      lazyFlag[k] = true;
      lazyEvaluate(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = min(node[k], min(node[2 * k + 1], node[2 * k + 2]));
    }
  }
  long long find(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a) return INF;
    if (a <= l && r <= b) return node[k];
    long long vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
    long long vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    table[i].first = b;
    table[i].second = a * 200000 + c;
  }
  sort(table, table + m);
  for (int i = 0; i < m; i++) {
    x[i] = table[i].second / 200000;
    y[i] = table[i].first;
    z[i] = table[i].second % 200000;
  }
  for (int i = 0; i < m; i++) {
  }
  LazySegmentTree seg(vector<long long>(n + 3, INF));
  seg.update(1ll, 2ll, 0ll);
  for (int i = 0; i < m; i++) {
    long long res = seg.find(x[i], y[i] + 1);
    seg.update(x[i], y[i] + 1, res + z[i]);
  }
  ans = seg.find(n, n + 1);
  if (ans == INF) {
    cout << -1 << "\n";
    ;
    return 0;
  }
  cout << ans << "\n";
  ;
  return 0;
}
