import java.util.*

fun main(args: Array<String>) {
    val (n, m) = readLine()!!.split(' ').map(String::toLong)
    val lrcList = mutableListOf<Triple<Long, Long, Long>>()
    (1..m).forEach {
        val (l, r, c) = readLine()!!.split(' ').map(String::toLong)
        lrcList.add(Triple(l, r, c))
    }
    lrcList.sortBy { it.third }
    lrcList.sortBy { it.first }
    val lrcG = lrcList.groupBy { it.first }

    val minDist = Array(n.toInt() + 1) { Long.MAX_VALUE / 2 }
    minDist[1] = 0

    val queue = PriorityQueue<Pair<Long, Long>>(n.toInt(), Comparator { o1: Pair<Long, Long>?, o2: Pair<Long, Long>? -> if (o1!!.second < o2!!.second) -1 else 1 })
    queue.add(Pair(1, 0))
    while (queue.isNotEmpty()) {
        val (cur, curDist) = queue.poll()
        if(lrcG.containsKey(cur).not()) continue
        val branches = lrcG[cur]!!
        for (branch in branches) {
            val (l, r, c) = branch
            for (i in (l + 1)..r) {
                val nextDist = curDist + c
                if (minDist[i.toInt()] > nextDist) {
                    minDist[i.toInt()] = nextDist
                    queue.add(Pair(i, nextDist))
                }
            }
        }
    }

    if (minDist[n.toInt()] > Long.MAX_VALUE / 3) {
        println(-1)
    } else {
        println(minDist[n.toInt()])
    }
}