#include <bits/stdc++.h>
using namespace std;

const int N = 100005;
const long long INF = 1e18;

int n, m;
int l[N], r[N], c[N];
vector <int> lef[N], rig[N];
long long smallest[N];
long long dis[N];

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

    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        cin >> l[i] >> r[i] >> c[i];
        lef[l[i]].push_back(i);
        rig[r[i]].push_back(i);
    }
    
    multiset <long long> ms;
    for (int i = 1; i <= n; ++i) {
        for (int &id : rig[i-1]) {
            ms.erase(ms.find(smallest[id]));
        }
        if (i != 1) {
            if (!ms.empty()) {
                dis[i] = *(ms.begin());
            } else {
                dis[i] = INF;
            }
        }
        for (int &id : lef[i]) {
            smallest[id] = min(INF, dis[i] + c[id]);
            ms.insert(smallest[id]);
        }
    }

    if (dis[n] >= INF) {
        cout << -1 << endl;
    } else {
        cout << dis[n] << endl;
    }
}