#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <algorithm> 
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <list>
#include <utility>
#include <set>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <bitset>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <random>

using namespace std;

static const double EPS = 1e-9;
template<class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template<class T> void amin(T &a, T v) { if (a > v) a = v; }
template<class T> void amax(T &a, T v) { if (a < v) a = v; }
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) { return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y))); }
double SQSUM(double x, double y) { return x * x + y * y; }
#define PI  (acos(-1))
#define ARRAY_NUM(a) (sizeof(a)/sizeof(a[0])) 
#define NG (-1)
#define BIG ((int)1e9+10)
#define BIGLL ((ll)4e18)
#define SZ(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
typedef unsigned long long ull;
typedef long long ll;

using namespace std;
typedef long long ll;

// modint: mod 計算を int を扱うように扱える構造体
template<int MOD>
struct ModInt {
	long long val;
	constexpr ModInt(long long v = 0) noexcept : val(v % MOD) {
		if (val < 0) v += MOD;
	}
	constexpr int getmod() { return MOD; }
	constexpr ModInt operator - () const noexcept {
		return val ? MOD - val : 0;
	}
	constexpr ModInt operator + (const ModInt& r) const noexcept { return ModInt(*this) += r; }
	constexpr ModInt operator - (const ModInt& r) const noexcept { return ModInt(*this) -= r; }
	constexpr ModInt operator * (const ModInt& r) const noexcept { return ModInt(*this) *= r; }
	constexpr ModInt operator / (const ModInt& r) const noexcept { return ModInt(*this) /= r; }
	constexpr ModInt& operator += (const ModInt& r) noexcept {
		val += r.val;
		if (val >= MOD) val -= MOD;
		return *this;
	}
	constexpr ModInt& operator -= (const ModInt& r) noexcept {
		val -= r.val;
		if (val < 0) val += MOD;
		return *this;
	}
	constexpr ModInt& operator *= (const ModInt& r) noexcept {
		val = val * r.val % MOD;
		return *this;
	}
	constexpr ModInt& operator /= (const ModInt& r) noexcept {
		long long a = r.val, b = MOD, u = 1, v = 0;
		while (b) {
			long long t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		val = val * u % MOD;
		if (val < 0) val += MOD;
		return *this;
	}
	constexpr bool operator == (const ModInt& r) const noexcept {
		return this->val == r.val;
	}
	constexpr bool operator != (const ModInt& r) const noexcept {
		return this->val != r.val;
	}

	friend ostream& operator << (ostream &os, const ModInt<MOD>& x) noexcept {
		return os << x.val;
	}
	friend istream& operator >> (istream &is, ModInt<MOD>& x) noexcept {
		return is >> x.val;
	}
	static constexpr ModInt<MOD> modpow(const ModInt<MOD> &a, long long n) noexcept {
		if (n == 0) return 1;
		auto t = modpow(a, n / 2);
		t = t * t;
		if (n & 1) t = t * a;
		return t;
	}
};

template<class T>
vector<T> make_vector(int n, T t) {
	return vector<T>(n, t);
}

template<class ...Ts>
auto make_vector(int n, Ts ... ts) {
	return vector<decltype(make_vector(ts...))>(n, make_vector(ts...));
}

struct DIJK_LINK
{
	int from;
	int to;
	ll cost;
};

// リンクのリストから隣接リストをつくる
void dijkstra_make_G(vector< vector< pair<int, ll> > >& G, const vector < DIJK_LINK >& links, int N)
{
	G.clear();
	G.resize(N);

	for (int i = 0; i<SZ(links); i++)
	{
		G[links[i].from].push_back(make_pair(links[i].to, links[i].cost));
	}
}


void dijkstra(vector <ll>& D, const int start, const int N, const vector< vector< pair<int, ll> > >& G)
{
	D.clear();
	for (int i = 0; i<N; i++)
	{
		D.push_back(BIGLL);
	}
	priority_queue<pair<ll, int>, vector< pair<ll, int> >, greater< pair<ll, int> > > Q;
	D[start] = 0;
	Q.push(pair<ll, int>(0, start));

	while (!Q.empty()) {

		pair<ll, int> top = Q.top();
		Q.pop();

		int v = top.second;
		ll d = top.first;
		if (d <= D[v]) {
			for (int di = 0; di<SZ(G[v]); di++)
			{
				int v2 = G[v][di].first;
				ll cost = G[v][di].second;
				if (D[v2] > D[v] + cost) {
					D[v2] = D[v] + cost;
					Q.push(pair<ll, int>(D[v2], v2));
				}
			}
		}
	}
}

int main()
{
	int N, M;
	cin >> N >> M;

	int firstN = N;

	vector< vector< pair<int, ll> > > G;
	vector < DIJK_LINK > links;

	for(int i=0;i<M;i++)
	{
		DIJK_LINK tmp;
		int l, r, c;	
		cin >> l >> r >> c;
		l--;
		r--;

		for (int x = l; x <= r; x++)
		{
			tmp.from = x;
			tmp.to = N;
			tmp.cost = c;
			links.push_back(tmp);
			swap(tmp.from, tmp.to);
			links.push_back(tmp);
		}
		N++;
	}

	dijkstra_make_G(G, links, N);
	vector <ll> D;
	dijkstra(D, 0, N, G);


	if (D[firstN - 1] >= BIGLL)
	{
		D[firstN - 1] = -1;
	}
	else
	{
		D[firstN - 1] /= 2;
	}

	cout << D[firstN - 1] << endl;
	
	return 0;
}
