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;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i] << " ";
  }
  cout << endl;
}


int main () {
  cin.tie(0);
  cout << setprecision(10);

  while(1) {
    int n, m; cin >> n >> m;
    if(n == 0 && m == 0) break;
    vi c(m), x(n+1);
    REP(i, m) {
      cin >> c[i];
    }
    REP(i, n) {
      cin >> x[i+1];
    }

    vector<vector<ll>> dp(n+1, vector<ll>(256, LINF));
    REP(i, 256) {
      dp[0][i] = 0;
    }

    FOR(i, 1, n+1) {
      REP(j, 256) {
        REP(k, m) {
          ll next = min(255, max(0, j+c[k]));
          dp[i][next] = min(dp[i][next], dp[i-1][j] + (x[i] - next) * (x[i] - next));
        }
      }
    }

    ll ans = LINF;
    REP(i, 256) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}

C++: //Differential Pulse Code Modulation
#include<bits/stdc++.h>
using namespace std;

#define INF 1000000000
int N, M;
int C[16], x[20000];
int dp[20000][257]; //dp[p] := i???????????°?????\????????\???????????¨?????????????????????????????¨?????????????????????????°????

//p:?????? w:?????¨?????????
int solve(int p, int w){
  if(p==N)return 0;
  if(dp[p][w]!=-1)return dp[p][w];

  int mn=INF;
  for(int i=0; i<M; i++){
    int nw = w+C[i];
    if(nw<0)nw=0;
    if(nw>255)nw=255;
    int tmp=solve(p+1, nw)+(nw-x[p])*(nw-x[p]);
    if(mn>tmp)mn=tmp;
  }
  return dp[p][w]=mn;
}

int main(){
  while(cin >> N >> M, !(N==0 && M==0)){
    //??\???
    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 << solve(0, 128) << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

#define MAX_N 20010

int f(int x) {
	return max(0, min(255, x));
}

signed main() {
	static int dp[MAX_N][256] = {};
	for (int N, M; cin >> N >> M&&N;) {
		memset(dp, 0x3f, sizeof(dp));
		dp[0][128] = 0;
		vector<int> C(M); rep(i, 0, M) { cin >> C[i]; }
		rep(i, 0, N) {
			int x; cin >> x;
			rep(j, 0, 256) {
				if (dp[i][j] == INF)continue;
				rep(k, 0, M) {
					int y = f(j + C[k]);
					dp[i + 1][y] = min(dp[i + 1][y], dp[i][j] + (y - x)*(y - x));
				}
			}
		}
		int ans = INF;
		rep(i, 0, 256) {
			ans = min(ans, dp[N][i]);
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;


class DP{
public:
	int cost, n, x;
	
	DP(){}
	DP(int xx, int nn, int cc){
		cost = cc;x=xx;n=nn;
	}
	
	bool operator< (const DP &opp) const {
		return cost>opp.cost;
	}
};
int C[20];
int x[20001];
bool visit[20001][256];
int N, M;
main(){
	int i,j;
	while(cin >> N >> M, N){
		memset(visit, 0, sizeof(visit));
		for(i=0;i<M;i++) cin >> C[i];
		for(i=0;i<N;i++) cin >> x[i];
		priority_queue<DP> dp;
		DP S;
		dp.push(DP(128, 0, 0));
		while(!dp.empty()){
			S = dp.top();
			dp.pop();

			if(S.n == N) break;
			for(i=0;i<M;i++){
				DP T=S;
				T.x += C[i];
				if(T.x > 255) T.x = 255;
				if(T.x < 0) T.x = 0;
				
				T.cost += (x[T.n]-T.x)*(x[T.n]-T.x);
				T.n ++;
				if(visit[T.n][T.x]) continue;
				visit[T.n][T.x] = true;
				dp.push(T);
			}
		}
		cout << S.cost << endl;
	}
	return 0;
}