#include <bits/stdc++.h>
using namespace std;

const int N = 110000;
const long long INF = 1LL << 60;

int L[N], R[N], C[N];
long long val[N];

vector<int> st[N], en[N];

set<pair<long long, int>> S;

int main() {
	
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int n, m; cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		cin >> L[i] >> R[i] >> C[i];
		st[L[i]].push_back(i);
		en[R[i]].push_back(i);
	}
	
	for (int i = 1; i <= n; i++) val[i] = INF;
	val[1] = 0;
	for (int i = 1; i <= n; i++) {
		if (S.size()) val[i] = min(val[i], S.begin() -> first);
		for (auto it : st[i]) S.emplace(val[L[it]] + C[it], it);
		for (auto it : en[i]) S.erase(make_pair(val[L[it]] + C[it], it));
	}
	if (val[n] == INF) cout << "-1\n";
	else cout << val[n] << "\n";
	
	return 0;
}
