#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
const int N=1e5+5;
int n,m;
typedef long long ll;
typedef std::pair<ll,int> P;
std::priority_queue<P,std::vector<P>,std::greater<P> > q;
std::vector<P> g[N];
ll dp[N];
const ll INF=1e18;
int main() {
    scanf("%d%d",&n,&m);
    for(int l,r,c;m--;) {
        scanf("%d%d%d",&l,&r,&c);
        g[l].push_back(P(c,r));
    }
    dp[1]=0;
    for(P c:g[1]) q.push(c);
    for(int i=2;i<=n;i++) {
        while(!q.empty()&&q.top().second<i) q.pop();
        if(q.empty()) {
            dp[i]=INF;
        }
        else {
            dp[i]=q.top().first;
            for(P c:g[i]) q.push(P(dp[i]+c.first,c.second));
        }
    }
    printf("%lld",dp[n]<INF?dp[n]:-1LL);
    return 0;
}
