FROM = 0
TO = 1
COST = 2
MIN_COST = 3

def min(a, b)
  a < b ? a : b
end

def saiki(n_way:, n_cost:)
  target_ways = fetch_target_ways(from: n_way[FROM], to: n_way[TO])

  target_ways.sort_by { |way| way[COST] }.each do |way|
    next unless way[MIN_COST] > n_cost
    way[MIN_COST] = n_cost

    if way[TO] == N
      $min_answer = min($min_answer, n_cost + way[COST])
      next
    end

    way[MIN_COST] = n_cost
    saiki(n_way: way, n_cost: n_cost + way[COST])
  end
end

def fetch_target_ways(from:, to:)
  left = $ways.bsearch_index { |way| way[FROM] >= from + 1 }
  right = $reverse_ways.bsearch_index { |way| way[FROM] <= to }
  return [] if right.nil?
  $ways[left..(M - right - 1)]
end

N, M = gets.split.map(&:to_i)
$ways = M.times.map{ gets.split.map(&:to_i).push(Float::INFINITY) }.sort_by{|way| [way[FROM], way[TO]]}
$reverse_ways = $ways.reverse

$min_answer = Float::INFINITY

saiki(n_way: [0, 1, 0, 0], n_cost: 0)

if $min_answer == Float::INFINITY
  puts -1
else
  puts $min_answer
end