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;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define bFOR(i,n1,n2) for(int i=n1;i>=n2;i--)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<30)-1;
const double INFd=100000000000.0;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int ddx[6]={0,1,0,-1,1,-1},ddy[6]={-1,0,1,0,1,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int n,m;
int c[20];
int dp[20010][256];
int main(){
	while(1){
		cin>>n>>m;
		if(n==0&&m==0)break;
		REP(i,20010)
			REP(j,256)
			dp[i][j]=1e8;
		REP(i,m)cin>>c[i];
		dp[0][128]=0;
		for(int i=1;i<=n;i++){
			int x;
			cin>>x;
			for(int j=0;j<=255;j++){
				for(int k=0;k<m;k++){
					if(dp[i-1][j]==1e8)continue;
					int nl=j+c[k];
					if(nl>255)nl=255;
					if(nl<0)nl=0;
					dp[i][nl]=min(dp[i-1][j]+(x-nl)*(x-nl),dp[i][nl]);
				}
			}
		}
		int ans=1e8;
		for(int i=0;i<256;i++){
			ans=min(ans,dp[n][i]);
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define rep(i,n) REP(i, 0, n)
int square(int x){
    return x * x;
}
int compress(int x){
    if(x >= 256) return 255;
    if(x < 0) return 0;
    return x;
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    while(cin >> N >> M && N){
        vector<int> c(M), x(N);
        rep(i, M) cin >> c[i];
        rep(i, N) cin >> x[i];
        vector<vector<int> > dp(2, vector<int>(256, INF));
        //dp[i][j] := i???????????§???????????????j?????¨??????????????????????°????
        int crr = 0, nxt = 1;
        dp[crr][128] = 0;
        rep(k, N){
            rep(i, 256) dp[nxt][i] = INF;
            rep(i, 256) rep(j, M){
                int it = compress(i + c[j]);
                dp[nxt][it] = min(dp[nxt][it], dp[crr][i] + square(it - x[k]));
            }
            swap(crr, nxt);
        }
        int ans = INF;
        rep(i, 256) ans = min(ans, dp[crr][i]);
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pll> vp;
typedef vector<ll> vll;

const ll inf = 1e15;
ll dp[20001][256];

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int N, M;
    while (cin >> N >> M && N) {
        vi C(M);
        rep(i, M) cin >> C[i];
        vi x(N);
        rep(i, N) cin >> x[i];

        rep(i, N + 1) rep(j, 256) dp[i][j] = inf;
        dp[0][128] = 0;
        rep(i, N) {
            rep(j, 256) {
                if (dp[i][j] == inf) continue;
                rep(k, M) {
                    int nxtj = j + C[k];
                    if (nxtj < 0) nxtj = 0;
                    if (nxtj > 255) nxtj = 255;
                    dp[i + 1][nxtj] = min(dp[i + 1][nxtj], dp[i][j] + (x[i] - nxtj) * (x[i] - nxtj));
                }
            }
        }

        ll ans = inf;
        rep(j, 256) ans = min(ans, dp[N][j]);
        cout << ans << endl;

    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
typedef pair<int,int> P;
const int inf=1000000007;
const int MOD=1000000007;
int solve(int n,int m){
    vector<int> c(m);
    rep(i,m)cin>>c[i];
    vector<int> x(n+1,0);
    rep1(i,n)cin>>x[i];
    vector<vector<int>> dp(n+1,vector<int>(256,inf));
    dp[0][128]=0;
    rep1(i,n){
        rep(j,256){
            rep(k,m){
                int buf=j+c[k];
                buf=min(255,buf);buf=max(0,buf);
                int S=(buf-x[i])*(buf-x[i]);
                dp[i][buf]=min(dp[i][buf],S+dp[i-1][j]);
            }
        }
    }
    int res=inf;
    rep(i,256){
        res=min(res,dp[n][i]);
    }
    return res;
}
signed main(){
    vector<int> res;
    while(1){
        int n,m;cin>>n>>m;
        if(n==0&&m==0)break;
        res.push_back(solve(n,m));
    }
    for(auto& val:res){
        cout<<val<<endl;
    }
    return 0;
}