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 <vector>
#include <numeric>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <string.h>

using namespace std;
#define LL long long
using VS = vector<string>;
using VI = vector<int>;
using VL = vector<LL>;
using VVI = vector<VI>;
using VVL = vector<VL>;
#define FOR(i,s,e) for(i = s; i < e; i++)
#define FORR(i,s,e) for(i = e-1; i >= s; i--)
const LL INF = (LL)1e16;
const LL MOD = (LL)1e9 + 7;

int main(){
  LL n,m,i,j,l,k,cost,d,ma = 256;
  while (1){
    cin >> n >> m;
    if (n == 0){
      return 0;
    }
    LL c[m],x[n];
    FOR(i,0,m) cin >> c[i];
    FOR(i,0,n) cin >> x[i];
    LL dp[n+1][ma];
    FOR(i,0,n+1)FOR(j,0,ma)dp[i][j] = -1;
    dp[0][128] = 0;
    FOR(i,0,n){
      FOR(j,0,ma){
        if (dp[i][j] < 0)continue;
        FOR(k,0,m){
          l = j+c[k];
          if (l < 0)l = 0;
          else if (l >= ma)l = 255;
          cost = (l-x[i])*(l-x[i]);
          d = dp[i][j]+cost;
          if (dp[i+1][l] < 0)dp[i+1][l] = d;
          else if (dp[i+1][l] > d) dp[i+1][l] = d;
        }
      }
    }
    LL ans = INF;
    FOR(i,0,ma){
      if (dp[n][i] >= 0 && dp[n][i] < ans) ans = dp[n][i];
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

struct node {
	ll cost;
	int ind;
	ll val;
	node(ll c, int i, int v) {
		cost = c;
		ind = i;
		val = v;
	}
};

bool checked[20001][256];

bool operator<(const node& lhs, const node& rhs) {
	if( lhs.cost == rhs.cost ) {
		return lhs.ind < rhs.ind;
	}
	return lhs.cost > rhs.cost;
}

int main() {
	int n, m;
	while(cin >> n >> m, n || m) {
		memset(checked, 0, sizeof(checked));
		vector<ll> c(m), x(n);
		rep(i,m) cin >> c[i];
		sort(c.begin(), c.end());
		rep(i,n) cin >> x[i];
		priority_queue<node> que;
		que.push(node(0, 0, 128));
		while(!que.empty()) {
			node nd = que.top();
			que.pop();
			if( nd.ind == n ) {
				cout << nd.cost << endl;
				break;
			}
			if( checked[nd.ind][nd.val] ) {
				continue;
			}
			checked[nd.ind][nd.val] = true;
			rep(i, m) {
				ll nval = min(max(nd.val+c[i], 0LL), 255LL);
				if( !checked[nd.ind+1][nval] ) {
					que.push( node(nd.cost+(x[nd.ind]-nval)*(x[nd.ind]-nval), nd.ind+1, nval) );
				}
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <climits>
#include <string>
#include <bitset>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define fin(ans) cout<<(ans)<<endl
#define STI(s) atoi(s.c_str())
#define mp(p,q) make_pair(p,q)
#define pb(n) push_back(n)
#define Sort(a) sort(a.begin(),a.end())
#define Rort(a) sort(a.rbegin(),a.rend())
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
const int P = 1000000007;
const int INF = INT_MAX;
const long long LLINF = 1LL<<60;
//g++ -std=c++1z temp.cpp
//./a.out


int main(void){ios::sync_with_stdio(false);cin.tie(0);
//////////////////////////////////////////////////////
	int m,n;
    
    while(1){
        cin>>n>>m;
        if(n==0||m==0)break;
        vvll dp(n+1,vll (256,INF));
        dp[0][128]=0;

        vi c(m),x(n);
        rep(i,m)cin>>c[i];
        rep(i,n)cin>>x[i];

        for(int i=1;i<=n;i++){
            int nowx=x[i-1];
            for(int from=0;from<256;from++){
                for(int j=0;j<m;j++){
                    int p=from+c[j];
                    p=max(p,0);
                    p=min(p,255);
                    int cost=(nowx-p)*(nowx-p);
                    dp[i][p]=min(dp[i][p],dp[i-1][from]+cost);
                }
            }
        }
        sort(dp[n].begin(),dp[n].end());
        cout<<dp[n][0]<<endl;
    }

/////////////////////////////////////////////////////
return 0;}