#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18;
struct LazySegmentTree {
  int n;
  vector<ll> node, lazy;
  vector<bool> lazyFlag;
  LazySegmentTree(vector<ll> 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] = min(node[k], lazy[k]);
      if (r - l > 1) {
        lazy[k * 2 + 1] = min(lazy[k * 2 + 1], lazy[k]);
        lazy[k * 2 + 2] = min(lazy[k * 2 + 2], lazy[k]);
        lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
      }
      lazyFlag[k] = false;
    }
  }
  void update(int a, int b, ll 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] = 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[2 * k + 1], node[2 * k + 2]);
    }
  }
  ll 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 1e18;
    if (a <= l && r <= b) return node[k];
    ll vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<tuple<ll, ll, ll>> lrc(m);
  for (int i = (0); i < (m); i++)
    cin >> get<0>(lrc[i]) >> get<1>(lrc[i]) >> get<2>(lrc[i]);
  sort(lrc.begin(), lrc.end());
  vector<ll> z(n + 1, 1e16);
  z[0] = z[1] = 0;
  LazySegmentTree seg(z);
  for (int i = (0); i < (m); i++) {
    int l = get<0>(lrc[i]), r = get<1>(lrc[i]);
    ll x = get<2>(lrc[i]);
    x += seg.find(l, l + 1);
    seg.update(l + 1, r + 1, x);
  }
  ll ans = seg.find(n, n + 1);
  if (ans < 1e18)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
