///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<ll,ll>
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e17;
const int mod=1e9+7;
const int M=100005;
int n,m;
vector<pi>adj[M];
int d[M];
bool vis[M];
 main()

{
    fast
    cin>>n>>m;
    f(i,1,m)
    {
        int lft,rgt,c;
        cin>>lft>>rgt>>c;
        adj[lft].pb(mp(rgt,c));
        adj[rgt].pb(mp(lft,c));
        for(int j=lft+1;j<=rgt;j++)
        {
            adj[j].pb(mp(j-1,0));
        }
    }
    priority_queue<pi,vector<pi>,greater<pi> >pq;
    f(i,1,n)d[i]=inf;
    d[1]=0;
    pq.push(mp(d[1],1));
    while(!pq.empty())
    {
        pi tmp=pq.top();
        pq.pop();
        int u=tmp.si;
        int cost=tmp.fi;
        if(vis[u])continue;
        vis[u]=1;
        for(auto x:adj[u])
        {
            int v=x.fi;
            int c=x.si;
            if(d[v]<=c+cost)continue;
            d[v]=c+cost;
            pq.push(mp(d[v],v));
        }
    }
    if(d[n]>=inf)d[n]=-1;
    cout<<d[n]<<endl;
    return 0;

}



































