#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define Maxn 100000
#define ll long long
#define Inf 0x3f3f3f3f3f3f3f3fll
int head[Maxn+5],arrive[Maxn<<1|5],nxt[Maxn<<1|5],val[Maxn<<1|5],tot;
void add_edge(int from,int to,int w){
	arrive[++tot]=to;
	nxt[tot]=head[from];
	val[tot]=w;
	head[from]=tot;
}
ll dis[Maxn+5];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
bool vis[Maxn+5];
void dij(){
	memset(dis,0x3f,sizeof dis);
	dis[1]=0;
	q.push(make_pair(0,1));
	int u;
	while(!q.empty()){
		u=q.top().second;
		q.pop();
		if(vis[u]){
			continue;
		}
		vis[u]=1;
		for(int i=head[u];i;i=nxt[i]){
			int v=arrive[i];
			if(dis[u]+val[i]<dis[v]){
				dis[v]=dis[u]+val[i];
				q.push(make_pair(dis[v],v));
			}
		}
	}
}
int main(){
	int n,m;
	int l,r,c;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&l,&r,&c);
		add_edge(l,r,c);
	}
	for(int i=1;i<n;i++){
		add_edge(i+1,i,0);
	}
	dij();
	cout<<(dis[n]==Inf?-1:dis[n])<<endl;
	return 0;
}
