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;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
#define fore(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep(i,n) fore(i,0,(n)-1)
#define rfore(i,a,b) for(ll i=(b);i>=(a);--i)
#define rrep(i,n) rfore(i,0,(n)-1)
#define all(x) (x).begin(),(x).end()
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
template<class T>
bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>
bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}

#define N 1000005
ll dp[N][256];
void solve(){
    while(1){
        ll n,m;cin>>n>>m;
        if(n==0&&m==0)return;
        vl c(m);rep(i,m)cin>>c[i];
        vl x(n);rep(i,n)cin>>x[i];

        rep(i,n+1)rep(j,256)dp[i][j]=LINF;
        dp[0][128]=0;
        rep(i,n){
            rep(j,256){
                rep(k,m){
                    ll to=max(0LL,min(j+c[k],255LL));
                    chmin(dp[i+1][to],dp[i][j]+(to-x[i])*(to-x[i]));
                }
            }
        }
        ll ans=LINF;
        rep(i,256)chmin(ans,dp[n][i]);
        cout<<ans<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    solve();
}

C++: // g++ -std=c++11 temp.cpp
#include <bits/stdc++.h>
using namespace std;

#define For(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define Rep(i,n) For(i,0,n)
#define Each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;

#define INF INT_MAX/3

#define MAX_M 16
#define MAX_N 20000

int N, M;
int C[MAX_M], x[MAX_N];
int dp[MAX_N][256];

/*
bool sorter(const A& x, const A& y){
	return x.a < y.a;
}

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

int lcm(int a, int b){
	if(a == 0 || b == 0) return 0;
	return a*b/gcd(a,b);
}
*/

void solve() {
	Rep(i,MAX_N)Rep(j,256) dp[i][j] = INF;
	dp[0][128] = 0;
	Rep(i,N)Rep(j,256)Rep(k,M){
		int xx = j + C[k];
		if(xx < 0) xx = 0;
		if(xx > 255) xx = 255;
		int cost = (x[i]-xx)*(x[i]-xx);
		dp[i+1][xx] = min(dp[i+1][xx], dp[i][j]+cost);
	}
	int res = INF;
	Rep(i,256) res = min(res, dp[N][i]);
	printf("%d\n", res);
}

int main(){
	while(1){
		scanf("%d %d", &N, &M);
		if(N == 0) break;
		Rep(i,M){
			scanf("%d", &C[i]);
		}
		Rep(i,N) scanf("%d", &x[i]);
		solve();
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<math.h>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
#include<set>
#include<map>
#define INF 999999999999
#define EPS 1.0e-6

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int n, m;
int c[20];
ll x[21000];
ll d[21000][260];

int main()
{
	while (cin >> n >> m) {
		if (n == 0 && m == 0)return 0;
		for (int i = 0; i < m; i++) {
			cin >> c[i];
		}

		for (int i = 1; i <= n; i++) {
			cin >> x[i];
		}
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= 256; j++) {
				d[i][j] = INF;
			}
		}
		d[0][128] = 0;

		for (int i = 0; i < n ; i++) {
			for (int j = 0; j <= 255; j++) {
				if (d[i][j] != INF) {
					for (int k = 0; k < m; k++) {
						int l = j + c[k];
						//cout << l << endl;
						if (l < 0)d[i + 1][0] = min(d[i + 1][0], d[i][j]+x[i + 1] * x[i + 1]);
						else if (l > 255) {
							d[i + 1][255] = min(d[i + 1][255], d[i][j]+(x[i + 1] - 255)*(x[i + 1] - 255));
							//cout << d[i + 1][256] << endl;
						}
						else{
							d[i+1][l]= min(d[i + 1][l], d[i][j] + (x[i + 1] - l)*(x[i + 1] - l));
							//cout << i + 1 << " " << j << " " << l << " " << d[i + 1][l];
						}
						//cout << i + 1 << " " << j << " " << l << " " << d[i + 1][l]<<endl;
						
					}
				}
			}
		/*
			for (int j = 0; j <= 256; j++) {
				cout << i<< "j:" <<j<<" "<<d[i][j] << endl;

			}
			cout << endl;
			*/
			
		}
		ll ans = INF;
		for (int i = 0; i <= 255; i++) {
			//cout <<"d[n-1][i]:"<< d[n][i] << endl;
			ans = min(ans, d[n][i]);
		}
		cout << ans << endl;
	}

	return 0;
}