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 = []
  $M.times { $C << STDIN.gets.to_i }
  $dp[0].fill(Inf)
  $dp[0][128] = 0
  i = 1
  while i <= $N do
    x = STDIN.gets.to_i
    a = $dp[1-i%2]
    b = $dp[i%2]
    b.fill(Inf)
    prev = 0
    while prev < a.size do
      if a[prev] >= Inf
        prev += 1
        next
      end
      j = 0
      while j < $C.size do
        t = prev + $C[j]
        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
        j += 1
      end
      prev += 1
    end
    i += 1
  end
  puts $dp[$N%2].min
end