#include <bits/stdc++.h>

int64_t solve();

int main()
{
	printf("%lld\n", solve());

	return 0;
}

int64_t solve()
{
	int N, M;
	scanf("%d%d", &N, &M);
	std::vector<std::array<int, 3>> edges(M);
	for (auto& e: edges)
	{
		for (auto& f: e)
			scanf("%d", &f);
		e[0]--;
	}
	std::sort(edges.begin(), edges.end());
	edges.push_back({1 << 30, 1 << 30, 1 << 30});
	using pii = std::pair<int64_t, int>;
	std::priority_queue<pii, std::vector<pii>, std::greater<pii>> pq;	// {dist, r}, [l, r)
	pq.push({0, 1});
	for (int i{}, e_i{}; i < N; i++)
	{
		while (!pq.empty() && pq.top().second <= i) pq.pop();
		if (pq.empty()) return -1;
		auto now{pq.top()};
		while (edges[e_i][0] == i)
		{
			pq.push({now.first + edges[e_i][2], edges[e_i][1]});
			e_i++;
		}
	}

	if (pq.empty()) return -1;
	else return pq.top().first;
}