import java.util.*

fun main(args: Array<String>) {
    val (N, M) = readLine()!!.split(" ").map{it.toInt()}

    val es = Array(N) { ArrayList <Pair<Long, Long>>() }

    for (i in 1..M) {
        val (L, R, C) = readLine()!!.split(" ").map{it.toInt()}
        es[L - 1].add(Pair(R - 1L, C.toLong()))
    }

    val d = TreeMap<Long, Long>()
    val INF = 1e18.toLong()

    d[0L] = 0L

    for (i in 0 until N - 1) {
        val e = d.ceilingEntry(i.toLong())

        if (e == null) {
            continue
        }

        for (p in es[i]) {
            val f = d.ceilingEntry(p.first)

            if (f == null || f.value > e.value + p.second) {
                d[p.first] = e.value + p.second

                while (true) {
                    val g = d.lowerEntry(p.first)
                    if (g == null || g.value < e.value + p.second) break
                    d.remove(g.key)
                }
            }
        }
    }

    val e = d.ceilingEntry(N - 1L)
    println(e?.value ?: -1)
}
