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;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int c[16], x[20010], dp[20010][300];
signed main(void)
{
  while(true) {
    int n, m;
    cin >> n >> m;
    if(!n && !m) break;
    REP(i, m) cin >> c[i];
    REP(i, n) cin >> x[i];

    REP(i, n+1) REP(j, 256) dp[i][j] = INF;
    dp[0][128] = 0;
    REP(i, n) REP(j, 256) {
      if(dp[i][j] != INF) {
        REP(k, m) {
          int tmp = j+c[k];
          tmp = max(tmp, (ll)0);
          tmp = min(tmp, (ll)255);
          dp[i+1][tmp] = min(dp[i+1][tmp], dp[i][j] + (x[i]-tmp)*(x[i]-tmp));
        }
      }
    }
    /*
    REP(j, n+1) {
      FOR(i, 125, 140) {
        if(dp[j][i] == INF) cout << -1 << " ";
        else cout << dp[j][i] << " ";
      }
      cout << endl;
    }*/

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

  return 0;
}

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

#define p(x) (x)*(x)

int round(int x)
{
  if(x > 255) return 255;
  else if(x < 0) return 0;
  else return x;
}

int main()
{
  while(1){
    int N, M;
    cin >> N >> M;
    if(N == 0 && M == 0) break;
    int C[M], x[N];
    for(int i = 0; i < M; i++)
      cin >> C[i];
    for(int i = 0; i < N; i++)
      cin >> x[i];
    int a[256];
    fill(a,a+256,INT_MAX);
    for(int i = 0; i < M; i++){
      int n = round(128+C[i]);
      a[n] = p(n-x[0]);
    }
    int b[256];
    for(int t = 1; t < N; t++){
      fill(b,b+256,INT_MAX);
      for(int i = 0; i < 256; i++){
        if(a[i] == INT_MAX) continue;
        for(int j = 0; j < M; j++){
          int n = round(i+C[j]);
          b[n] = min(b[n],a[i]+p(n-x[t]));
        }
      }
      copy(b,b+256,a);
    }
    cout << *min_element(a,a+256) << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define INF 1876543210

int main()
{
  int n,m;cin>>n>>m;
  while(n!=0&&m!=0)
  {
    vector<int> c(m);
    vector<int> x(n);
    int i,j,k;
    rep(i,m)cin>>c[i];
    rep(i,n)cin>>x[i];
    vector<vector<int>> dp(n+1,vector<int>(256,INF));
    dp[0][128]=0;
    rep(i,n)rep(j,256)rep(k,m)
    {
      int aft=j+c[k];
      if(aft>255)aft=255;
      if(aft<0)aft=0;
      int add=(x[i]-aft);
      dp[i+1][aft]=min(dp[i+1][aft],dp[i][j]+add*add);
    }
    int ans=INF;
    rep(i,256)ans=min(ans,dp[n][i]);
    cout<<ans<<endl;
    cin>>n>>m;
  }
}

C++: #include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[a]; if (a != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
int n, m;
int dfs(vvi &dp, const vi &c, const vi &x, int i,int j) {
	if (i == n)return 0;
	if (dp[i][j] != -1)return dp[i][j];
	int ret = INF;
	REP(k, m) {
		int tmp = j + c[k];
		if (tmp > 255)tmp = 255;
		if (tmp < 0)tmp = 0;
		ret = min(ret, (tmp - x[i])*(tmp - x[i]) + dfs(dp, c, x, i + 1, tmp));
	}
	return dp[i][j] = ret;
}
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (1) {
	
		cin >> n >> m;
		if (!n)break;
		vi c(m), x(n);
		REP(i, m)cin >> c[i];
		REP(i, n)cin >> x[i];
		vvi dp(n, vi(256, -1));
		cout << dfs(dp, c, x, 0, 128) << endl;
	}
	return 0;
}