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++: // 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+1][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+1)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 <string>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
#include <sstream>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-9

int pulse[20000];
int dp[2][256];

int main(){
	int n,m;
	int book[16];
	while(scanf("%d%d",&n,&m),n|m){
		rep(i,m)scanf("%d",&book[i]);
		rep(i,n)scanf("%d",&pulse[i]);
		rep(i,256)dp[0][i]=dp[1][i]=INT_MAX;
		
		dp[0][128] = 0;
		rep(i,n){
			int cur = i%2;
			int next = (i+1)%2;
			rep(j,256)if( dp[cur][j]!=INT_MAX ){
				rep(k,m){
					int d = j+book[k];
					if( d<0 )d=0;
					if( d>255 )d=255;
					dp[next][d] = min(dp[next][d], dp[cur][j]+(pulse[i]-d)*(pulse[i]-d));
				}
			}
			rep(j,256)dp[cur][j]=INT_MAX;
		}
		int ans = INT_MAX;
		rep(i,256)ans=min(ans,dp[n%2][i]);
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 2000000000;

int main()
{
	ll N, M;
	while (cin >> N >> M, N | M) {
		vector<ll> C(M), x(N);
		for (int i = 0; i < M; i++) {
			cin >> C[i];
		}
		sort(C.begin(), C.end());
		for (int i = 0; i < N; i++) {
			cin >> x[i];
		}
		vector<vector<ll>> dp(N + 1, vector<ll>(256, INF));
		dp[0][128] = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < 256; j++) {
				for (int k = 0; k < M; k++) {
					ll id = min(255ll, max(0ll, j + C[k])), d = id - x[i]; d *= d;
					dp[i + 1][id] = min(dp[i + 1][id], dp[i][j] + d);
				}
			}
		}
		ll res = INF;
		for (int i = 0; i < 256; i++) {
			res = min(res, dp[N][i]);
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include <stdio.h>

int a[20001][256];

int main(void){

int i,j,k;
int n,m,dummy;
int min = 2000000000;
int c[17];
int x[20001];



while(1){
scanf("%d %d",&n,&m);
if(n == 0 && m == 0){
return 0;
}

for(i = 0; i < m; i++){
scanf("%d",&c[i]);
}
for(i = 0; i < n; i++){
scanf("%d",&x[i]);
}

for(i = 0; i < n + 1; i++){
for(j = 0; j < 256; j++){
a[i][j] = 2000000000;
}
}

a[0][128] = 0;

for(i = 0; i < n; i++){
for(j = 0; j < 256; j++){
if(a[i][j] != 2000000000){
for(k = 0; k < m; k++){
dummy = j + c[k];
if(dummy < 0){
dummy = 0;
}else if(dummy > 255){
dummy = 255;
}
if(a[i+1][dummy] > a[i][j] + (dummy - x[i])*(dummy - x[i])){
a[i+1][dummy] = a[i][j] + (dummy - x[i])*(dummy - x[i]);
}
}
}
}
}

for(j = 0; j < 256; j++){
if(a[n][j] > -1){
if(min > a[n][j]){
min = a[n][j];
}
}
}

printf("%d\n",min);

}
return 0;

}