import java.util.*

fun main(args:Array<String>) {
    val (n,m) = readLine()!!.split(" ").map { it.toInt() }
    val roadList = (1..m).map { Road(readLine()!!.split(" "))}
    val roadDic = TreeMap<Int,TreeMap<Int,Long>>()
    for(r in roadList) {
        if(!roadDic.containsKey(r.fromPos)) {
            roadDic[r.fromPos] = TreeMap()
        }
        if(roadDic[r.fromPos]!!.containsKey(r.toPos)) {
            roadDic[r.fromPos]!![r.toPos] = Math.min(roadDic[r.fromPos]!![r.toPos]!!, r.len)
        } else {
            roadDic[r.fromPos]!![r.toPos] = r.len
        }
    }
    val map = (1..n).map { Long.MAX_VALUE }.toMutableList()
    map[0] = 0
    val que = Stack<Int>()
    que.push(0)
    val fromKeyList = roadDic.keys.toList()
    var lastIndex = 0
    while(que.isNotEmpty()) {
        val current = que.pop()
        for(fromIndex in (lastIndex until fromKeyList.size)) {
            val fromKey = fromKeyList[fromIndex]
            if(fromKey > current) {
                break
            }
            if(roadDic[fromKey]!!.firstKey()*-1 < current) {
                lastIndex++
                continue
            }
            var min = Long.MAX_VALUE
            for(toKey in roadDic[fromKey]!!.keys) {
                val toPos = toKey * -1
                if(toPos < current) {
                    break
                }
                val len = roadDic[fromKey]!![toKey]!! + map[current]
                if(min < len) {
                    break
                }
                min = len
                if(map[toPos] > len) {
                    map[toPos] = len
                    que.push(toPos)
                }
            }
        }
    }
    if(map.last() == Long.MAX_VALUE) {
        println(-1)
    } else {
        println(map.last())
    }
}

class Road(val inpt:List<String>) {
    val fromPos = inpt[0].toInt() - 1
    val toPos = (inpt[1].toInt() - 1) * -1
    val len = inpt[2].toLong()
}