#include <iostream>
#include <vector>
#include <set>
using namespace std;

#define ll long long
#define maxd 100000000000001

int main() {
    int n, m;
    cin >> n >> m;
    int l, r;
    ll c;
    vector<pair<int, ll>> adj[n + 1];
    set<pair<ll, int>> s;
    for (int i = 0; i < m; i++) {
        cin >> l >> r >> c;
        adj[l].emplace_back(r, c);
    }
    ll d[n + 1], temp;
    for (int i = 2; i <= n; i++) {
        d[i] = maxd;
        s.insert({maxd, i});
    }
    d[1] = 0;
    s.insert({0, 1});
    for (int i = 1; i < n; i++) {
        temp = s.begin()->first;
        for (auto p:adj[i]) {
            r = p.first;
            c = p.second;
            if (temp + c < d[r]) {
                s.erase({d[r], r});
                d[r] = temp + c;
                s.insert({d[r], r});
            }
        }
        s.erase({d[i], i});
    }
    if (d[n] == maxd) cout << "-1\n";
    else cout << d[n] << "\n";
    return 0;
}