loop do
    n, m = gets.split.map(&:to_i)
    break if n == 0 && m == 0
    c = m.times.map{gets.to_i}
    x = n.times.map{gets.to_i}
    dp = Hash.new(Float::INFINITY)
    dp[128] = 0
    x.each_with_index do |xx,i|
        dp2 = Hash.new(Float::INFINITY)
        dp.each do |k,v|
            c.each do |cc|
                num = k + cc
                num = 0 if num < 0
                num = 255 if num > 255
                dp2[num] = v + (num - xx)**2 if v + (num - xx)**2 < dp2[num]
            end
        end
        dp = dp2
    end
    puts dp.values.min
end