#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n-1);i>=0;i--)
#define FOR(i,n,m) for(int i=n;i<=(int)(m);i++)
#define RFOR(i,n,m) for(int i=(int)(n);i>=m;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())
typedef long long ll;
const int INF = 1e9;
const int MOD = 998244353;
const ll LINF = 1e18;
const double PI=3.14159265358979323846;
using namespace std;
vector<int> dx={1,0,-1,0};
vector<int> dy={0,1,0,-1};
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

struct SegmentTree{
private:
    int n;
    vector<ll> node;
public:
    SegmentTree(vector<ll> v){
        int sz=v.size();
        n=1;
        while(n<sz)n*=2;
        node.resize(2*n-1,LINF);
        for(int i=0;i<sz;i++){
            node[i+n-1]=v[i];
        }
        for(int i=n-2;i>=0;i--){
            node[i]=min(node[2*i+1],node[2*i+2]);
        }
    }
    void update(int x,ll val){
        x+=(n-1);
        node[x]=val;
        while(x>0){
            x=(x-1)/2;
            node[x]=min(node[2*x+1],node[2*x+2]);
        }
    }
    ll getmin(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0)r=n;
        if(r<=a||b<=l)return LINF;
        if(a<=l&&r<=b) return node[k];
        ll vl=getmin(a,b,2*k+1,l,(l+r)/2);
        ll vr=getmin(a,b,2*k+2,(l+r)/2,r);
        return min(vl,vr);
    }
};

int main() {
    int n,m;
    cin>>n>>m;
    vector<pair<pair<int,int>,ll>> vec(m);
    rep(i,m) {
        int l,r;
        ll c;
        cin>>l>>r>>c;
        vec[i]={{r,l},c};
    }
    sort(all(vec));
    
    vector<ll> v(n,LINF);
    SegmentTree st(v);
    rep(i,m) {
        int l=vec[i].first.second;
        int r=vec[i].first.first;
        ll c=vec[i].second;
        ll MIN=(i==0 ? 0 : st.getmin(l-1,n));
        v[r-1]=min(v[r-1],MIN+c);
        st.update(r-1,v[r-1]);
    }
    cout<<(v[n-1]==LINF ? -1 : v[n-1])<<endl;
}

