import java.util.PriorityQueue

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

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

    for (i in 1..M) {
        val (L, R, C) = readInputLine().split(" ").map { it.toInt() }
        edges[L - 1].add(Pair(R - 1, C.toLong()))
    }
    
    val cost = LongArray(N) { Long.MAX_VALUE }
    cost[0] = 0L
    val queue = PriorityQueue<Pair<Int, Long>>(N) { x, y -> x.second.compareTo(y.second) }
    
    queue.add(Pair(0, 0L))
    
    while (!queue.isEmpty()) {
        val (s, c) = queue.poll()!!
        
        if (cost[s] < c) {
            continue
        }
        
        for ((ed, ec) in edges[s]) {
            if (cost[ed] > cost[s] + ec) {
                cost[ed] = cost[s] + ec
                queue.add(Pair(ed, cost[ed]))
            }
        }
    }

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

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