#include "bits/stdc++.h"
using namespace std;
void __dump(int x){cerr << x;}
void __dump(long long x){cerr << x;}
void __dump(long double x){cerr << fixed << setprecision(3) << x;}
void __dump(char x){cerr << '\'' << x << '\'';}
void __dump(const string &x){cerr << '"' << x << '"';}
void __dump(const char *x){cerr << '"' << x << '"';}
void __dump(bool x){cerr << (x ? "true" : "false");}
void _dump(){cerr << "\n";}
template <typename T, typename U> void __dump(const pair<T, U> &x){cerr << '{'; __dump(x.first); cerr << ','; __dump(x.second); cerr << '}';}
template <typename T, typename U, typename V> void __dump(const tuple<T, U, V> &x){cerr << '{'; __dump(get<0>(x)); cerr << ',';  __dump(get<1>(x)); cerr << ',';  __dump(get<2>(x)); cerr << '}';}
template <typename T> void __dump(const T& x){int f=0; cerr << '{'; for(auto&i:x) cerr << (f++ ? "," : ""), __dump(i); cerr << "}";}
template <typename T, typename ... U> void _dump(T t, U ... u){__dump(t); if(sizeof...(u)) cerr << ", "; _dump(u...);}
#define dump(x ...) cerr  <<  "|| "  <<  # x  <<  "  =  ", _dump(x) 
//#define int long long
#define FAST ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define size(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define ln '\n'
#define __ ' '
#define LL long long
#define LD long double
#define pii pair<int,int>
const int INF = 2e9;
const int NN = 1e5;
int n,m;
vector<pii> queries[NN+3];
set<pair<LL,int>> dist_end;
set<pair<int,LL>> end_dist;

int main(){ FAST
	cin >> n >> m;
	for(int i=1; i<=m; ++i){
		int l,r,cost; cin >> l >> r >> cost;
		queries[l].emplace_back(r,cost);
	}

	dist_end.insert({0, 1});
	end_dist.insert({1, 0});
	for(int i=1; i<=n; ++i){
		assert(dist_end.empty() == end_dist.empty());
		while(!empty(end_dist) && begin(end_dist)->first < i){
			int en; LL d;
			tie(en, d) = *begin(end_dist);
			end_dist.erase({en, d});
			dist_end.erase({d, en});
		}
		if(empty(dist_end)){
			cout << -1 << ln;
			return 0;
		}

		LL curDist = begin(dist_end)->first;

		if(i == n){
			cout << curDist << ln;
			return 0;
		}

		for(auto [en,cost]: queries[i]){
			dist_end.insert({curDist + cost, en});
			end_dist.insert({en, curDist + cost});
		}
	}

	assert(false);
}