<?php
list($n, $m) = ints();
for($i = 2; $i <= $n; $i++){
  $map[$i][$i-1] = 0;
}
for($i = 0; $i < $m; $i++){
  list($from, $to, $d) = ints();
  $map[$from][$to] = $d;
}
$D = [];
for($i = 1; $i <= $n; $i++){
  $D[$i] = $i == 1 ? 0 : 10**18;
}
$pq = new SplPriorityQueue();
$pq->insert(1, 0);
while($pq->count()){
  $node = $pq->extract();
  foreach($map[$node] as $to => $length){
    $alt = $D[$node] + $length;
    if($D[$to] > $alt){
      $D[$to] = $alt;
      $pq->insert($to, -$alt);
    }
  }
}
printf("%d", $D[$n] == 10**18 ? -1 : $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";
    }
}