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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

Int dp[20010][300];
int c[20];
int x[20010];

int main() {
	int n,m;
	while(cin>>n>>m,n){
		int i,j,k;
		for(i=0;i<m;i++){
			cin>>c[i];
		}
		for(i=0;i<n;i++){
			cin>>x[i];
		}
		memset(dp,-1,sizeof(dp));
		dp[0][128]=0;
		int next;
		for(i=0;i<n;i++){
			for(j=0;j<256;j++){
				if(dp[i][j]==-1)continue;
				for(k=0;k<m;k++){
					next=j+c[k];
					if(next<0)next=0;
					if(next>=256)next=255;
					if(dp[i+1][next]==-1||dp[i+1][next]>dp[i][j]+(next-x[i])*(next-x[i]))dp[i+1][next]=dp[i][j]+(next-x[i])*(next-x[i]);
				}
			}
		}
		Int mn=-1;
		for(j=0;j<256;j++){
			if(dp[n][j]!=-1){
				if(mn==-1||mn>dp[n][j])mn=dp[n][j];
			}
		}
		cout<<mn<<endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int C[16];
int x[2001];
int dp[2001][256];
const int INF = 1 << 30;
int main() {
	int N, M;
	while (cin >> N >> M, N) {
		for (int i = 0; i < M; i++) {
			cin >> C[i];
		}
		for (int i = 0; i < N; i++) {
			cin >> x[i];
		}
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j < 256; j++) dp[i][j] = INF;
		}
		dp[0][128] = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < 256; j++) {
				if (dp[i][j] == INF) continue;
				for (int k = 0; k < M; k++) {
					int nj = j + C[k];
					if (0 > nj) nj = 0;
					if (nj > 255) nj = 255;
					int tmp = (nj - x[i])*(nj - x[i]);
					if (dp[i][j] + tmp < dp[i+1][nj]) {
						dp[i+1][nj] = dp[i][j] + tmp;
					}
				}
			}
		}
		int ans = INF;
		for (int i = 0; i < 256; i++) {
			ans = min(ans, dp[N][i]);
		}
		cout << ans << endl;
	}
	
}

C++: # define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;

# define INF ((int)(1<<30))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define DEBUG(x) cout<<#x<<" :"<<x<<endl;
# define EPS 1e-12

template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = { 0,-1,1,0 }, dy[4] = { 1,0,0,-1 };

int clamp(int a, int mi, int ma)
{
	return max(mi, min(a, ma));
}

int n, m;
int c[17];
int a[20002];
int memo[20002][256];

int dp(int now,int o)
{
	if (now > n)return 0;
	if (memo[now][o] != -1)return memo[now][o];
	int ans = INT_MAX;
	for (int i = 0; i < m; i++)
	{
		ans = min(ans, dp(now + 1, clamp(o + c[i], 0, 255)));
	}
	ans += (o - a[now])*(o - a[now]);
	return memo[now][o] = ans;
}

int main()
{
	
	while (cin >> n >> m&&n)
	{
		memset(memo, -1, sizeof memo);
		REP(i, m)cin >> c[i];
		REP(i, n)cin >> a[i+1];
		a[0] = 128;
		cout << dp(0,128) << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265

int dp[21000][300];

void f(int n,int m){

  FOR(i,1,n+1){
    rep(j,300){
      dp[i][j] = inf;
    }
  }
  int c[1000];
  rep(i,m){
    cin>>c[i];
  }

  FOR(i,1,n+1){
    int x;
    cin>>x;
    rep(j,256){
      rep(k,m){
        int a = j+c[k];
        a = min(255,a);
        a = max(0,a);
        int d = (a-x)*(a-x);
        dp[i][a] = min(dp[i][a],dp[i-1][j]+d);
      }
    }
  }
  int ans = inf;
  rep(i,300){
    ans = min(ans,dp[n][i]);
  }

  cout<<ans<<endl;

}

int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(n==0&&m==0) break;
    f(n,m);
  }

  return 0;
}