<?php
list($n, $m) = array_map("intval", explode(" ", trim(fgets(STDIN))));
for($i = 2; $i <= $n; $i++){
  $map[$i][$i-1] = 0;
}
for($i = 0; $i < $m; $i++){
  list($from, $to, $d) = array_map("intval", explode(" ", trim(fgets(STDIN))));;
  $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]);
