#include <bits/stdc++.h>
using namespace std;
 
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")
 
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FILL(a, value) memset(a, value, sizeof(a))
 
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
 
typedef long long LL;
typedef pair<int, int> PII;

const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = (LL)INF * INF;
const int MAX = 1 << 17;

int r[MAX], c[MAX];
vector<int> idxes[MAX];
vector<LL> vec[MAX];

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n, m;
	cin >> n >> m;
	FOR(i, 0, m)
	{
		int l;
		cin >> l >> r[i] >> c[i];
		l--;
		idxes[l].push_back(i);
	}
	multiset<LL> s;
	FOR(i, 0, n)
	{
		for(LL x: vec[i])
			s.erase(s.find(x));
		if(i && s.empty())
		{
			cout << "-1\n";
			return 0;
		}
		LL d = i == 0 ? 0 : *s.begin();
		if(i == n - 1)
		{
			cout << d << "\n";
			return 0;
		}
		for(int id: idxes[i])
		{
			s.insert(d + c[id]);
			vec[r[id]].push_back(d + c[id]);
		}
	}
	assert(false);
	return 0;
}