#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <tuple>
#include <set>
using namespace std;
typedef long long ll;

int main() {
    ll n, m;
    cin >> n >> m;

    vector<tuple<int, int, ll>> v;
    for (int i = 0; i < m; i++) {
        ll l, r, c;
        cin >> l >> r >> c;
        v.emplace_back(l - 1, r - 1, c);
    }

    sort(v.begin(), v.end());
    ll d[100010], INF = 1e18;
    set<ll> s;
    s.insert(0);
    s.insert(INF);
    for (int i = 1; i < n; i++) d[i] = INF;
    d[0] = 0;
    for (int i = 0; i < m; i++) {
        int k = get<0>(v[i]);
        decltype(s)::iterator l = s.lower_bound(k);
        if (*l == INF) continue;

        d[get<1>(v[i])] = min(d[get<1>(v[i])], d[*l] + get<2>(v[i]));
		s.insert(get<1>(v[i]));
        /*
        for (int j = 0; j < n; j++) cout << d[j] << " ";
      	cout << endl;*/
    }

    
    if (d[n - 1] == INF) cout << -1 << endl;
    else cout << d[n - 1] << endl;

}