#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;
using ll = long long;
int mod = 1000000007;

ll bignum = 1000000000000000;

int main(void){
    int N, M; cin >> N >> M;
    ll cost[N+1] = {};
    rep(i, N+1) cost[i] = bignum;
    cost[1] = 0;

    pair<pair<int, int>, ll> edge[M];
    rep(i, M){
        int l, r; ll c; cin >> l >> r >> c;
        edge[i].first.first = l;
        edge[i].first.second = r;
        edge[i].second = c;
    }
    sort(edge, edge+M);

    int step = 1; //最初、更新用の人は1にいる
    rep(i, M){
        int from = edge[i].first.first;
        int to = edge[i].first.second;
        ll c = edge[i].second;
        cost[to] = min(cost[to], cost[from]+c);
        while(step < to){
            cost[step] = min(cost[step], cost[to]);
            step++;
        }
    }

    if(cost[N] == bignum) cout << -1 << endl;
    else cout << cost[N] << endl;
    return 0;
}