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;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i] << " ";
  }
  cout << endl;
}


int main () {
  cin.tie(0);
  cout << setprecision(10);

  while(1) {
    int n, m; cin >> n >> m;
    if(n == 0 && m == 0) break;
    vi c(m), x(n+1);
    REP(i, m) {
      cin >> c[i];
    }
    REP(i, n) {
      cin >> x[i+1];
    }

    vector<vector<ll>> dp(n+1, vector<ll>(256, LINF));
    dp[0][128] = 0;

    FOR(i, 1, n+1) {
      REP(j, 256) {
        REP(k, m) {
          int next = min(255, max(0, j+c[k]));
          dp[i][next] = min(dp[i][next], dp[i-1][j] + (x[i] - next) * (x[i] - next));
        }
      }
    }


/*     REP(i, n) {
      printv(dp[i]);
    } */

    ll ans = LINF;
    REP(i, 256) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}

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

long long int dp[20001][256] = {};

int main() {

  while( true ) {

    long long int n, m;
    cin >> n >> m;
    if ( n == 0 ) break;

    vector< long long int > c;

    for ( long long int i = 0; i < m; i++ ) {

      long long int in;
      cin >> in;
      c.push_back( in );

    }

    for ( long long int i = 0; i <= n; i++ ) {
      for ( long long int j = 0; j < 256; j++ ) {
	dp[i][j] = 0;
      }
    }
    dp[0][128] = 1;

    for ( long long int i = 1; i <= n; i++ ) {

      long long int in;
      cin >> in;
      for ( long long int j = 0; j < 256; j++ ) {

	if ( dp[i-1][j] > 0 ) {

	  for ( long long int k = 0; k < m; k++ ) {

	    long long int y = j + c[k];
	    if ( y < 0 ) y = 0;
	    if ( y > 255 ) y = 255;
	    long long int d = y - in;
	    d *= d;
	    d += dp[i-1][j];
	    if ( dp[i][y] == 0 || dp[i][y] > d ) dp[i][y] = d;

	  }

	}

      }

    }

    long long int ans = -1;

    for ( long long int i = 0; i < 256; i++ ) {

      if ( dp[n][i] > 0 ) {

	if ( ans == -1 || ans > dp[n][i] ) ans = dp[n][i];

      }

    }

    cout << ans-1 << endl;

  }

}

C++: #include <iostream>

using namespace std;

const int MAX_N = 20000;
const int MAX_M = 16;
const int INF = 1e9;
const int y0 = 128;
int N, M;
int C[MAX_M + 1], x[MAX_N + 1];
int dp[MAX_N + 10][260];

int main()
{
    while (cin >> N >> M, N || M) {
        // Input
        for (int i = 0; i < M; ++i)
            cin >> C[i];
        for (int i = 0; i < N; ++i)
            cin >> x[i];

        // Init
        for (int i = 0; i <= N; ++i)
            fill(dp[i], dp[i] + 260, INF);
        for (int i = 0; i <= 255; ++i)
            dp[0][i] = 0;

        // Solve
        for (int n = 0; n < N; ++n) {
            for (int y = 0; y <= 255; ++y) {
                for (int i = 0; i < M; ++i) {
                    int next_y = y + C[i];
                    if (next_y < 0)
                        next_y = 0;
                    else if (next_y > 255)
                        next_y = 255;

                    dp[n + 1][next_y] = min(dp[n + 1][next_y],
                                            dp[n][y] + (x[n] - next_y) * (x[n] - next_y));
                }
            }
        }

        int ans = INF;
        for (int i = 0; i <= 255; ++i)
            ans = min(ans, dp[N][i]);

        cout << ans << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define out(S) cout<<(S)<<endl;
#define ShowAll(collection) for(auto i:collection){out(i);}
#define beginend(v) v.begin(),v.end()
#define Foreach(item,collection) for(auto item : collection)

using pii=pair<int,int>;
using vb=vector<bool>;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vvc=vector<vector<char>>;
using ti3=tuple<int,int,int>;
template<typename T> void removeAt(vector<T>& v, int index) { v.erase(v.begin() + index); }
#define OCB(c) (c&1)|(c&2)<<2|(c&4)<<4|(c&8)<<6|(c&16)<<8|(c&32)<<10|(c&64)<<12|(c&128)<<14

int dp[20001][256]; //dp[i][j]=i番目のサンプルをjにできる最小の差分

int solve(int n, int m){
  vi c(m,0),x(n,0);
  rep(i,m) cin>>c[i];
  rep(i,n) cin>>x[i];

  rep(i,n+1)rep(j,256) dp[i][j]=INT_MAX; 
  dp[0][128]=0;

  rep(i,n) rep(j,256) {
    if(dp[i][j]==INT_MAX) continue;
    Foreach(item,c){
      auto yi=j+item;
      if(yi<0) yi=0;
      if(yi>255) yi=255;
      auto diff=x[i]-yi;
      diff=diff*diff;
      dp[i+1][yi]=min(dp[i+1][yi],dp[i][j]+diff);
    }
  }

  return *min_element(dp[n],dp[n]+256);
}

int main(){
  int n,m;
  while(cin>>n>>m,n|m){
    out(solve(n,m));
  }
}