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>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for(LL i = 0; i < n; ++i)
#define FOR(i, a, n) for(LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

//#define int long long

/*************** using variables ***************/
int n, m;
vector<int> c, x;
LL dp[20005][256];
/**********************************************/

signed main(){
    while(cin >> n >> m, n != 0 || m != 0){
        c.resize(m);
        x.resize(n);
        REP(i, m) cin >> c[i];
        REP(i, n) cin >> x[i];

        REP(i, 20005) REP(j, 256) dp[i][j] = INF;
        dp[0][128] = 0;
        FOR(i, 1, n+1){
            for(int j = 0; j <= 255; j++){
                bool flag = false;
                if(j == 0){
                    REP(k, m){
                        for(int z = 0 - c[k]; z >= 0; z--){
                            if(dp[i-1][z] != INF){
                                dp[i][0] = min(dp[i][0], dp[i-1][z] + (LL)pow(abs(x[i-1] - (0)), 2));
                            }
                        }
                    }
                }else if(j == 255){
                    REP(k, m){
                        for(int z = 255 - c[k]; z <= 255; z++){
                            if(dp[i-1][z] != INF){
                                dp[i][255] = min(dp[i][255], dp[i-1][z] + (LL)pow(abs(x[i-1] - (255)), 2));
                            }
                        }
                    }
                }else{
                    REP(k, m) flag |= dp[i-1][j-c[k]] != INF;
                    if(flag){
                        REP(k, m){
                            if(j-c[k] >= 0 && j-c[k] <= 255 && dp[i-1][j-c[k]] != INF){
                                dp[i][j] = min(dp[i][j], dp[i-1][j-c[k]] + (LL)pow(abs(x[i-1] - j), 2));
                                //cout << i << " " << dp[i][j] << " " << x[i-1] << " " << j-c[k] << endl;
                            }
                        }
                    }
                }
            }
        }

        LL ans = INF;
        for(int i = 0; i <= 255; i++) ans = min(ans, dp[n][i]);
        cout << ans << endl;
    }

}

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

#define REP(i,num) for(ll i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){T x; cin >> x; return (x);}
template<typename T=ll,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

template<typename T>
bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>
bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	while(true){
		int N=in(),M=in();
		if(!N && !M) break;
		vll C=vecin(M);
		vll X=vecin(N);
		
		vvll dp(N+1,vll(256,1ll<<60));
		dp[0][128]=0;
		REP(i,N){
			REP(j,256){
				REP(k,M){
					int next = max<int>(min<int>(j+C[k],255),0);
					chmin(dp[i+1][next],dp[i][j]+(next-X[i])*(next-X[i]));
				}
			}
		}

		out(*min_element(ALL(dp[N])));
	}
	return 0;
}

C++: #include <iostream>
#define INF 1e+18
#define int long long
using namespace std;

int dp[20001][256];

signed main(){
	int n,m,c[16];
	while(cin >> n >> m,n){
		for(int i = 0;i <= n;i++){
			for(int j = 0;j < 256;j++) {
				if(i == 0 && j == 128) dp[i][j] = 0;
				else dp[i][j] = INF;
			}
		}
		for(int i = 0;i < m;i++) cin >> c[i];
		for(int i = 0;i < n;i++){
			int x;
			cin >> x;
			for(int j = 0;j < 256;j++){
				for(int k = 0;k < m;k++){
					int d = j + c[k];
					if(d < 0) d = 0;
					else if(d > 255) d = 255;
					dp[i + 1][d] = min(dp[i + 1][d],dp[i][j] + (x - d) * (x - d));
				}
			}
		}
		int mi = INF;
		for(int i = 0;i < 256;i++) mi = min(mi,dp[n][i]);
		cout << mi << endl;
	}
	return 0;
}