## main

d, n = gets.strip.split(' ').map{|s| s.to_i}

ts = d.times.map{gets.strip.to_i}
wears = n.times.map{gets.strip.split(' ').map{|s| s.to_i}}
#p ts
#p wears

max_dc = []

for i in (0...n)
  if wears[i][0] <= ts[0] && ts[0] <= wears[i][1]
    max_dc << [wears[i][2], 0]
  end
end

for j in (1...d)
  p_max_dc = max_dc
  max_dc = []

  for i in (0...n)
    if wears[i][0] <= ts[j] && ts[j] <= wears[i][1]
      ci = wears[i][2]
      sdc = 0
      for pmdc in p_max_dc
        pci, psdc = pmdc
        dc = (ci - pci).abs + psdc
        sdc = dc if sdc < dc
      end
      max_dc << [ci, sdc]
    end
  end
end

puts max_dc.map{|mdc| mdc[1]}.max