#ifdef LOCAL_EXEC
#include "include.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define trace(...)
#endif

using LL = long long;
using PLI = pair<LL, int>;
const LL INF = 1e18;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	int n, m; cin >> n >> m;
	vector<vector<int>> out(n);
	vector<vector<pair<int, int>>> in(n);
	for (int i = 0; i < m; i++) {
		int l, r, c; cin >> l >> r >> c; l--, r--;
		in[l].emplace_back(c, i);
		out[r].push_back(i);
	}

	vector<LL> dis(n, INF);
	priority_queue<PLI, vector<PLI>, greater<PLI>> heap;
	vector<bool> to_del(m);

	dis[0] = 0;
	for (int i = 0; i < n; i++) {
		while (!heap.empty() && to_del[heap.top().second]) heap.pop();
		if (!heap.empty()) {
			dis[i] = min(dis[i], heap.top().first);
		}

		for (auto& e: in[i]) {
			heap.emplace(dis[i] + e.first, e.second);
		}

		for (int x: out[i]) {
			to_del[x] = true;
		}
	}

	LL ans = dis[n - 1];
	if (ans == INF) ans = -1;
	cout << ans << endl;

	return 0;
}
