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++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const double PI = acos(-1);
const ll MOD = 1000000007;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
///////////////////////////////////////////////////////////////

vector<ll> h;

ll N,A,B;

bool f(ll x) {
  ll count = 0;
  rep(i,N) {
    ll k = max(0LL,h[i] - x * B);
    count += ((k + A - B - 1) / (A-B));
  }
  if (count <= x) return true;
  else return false;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr); //入出力高速化
  while (true) {
    int N,M; cin >> N >> M;
    if (N == 0 && M == 0)break;
    vector<ll> C(M),x(N);
    rep(i,M) cin >> C[i];
    rep(i,N) cin >> x[i];

    vector<vector<ll>> dp(N+1, vector<ll>(256,1e18));
    dp[0][128] = 0;
    rep(i,N) {
      rep(j,256) {
        if (dp[i][j] == 1e18) continue;
        rep(k,M) {
          int y = j + C[k];
          if (y < 0) y = 0;
          if (y > 255) y = 255;
          chmin(dp[i+1][y],dp[i][j] + (y - x[i]) * (y - x[i]));
        }
      }
    }
    ll ans = 1e18;
    rep(i,256) chmin(ans,dp[N][i]);

    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)
#define all(a) a.begin(),a.end()
#define INF 2e9
#define updmin(dp,x) (dp = min(dp,x))

int main(void){
    int n,m;
    while(cin>>n>>m, n|m){
        vector<int> c(m);
        rep(i,m) cin>>c[i];
        vector<int> x(n);
        rep(i,n) cin>>x[i];
        vector<vector<int>> dp(n+1,vector<int>(256,INF));
        dp[0][128] = 0;
        rep(i,n)rep(y,256)rep(k,m){
            int ny = max(0,min(255,y+c[k]));
            updmin(dp[i+1][ny], dp[i][y]+(int)pow(x[i]-ny,2));
        }
        cout<<*min_element(all(dp[n]))<<endl;
    }
    return 0;
}

C++: ///
// File:  2199.cpp
// Author: ymiyamoto
//
// Created on Sat Dec  9 23:16:36 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

#define MAX UINT32_MAX

using namespace std;

int32_t main()
{
  while (true) {
    uint32_t N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) break;

    vector<int32_t> C(M);
    vector<int32_t> x(N + 1);

    for (uint32_t i = 0; i < M; i++) {
      cin >> C[i];
    }

    for (uint32_t i = 1; i <= N; i++) {
      cin >> x[i];
    }

    vector<vector<uint64_t>> dp(N + 1, vector<uint64_t>(256, MAX));
    dp[0][128] = 0;

    for (uint32_t i = 0; i < N; i++) {
      for (int32_t j = 0; j < 256; j++) {
        if (dp[i][j] == MAX) continue;
        for (uint32_t k = 0; k < C.size(); k++) {
          int32_t val = min(max(0, j + C[k]), 255);
          int32_t diff = (x[i + 1] - val);
          dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + diff * diff);
        }
      }
    }

    uint64_t minval = MAX;
    for (int32_t i = 0; i < 256; i++) {
      minval = min(minval, dp[N][i]);
    }

    cout << minval << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,s,e) for(int i=(s); i<(e);i++)
#define rep(i,n) REP(i,0,n)
#define rep1(i,n) REP(i,1,n)
#define repe(i,n) for(auto &&i:n)
#define all(v) (v).begin(),(v).end()
#define decimal fixed<<setprecision(20)
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
using namespace std;
using LL = long long;
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 INF = 1e9;
const LL LLINF = 1e16;

LL dp[2][260];
vector<int> c, x;

int main(){
    fastcin();

    int n, m;
    cin >> n >> m;
    while(n > 0){
        c.resize(m);
        x.resize(n);
        rep(i, m) cin >> c[i];
        rep(i, n) cin >> x[i];
        rep(i, 2)rep(j, 256) dp[i][j] = LLINF;
        dp[0][128] = 0;
        rep(i, n){
            rep(j, 256) dp[(i+1)%2][j] = LLINF;
            rep(j, 256)if(dp[i%2][j] < LLINF){
                rep(k, m){
                    int nxt = max(0, min(255, j+c[k]));
                    int df = nxt-x[i];
                    chmin(dp[(i+1)%2][nxt], dp[i%2][j]+df*df);
                }
            }
        }
        // rep(i, n){
        //     rep(j, 256) cout << dp[i+1][j] << " ";
        //     cout << endl;
        // }
        LL ans = LLINF;
        rep(i, 256){
            chmin(ans, dp[n%2][i]);
        }
        cout << ans << endl;
        cin >> n >> m;
    }

}