#include <bits/stdc++.h>

typedef long double ld;
typedef long long ll;

using namespace std;

const ll inf = 1e18;
struct Segment {
    int l;
    int r;
    int c;
    bool operator<(const Segment &rhs) {
        return l < rhs.l;
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
//    freopen("input.txt", "r", stdin);
    cout << setprecision(20);
    int l, n;
    cin >> l >> n;
    vector<Segment> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i].l >> a[i].r >> a[i].c;
    }
    sort(a.begin(), a.end());
    ll ans = inf;
    set<pair<ll, int>> len;
    int dist = 0;
    int j1 = 0;
    len.insert(make_pair(0, 1));
    auto lol = make_pair(inf, l);
    len.insert(lol);
    while (!len.empty()) {
        if (dist >= len.begin()->second) {
            len.erase(len.begin());
            continue;
        }
        pair<int, int> x = *len.begin();
        int pos = x.second;
        dist = pos;
        ll cost = x.first;
        int j2 = j1;
        while (j2 < n && a[j2].l <= pos) {
            if (a[j2].r > pos) {
                ll ccost = a[j2].c + cost;
                if (a[j2].r == l) {
                    ans = min(ans, ccost);
                }
                len.insert(make_pair(ccost, a[j2].r));
            }
            j2++;
        }
        j1 = j2;
    }
    if (ans == inf) {
        cout << "-1";
    } else {
        cout << ans;
    }
    return 0;
}
