n,m=gets.split.map(&:to_i)
node = (0..n).map{[]}

m.times{
  l,r,c=gets.split.map(&:to_i)
  node[l] << [r,c]
}

path_list = [[1,0]]
min = 0
available_branch = []
(1..n-1).each{|ni|
#  p [ni,available_branch,path_list,min]
  while (available_branch[0] && available_branch[0][0] <= ni)
    available_branch.shift
  end
  branch_list = node[ni]
  available_branch = (available_branch + branch_list).sort if branch_list != []
  while path_list[0] && path_list[0][0] == ni
    now,cost = path_list.shift
    if min == cost
      available_branch.each{|r,c|
        path_list << [r,cost+c]
      }
      path_list.sort!
      min = path_list.map{|a,b|b}.min
    end
  end
}
p min||-1