def rec(step, x)
  return 0 if step == $n
  return $memo[step][x] if $memo[step][x]
  candidate = Array.new
  $m.times do |i|
    nx = x+$c[i]
    nx = [0, nx].max
    nx = [nx, 255].min
    candidate.push(rec(step+1, nx) + (nx - $x[step])**2)
  end
  $memo[step][x] = candidate.min
end

while ($n, $m = gets.split.map(&:to_i)).inject(:+) != 0 do
  $c = Array.new
  $memo = Array.new($n) {Array.new(256)}
  $m.times do
    $c.push(gets.to_i)
  end
  $x = Array.new
  $n.times do
    $x.push(gets.to_i)
  end
  p rec(0, 128)
end