STDOUT.sync = true
Inf = 256 * 256 * 20000
$dp = [Array.new(256,Inf), Array.new(256,Inf)]
while line = STDIN.gets
  $N, $M = line.split(" ").map{|s| s.to_i}
  break if $N == 0
  $C = []
  (1..$M).each { $C << STDIN.gets.to_i }
  $dp[0].fill(Inf)
  $dp[0][128] = 0
  (1..$N).each { |i|
    x = STDIN.gets.to_i
    a = $dp[1-i%2]
    b = $dp[i%2]
    b.fill(Inf)
    a.each_index { |prev|
      next if a[prev] >= Inf
      $C.each { |c|
        t = prev + c
        t = 255 if t > 255 # t = [t,255].min は遅い
        t = 0 if t < 0
        cost = (x-t)*(x-t) + a[prev]
        b[t] = cost if b[t] > cost
      }
    }
  }
  puts $dp[$N%2].min
end