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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n, m;
ll ans = -1;

int main() {
	cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    vector<pair<pii, int>> edges;
    for (int i = 0; i < m; ++i) {
        int l, r, c;
        cin >> l >> r >> c;
        edges.push_back({{l, r}, c});
    }

    sort(all(edges));

    // Dist, To which node
    set<pair<ll, ll>> cur;
    cur.insert({0, 1});
    
    for (int i = 0; i < m; ++i) {
        int l, r, c;
        l = edges[i].first.first, r = edges[i].first.second, c = edges[i].second;
        while (sz(cur) && cur.begin()->second < l)
            cur.erase(cur.begin());

        if (!cur.size()) break;
        if (r == n) {
            if (ans == -1) ans = cur.begin()->first + c;
            else ans = min(ans, cur.begin()->first + c);
        }

        cur.insert({cur.begin()->first + c, r});
    }

    cout << ans;
}
