#include<bits/stdc++.h>

using namespace std;
#define INFS (1LL<<28)
#define INF (1LL<<60)
#define DEKAI 1000000007
#define MOD 998244353
#define lp(i,n) for(int i=0;i<n;i++)
#define double long double
#define all(c) begin(c), end(c)

#define int long long 

namespace {
#define __DECLARE__(C)    \
    template <typename T> \
    std::ostream &operator<<(std::ostream &, const C<T> &);

#define __DECLAREM__(C)               \
    template <typename T, typename U> \
    std::ostream &operator<<(std::ostream &, const C<T, U> &);

__DECLARE__(std::vector)
__DECLARE__(std::deque)
__DECLARE__(std::set)
__DECLARE__(std::stack)
__DECLARE__(std::queue)
__DECLARE__(std::priority_queue)
__DECLARE__(std::unordered_set)
__DECLAREM__(std::map)
__DECLAREM__(std::unordered_map)

template <typename T, typename U>
std::ostream &operator<<(std::ostream &, const std::pair<T, U> &);
template <typename... T>
std::ostream &operator<<(std::ostream &, const std::tuple<T...> &);
template <typename T, std::size_t N>
std::ostream &operator<<(std::ostream &, const std::array<T, N> &);

template <typename Tuple, std::size_t N>
struct __TuplePrinter__ {
    static void print(std::ostream &os, const Tuple &t) {
        __TuplePrinter__<Tuple, N - 1>::print(os, t);
        os << ", " << std::get<N - 1>(t);
    }
};

template <typename Tuple>
struct __TuplePrinter__<Tuple, 1> {
    static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }
};

template <typename... T>
std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {
    os << '(';
    __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);
    os << ')';
    return os;
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {
    return os << '(' << v.first << ", " << v.second << ')';
}

#define __INNER__                             \
    os << '[';                                \
    for (auto it = begin(c); it != end(c);) { \
        os << *it;                            \
        os << (++it != end(c) ? ", " : "");   \
    }                                         \
    return os << ']';

template <typename T, std::size_t N>
std::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {
    __INNER__
}

#define __DEFINE__(C)                                           \
    template <typename T>                                       \
    std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
        __INNER__                                               \
    }

#define __DEFINEM__(C)                                             \
    template <typename T, typename U>                              \
    std::ostream &operator<<(std::ostream &os, const C<T, U> &c) { \
        __INNER__                                                  \
    }

#define __DEFINEW__(C, M1, M2)                                  \
    template <typename T>                                       \
    std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
        std::deque<T> v;                                        \
        for (auto d = c; !d.empty(); d.pop()) v.M1(d.M2());     \
        return os << v;                                         \
    }

__DEFINE__(std::vector)
__DEFINE__(std::deque)
__DEFINE__(std::set)
__DEFINEW__(std::stack, push_front, top)
__DEFINEW__(std::queue, push_back, front)
__DEFINEW__(std::priority_queue, push_front, top)
__DEFINE__(std::unordered_set)
__DEFINEM__(std::map)
__DEFINEM__(std::unordered_map)
}

using Weight = int;
using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph &g, int a, int b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
    g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }
/*
vector<pair<int,int>> v;
int good[2][100001];
set<pair<int,int>> m;
map<int,int> place[2];
signed main(){
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	lp(i,n){
		cin>>a[i];
	}
	lp(i,n){
		cin>>b[i];
	}
	lp(i,n){
		v.push_back({a[i],b[i]});
		m.insert({a[i],b[i]});
	}
	sort(all(a));
	sort(all(b));
	lp(i,n){
		place[0][a[i]]=i;
		place[1][b[i]]=i;
	}
	a.push_back(INF);
	b.push_back(INF);
	bool ans=true;
	lp(i,n){
		if(a[i]>b[i]){
			cout<<"No"<<endl;
			return 0;
		}
		if(a[i]<=b[i+1])good[0][i]=1;
		if(b[i]>=a[i+1])good[1][i]=1;
	}
	lp(i,n){
		if(m.find({a[i],b[i]})!=m.end()){
			cout<<"Yes"<<endl;
			return 0;
		}
	}
	for(int i=100000;i>=0;i--){
		int befa=-1,befb=-1;
		if(good[0][i]==0){
			good[0][i]=-1;
			befa=-1;
		}
		else{
			if(befa==-1) befa=i;
			good[0][i]=befa;
		}
		if(good[1][i]==0){
			good[1][i]=-1;
			befb=-1;
		}
		else{
			if(befb==-1) befb=i;
			good[1][i]=befb;
		}
	}

	lp(i,n){
		int ax=v[i].first;
		int bx=v[i].second;
		int pa=place[0][ax];
		int pb=place[1][bx];
		if(!ax<=bx)continue;
		if(pb<pa){
			if(good[0][pb]>=pa-1){
				cout<<"Yes"<<endl;
				return 0;
			}
		}
		else{
			if(good[1][pa]>=pb-1){
				cout<<"Yes"<<endl;
				return 0;
			}
		}
	}
	cout<<"No"<<endl;
	return 0;
}
*/
#define HOO pair<int,pair<int,int>> 
signed main(){
	int n,m;
	cin>>n>>m;
	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> mini;
	priority_queue<HOO,vector<HOO>,greater<HOO>> task;
	lp(i,m){
		int a,b,c;
		cin>>a>>b>>c;
		task.push({a,{c,b}});
	}
	lp(i,m){
		int start=task.top().first;
		int cost=task.top().second.first;
		int end=task.top().second.second;
		task.pop();
		if(start==1){
			mini.push({cost,end});
		}
		else{
			while(1){
				if(mini.empty()){
					cout<<-1<<endl;
					return 0;
				}
				if(mini.top().second<start){
					mini.pop();
				}
				else break;
			}
			mini.push({cost+mini.top().first,end});
		}
	}
	while(1){
		if(mini.empty()){
			cout<<-1<<endl;
			return 0;
		}
		if(mini.top().second==n){
			cout<<mini.top().first<<endl;
			return 0;
		}
		mini.pop();
	}
	return 0;
}

Measured memory_bytes: 6640