Go: package main

import (
	"bufio"
	_ "container/heap"
	"fmt"
	"os"
	_ "sort"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func lcm(a, b int) int {
	return a / gcd(a, b) * b
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
const MOD = int(1e+9) + 7
const INF = 1 << 60

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	defer wtr.Flush()
	for true {
		N, M := sc.nextInt(), sc.nextInt()
		if N == 0 && M == 0 {
			break
		}
		C := sc.nextIntSlice(M)
		X := sc.nextIntSlice(N)
		dp := make([][256]int, N+1)
		for i := 0; i < N+1; i++ {
			for j := 0; j < 256; j++ {
				dp[i][j] = INF
			}
		}
		dp[0][128] = 0
		for i := 0; i < N; i++ {
			for j := 0; j < 256; j++ {
				if dp[i][j] == INF {
					continue
				}
				for k := 0; k < M; k++ {
					nex := j + C[k]
					if nex < 0 {
						nex = 0
					}
					if nex > 255 {
						nex = 255
					}
					dp[i+1][nex] = min(dp[i+1][nex], dp[i][j]+pow(X[i]-nex, 2))
				}
			}
		}
		ans := INF
		for i := 0; i < 256; i++ {
			ans = min(ans, dp[N][i])
		}
		fmt.Fprintln(wtr, ans)
	}
}

C++: //#include "bits/stdc++.h"

#include <iostream>
#include <vector>
// #include <map>
// #include <string>
#include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

int n;
int m;
uint x;
vector<int> c;
bool bef = 0;
bool aft = 1;
const int size = 255 + 1;
vector<vector<uint>> vec(2, vector<uint>(size, -1));

inline uint mse(uint a, uint b) {
	uint diff = (a > b) ? (a - b) : (b - a);
	return diff * diff;
}

uint calcBefMin(int i) {
	uint min_val = -1;
	for (auto v : c) {
		int key = i - v;
		if (key < 0 or key > 255) {
			continue;
		}
		min_val = min(min_val, vec[bef][key]);
	}
	return min_val;
}

inline uint myplus(uint a, uint b) {
	uint m = -1;
	if (b <= m - a) { // check overflow
		return a + b;
	}
	return -1;

}

void solve() {
	c.resize(m);
	for (int i = 0; i < m; i++) {
		cin >> c[i];
	}
	int c_max = *std::max_element(begin(c), end(c));
	int c_min = *std::min_element(begin(c), end(c));
	for (int i = 0; i <= 255; i++) {
		vec[bef][i] = -1;
	}
	vec[bef][128] = 0;
	for (int i = 0; i < n; i++) {
		cin >> x;
		int bef_key;
		// do
		for (bef_key = 1; bef_key <= 254; bef_key++) {
			vec[aft][bef_key] = myplus(calcBefMin(bef_key), mse(bef_key, x));
		}
		// do for 255
		bef_key = 255;
		uint min_val = -1;
		for (int key = bef_key - c_max; key <= 255; key++) {
			min_val = min(min_val, vec[bef][key]);
		}
		vec[aft][bef_key] = myplus(min_val, mse(bef_key, x));
		// do for 0
		bef_key = 0;
		min_val = -1;
		for (int key = bef_key - c_min; key >= 0; key--) {
			min_val = min(min_val, vec[bef][key]);
		}
		vec[aft][bef_key] = myplus(min_val, mse(bef_key, x));
		// for next
		bef = !bef;
		aft = !aft;
	}
	cout << *std::min_element(begin(vec[bef]), end(vec[bef])) << endl; // bef is correct, because swaped
}

int main() {


	while (true) {
		cin >> n >> m;
		if (n == 0 and m == 0) {
			break;
		}
		solve();
	}

	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}

C++: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int main(){
    int n, m;
    
    while(cin >> n >> m, n+m != 0){
        int c[m];
        rep(i, m)cin >> c[i];

        int x[n];
        rep(i, n)cin >> x[i];

        //dp[i][j] : 列iのyがjであるときの二乗和の最小数
        ll dp[n+1][256];
        rep(i, n+1)rep(j, 256)dp[i][j] = INF;

        dp[0][128] = 0;
        rep(i, n)rep(j, 256)rep(k, m){
            if(dp[i][j] >= INF)continue;

            int next = max(0, (int)j + c[k]); 
            next = min(next, 255);

            ll dif = pow(x[i] - next, 2);

            dp[i+1][next] = min(dp[i+1][next], dp[i][j] + dif);
        }

        ll ans = INF;
        rep(i, 256)ans = min(ans, dp[n][i]);

        cout << ans << endl;
    }

}

C++: #include <iostream>
#include <algorithm>

using namespace std;

inline int adj(int x) {
  return min(max(x, 0), 255);
}
#define SQ(x) ((x)*(x))
int dp[2][256];
int main() {
  int N, M, INF=1<<28;
  while(cin >> N >> M && (N|M)) {
    int C[M];
    for(int i=0; i<M; i++) cin >> C[i];
    fill(dp[0], dp[0]+512, INF);
    dp[0][128] = 0;
    for(int k=0; k<N; k++) {
      for(int i=0; i<256; i++) dp[(k+1)%2][i] = INF;
      int in; cin >> in;
      for(int i=0; i<256; i++) {
        if(dp[k%2][i]>=INF) continue;
        for(int j=0; j<M; j++) {
          dp[(k+1)%2][adj(i+C[j])] = min(dp[(k+1)%2][adj(i+C[j])], dp[k%2][i]+SQ(in-adj(i+C[j])));
        }
      }
    }
    cout << *min_element(dp[N%2], dp[(N%2)+1]) << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = (1 << 29);
using namespace std;

int dis(int a, int b){
    return (a - b) * (a - b);
}

int solve(int c[20], int x[20001], int n, int m){
    int dp[256][2];
    rep(i,256) rep(j,2) dp[i][j] = INF;
    dp[128][0] = 0;

    rep(j,n){
        rep(i,256){
            rep(k,m){
                int temp = i + c[k];
                if(temp < 0) temp = 0;
                else if(temp > 255) temp = 255;
                dp[temp][(j + 1) % 2] = min(dp[temp][(j + 1) % 2], dp[i][j % 2] + dis(temp, x[j]));
                /*
                if(dp[i][(j + 1) % 2] < 0){
                    cout << i << ' ' << j << ' ' << k << endl;
                    cout << dp[i][(j + 1) % 2] << endl;
                    cout << dp[temp][j % 2] + dis(i, x[j]) << endl;
                }
                */
            }
        }
        rep(i,256) dp[i][j % 2] = INF;
    }
    int ret = INF;
    rep(i,256){
        ret = min(ret, dp[i][n % 2]);
    }
    return ret;
}

int main(){
    int n, m;
    while(cin >> n >> m,n||m){
        int c[20], x[20001];
        rep(i,m) cin >> c[i];
        rep(i,n) cin >> x[i];
        cout << solve(c,x,n,m) << endl;
    }

}