/** ------------------------------------------------------------------------ **/
#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

template<typename T> using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> using ordered_map =
tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using hash_set = gp_hash_table<T, null_type>;
template<typename T1, typename T2> using hash_map = gp_hash_table<T1, T2>;

mt19937 rng(time(nullptr));
mt19937_64 rng64(time(nullptr));

#define rep(i, s, t) for (int i = s; i < int(t); ++i)
#define for_iter(i, c) for (auto i = c.begin(); i != c.end(); ++i)
#define for_const_iter(i, c) for (auto i = c.cbegin(); i != c.cend(); ++i)

#define sc	scanf
#define pr	printf
#define ff	first
#define ss	second
#define mp	make_pair
#define pb	push_back
#define eb	emplace_back
#define range(c)	c.begin(), c.end()
#define const_range(c)	c.cbegin(), c.cend()
#define lowbit(x)	((x) & -(x))
#define pop_count	__builin_popcount
#define pop_count64	__builtin_popcountll
#define tail_count	__builtin_ctz
#define tail_count64	__builtin_ctzll
#define lead_count	__builtin_clz
#define lead_count64	__builtin_clzll

template<typename T1, typename T2>
ostream& operator<< (ostream& os, const pair<T1, T2>& p) {
	return os << "<" << p.ff << "," << p.ss << ">";
}
#define container_ostream(container)				\
template<typename... T>						\
ostream& operator<< (ostream& os, const container<T...>& c) {	\
	if (c.empty()) return os << "{ }";			\
	auto iter = c.cbegin();					\
	os << "{ " << *iter;					\
	while (++iter != c.cend()) os << ", " << *iter;		\
	return os << " }";					\
}
container_ostream(vector)
container_ostream(set)
container_ostream(map)

void dbg() { cerr << endl;}
template<typename Head, typename... Tail> void dbg(Head h, Tail... t) {
	cerr << " " << h;
	dbg(t...);
}

#ifdef LOCAL
#define debug(...) cerr << "[ "#__VA_ARGS__" ] :", dbg(__VA_ARGS__)
#else
#define debug(...) 0
#endif

typedef long long i64;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

constexpr const int maxn = 1 << 17;
vector<pii> adj[maxn << 2 ^ maxn];

#define mid  (begin + end >> 1)
#define lson (index << 1)
#define rson (lson ^ 1)
#define lll  begin, mid, lson
#define rrr  mid, end, rson
#define decl int begin, int end, int index

void build(decl) {
	if (begin + 1 == end) return;
	adj[lson].eb(index, 0);
	adj[rson].eb(index, 0);
	adj[maxn << 1 ^ index].eb(maxn << 1 ^ lson, 0);
	adj[maxn << 1 ^ index].eb(maxn << 1 ^ rson, 0);
	build(lll);
	build(rrr);
}

void link(decl, int l, int r, int c, int k) {
	if (begin >= r || end <= l) return;
	if (begin >= l && end <= r) {
		debug(begin, end, index, c);
		adj[index].eb(k, c);
		adj[k].eb(maxn << 1 ^ index, 0);
		return;
	}
	link(lll, l, r, c, k);
	link(rrr, l, r, c, k);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n, m;
	cin >> n >> m;
	rep(i, 0, n) {
		adj[maxn ^ i].eb(maxn << 1 ^ maxn ^ i, 0);
		adj[maxn << 1 ^ maxn ^ i].eb(maxn ^ i, 0);
	}
	build(0, maxn, 1);
	while (m--) {
		int l, r, c;
		cin >> l >> r >> c;
		link(0, maxn, 1, l - 1, r, c, maxn << 2 ^ m);
	}
	constexpr const i64 inf = 0x3f3f3f3f3f3f3f3fLL;
	vector<i64> dis(maxn << 2 ^ maxn, inf);
	vector<bool> vis(maxn << 2 ^ maxn);
	priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> heap;
	heap.emplace(0LL, maxn);
	dis[maxn] = 0;
	while (!heap.empty()) {
		auto top = heap.top();
		heap.pop();
		if (vis[top.ss]) continue;
		debug(top);
//		dis[top.ss] = top.ff;
		for (const auto& e : adj[top.ss]) if (top.ff + e.ss < dis[e.ff]) {
			debug(e);
			dis[e.ff] = top.ff + e.ss;
			heap.emplace(dis[e.ff], e.ff);
		}
	}
	i64 ans = dis[maxn << 1 ^ maxn ^ n - 1];
	if (ans == inf) ans = -1;
	cout << ans << endl;
}


Measured memory_bytes: 71540