#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAX = 1e17 + 7;
const ll N = 1e5+7;

struct aa{
    ll u,d;
    bool operator < (const aa & a) const{
        return d>a.d;
    }
};

vector<aa>arr[N];

bool vis[N];
ll n,m;
ll d[N];

void Dij(){
    fill(d+1,d+n+1,MAX);
    d[1]=0;
    priority_queue<aa>Q;
    Q.push({1,0});
    while(!Q.empty()){
        aa x=Q.top();
		Q.pop();
        ll u=x.u;
        if(vis[u])continue;
        vis[u]=1;
        for(ll i=0;i<arr[u].size();i++){
            aa e=arr[u][i];
            if(d[e.u]>d[u]+e.d){
                d[e.u]=d[u]+e.d;
                Q.push({e.u,d[e.u]});
            }
        }
    }
}

int main(){
	ll a,b,w;		
	scanf("%lld%lld",&n,&m);
	for(ll i=2;i<=n;i++){
		arr[i].push_back({i-1,0});
	}
	for(ll i=0;i<m;i++){
		scanf("%lld%lld%lld",&a,&b,&w);
		arr[b].push_back({a,w});
		arr[a].push_back({b,w});
	}
	Dij();
	if(d[n]==MAX) d[n]=-1;
	cout<<d[n]<<endl;
	
	
	return 0;
}