#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int,int>>> q(n);
  for (int i = 0; i < m; i++) {
    int l, r, c;
    cin >> l >> r >> c;
    q[r-1].emplace_back(l-1,c);
  }
  ll B = 1;
  while (B < n) B *= 2;
  vector<ll> mi(B*2, 1e18);
  for (int i = 0; i < n; i++) {
    ll v = 1e18;
    if (i == 0) v = 0;
    for (auto p : q[i]) {
      int l = p.first, c = p.second;
      ll val = mi[l+=B];
      for (; l; l >>= 1) {
	if (l%2 == 0)
	  val = min(val, mi[l+1]);
      }
      v = min(v, val+c);
    }
    mi[i+B] = v;
    for (int j = (i+B)/2; j; j >>= 1) {
      mi[j] = min(mi[j*2], mi[j*2+1]);
    }
  }
  ll ans = mi[B+n-1];
  if (ans >= 1e17) cout << -1 << endl;
  else cout << ans << endl;
}
