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 <iostream>
#include <algorithm>
#define int long long
using namespace std;

int n, m;
int c[16];
int x[20000];
int dp[20001][256];

signed main() {
	int i, j, k;
	
	while (cin >> n >> m) {
		if (!n) break;
		for (i = 0; i < m; i++) cin >> c[i];
		for (i = 0; i < n; i++) cin >> x[i];
		for (i = 0; i <= n; i++) for (j = 0; j < 256; j++) dp[i][j] = 1145141919810;
		
		dp[0][128] = 0;
		for (i = 0; i < n; i++) {
			for (j = 0; j < 256; j++) {
				for (k = 0; k < m; k++) {
					int ny = max(0LL, min(255LL, j + c[k]));
					dp[i + 1][ny] = min(dp[i + 1][ny], dp[i][j] + (x[i] - ny) * (x[i] - ny));
				}
			}
		}
		
		int ans = 1145141919810;
		for (i = 0; i < 256; i++) ans = min(ans, dp[n][i]);
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

inline int adj(int x) {
  return min(max(x, 0), 255);
}
#define SQ(x) ((x)*(x))
int main() {
  int N, M, INF=1<<28;
  while(cin >> N >> M && (N|M)) {
    int C[M];
    for(int i=0; i<M; i++) cin >> C[i];
    int dp[2][256]; fill(dp[0], dp[0]+512, INF);
    dp[0][128] = 0;
    for(int k=0; k<N; k++) {
      for(int i=0; i<256; i++) dp[(k+1)%2][i] = INF;
      int in; cin >> in;
      for(int i=0; i<256; i++) {
        if(dp[i]>=INF) continue;
        for(int j=0; j<M; j++) {
          dp[(k+1)%2][adj(i+C[j])] = min(dp[(k+1)%2][adj(i+C[j])], dp[k%2][i]+SQ(in-adj(i+C[j])));
        }
      }
    }
    cout << *min_element(dp[N%2], dp[(N%2)+1]) << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;

#define INF INT_MAX/3
#define MAX_N 1000
ll n,m;
ll dp[200001][256];
ll c[16],x[20000];
ll rec(ll p,ll y){
  if(dp[p][y] >= 0) return dp[p][y];

  if(p == n) return 0;

  long ret=LONG_MAX;
    for(ll i=0; i<m; ++i){ //??????????????????????????¨????????????????????°????????????
        ll ny = y+c[i];
        if(ny<0) ny=0;
        else if(ny>255) ny=255;

        ret = MIN(ret, rec(p+1,ny)+(ny-x[p])*(ny-x[p]));
    }
    return dp[p][y]=ret;
}
void solve(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(1){
    cin>>n>>m;
    if(n==0) break;
    rep(i,m) cin>>c[i];
    rep(i,n) cin>>x[i];
    memset(dp,-1,sizeof(dp));
    printf("%lld\n",rec(0,128));
  }

}
int main(){
  solve();
  return 0;
}

C++: #include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int a[16],b[20000],c[20001][256],n,m;
int fi(int p,int q){
  int i;
  if(c[p][q]>=0)
    return c[p][q];  
  int mn=1<<30;
  for(i=0;i<m;++i){
    int r=min(max(0,q+a[i]),255);
    mn=min(mn,fi(p+1,r)+(b[p]-r)*(b[p]-r));
  }
  return c[p][q]=mn;
}
int main(){
  int i;
  //int n,m;
  while(cin>>n>>m,n||m){
    //int a[16],b[20000];
    for(i=0;i<m;++i)
      cin>>a[i];
    for(i=0;i<n;++i)
      cin>>b[i];
    memset(c,-1,n*256*sizeof(int));
    memset(c[n],0,256*sizeof(int));
    cout<<fi(0,128)<<endl;
  }
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

template <typename T> T &chmin(T &a, const T &b) {return a = min(a, b);}
template <typename T> T &chmax(T &a, const T &b) {return a = max(a, b);}

using ll = long long;
using ld = long double;

const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);

typedef pair<int, int> pii;

int N, M;
vector<int> code(16);
vector<int> sig(20000);
int score[20005][256];
int val[20005][256];

int main(){
	while(cin >> N >> M and N + M){
		REP(i, M) code[i] = 0;
		REP(i, N) sig[i] = 0;
		REP(i, N+1) REP(j, 256) score[i][j] = INF;
		score[0][128] = 0;
		val[0][128] = 128;
		REP(i, M) cin >> code[i];
		REP(i, N) cin >> sig[i];
		REP(i, N) REP(j, 256){
			if(score[i][j] == INF) continue;
			REP(k, M){
				int tmp = min(255, max(0, val[i][j] + code[k]));
				if(score[i+1][tmp] > score[i][val[i][j]] + (sig[i] - tmp) * (sig[i] - tmp)) {
					score[i+1][tmp] = score[i][val[i][j]] + (sig[i] - tmp) * (sig[i] - tmp);
					val[i+1][tmp] = tmp;
				}
			}
		}
		int minim = INF;
		REP(i, 256) chmin(minim, score[N][i]);
		cout << minim << endl;
	}			
	return 0;
}