# include <bits/stdc++.h>

typedef long long ll;
using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<tuple<ll, ll, ll>> paths;
    for (int i = 0; i < m; i++) {
        ll l, r, c;
        cin >> l >> r >> c;
        paths.push_back({ l, r, c });
    }
    sort(paths.begin(), paths.end());
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<>> q;
    ll ss = get<0>(paths[0]);
    ll ee = get<1>(paths[0]);
    ll cc = get<2>(paths[0]);
    if (ss != 0) {
        cout << -1 << endl;
        return 0;
    }
    q.push({ ee, cc });
    for (int i = 1; i < m; i++) {
        auto t = paths[i];
        ll s = get<0>(t);
        ll e = get<1>(t);
        ll c = get<2>(t);
        vector<pair<ll, ll>> to;
        while (!q.empty()) {
            auto front = q.top();
            q.pop();
            if (front.first < s) {
                continue;
            }
            pair<ll, ll> np;
            np.first = ee;
            np.second = front.second + cc;
            to.push_back(front);
            to.push_back(np);
        }
        if (to.size()) {
            for (auto &p : to) {
                q.push(p);
            }
        } else {
            cout << -1 << endl;
            return 0;
        }
    }

    while (!q.empty()) {
        auto front = q.top();
        if (front.first == n) {
            cout << front.second << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}
