/*
    Mbak Rose Dong
*/
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int l[100100], r[100100];
ll c[100100];
ll dp[100100];
bool pend[100100];
vector<int> in[100100], out[100100];
int main()
{
    int n,i,j,k,x,y,ans,m,q,curr,temp;
    scanf("%d %d",&n,&m);
    for(i=1;i<=n;i++) dp[i] = 1e16;
    for(i=1;i<=m;i++)
    {
        scanf("%d %d %lld",&l[i], &r[i], &c[i]);
        in[l[i]].push_back(i);
        out[r[i]].push_back(i);
    }
    set<pair<ll,int> > s;
    for(i=1;i<=n;i++)
    {
        while(!s.empty() && pend[(*s.begin()).second] == 1) s.erase(s.begin());
        if(!s.empty())
        {
            curr = (*s.begin()).second;
            ll cost = (*s.begin()).first;
            dp[i] = min(dp[i], cost);
        }
        for(auto x : in[i])
        {
            ll aa = 0;
            if(l[x] != 1) aa = dp[l[x]];
            s.insert({c[x] + aa, x});
        }
        for(auto x : out[i])
        {
            pend[x] = 1;
        }
    }
    if(dp[n] == 1e16) dp[n] = -1;
    printf("%lld\n", dp[n]);
    return 0;
}
