while (n, m = gets.split.map(&:to_i)).inject(:+) != 0 do
  c = Array.new
  m.times do
    c.push(gets.to_i)
  end
  x = Array.new
  n.times do
    x.push(gets.to_i)
  end
  dp = Array.new(n+1) {Array.new(255+1, (1e10).to_i)}
  dp[0][128] = 0
  for i in 0...n do
    for j in 0..255 do
      for k in 0...m do
        l = [j+c[k], 255].min
        dp[i+1][l] = [dp[i+1][l], dp[i][j]+(x[i]-l)**2].min
      end
    end
  end
  puts dp[n].min
end