<?php
list($n, $m) = ints();
for($i = 0; $i < $m; $i++){
  $map[] = ints();
}
array_multisort($map);
//o($map);
$d[1] = 0;
for($i = 2; $i <= $n; $i++){
  $d[$i] = 10**18;
}
for($i = 0; $i < $m; $i++){
  for($j = $map[$i][0] + 1; $j <= $map[$i][1]; $j++){
//    o($j, min($d[$j], $d[$map[$i][0]] + $map[$i][2]));
    $d[$j] = min($d[$j], $d[$map[$i][0]] + $map[$i][2]);
  }
}
if($d[$n] == 10**18){
  echo -1;
}else{
  echo $d[$n];
}

function str(){
  return trim(fgets(STDIN));
}
function ints(){
  return array_map("intval", explode(" ", trim(fgets(STDIN))));
}
function int(){
  return intval(trim(fgets(STDIN)));
}
function o(...$val){
    if(count($val)==1)$val = array_shift($val);
    $trace = debug_backtrace();
    echo $trace[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0){
            echo "empty array";
        }elseif(!is_array(current($val))){
            echo "array:";
            echo implode(" ", $val)."\n";
        }else{
            echo "array:array\n";
            foreach($val as $row){
                echo implode(" ", $row)."\n";
            }
        }
    }else{
        echo $val."\n";
    }
}