#include"bits/stdc++.h"
#include<boost/multi_array.hpp>
#include<boost/optional.hpp>
#include<boost/range/irange.hpp>
#include<boost/range/algorithm.hpp>
#include<boost/range/adaptors.hpp>
#include<boost/operators.hpp>
#include<boost/dynamic_bitset.hpp>

namespace adaptor = boost::adaptors;
using index_t = std::size_t;

template<class T>using p_queue=std::priority_queue<T, std::vector<T>, std::greater<>>;

auto upper_bound(std::set<std::tuple<int, int, std::int64_t>>const& set, int v)
{
	return set.upper_bound(std::make_tuple(v, std::numeric_limits<int>::max(), std::numeric_limits<std::int64_t>::max()));
}

void Main()
{
	int N, M;
	std::cin >> N >> M;
	std::set<std::tuple<int, int, std::int64_t>>query;
	for (auto i : boost::irange(0, M))
	{
		int L, R;
		std::int64_t C;
		std::cin >> L >> R >> C;
		query.emplace(L, R, C);
	}
	p_queue<std::pair<std::int64_t, int>> queue;
	auto end = upper_bound(query, 1);
	for (auto ite = query.begin(); ite != end; ++ite)
	{
		queue.emplace(std::get<2>(*ite), std::get<1>(*ite));
	}
	query.erase(query.begin(), end);
	std::int64_t result = 0;
	int reach = 1;
	while (queue.size())
	{
		std::int64_t C;
		int R;
		std::tie(C, R) = queue.top();
		queue.pop();
		if (R <= reach)
		{
			continue;
		}
		result += C;
		reach = R;
		end = upper_bound(query,reach);
		for (auto ite = query.begin(); ite != end; ++ite)
		{
			queue.emplace(std::get<2>(*ite), std::get<1>(*ite));
		}
		query.erase(query.begin(), end);
	}
	std::cout << (reach != N ? -1 : result) << std::endl;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios_base::sync_with_stdio(false);
	std::cout << std::fixed << std::setprecision(15);
	Main();
}