#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, int>;
#define rep(i,n) for(int i=0; i<(n); i++)
const int MX = 100005;
int n, m, l, r, c;
ll dis;
vector<P> a[MX];
priority_queue<P, vector<P>, greater<P>> qu;

int main(){
	scanf("%d%d", &n, &m);
	rep(i,m){
		scanf("%d%d%d", &l, &r, &c);
		l--; r--;
		a[l].push_back(P(c,r));
	}
	rep(i,n){
		while(!qu.empty() && qu.top().second < i) qu.pop();
		if(i && qu.empty()) return 0*puts("-1");
		dis = i ? qu.top().first : 0;
		for(P p: a[i]) qu.push(P(p.first+dis,p.second));
	}
	printf("%lld\n", dis);
}