import java.util.Scanner
import kotlin.system.exitProcess

fun main(args: Array<String>) {
	val scn = Scanner(System.`in`)
	val n = scn.nextInt()
	val m = scn.nextInt()
	val arr = arrayOfNulls<Int?>(n)
	arr[0] = 0
	val bs = Array(m){Triple(scn.nextInt()-1,scn.nextInt()-1,scn.nextInt())}
	bs.sortBy { it.first }
	bs.forEach {
		val (l, r, c) = it
		if (arr[l]==null) retire()
		for (i in l+1..r){
			arr[i] = if (arr[i]==null) arr[l]!!+c else Math.min(arr[i]!!, arr[l]!!+c)
		}
	}
	println(if (arr[n-1]!=null) arr[n-1] else -1)
}

fun retire(){
	println(-1)
	exitProcess(0)
}