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++: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

int dp[20010][256];
int solve_testcase() {
    int N, M; scanf("%d%d", &N, &M);
    if(N == 0 and M == 0) return 1;

    vector<int> C(M), X(N);
    for(int i=0; i<M; i++) scanf("%d", &C[i]);
    for(int i=0; i<N; i++) scanf("%d", &X[i]);

    fill(dp[0], dp[N+1], INF);
    dp[0][128] = 0;
    for(int i=0; i<N; i++) {
        for(int j=0; j<256; j++) {
            for(int k=0; k<M; k++) {
                int nj = j + C[k];
                if(nj < 0) nj = 0;
                if(nj > 255) nj = 255;
                int diff = (X[i] - nj);
                chmin(dp[i+1][nj], dp[i][j] + diff*diff);
            }
        }
    }

    int ans = INF;
    for(int i=0; i<256; i++) chmin(ans, dp[N][i]);
    printf("%d\n", ans);
    return 0;
}

int main() {
    while(!solve_testcase());
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
int N,M;
int C[20];
int X[20004];
const long long INF = 1000000000000;
long long dp[20004][130];
long long dfs(int i_,int y_) {
    if(i_ == N) return 0;
    if(dp[i_][y_]!=-1) return dp[i_][y_];

    long long ret = INF;

    rep(i,M) {
        int y = y_ + C[i];
        y = max(y,0);
        y = min(y,255);
        //cout<<y<<","<<X[i_]<<endl;
        ret = min(ret,dfs(i_+1,y)+(X[i_]-y)*(X[i_]-y));

    }
    return dp[i_][y_]=ret;

}
void solve() {
    rep(i,20004) rep(j,130) dp[i][j] = -1;
    rep(i,M) cin>>C[i];
    rep(i,N) cin>>X[i];
    long long ans = INF;
    ans = min(ans,dfs(0,128));
    cout<<ans<<endl;
    rep(i,M) C[i] = 0;
    rep(i,N) X[i] = 0;

}
int main() {
    while(cin>>N>>M) {
        if(N+M==0) return 0;
        solve();
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int N, M;
int C[16], x[20000];
int dp[20001][256];
const int INF = 1000000000;

int main() {
  while(true) {
    cin >> N >> M;
    if(N == 0 && M == 0) 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;
    for(int i = 0; i < N; i++) {
      for(int j = 0; j <= 255; j++) {
        for(int k = 0; k < M; k++) {
          int next = j + C[k];
          if(next < 0) next = 0;
          if(next > 255) next = 255;
          dp[i+1][next] = min(dp[i+1][next], dp[i][j] + (x[i] - next) * (x[i] - next));
        }
      }
    }
    int ans = INF;
    rep(i, 256) {
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;

  }
  return 0;
}

C++: // ------------------
// author: MatsuTaku
// date  : 27/02/2020
// ------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  constexpr ll INF = 1e18;
  int n,m;
  while (cin>>n>>m and (n!=0 or m!=0)) {
    vector<int> code(m); for(auto&v:code) cin >> v;
    vector<array<ll, 256>> dp(n+1);
    for (int j = 0; j < n+1; j++) for (int i = 0; i < 256; i++)
      dp[j][i] = INF;
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      ll v; cin>>v;
      for (int f = 0; f < 256; f++) {
        if (dp[i][f]==INF) continue;
        for (auto c:code) {
          ll t = max(min(f+c, 255), 0);
          dp[i+1][t] = min(dp[i+1][t], dp[i][f]+(v-t)*(v-t));
        }
      }
    }
    cout << *min_element(dp[n].begin(), dp[n].end()) << endl;
  }

  return 0;
}

C++: #include <iostream>

using namespace std;

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

void Rec(int index, int sum, int y)
{
    if (index == N) {
        ans = min(ans, sum);
        return ;
    }

    for (int i = 0; i < M; ++i) {
        int y_n = y + C[i];
        if (y_n < 0)
            y_n = 0;
        else if (y_n > 255)
            y_n = 255;

        int sum_n = sum + (x[index] - y_n) * (x[index] - y_n);
        if (sum_n < ans)
            Rec(index + 1, sum_n, y_n);
    }
}

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];
        ans = 1e9;

        Rec(0, 0, y0);
        cout << ans << endl;
    }

    return 0;
}