#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
int main(){
    ll n,m,L[200001],R[200001],C[200001].B[200001];
    cin>>n>>m;
    rep(i,m) cin>>L[i]>>R[i]>>C[i];
    ll ans=0;
    vector<pair<ll,ll>> A;
    rep(i,m) A.push_back(make_pair(R[i],i));
    sort(A.begin(),A.end());
    rep(i,m) B[i]=A[i].second;
    ll a=1,b=1e9,c,ans=0;
    rep(i,m){
        if(L[B[i]]>a){cout<<-1;return 0;}
        if(b>C[B[i]]) c=B[i],b=C[B[i]];
        if(i<m-1&&L[B[i+1]]>a){
            ans+=b,a=R[B[i]],b=1e9;
        }
    }
    cout<<ans;
}