#include <iostream>
#include <utility>
#include <algorithm>
#include <queue>
#include <vector>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pll pair<long long, long long>
#define ff first
#define ss second

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m; cin >> n >> m;
    piii inp[m]; for(auto &i : inp) {cin >> i.ff.ff >> i.ff.ss >> i.ss; --i.ff.ff;}
    sort(inp, inp + m);

    priority_queue<pll, vector<pll>, greater<pll>> Q;
    Q.push({0, 1});
    for(auto &i : inp)
    {
        while(!Q.empty() && Q.top().ss <= i.ff.ff) Q.pop();
        if(Q.empty()) {cout << -1; return 0;}
        Q.push({Q.top().ff + i.ss, i.ff.ss});
    }

    while(!Q.empty() && Q.top().ss < n) Q.pop();
    if(Q.empty()) {cout << -1; return 0;}
    cout << Q.top().ff;
    return 0;
}
