<?php

fscanf(STDIN, "%d %d", $n, $m);

for($i = 0; $i<$m; $i++){
    $temp= explode(" ",trim(fgets(STDIN)));
    if(isset($map[$temp[0]][$temp[2]])){
        $map[$temp[0]][$temp[2]] = max($map[$temp[0]][$temp[2]], $temp[1]);
    }else {
        $map[$temp[0]][$temp[2]] = $temp[1];
    }
    if(isset($mmap[$temp[0]])){
        $mmap[$temp[0]] = max($mmap[$temp[0]], $temp[1]);
    }else{
        $mmap[$temp[0]] = $temp[1];
    }

}

ksort($map);
$cmap = array_fill(2,$n-1,PHP_INT_MAX);
$cmap[1] = 0;

foreach($map as $start => $value){
    ksort($value);
    $startcost = $cmap[$start];
    if($startcost == PHP_INT_MAX){
        echo -1;
        exit;
    }
    $k=0;
    foreach($value as $cost=>$end){
        if($k==0){
            $k=1;
            $mincost = $startcost + $cost;
            $update = 0;
            for($i=$start;$i<=$mmap[$start];$i++){
                if($cmap[$i] >= $mincost){
                    $update = $i;
                    break;
                }
            }
            if($update == 0){
                break;
            }
        }
        $cmap = array_replace($cmap, array_fill($update,$end - $update + 1, $startcost + $cost));
        $update = $end + 1;

    }
}
if($cmap[$n]==PHP_INT_MAX){
    echo -1;
}else{
    echo $cmap[$n];
}
