#include<algorithm>
#include<cstdio>
#include<queue>
#include<utility>
#include<vector>
using namespace std;
typedef pair<long, int> P;

const int N_MAX = 1e5;
int n, m;
vector<pair<int, int>> v[N_MAX];
priority_queue<P, vector<P>, greater<P>> q;

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        int l, r, c;
        scanf("%d %d %d", &l, &r, &c);
        v[l - 1].emplace_back(r - 1, c);
    }
    
    long ans = 1e18;
    q.emplace(0, 0);
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0, e = v[i].size(); j < e; j++) {
            while (!q.empty() && q.top().second < i) q.pop();
            if (q.empty()) {
                printf("-1\n");
                return 0;
            }
            if (v[i][j].first == n - 1) {
                ans = min(ans, q.top().first + v[i][j].second);
            }
            else {
                q.emplace(q.top().first + v[i][j].second, v[i][j].first);
            }
        }
    }

    printf("%ld\n", (ans == 1e18) ? -1 : ans);
    return 0;
}
