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 <stdio.h>
#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<cctype>
#include<fstream>


#define FOR(i, a, b) for( int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for( int i=(a);i>=(b);i--)
#define LFOR(i, a, b) for( long long int i=(a);i<=(b);i++)
#define LRFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;

int dy[5] = { 0,0,1,-1,0 };
int dx[5] = { 1,-1,0,0,0 };


int main(void) {
	
	while (1) {
		long long int n, m;

		cin >> n >> m;
		if (n == 0 && m == 0) {
			break;
		}

		vector< vector<long long int> > dp(256, vector<long long int>(n + 1, LINF));
		vector<long long int > c(m + 1);
		vector<long long int> x(n + 1);

		FOR(i, 1, m) {
			cin >> c[i];
		}
		FOR(i, 1, n) {
			cin >> x[i];
		}

		dp[128][0] = 0;

		long long int now;

		long long int mini = LINF;

		LFOR(i, 1, n) {
			LFOR(k, 0, 255) {
				if (dp[k][i - 1] != LINF) {
					LFOR(j, 1, m) {
						if (k + c[j] > 255) {
							now = 255;
						}
						else if (k + c[j] < 0) {
							now = 0;
						}
						else {
							now = k + c[j];
						}

						dp[now][i] = min(dp[now][i],dp[k][i - 1] + (now - x[i])*(now - x[i]));
					}
				}
			}

		}

		FOR(i, 0, 255) {
			mini = min(mini, dp[i][n]);
		}

		cout << mini << endl;



	}



	
	

	return 0;
}

C++: //Yattazo! Watashiha-Yosen-tsuukashita! It's 460points/600points! OHO!!!!!
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int x[64000][256],n,m,y[64000],z[64000],d,minx;
int main(){
	while(true){
		minx=2000000000;
		for(int i=0;i<64000;i++){for(int j=0;j<256;j++){x[i][j]=2000000000;}}
		cin>>n>>m;if(!n){break;}
		for(int i=0;i<m;i++){cin>>y[i];}
		for(int i=1;i<=n;i++){cin>>z[i];}
		x[0][128]=0;
		for(int i=1;i<=n;i++){
			for(int j=0;j<256;j++){
				if(x[i-1][j]<1000000000){
					for(int k=0;k<m;k++){
						d=y[k]+j;
						if(d<0){d=0;}
						if(d>255){d=255;}
						x[i][d]=min(x[i][d],x[i-1][j]+(int)pow(1.0*(z[i]-d),2));
					}
				}
			}
		}
		for(int i=0;i<256;i++){minx=min(minx,x[n][i]);}
		cout<<minx<<endl;
	}
}

C++: #include <algorithm>
#include <iostream>
using namespace std;
int main(){
  int N, M;
  while(cin >> N >> M, N){
    long long int C[M];
    long long int INF = 10000000000LL;
    for(int i = 0; i < M; ++i)
      cin >> C[i];
    sort(C,C+M);
    long long int x[N];
    for(int i = 0; i < N; ++i)
      cin >> x[i];
    long long int dp[N][256];
    for(int i = 0; i < N; ++i){
      fill(dp[i],dp[i]+256,INF);
    }
    for(int i = 0; i < M; ++i){
      long long int t = max(min(255LL,128LL+C[i]),0LL);
      dp[0][t] = (x[0]-t)*(x[0]-t);
    }
    for(int i = 1; i < N; ++i){
      for(int j = 0; j < 256; ++j){
	if(dp[i-1][j] == INF) continue;
	for(int k = 0; k < M; ++k){
	  long long int t = max(min(255LL,(long long)j+C[k]),0LL);
	  dp[i][t] = min(dp[i][t],dp[i-1][j] + (x[i]-t)*(x[i]-t));
	  if(j + (int)C[k] > 255) break;
	}
      }
    }
    long long int ans = INF;
    for(int i = 0; i < 256; ++i){
      ans = min(ans, dp[N-1][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=2e9;
double EPS=1e-10;
int main(){
      int n,m;
      while(cin>>n>>m&&(n||m)){
            int a[n],b[m];
            ll dp[n+1][256];
            REP(i,m)cin>>b[i];
            REP(i,n)cin>>a[i];
            REP(i,n+1)REP(j,256)dp[i][j]=-1;
            dp[0][128]=0;
            REP(i,n){
                  REP(j,256){
                        if(dp[i][j]==-1)continue;
                        REP(k,m){
                              int nxt=j+b[k];
                              nxt=max(0,min(255,nxt));
                              if(dp[i+1][nxt]==-1)dp[i+1][nxt]=dp[i][j]+(a[i]-nxt)*(a[i]-nxt);
                              else dp[i+1][nxt]=min(dp[i+1][nxt],dp[i][j]+(a[i]-nxt)*(a[i]-nxt));
                        }
                  }
            }
            ll ans=-1;
            REP(i,256)if(dp[n][i]!=-1){
                  if(ans==-1)ans=dp[n][i];
                  else ans=min(ans,dp[n][i]);
            }
            cout<<ans<<endl;
      }
}