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

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

def saiki(n_way:, n_cost:)
#  output n_way
  target_ways = fetch_target_ways(from: n_way[FROM], to: n_way[TO], index: n_way[INDEX])
#  binding.pry

  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:, index:)
#  output from, to, index
  left = $ways[(index+1)..(M-1)].bsearch_index { |way| way[FROM] >= from + 1 }
  right = $reverse_ways[0..(M-1-left)].bsearch_index { |way| way[FROM] <= to }
  # output index
  # binding.pry
  return [] if right.nil?
  $ways[left..(M - right - 1)]
end

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

$min_answer = Float::INFINITY

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

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