#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,n) for(long long i=0;i<n;++i)
#define REPP(i,m,n) for(long long i=m;i<n;++i)
#define rep(i,n) for(long long i = n-1;i>=0;--i)
#define repp(i,n,m) for(long long i = n-1; i >= m; --i)
#define ALL(N) (N.begin(),N.end())
#define de cout << "line : " << __LINE__ << " debug" << endl;
#define pb push_back
#define pq priority_queue
#define Dcout(N) cout << setprecision(20) << N << endl
#define fi first
#define se second
constexpr int INF = 2147483647;
constexpr long long INFF = 9223372036854775807;
signed main() {
    cin.tie(0);
	ios::sync_with_stdio(false);
    int N,M;cin >> N >> M;
    vector<pair<ll,pair<ll,ll>>> V;ll A,B,C;
    REP(i,M){
        cin >> A >> B >> C;--A;--B;
        V.pb({A,{C,B}});
        //cin >> V[i].fi >> V[i].second.second >> V[i].second.first >> endl;
        //--V[i].fi;--V[i].se.fi;
        //Vにはfrom cost to
    }
    sort ALL(V);
    ll dp[N];REP(i,N) dp[i] = INF;
    dp[0] = 0;
    if(V[0].fi != 0){
        cout << -1 << endl;
        return 0;
    }
  bool flag[N] = {};flag[0] = 1;
    REPP(i,V[0].fi + 1,V[0].se.se + 1){dp[i] = V[0].se.fi;flag[i] = 1;}
    REPP(i,1,M){
        if(V[i].fi == V[i-1].fi){
            REPP(j,V[i-1].se.se + 1,V[i].se.se + 1){
                ll A = V[i].fi,B = V[i].se.fi;
                dp[j] = min(dp[j],B);
            	flag[j] = 1;
            }
        }
        else{
            REPP(j,V[i].fi+1,V[i].se.se + 1){
                int A = V[i].fi,B = V[i].se.fi;
                dp[j] = min(dp[j],dp[A] + B);
            	flag[j] = 1;
            }
        }
    }
    REP(i,N){
        if(!flag[i]){
            cout << -1 << endl;
            return 0;
        }
    }
    if(dp[N-1] != INF) cout << dp[N-1] << endl;
    else cout << -1 << endl;
}