n, m = gets.split.map(&:to_i)
l = gets.split.map(&:to_i)
a = [0]*n
l.each_cons(2){|s, t| s,t = t,s if s > t; a[s-1] += 1; a[t-1] -= 1 }
(1...n-1).each{ |i| a[i] += a[i-1] }
p (n-1).times.map{|i| gets.split.map(&:to_i).zip([a[i],a[i],1]).map{|a,b|a*b}.insert(1,0).each_slice(2).to_a.map{|a,b|a+b}.min}.reduce(:+)