import java.math.*
import java.util.ArrayDeque

fun main(args: Array<String>) {
    val (N, M) = readInputLine().split(" ").map { it.toInt() }
    
    val edges = Array(N) { mutableMapOf<Int, Long>() }

    for (i in N - 1 downTo 1) {
        edges[i].put(i - 1, 0L)
    }

    for (i in 1..M) {
        val (L, R, C) = readInputLine().split(" ").map { it.toInt() }
        val maxCost = edges[L - 1].get(R - 1) ?: Long.MAX_VALUE
        edges[L - 1].put(R - 1, Math.min(C.toLong(), maxCost))
    }
    
    val cost = LongArray(N) { Long.MAX_VALUE }
    cost[0] = 0L
    val queue = ArrayDeque<Int>()
    
    queue.add(0)
    
    while (!queue.isEmpty()) {
        val node = queue.poll()!!
        
        for ((k, v) in edges[node]) {
            if (cost[k] > cost[node] + v) {
                cost[k] = cost[node] + v
                queue.add(k)
            }
        }
    }

    if (cost[N - 1] == Long.MAX_VALUE) {
        println(-1)
    } else {
        println(cost[N - 1])
    }
}

fun readInputLine(): String {
    return readLine()!!
}
