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"

#include <iostream>
#include <vector>
// #include <map>
// #include <string>
#include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

int n;
int m;
uint x;
vector<int> c;
bool bef = 0;
bool aft = 1;
const int size = 255 + 1;
vector<vector<uint>> vec(2, vector<uint>(size, -1));

inline uint mse(uint a, uint b) {
	uint diff = (a > b) ? (a - b) : (b - a);
	return diff * diff;
}

uint calcBefMin(int i) {
	uint min_val = -1;
	for (auto v : c) {
		int key = i - v;
		if (key < 0 or key > 255) {
			continue;
		}
		min_val = min(min_val, vec[bef][key]);
	}
	return min_val;
}

inline uint myplus(uint a, uint b) {
	uint m = -1;
	if (b <= m - a) { // check overflow
		return a + b;
	}
	return -1;

}

void solve() {
	c.resize(m);
	for (int i = 0; i < m; i++) {
		cin >> c[i];
	}
	int c_max = *std::max_element(begin(c), end(c));
	int c_min = *std::min_element(begin(c), end(c));
	for (int i = 0; i <= 255; i++) {
		vec[bef][i] = mse(128, i);
	}
	for (int i = 0; i < n; i++) {
		cin >> x;
		int bef_key;
		// do
		for (bef_key = 1; bef_key <= 254; bef_key++) {
			vec[aft][bef_key] = myplus(calcBefMin(bef_key), mse(bef_key, x));
		}
		// do for 255
		bef_key = 255;
		uint min_val = -1;
		for (int key = bef_key - c_max; key <= 255; key++) {
			min_val = min(min_val, vec[bef][key]);
		}
		vec[aft][bef_key] = myplus(min_val, mse(bef_key, x));
		// do for 0
		bef_key = 0;
		min_val = -1;
		for (int key = bef_key - c_min; key >= 0; key--) {
			min_val = min(min_val, vec[bef][key]);
		}
		vec[aft][bef_key] = myplus(min_val, mse(bef_key, x));
		// for next
		bef = !bef;
		aft = !aft;
	}
	cout << *std::min_element(begin(vec[bef]), end(vec[bef])) << endl; // bef is correct, because swaped
}

int main() {


	while (true) {
		cin >> n >> m;
		if (n == 0 and m == 0) {
			break;
		}
		solve();
	}

	//std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
#define INF 100000000
int dp[20001][256];
 main(){
  int n,m;
  while(cin>>n>>m,n){
    int b[m];int a[n+1];
    r(i,m)cin>>b[i];
    r(i,n)cin>>a[i+1];
    r(i,n+1)r(j,256)dp[i][j]=INF;
    dp[0][128]=1;
    r(i,n)r(j,256)
        if(dp[i][j]!=INF)r(k,m){
          int p=j+b[k];
          if(p>255)p=255;
          if(p<0)p=0;
          dp[i+1][p]=min(dp[i+1][p],dp[i][j]+(a[i+1]-p)*(a[i+1]-p));
        }
    int p=100000000;
    r(i,256)if(dp[n][i]!=INF)
      p=min(p,dp[n][i]);
    cout<<p-1<<endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {

	while (1) {
		int n, m;
		cin >> n >> m;

		if (n == 0 && m == 0) { return 0; }

		vector<int> c(m + 1);
		for (int i = 1; i <= m; i++) {
			cin >> c[i];
		}
		vector<int> x(n + 1);
		for (int i = 1; i <= n; i++) {
			cin >> x[i];
		}

		vector<vector<long long>> dp(n + 1, vector<long long>(256, 2000000000000));

			for (int j = 1; j <= m; j++) {
				dp[0][128] = 0;
			}
//		dp[1][128] = 1000000000;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= 255; j++) {

				for (int k = 1; k <= m; k++) {

					if (dp[i - 1][j] > 2000000000) { continue; }

					int tmp = j + c[k];
					if (tmp < 0) { tmp = 0; }
					if (tmp > 255) { tmp = 255; }

					dp[i][tmp] = min(dp[i][tmp], dp[i - 1][j] + (x[i] - tmp)*(x[i] - tmp));

				}
			}
		}
		long long res = 2000000000000;
		for (int i = 0; i <= 255; i++) {
			if (dp[n][i] < 2000000000) {
				res = min(res, dp[n][i]);
			}
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#define INF 100000000

using namespace std;

int main(void) {
	int N,M;
	while (cin >> N >> M, N) {
		vector<int> C(M);
		for (int i = 0; i < M; ++i) {
			cin >> C[i];
		}
		vector<int> x(N);
		for (int i = 0; i < N; ++i) {
			cin >> x[i];
		}

		vector< vector<int> > dp(256,vector<int>(N+1));
		for (int i = 0; i < 256; ++i) {
			for (int j = 0; j < N+1; ++j) {
				dp[i][j] = INF;
			}
		}
		dp[128][0] = 0;

		for (int j = 0; j < N; ++j) {
			for (int i = 0; i < 256; ++i) {
				if (dp[i][j] != INF) {
					for (int k = 0; k < M; ++k) {
						if (C[k]+i > 255) {
							dp[255][j+1] = min(dp[255][j+1],dp[i][j]+(int)pow(x[j]-255,2));
						}
						else if (C[k]+i < 0) {
							dp[0][j+1] = min(dp[0][j+1],dp[i][j]+(int)pow(x[j]-0,2));
						}
						else {
							dp[C[k]+i][j+1] = min(dp[C[k]+i][j+1],dp[i][j]+(int)pow(x[j]-C[k]-i,2));
						}
					}
				}
			}
		}

		int res = INF;
		for (int i = 0; i < 256; ++i) {
			if (dp[i][N] < res) {
				res = dp[i][N];
			}
		}
		cout << res << endl;
	}
	return 0;
}