import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; 
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint, m = rint;
	
	P[] ps;
	foreach(_; 0 .. m){
		int l = rint - 1, r = rint - 1;
		long c = rlong;
		ps ~= P(l, r, c);
	}
	ps.sort!"a.l<b.l"();
	log("ps:", ps);
	
	P[] qs;
	auto ph = new RedBlackTree!(P, "a.c<b.c", true);
	
	int i = 0, pi = 0;
	while(i < n - 1){
		log("i:", i, "pi:", pi);
		while(pi < ps.length && ps[pi].l <= i){
			ph.insert(ps[pi]);
			pi ++;
		}
		log("pi:", pi, "ph:", ph[]);
		while(ph.length && ph.front.r < i) ph.removeFront;
		log("ph:", ph[]);
		if(ph.length == 0){
			"-1".writeln;
			return;
		}
		qs ~= ph.front;
		i = ph.front.r;
		ph.removeFront;
		log("qs:", qs, "ph:", ph[]);
	}
	
	qs.sort!"a.r>b.r"();
	log("qs:", qs);
	
	P[] ans;
	auto qh = new RedBlackTree!(P, "a.c<b.c", true);
	
	i = n - 1;
	int qi = 0;
	while(i > 0){
		log("i:", i, "qi:", qi);
		while(qi < qs.length && qs[qi].r >= i){
			qh.insert(qs[qi]);
			qi ++;
		}
		log("qi:", qi, "qh:", qh[]);
		while(qh.length && qh.front.l > i) qh.removeFront;
		log("qh:", qh[]);
		if(qh.length == 0){
			"-1".writeln;
			return;
		}
		ans ~= qh.front;
		i = qh.front.l;
		qh.removeFront;
		log("ans:", ans, "qh:", qh[]);
	}
	
	
	ans.map!(x => x.c).sum.writeln;
}

struct P{
	int l, r;
	long c;
}
