#include <stdio.h>
#include <algorithm>
#include <queue>

using namespace std;

//long long
struct node {
	long long v , c;
	node *next;
} pool[2100000] , *g[610000];
struct tree {
	long long l , r , nd;
	tree *ll , *rr;
} poolt[410000] , *t1 , *t2;
long long topn , topt;
long long n , m;
long long index;
long long dis[610000] , f[610000];
queue < long long > q;
void add ( long long u , long long v , long long c ) {
	node *tmp = &pool[++topn];
	//printf ( "  %lld %lld %lld\n" , u , v , c );
	tmp -> v = v; tmp -> c = c; tmp -> next = g[u]; g[u] = tmp;
}
void buildtree ( tree *id , long long l , long long r , long long f ) {
	id -> l = l; id -> r = r; id -> nd = ++index;
	if ( l == r ) {
		if ( f == 1 ) add ( id -> nd , l , 0 );
		else add ( l , id -> nd , 0 );
		return ;
	}
	long long mid = (l+r)/2;
	id -> ll = &poolt[++topt];
	id -> rr = &poolt[++topt];
	buildtree ( id -> ll , l , mid , f );
	buildtree ( id -> rr , mid + 1 , r , f );
	if ( f == 1 ) {
		add ( id -> nd , id -> ll -> nd , 0 );
		add ( id -> nd , id -> rr -> nd , 0 );
	}
	else {
		add ( id -> ll -> nd , id -> nd , 0 );
		add ( id -> rr -> nd , id -> nd , 0 );
	}
}
void change ( tree *id1 , tree *id2 , long long l , long long r , long long c , long long num ) {
	if ( id1 -> l == l && id1 -> r == r ) {
		add ( num , id1 -> nd , c );
		add ( id2 -> nd , num , 0 );
		return ;
	}
	long long mid = (id1->l+id1->r)/2;
	if ( r <= mid ) change ( id1 -> ll , id2 -> ll , l , r , c , num );
	else {
		if ( l > mid ) change ( id1 -> rr , id2 -> rr , l , r , c , num );
		else {
			change ( id1 -> ll , id2 -> ll , l , mid , c , num );
			change ( id1 -> rr , id2 -> rr , mid + 1 , r , c , num );
		}
	}
}
void spfa () {
	long long i , k;
	for ( i = 1 ; i <= n + m + n * 4 ; i++ ) {
		dis[i] = 2147483647777777ll;
		f[i] = 0;
	}
	dis[1] = 0;
	f[1] = 1;
	q.push ( 1 );
	while ( q.size () ) {
		k = q.front(); q.pop();
		//printf ( "%lld %lld\n" , k , dis[k] );
		f[k] = 0;
		for ( node *j = g[k] ; j ; j = j -> next ) {
			if ( dis[j->v] > dis[k] + j -> c ) {
				dis[j->v] = dis[k] + j -> c;
				if ( !f[j->v] ) q.push ( j -> v );
				f[j->v] = 1; 
			}
		}
	}
}
void work () {
	long long i , l , r , c;
	scanf ( "%lld%lld" , &n , &m );
	index = n + m;
	t1 = &poolt[++topt];
	buildtree ( t1 , 1 , n , 1 );
	t2 = &poolt[++topt];
	buildtree ( t2 , 1 , n , 2 );
	for ( i = 1 ; i <= m ; i++ ) {
		scanf ( "%lld%lld%lld" , &l , &r , &c );
		change ( t1 , t2 , l , r , c , n + i );
	}
	spfa ();
	if ( dis[n] == 2147483647777777ll ) dis[n] = -1;
	printf ( "%lld\n" , dis[n] );
}
int main () {
	work ();
	return 0;
}
