import java.math.*

fun main(args: Array<String>) {
    val (N, M) = readInputLine().split(" ").map { it.toInt() }
    
    val route = mutableListOf<Triple<Int, Int, Long>>()
    
    for (i in 1..M) {
        val (L, R, C) = readInputLine().split(" ").map { it.toInt() }
        route.add(Triple(L - 1, R - 1, C.toLong()))
    }
    
    route.sortBy { it.first }
    route.sortByDescending { it.second }
    
    var size = 1
    while (size < N) {
        size *= 2
    }
    
    val segTree = LongArray(size * 2 - 1) { Long.MAX_VALUE }
    
    fun update(x: Int, value: Long) {
        var tmp = x + size - 1
        
        segTree[tmp] = value
        while (tmp > 0) {
            tmp = (tmp - 1) / 2
            segTree[tmp] = Math.min(segTree[2 * tmp + 1], segTree[2 * tmp + 2])
        }
    }
    
    fun getMin(a: Int, b: Int, k: Int = 0, l: Int = 0, r: Int = size): Long {
        if (r <= a || b <= l) {
            return Long.MAX_VALUE
        }
        if (a <= l && r <= b) {
            return segTree[k]
        }
        
        val vl = getMin(a, b, 2 * k + 1, l, (l + r) / 2)
        val vr = getMin(a, b, 2 * k + 2, (l + r) / 2, r)
        
        return Math.min(vl, vr)
    }
    
    update(N - 1, 0L)

    for (r in route) {
        val (L, R, C) = r

        val nowCost = getMin(L, R + 1)
        val compCost = getMin(0, L)
        
        if (nowCost == Long.MAX_VALUE) {
            break
        }

        if (compCost > nowCost + C) {
            update(L, nowCost + C)
        }
    }

    if (segTree[size - 1] == Long.MAX_VALUE) {
        println(-1)
    } else {
        println(segTree[size - 1])
    }
}

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