#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
vector<pair<long long , int> > st[N] , en[N];
long long dp[N];

int main() {
    int n , m;
    scanf("%d%d" , &n , &m);

    int s , e , c;
    while (m--) {
        scanf("%d%d%d" , &s , &e , &c);
        en[e].push_back({s , c});
    }

    // first : shortest path to n
    // second : node
    multiset<pair<long long , int> > info;
    for (int i = n ;i >= 1 ;i--) {
        if (i == n)
            dp[i] = 0;
        else
            dp[i] = info.size() ? info.begin()->first : -1;

        if (dp[i] == -1) {
            puts("-1");
            return 0;
        }

        for (auto &p : en[i]) {
            info.insert({p.second + dp[i] , i});
            st[p.first].push_back({p.second + dp[i] , i});
        }

        for (auto &c : st[i])
            info.erase(info.find(c));
    }

    printf("%lld" , dp[1]);
}