#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define N 100003

class SegmentTree {
  const ll inf = 1e18;
  int n, n0;
  ll max_v[4*N], smax_v[4*N], max_c[4*N];

  // 最大値の差分を、pushdownの最大値の差分ではなく一つのlazy tagとして持つバージョン
  ll lazy_a[4*N], lazy_b[4*N];
  ll sum_a[4*N], sum_b[4*N];
  ll len[4*N];

  void addall_max(int k, ll v) {
    sum_a[k] += v * max_c[k];
    max_v[k] += v;
    lazy_a[k] += v;
  }

  void addall_nmax(int k, ll v) {
    sum_b[k] += v * (len[k] - max_c[k]);
    smax_v[k] += v;
    lazy_b[k] += v;
  }

  void push(int k) {
    if(max_v[2*k+1] < max_v[2*k+2]) {
      addall_max(2*k+1, lazy_b[k]);
      addall_max(2*k+2, lazy_a[k]);
    } else if(max_v[2*k+1] > max_v[2*k+2]) {
      addall_max(2*k+1, lazy_a[k]);
      addall_max(2*k+2, lazy_b[k]);
    } else {
      addall_max(2*k+1, lazy_a[k]);
      addall_max(2*k+2, lazy_a[k]);
    }
    addall_nmax(2*k+1, lazy_b[k]);
    addall_nmax(2*k+2, lazy_b[k]);

    lazy_a[k] = lazy_b[k] = 0;
  }

  void update(int k) {
    sum_b[k] = sum_b[2*k+1] + sum_b[2*k+2];

    if(max_v[2*k+1] < max_v[2*k+2]) {
      max_v[k] = max_v[2*k+2];
      max_c[k] = max_c[2*k+2];
      smax_v[k] = max(max_v[2*k+1], smax_v[2*k+2]);

      sum_a[k] = sum_a[2*k+2];
      sum_b[k] += sum_a[2*k+1];
    } else if(max_v[2*k+1] > max_v[2*k+2]) {
      max_v[k] = max_v[2*k+1];
      max_c[k] = max_c[2*k+1];
      smax_v[k] = max(smax_v[2*k+1], max_v[2*k+2]);

      sum_a[k] = sum_a[2*k+1];
      sum_b[k] += sum_a[2*k+2];
    } else {
      max_v[k] = max_v[2*k+1];
      max_c[k] = max_c[2*k+1] + max_c[2*k+2];
      smax_v[k] = max(smax_v[2*k+1], smax_v[2*k+2]);

      sum_a[k] = sum_a[2*k+1] + sum_a[2*k+2];
    }
  }

  void _update_min(ll x, int a, int b, int k, int l, int r) {
    if(b <= l || r <= a || max_v[k] <= x) {
      return;
    }
    if(a <= l && r <= b && smax_v[k] < x) {
      addall_max(k, x - max_v[k]);
      max_v[k] = x;
      return;
    }

    push(k);
    _update_min(x, a, b, 2*k+1, l, (l+r)/2);
    _update_min(x, a, b, 2*k+2, (l+r)/2, r);
    update(k);
  }

  ll _query_max(int a, int b, int k, int l, int r) {
    if(b <= l || r <= a) {
      return 0;
    }
    if(a <= l && r <= b) {
      return max_v[k];
    }
    push(k);
    ll lv = _query_max(a, b, 2*k+1, l, (l+r)/2);
    ll rv = _query_max(a, b, 2*k+2, (l+r)/2, r);
    return max(lv, rv);
  }

  ll _query_sum(int a, int b, int k, int l, int r) {
    if(b <= l || r <= a) {
      return 0;
    }
    if(a <= l && r <= b) {
      return sum_a[k] + sum_b[k];
    }
    push(k);
    ll lv = _query_sum(a, b, 2*k+1, l, (l+r)/2);
    ll rv = _query_sum(a, b, 2*k+2, (l+r)/2, r);
    return lv + rv;
  }
  void _add_val(ll x, int a, int b, int k, int l, int r) {
    if(b <= l || r <= a) {
      return;
    }
    if(a <= l && r <= b) {
      addall_max(k, x);
      addall_nmax(k, x);
      return;
    }
    push(k);
    _add_val(x, a, b, 2*k+1, l, (l+r)/2);
    _add_val(x, a, b, 2*k+2, (l+r)/2, r);
    update(k);
  }

public:
  SegmentTree(int n) {
    SegmentTree(n, nullptr);
  }

  SegmentTree(int n, ll *a) : n(n) {
    n0 = 1;
    while(n0 < n) n0 <<= 1;

    len[0] = n0;
    for(int i=0; i<n0-1; ++i) len[2*i+1] = len[2*i+2] = (len[i] >> 1);
    for(int i=0; i<2*n0-1; ++i) lazy_a[i] = lazy_b[i] = 0;

    for(int i=0; i<n; ++i) {
      max_v[n0-1+i] = sum_a[n0-1+i] = (a != nullptr ? a[i] : 0);
      sum_b[n0-1+i] = 0;
      smax_v[n0-1+i] = -inf;
      max_c[n0-1+i] = 1;
    }

    for(int i=n; i<n0; ++i) {
      max_v[n0-1+i] = smax_v[n0-1+i] = -inf;
      sum_a[n0-1+i] = sum_b[n0-1+i] = max_c[n0-1+i] = 0;
    }
    for(int i=n0-2; i>=0; i--) update(i);
  }

  void update_min(int a, int b, ll x) {
    return _update_min(x, a, b, 0, 0, n0);
  }

  ll query_max(int a, int b) {
    return _query_max(a, b, 0, 0, n0);
  }

  ll query_sum(int a, int b) {
    return _query_sum(a, b, 0, 0, n0);
  }

  void add_val(int a, int b, ll x) {
    _add_val(x, a, b, 0, 0, n0);
  }
};
ll a[N];
int main()
{
    int n, m;
    cin>>n>>m;
    vector<P> v[100010];
    for(int i=0; i<m; i++){
        int l, r;
        ll c;
        cin>>l>>r>>c;
        l--; r--;
        v[l].push_back({r, c});
    }
    const ll INF=1e18;
    fill(a, a+N, INF);
    SegmentTree seg(n, a);
    seg.update_min(0, 1, 0);
    for(int i=0; i<n-1; i++){
        ll d=seg.query_max(i, i+1);
        for(auto p:v[i]){
            seg.update_min(i+1, p.first+1, d+p.second);
        }
    }
    ll ans=seg.query_max(n-1, n);
    if(ans>=INF/2){
        cout<<-1<<endl;
    }else{
        cout<<ans<<endl;
    }
	return 0;
}

Measured memory_bytes: 30848