#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

ll inf = 1e18;
int const N = 100000;
vector<pair<int, int> > ed[N + 1];

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	f(i, 0, m){
		int l, r, c;
		scanf("%d%d%d", &l, &r, &c);
		ed[r].emplace_back(l, c);
	}
	map<int, ll> st;
	st[1] = 0;
	f(i, 2, n + 1){
		ll an = inf;
		f(j, 0, ed[i].size()){
			int l = ed[i][j].first, c = ed[i][j].second;
			an = min(an, st.lower_bound(l)->second + c);
		}
		while (st.rbegin()->second >= an)st.erase(--st.end());
		st[i] = an;
	}
	printf("%lld\n", st.rbegin()->second >= inf ? -1 : st.rbegin()->second);
}