#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define debug cerr << "-- " << __LINE__ << " --" << endl
#define MOD 1000000007
typedef long long int ll;

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
template <typename T> string to_string(const vector<T> &xs) {string s="["; for(int i=0;i<xs.size();i++) s += (i>0 ? " " : "") + to_string(xs[i]); return s + "]";}



ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
ll gcd(ll a, ll b) { ll x, y; return extgcd(a, b, x, y);}

class llm{
	ll n;
public:
	llm(){n = 0;}
	llm(ll n){this->n = (n >= 0) ? (n % MOD) : (MOD - 1 - (-n-1 % MOD));}
	operator ll() const {return n;}
	llm operator+(const llm &y) const{ return llm((ll)*this + (ll)y);}
	llm operator+=(const llm &y) { *this = *this + y; return *this;}
	llm operator-(const llm &y) const{ return llm((ll)*this - (ll)y);}
	llm operator-=(const llm &y) { *this = *this - y; return *this;}
	llm operator-() const{ return llm(0) - *this;}
	llm operator*(const llm &y) const{ return llm((ll)*this * (ll)y);}
	llm operator*=(const llm &y) { *this = *this * y; return *this;}
	llm operator/(const llm &y) const{ return *this * y.inv();}
	llm operator/=(const llm &y) { *this = *this / y; return *this;}
	llm inv() const{
		ll x, y;
		if (extgcd(n, MOD, x, y) == 1) return llm(x);
  		return llm(0); // unsolvable
	}
	friend istream& operator >> (istream& os, llm& x);
	friend ostream& operator << (ostream& os, const llm& x);
};
istream& operator >> (istream& is, llm& x){ll n; is >> n; x = llm(n); return is;}
ostream& operator << (ostream& os, const llm& x){os << (ll)x; return os;}

template<typename T> T comb(ll n, ll k){
	T ret = 1;
	for(ll i=1;i<=k;i++) ret = ret * T(n - k + i) / T(i);
	return ret;
}


class MinSegTree{
	vector<ll> data;
	int baseSize, baseOffset;
public:
	MinSegTree(vector<ll> &ary){
		baseSize = 1;
		for(;;baseSize*=2){if (ary.size()<=baseSize) break;}
		baseOffset = baseSize - 1;
		data = vector<ll>(baseSize * 2, LLONG_MAX/10);
		for(int i=0;i<ary.size();i++) data[baseOffset+i] = ary[i];
		//initial upward propagation
		for(int i=baseOffset-1;i>=0;i--){
			data[i] = min(data[2*i+1], data[2*i+2]);
		}
	}
	ll getMin(int s, int e, int k=0, int l=0, int r=-1){
		/* getMin(s, e) で [s, e) の最小値を計算できます．

			クエリ区間 [s, e) と管理区間 [l, r) の共通部分について解く．
		  （(k,l,r)==(0,0,baseSize) から始めて，分割統治していく） */

		// cerr << "getMin(" << s << "," << e << "," << k << "," << l << "," << r << ")" << endl;
		if (r < 0) r = baseSize; //初期の管理区間

		ll ret;
		if (e <= l || r <= s) ret = LLONG_MAX/10; //クエリ区間と管理区間が交わらないケース（空区間）// !!
		else if (s <= l && r <= e) ret = data[k]; //クエリ区間が管理区間を被覆するケース
		else ret = min(getMin(s, e, k*2+1, l, (l+r)/2), getMin(s, e, k*2+2, (l+r)/2, r)); //分割統治 !!
		// cerr << "getMin(" << s << "," << e << "," << k << "," << l << "," << r << ") == " << ret << endl;
		return ret;
	}
	void update(int idx, ll val){
		/* (0 <= idx < baseSize) */
		// cerr << "======================" << endl;
		// cerr << "UPDATE " << idx << " TO " << val << endl;
		// cerr << to_string(data) << endl;
		idx += baseOffset;
		data[idx] = val;
		while(idx > 0){
			idx = (idx - 1) / 2;
			data[idx] = min(data[idx*2+1], data[idx*2+2]);
		}
		// cerr << to_string(data) << endl;
		// cerr << "====================" << endl;
		return;
	}
	ll at(int idx){
		return data[idx + baseOffset];
	}
	//TODO: 遅延評価セグ木（区間に一様に値を変更できるようになる・・・はず）
	//http://tsutaj.hatenablog.com/entry/2017/03/30/224339
};



ll n, m;
ll l[114514], r[114514], c[114514];

int main(void){
	cin >> n >> m;
	vector<vector<ll>> edges;
	for(int i=0;i<m;i++){
		cin >> l[i] >> r[i] >> c[i];
		l[i]--; r[i]--;
		edges.pb(vector<ll> {r[i], l[i], c[i]});
	}
	sort(edges.begin(), edges.end());


	vector<ll> ary(n, LLONG_MAX/10); ary[0] = 0;
	// cerr << to_string(ary) << endl;
	auto st = MinSegTree(ary);

	for(auto &edge : edges){
		// cerr << to_string(edge) << endl;
		ll cost = min(st.at(edge[0]), st.getMin(edge[1], edge[0]) + edge[2]);
		// cerr << "st.getMin(4, 8) == " << st.getMin(4, 8) << endl;
		// cerr << "At: ";
		// for(int i=0;i<n;i++){
		// 	cerr << st.at(i) << " ";
		// }cerr << endl;

		st.update(edge[0], cost);
		// cerr << edge[0] << ":" << cost << endl;
	}
	ll ans = st.at(n-1);
	if (ans >= LLONG_MAX/10) ans = -1;
	cout << ans << endl;


	


    return 0;
}













Measured memory_bytes: 11176