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>
#define all(vec) vec.begin(),vec.end()
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll MOD=998244353;
const ll INF=1000000000;
const ll LINF=1000000000000000010;
int dx[8]={0,0,1,1,1,-1,-1,-1};
int dy[8]={1,-1,1,-1,0,1,-1,0};
int n,m;
int c[20],x[20010];
ll dp[20010][256];
ll dfs(int now,int pre){
    if(dp[now][pre]!=-1)return dp[now][pre];
    ll res=LINF;
    for(int i=0;i<m;i++){
        int to=min(255,max(0,pre+c[i]));
        ll s=(to-x[now])*(to-x[now]);
        if(now+1<n)s+=dfs(now+1,to);
        res=min(res,s);
    }
    return dp[now][pre]=res;
}
int main(){
    while(cin>>n>>m,n+m){
        for(int i=0;i<m;i++)cin>>c[i];
        for(int i=0;i<n;i++)cin>>x[i];
        memset(dp,-1,sizeof(dp));
        cout<<dfs(0,128)<<endl;
    }
}

C++: #include <iostream>
#include <algorithm>

#define INF (1 << 29)

#define MAX_N 20000
#define MAX_M 16

using namespace std;

int N, M, C[MAX_M], X, dp[MAX_N + 1][256];

int main()
{
	while (true)
	{
		for (int i = 0; i < MAX_N + 1; i++)
		{
			for (int j = 0; j < 256; j++)
			{
				dp[i][j] = INF;
			}
		}

		dp[0][128] = 0;

		cin >> N >> M;

		if (N == 0 && M == 0) { break; }

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

		for (int i = 0; i < N; i++)
		{
			cin >> X;

			for (int j = 0; j < 256; j++)
			{
				for (int k = 0; k < M; k++)
				{
					int ptr = j + C[k];

					if (ptr < 0) { ptr = 0; }
					if (ptr > 255) { ptr = 255; }

					int sabun = (X - ptr) * (X - ptr);

					dp[i + 1][ptr] = min(dp[i + 1][ptr], dp[i][j] + sabun);
				}
			}
		}

		cout << *min_element(dp[N], dp[N] + 256) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int C[20];

// i番目の値をjにする時の誤差のmin
int dp[20001][256];

inline int round(int x){
    int res = x;
    res = min(res, 255);
    res = max(res, 0);
    return res;
}

int main(){
    int n, m;
    int const inf = 1 << 28;
    while (cin >> n >> m && n | m){
        rep(i, m)cin >> C[i];
        rep(i, 20001)rep(j, 256)dp[i][j] = inf;
        dp[0][128] = 0;
        rep(i, n){
            int x; cin >> x;
            rep(j, 256){
                rep(k, m){
                    int next = round(j + C[k]);
                    int diff = x - next;
                    dp[i + 1][next] = min(dp[i + 1][next], dp[i][j] + diff*diff);
                }
            }
        }
        int res = inf;
        rep(i, 256){
            res = min(res, dp[n][i]);
        }
        cout << res << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T>inline bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>inline bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

void init();
void last();

void init() {

}

const int MAX_M = 20;
const int MAX_N = 20010;
int C[MAX_M], x[MAX_N];
int dp[MAX_N][256];

void solve(int n, int m) {
    for (int i = 0; i < m; i++) {
        scanf("%d", C + i);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", x + i);
    }
    for (int i = 0; i < MAX_N; i++) {
        for (int j = 0; j < 256; j++) {
            dp[i][j] = INF;
        }
    }
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 256; j++) {
            if (dp[i][j] == INF) continue;
            for (int k = 0; k < m; k++) {
                int jj = max(min(j + C[k], 255), 0);
                chmin(dp[i + 1][jj], dp[i][j] + (int)pow(jj - x[i], 2));
            }
        }
    }
    printf("%d\n", *min_element(begin(dp[n]), end(dp[n])));
}

void last() {
    exit(0);
}

int main(void) {
	int a, b, c, x, y, z, n, m, p;
	string s;
	while (cin >> n >> m, n) {
		solve(n, m);
		//break;
	}
    last();
}