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 r(i,n) for(int i=0;i<n;i++)
#define INF 100000000
int dp[20001][256],n,m,b[17],a[20001];
main(){
  while(cin>>n>>m,n){
    r(i,m)cin>>b[i];
    r(i,n)cin>>a[i+1];
    r(i,n+1)r(j,256)if(dp[i][j]!=INF)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++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2199&lang=jp
//Differential Pulse Code Modulation
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll=long long;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
template<class T>bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1; } return 0;}
int solve(){
	ll N,M; cin>>N>>M;
	if (N==0&&M==0) return -1;
	vector<ll> cm(M);
	for(auto&& c:cm){
		cin>>c;
	}

	vector<int> xn(N);
	for(auto&& x:xn) cin>>x;

	constexpr ll INF=2LL<<60;
	vector<vector<ll>> dp(N+1,vector<ll>(256,INF));
	dp[0][128]=0;
	FOR(i,0,N){
		auto x=xn.at(i);
		FOR(v,0,256){ //FIXME:0->
			FOR(c,0,cm.size()){
				ll cv= cm.at(c);
				ll next_v=v+cv;
				if (next_v<0) next_v=0;
				if (next_v>=255) next_v=255;
				ll ad=(next_v-x);
				if (dp[i][v]<INF) chmin(dp[i+1][next_v],dp[i][v]+ad*ad);
			}
		}
	}

	ll ret=INF;
	FOR(v,0,256){
		chmin(ret,dp[N][v]);
	}

	return ret;
}
int main() {
#if LOCAL&01
	std::ifstream in("./test/sample-1.in");
	//std::ifstream in("./input.txt");
	std::cin.rdbuf(in.rdbuf());
#else
	cin.tie(0);
	ios::sync_with_stdio(false);
#endif
	while(true){
		ll r=solve();
		if(r<0)break;
		cout<<r<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define N 20001
#define M 256
#define INF N*M

using namespace std;

int n, m;
int c[16];
int dp[N][M];

void init(){
  for(int i=0; i<N; ++i){
    for(int j=0; j<M; ++j){
      dp[i][j] = INF;
    }
  }
  dp[0][128] = 0;
}

int code(int k){
  if(k < 0) return 0;
  if(k >= M) return M-1;
  return k;
}

void solve(int& x, int& u, int& v){
  for(int i=0; i<m; ++i){
    int j = code(v + c[i]);
    dp[u][j] = min(dp[u][j], (x-j)*(x-j) + dp[u-1][v]);
  }
}

int main(){
  int x;
  while(1){
    cin >> n >> m;
    if(!n && !m) break;
    init();
    for(int i=0; i<m; ++i) cin >> c[i];
    for(int i=1; i<=n; ++i){
      cin >> x;
      for(int j=0; j<M; ++j){
	if(dp[i-1][j] != INF) solve(x, i, j);
      }
    }
    int ans=INF;
    for(int j=0; j<M; ++j) ans = min(ans, dp[n][j]);
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main(){
    int inf = 1e9;
    int N,M;
    while(cin >> N >> M && N){
        vector<int> C(M+1),X(N+1);
        vector<vector<int>> dp(N+1,vector<int>(256,inf));
        dp[0][128] = 0;
        for(int i=1;i<=M;i++) cin >> C[i];
        for(int i=1;i<=N;i++) cin >> X[i];
        for(int i=0;i<N;i++) for(int j=0;j<=255;j++){
            for(int k=1;k<=M;k++){
                int ne = j+C[k];
                if(ne>255) ne = 255;
                if(ne<0) ne = 0;
                dp[i+1][ne] = min(dp[i+1][ne],dp[i][j]+(X[i+1]-ne)*(X[i+1]-ne));
            }
        }
        int ans = inf;
        for(int i=0;i<=255;i++) ans = min(ans,dp[N][i]);
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vi in(m),s(n);
		rep(i,m)cin>>in[i];
		rep(i,n)cin>>s[i];
		vvi dp(n+1,vi(256,inf));
		dp[0][128]=0;
		rep(i,n)rep(j,256)if(dp[i][j]!=inf)rep(k,m){
			int I=i+1,J=j+in[k];
			if(J<0)J=0;
			if(J>255)J=255;
			dp[I][J]=min(dp[I][J],dp[i][j]+(s[i]-J)*(s[i]-J));
		}
		int out=inf;
		rep(i,256)out=min(out,dp[n][i]);
		cout<<out<<endl;
	}
}