#include<iostream>
#include<cstdio>
#include<queue>
#define ll long long
using namespace std;
int n,m,x,y,head[1000010],cnt=0;
ll z,dis[1000010];
struct Node{
	int u;
	ll dis;
	bool operator <(const Node &rhs)const{
		return dis>rhs.dis;
	}
};
priority_queue<Node>q;
struct Edge{
	int v,nxt;
	ll w;
}e[1000010];
void addEdge(int u,int v,ll w){
	e[++cnt].v=v;
	e[cnt].w=w;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
int main(){
	scanf("%d%d",&n,&m);
	for(register int i=2;i<=n;i++)addEdge(i,i-1,0);
	for(register int i=1;i<=m;i++){
		scanf("%d%d%d",&x,&y,&z);
		addEdge(x,y,z);
	}
	for(register int i=1;i<=n;i++)dis[i]=1e18;
	dis[1]=0;
	q.push((Node){1,0});
	while(!q.empty()){
		Node now=q.top();q.pop();
		if(now.dis!=dis[now.u])continue;
		for(register int i=head[now.u];i;i=e[i].nxt){
			if(dis[e[i].v]>dis[now.u]+e[i].w){
				dis[e[i].v]=dis[now.u]+e[i].w;
				q.push((Node){e[i].v,dis[e[i].v]});
			}
		}
	}
	printf("%lld",dis[n]<1e18?dis[n]:-1);
	return 0;
}