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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
/*
2
8
0
325125
65026
*/
vint dp(256,0);
void mainmain(){
	int n,m;
	while(cin>>n>>m,n||m){
		vint c(m),x(n);
		rep(i,m) cin>>c[i];
		rep(i,n) cin>>x[i];
		rep(i,256) dp[i]=INF;
		dp[128]=0;
		rep(i,n){
			vint tmp(256,INF);
			rep(j,m){
				rep(k,256){
					int t=k+c[j];
					if(t<0) tmp[0]=min(tmp[0],dp[k]);
					else if(t>255) tmp[255]=min(tmp[255],dp[k]);
					else tmp[t]=min(tmp[t],dp[k]);
				}
			}
			rep(j,256){
				if(tmp[j]==INF) continue;
				tmp[j]+=(j-x[i])*(j-x[i]);
			}
			dp=tmp;
			// rep(i,256) cout<<i<<" "<<dp[i]<<endl;
			// cout<<endl;
		}
		int ans=INF;
		rep(i,256){
			ans=min(ans,dp[i]);
		}
		cout<<ans<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include<iostream>
#include<vector>
#include<utility>
#include<complex>

#define INF (1 << 29)

using namespace std;

int main()
{
    int N, M;
    while(cin >> N >> M, (N))
    {

        vector<int> in_C(M);
        for(auto& _c : in_C)
        {
            cin >> _c;
        }

        vector<int> in_X(N);
        for(auto& _x : in_X)
        {
            cin >> _x;
        }
        exit(514);

        pair<int, int> dp[20000][16];
        for(int i = 0; i < M; i++)
        {
            int score, nextX;
            nextX = in_X[0] + in_C[i];
            score = (128 - nextX) * (128 - nextX);

            dp[0][i] = make_pair(score, nextX);
        }
        exit(114);

        for(int n = 1; n < N; n++)
        {
            for(int i = 0; i < M; i++)
            {
                int nextScore = INF;
                int nextX = 0;
                for(int j = 0; j < M; j++)
                {
                    int tmpScore = dp[n - 1][i].first + pow( ( dp[n - 1][i].second + in_C[i] - in_X[n]), 2.0 );
                    if(tmpScore < nextScore)
                    {
                        nextScore = tmpScore;
                        nextX = dp[n - 1][i].second + in_C[i];
                    }
                }
                dp[n][i] = make_pair(nextScore, nextX);
            }
        }

        int ans = INF;
        for(int i = 0; i < M; i++)
        {
            if( dp[N - 1][i].first < ans) ans = dp[N - 1][i].first;
        }
        cout << ans << endl;



    }



    return 0;
}

C++: #include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>

using namespace std;

#define INF 1000000007
int n,m;
long long dp[20001][256];
int c[16];
int x[20001];

long long dfs(int k){
  if(k > n){
    long long t = INF;
    for(int i = 0; i < 256; i++){
      t = t > dp[n][i] ? dp[n][i] : t;
    }
    return t;
  }
  for(int i = 0 ; i < 256; i++){
    if(dp[k-1][i] == INF) continue;
    for(int j = 0; j < m ; j++){
      long long t = i + c[j];
      if(t > 255) t = 255;
      if(t < 0) t = 0;
      dp[k][t] = dp[k][t] > dp[k-1][i] + (x[k] - t)*(x[k] - t) ? dp[k-1][i] + (x[k] - t)*(x[k] - t) : dp[k][t];
    }
  }
  return dfs(k+1);
}

int main(){
  while(cin >> n >> m && n != 0){
    for(int i = 0; i < 20001; i++){
      for(int j = 0; j < 256; j++){
        if(i == 0 && j == 128) dp[i][j] = 0;
        else dp[i][j] = INF;
      }
    }
    for(int i = 0; i < m; i++){
      cin >> c[i];
    }
    x[0] = 128;
    for(int i = 0; i < n ; i++){
      cin >> x[i+1];
    }
    cout << dfs(1) << endl;
  }

}