#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> graph[n + 1];
    for (int l, r, c; cin >> l >> r >> c; ) {
        graph[l].emplace_back(r, c);
    }
    unsigned long long dist[n + 1];
    for (int i = 1; i <= n; i++) {
        graph[i].emplace_back(i - 1, 0);
        dist[i] = -1;
    }
    dist[1] = 0;
    set<pair<unsigned long long, int>> set;
    set.emplace(0, 1);
    do {
        auto current = set.begin()->second;
        set.erase(set.begin());
        for (auto& p : graph[current]) {
            if (dist[p.first] > dist[current] + p.second) {
                set.erase({dist[p.first], p.first});
                dist[p.first] = dist[current] + p.second;
                set.emplace(dist[p.first], p.first);
            }
        }
    } while (!set.empty());
    cout << (long long)dist[n];
    return 0;
}
