#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

struct Bridge {
	int l;
	int r;
	int64_t c;
};

class SegTree {

public:
	SegTree(int n, int64_t idt) {
		int m = 1;
		while (m < n) m *= 2;
		this->data_ = vector<int64_t>(2 * m, idt);
		this->idt_ = idt;
		this->m_ = m;
	}

	int64_t query(int i) {
		int64_t v = this->idt_;
		int idx = i + m_ - 1;
		v = min(v, this->data_[idx]);
		while (idx != 0) {
			bool from_left = (idx & 1);
			idx = (idx - 1) / 2;
			if (!from_left) continue;
			if (this->data_[idx] != this->idt_) v = min(v, this->data_[idx]);
		}

		return this->idt_;
	}

	void update(int i, int64_t v) {
		if (v < query(i)) {
			int idx = i + m_ - 1;
			this->data_[idx] = v;

			while (idx != 0) {
				bool from_left = (idx & 1);
				idx = (idx - 1) / 2;
				if (!from_left) this->data_[idx] = min(this->data_[idx], v);
			}
		}
	}

private:
	vector<int64_t> data_;
	int     m_;
	int64_t idt_;
};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	int n, m; cin >> n >> m;
	vector<Bridge> bs(m);
	for (auto& b : bs) {
		cin >> b.l >> b.r >> b.c;
	}

	sort(bs.begin(), bs.end(), [](auto& lhs, auto& rhs) {
		if (lhs.l != rhs.l) return lhs.l < rhs.l;
		return lhs.r > rhs.r;
		});

	SegTree segt(n + 1, INT64_MAX);
	segt.update(1, 0);
	for (auto& b : bs) {
		int64_t v = segt.query(b.l);
		if (v == INT64_MAX) {
			continue;
		}

		segt.update(b.r, v + b.c);
	}

	int64_t v = segt.query(n);
	if (v == INT64_MAX) {
		cout << -1 << endl;
	}
	else {
		cout << v << endl;
	}

	return 0;
}