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 <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cstdio>

using namespace std;

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

#define all(v) v.begin(),v.end()
#define INF 1<<30
#define mp
#define pb push_back

#define fi first
#define se second

#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)

typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pii;
typedef vector<pair<int, int> > vp;
typedef vector<string> vs;

typedef long long ll;

int main(){
  while(1){
    int n,m; cin>>n>>m;
    if(n==0) break;
    vii dp(n+1,vi(256,INF));
    vi c(m); rep(i,m) cin>>c[i];
    vi x(n); rep(i,n) cin>>x[i];
    dp[0][128] = 0;
    rep(i,n){
      rep(j,256){
        if(dp[i][j]==INF) continue;
        rep(k,m){
          int y = max(0,min(255,j+c[k]));
          dp[i+1][y] = min(dp[i+1][y],dp[i][j]+(x[i]-y)*(x[i]-y));
        }
      }
    }
    int ans = INF;
    rep(i,256) ans = min(ans,dp[n][i]);
    cout<<ans<<endl;;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;

// →↑←↓
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};


const double EPS = 1e-9;


const int INF = 1 << 29;
int main(){
    while(true){
        int N,M;
        cin >> N >> M;
        if(N == 0 and M == 0) break;

        vector<int> C(M);
        for(int i=0;i<M;i++) cin >> C[i];

        vector<int> DP(256,INF);
        DP[128] = 0;
        for(int ind_n=0;ind_n<N;ind_n++){
            vector<int> NEX(256,INF);
            int x;
            cin >> x;
            for(int j=0;j<=255;j++){
                for(int i=0;i<M;i++){
                    if(DP[j] != INF){
                        int now = j + C[i];
                        if(now < 0) now = 0;
                        else if(now > 255) now = 255;
                        NEX[now] = min(NEX[now],DP[j]+(now-x)*(now-x));
                    }
                }
            }
            DP = NEX;
        }

        int mix = INF;
        for(int i=0;i<=255;i++){
            mix = min(DP[i],mix);
        }
        cout << mix << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++ )
#define NMAX 20001
#define MMAX 16
#define INFTY (1<<25)
int N, M, C[MMAX], X[NMAX], T[256][NMAX];

int compute(){
    rep(i, 256) rep(j, N+1) T[i][j] = INFTY;
    T[128][0] = 0;
    rep(j, N){
	rep(i,256){
	    if ( T[i][j] == INFTY ) continue;
	    rep(k, M){
		int p = i + C[k];
		if ( p > 255 ) p = 255;
		if ( p < 0 ) p = 0;
		T[p][j+1] = min(T[p][j+1], T[i][j] + (X[j+1]-p)*(X[j+1]-p));
	    }
	}
    }
    int minv = INFTY;
    rep(i, 256) minv = min(minv, T[i][N]);
    return minv;
}

main(){
    while(cin >> N >> M && N ){
	rep(i, M) cin >> C[i];
	rep(i, N) cin >> X[i+1];
	cout << compute() << endl;
    }
}