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 <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
 
using namespace std;
 
#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define MAX_N 256
#define INF INT_MAX

int nlist[MAX_N];
int plist[MAX_N];
int N,M,x;
 
int sq(int n){
	return n*n;
}

int main() {
	while(cin >> N >> M, N|M){
		fill_n(nlist, MAX_N, INF);
		vi C(M);
		REP(i, M){
			cin >> C[i];
		}
		nlist[128]=0;
		int res=INF;
		REP(i, N){
			fill_n(plist, MAX_N, INF);
			cin >> x;
			REP(j, MAX_N){
				if(nlist[j]!=INF){
					REP(k, M){
						int l=max(0,min(j+C[k],MAX_N-1));
						plist[l]=min(plist[l],nlist[j]+sq(x-l));
					}
				}
			}
			memcpy(nlist, plist, sizeof(plist[0])*MAX_N);
		}
		sort(nlist, nlist+MAX_N);
		cout << nlist[0] << endl;
	}
}

C++: 
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <iomanip>
 
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define CLR(a) memset((a), 0 ,sizeof(a))
#define MCLR(a) memset((a), -1 ,sizeof(a))
#define SZ(a) (sizeof(a))
#define FILL(a,v) fill((int*)a,(int*)(a+(SZ(a)/SZ(*(a)))),v)

bool cheak(int x,int y,int xMax,int yMax)
{return x >= 0 && y >= 0 && xMax > x && yMax > y;}
const int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
const int INF = 2147483647;
typedef pair<int,int> P;
typedef long long ll;
typedef unsigned long long ull;

const int MAX = 20020;
int dp[MAX][256];

int main()
{
	int n,m;
	while(cin >> n >> m && n)
	{
		int c[20],x[MAX];
		REP(i,m)cin >> c[i];
		REP(i,n)cin >> x[i];
		
		FILL(dp,INF);
		dp[0][128] = 0;

		REP(i,n)
		REP(j,256)
		if(dp[i][j] != INF)
		REP(k,m)
		{
			int tmp = j+c[k];
			if(tmp < 0)
				tmp = 0;
			if(tmp > 255)
				tmp = 255;
			
			int val = pow(x[i]-tmp,2);
			if(val+dp[i][j] < dp[i+1][tmp])
				dp[i+1][tmp] = val+dp[i][j];
		}

		int ans = INF;
		REP(j,256)
		ans = min(dp[n][j],ans);
		
		cout << ans << endl;
	}

	return 0;

}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int n,m;
  while(cin>>n>>m,n) {
    int c[m];
    REP(i,m)
      cin >> c[i];
    int x[n];
    REP(i,n)
      cin >> x[i];
    long long dp[256];
    REP(i,256) dp[i] = INF;
    dp[128] = 0;
    
    REP(i,n) {
      long long next[256];
      REP(j,256) next[j] = INF;
      REP(j,256) {
        if (dp[j]==INF) continue;
        REP(k,m) {
          long long hoge = max(min(j+c[k], 255), 0);
          next[hoge] = min(next[hoge], dp[j] + (hoge-x[i])*(hoge-x[i]));
        }
      }
      // REP(j,256){
      //   if (next[j]!=INF)
      //     printf("%d,%d ",j,next[j]);
      // }
      // cout << endl;
      memcpy(dp, next, sizeof(next));
    }
    cout << *min_element(dp, dp+256) << endl;
  }
}

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);

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

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, M) score[i][j] = INF;
		REP(j, M) score[0][j] = 0;
		REP(i, M) cin >> code[i];
		REP(i, N) cin >> sig[i];
		REP(i, M) val[0][i] = 128;
		REP(i, N){
			REP(j, M) REP(k, M) {
				int tgt = min(255, max(0,  val[i][j] + code[k]));
				if (score[i+1][k] > score[i][j] + (sig[i] - tgt) * (sig[i] - tgt)){
					score[i+1][k] = score[i][j] + (sig[i] - tgt) * (sig[i] - tgt);
					val[i+1][k] = tgt;
				}
			}
		}
		int minim = INF;
		REP(i, M) chmin(minim, score[N][i]);
		cout << minim << endl;
	}			
	return 0;
}