#include <bits/stdc++.h>

using namespace std;
typedef long long int LL;
typedef long long int ll;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(),(c).end())
#define BACKSORT(c) sort((c).begin(),(c).end(),std::greater<LL>())
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SP << " " <<

LL INF = 1000000007LL * 1000000007LL;

// range update and range minimum
struct RURM {
	using t1 = LL;
	using t2 = LL;
	static t1 id1() { return INF; }
	static t2 id2() { return -1; }
	static t1 op1(const t1& l, const t1& r) { return min(l, r); }
	static t1 op2(const t1& l, const t2& r) { return r != id2() ? r : l; }
	static t2 op3(const t2& l, const t2& r) { return r != id2() ? r : l; }
};

template <typename M>
class lazy_segment_tree {
	using T1 = typename M::t1;
	using T2 = typename M::t2;
	const int n;
	vector<T1> data;
	vector<T2> lazy;
	int size(int n) {
		int res = 1;
		while (res < n) res <<= 1;
		return res;
	}
	void push(int node) {
		if (lazy[node] == M::id2()) return;
		if (node < n) {
			lazy[node * 2] = M::op3(lazy[node * 2], lazy[node]);
			lazy[node * 2 + 1] = M::op3(lazy[node * 2 + 1], lazy[node]);
		}
		data[node] = M::op2(data[node], lazy[node]);
		lazy[node] = M::id2();
	}
	void suc(int l, int r, int node, int lb, int ub, T2 val) {
		if (ub <= l || r <= lb) return;
		if (l <= lb && ub <= r) {
			lazy[node] = M::op3(lazy[node], val);
			return;
		}
		push(node);
		int c = (lb + ub) / 2;
		suc(l, r, node * 2, lb, c, val);
		suc(l, r, node * 2 + 1, c, ub, val);
		data[node] = M::op1(M::op2(data[node * 2], lazy[node * 2]), M::op2(data[node * 2 + 1], lazy[node * 2 + 1]));
	}
	T1 sub(int l, int r, int node, int lb, int ub) {
		if (ub <= l || r <= lb) return M::id1();
		if (l <= lb && ub <= r) return M::op2(data[node], lazy[node]);
		int c = (lb + ub) / 2;
		return M::op2(M::op1(sub(l, r, node * 2, lb, c), sub(l, r, node * 2 + 1, c, ub)), lazy[node]);
	}
public:
	lazy_segment_tree(int n_) : n(size(n_)), data(n * 2, M::id1()), lazy(n * 2, M::id2()) {}
	lazy_segment_tree(int n_, T1 v1) : n(size(n_)), data(n * 2, v1), lazy(n * 2, M::id2()) {}
	lazy_segment_tree(const vector<T1>& data_) : n(size(data_.size())), data(n * 2, M::id1()), lazy(n * 2, M::id2()) {
		init(data_);
	}
	void init() {
		for (int i = n - 1; i >= 1; i--) data[i] = M::op1(data[i * 2], data[i * 2 + 1]);
	}
	void init(const vector<T1>& data_) {
		for (int i = 0; i < (int)data_.size(); i++) data[i + n] = data_[i];
		init();
	}
	void update(int l, int r, T2 val) {
		suc(l, r + 1, 1, 0, n, val);
	}
	T1 find(int l, int r) {
		return sub(l, r + 1, 1, 0, n);
	}
};

struct LRC{
  LL L,R,C;
  LRC(LL _L,LL _R,LL _C):L(_L),R(_R),C(_C){}
};

//昇順ソート
int xcomp(LRC& a, LRC& b) {
	return a.L < b.L;
}

#define XSORT(c) sort((c).begin(),(c).end(),xcomp)

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

	LL N, M;
	cin >> N >> M;
	
	vector<LRC> LRCs;

	lazy_segment_tree<RURM> seg(N);
	seg.init();

	seg.update(0, 0, 0);

	REP(i,M){
		LL l,r,c;
		cin>>l>>r>>c;
		l--;
		r--;
		LRCs.push_back(LRC(l, r, c));
	}

	XSORT(LRCs);
	
	REP(i,LRCs.size()){
		LL val = seg.find(LRCs[i].L, LRCs[i].R) + LRCs[i].C;
		
		int ng = LRCs[i].L-1; //
    int ok = LRCs[i].R+1; // 

    /* ok と ng のどちらが大きいかわからないことを考慮 */
    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;

        if (seg.find(mid,mid)>val) ok = mid;
        else ng = mid;
    }

		// cout << seg.find(LRCs[i].L, LRCs[i].L) << endl;
		// cout << seg.find(LRCs[i].R, LRCs[i].R) << endl;

		// cout << LRCs[i].L SP LRCs[i].R SP ok << endl;

		seg.update(ok, LRCs[i].R, val);
	}

	LL ans = seg.find(N - 1, N - 1);
	
	if(ans==INF){
		cout << -1 << endl;
	}
	else
	{
		cout << ans << endl;
	}
}


Measured memory_bytes: 8820