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++: /*
Hokudai Practice Contest 2014
F - Differential Pulse Code Modulation

ティツァツ」テヲツウツ陛ッツシツ咼P
y_iテ」ツつ津」ツ?づ」ツつ凝・ツ?、pテ」ツ?ォテァツャツヲテ・ツ渉キテ・ツ個姪」ツ?凖」ツつ凝」ツ?ィテ」ツ?催」ツ?ョテ」ツつウテ」ツつケテ」ツδ暗」ツ?ッテ」ツ?【_{i-1}テ」ツつ津」ツ?ゥテ」ツ?ョテ・ツ?、テ」ツ?ォテァツャツヲテ・ツ渉キテ・ツ個姪」ツ?療」ツ?淌」ツ?凝」ツ??
テ」ツ?ォテ」ツ?療」ツ?凝、ツセツ敕・ツュツ佚」ツ?療」ツ?ヲテ」ツ??」ツ?ェテ」ツ??」ツ?づ」ツつ暗」ツ?」テ」ツ?ヲテ」ツ??
テ」ツ??P[i][p] := y_iテ」ツつ恥テ」ツ?ォテァツャツヲテ・ツ渉キテ・ツ個姪」ツ?療」ツ?淌」ツ?ィテ」ツ?催」ツ?ョテ」ツ?（テ」ツ?セテ」ツ?ァテ」ツ?ョテヲツ慊?・ツーツ湘、ツコツ古、ツケツ療・ツ陳?
テ」ツ?ィテ」ツ?凖」ツつ凝」ツ?ィテ」ツ??
  DP[i][p] := min{ DP[i-1][p-c_j] + (p-c_j)^2 | 0<=j<m}
テ」ツ?ィテヲツシツクテ・ツ個姪・ツシツ湘」ツ?古ァツォツ凝」ツ?ヲテ」ツつ嘉」ツつ古」ツつ凝」ツ??テヲツュツ」テァツ「ツコテ」ツ?ォテ」ツ?ッテ」ツ??テ」ツつ?55テ」ツ?ァテ」ツ?ョテ、ツクツクテ」ツつ?」ツつづィツ?εヲツ?ョテ」ツ?凖」ツつ凝・ツソツ?ィツヲツ?」ツ?古」ツ?づ」ツつ?

DPテ」ツδ?」ツδシテ」ツδ姪」ツδォテ」ツ?ョNテδ?56テ」ツ?ョテ」ツ?敕」ツつ古」ツ?榲」ツつ古」ツ?ョテァツ環カテヲツ?凝」ツ?ォテ」ツ?、テ」ツ??」ツ?ヲテ」ツ??
テ」ツ?ゥテ」ツ?ョテ」ツつウテ」ツδシテ」ツδ嘉」ツδ姪」ツδε」ツつッテ」ツつ津ァツ板ィテ」ツ??」ツつ凝」ツ?ョテ」ツ?凝」ツつ津」ツ?凖」ツ?ケテ」ツ?ヲティツゥツヲテ」ツ?崚」ツ?ーテ」ツつ暗」ツ??」ツ?淌」ツつ?」ツ??
テヲツ卍づゥツ鳴禿ィツィツ暗ァツョツ療ゥツ?湘」ツ?ッ256*N*Mテ」ツ?ァテヲツ環妥」ツ?暗」ツつ嘉」ツつ古」ツつ凝」ツ??
*/

#include<iostream>
#include<cstring>
using namespace std;
//テァツュツ氾」ツ?暗」ツ?ョテァツ青?ィツォツ姪ヲツ慊?ヲツつェテ・ツ?、テ」ツ?栗NT_MAXテ」ツ?ォティツソツ妥」ツ?湘ヲツ?ェテ」ツ?療」ツ??」ツ?淌」ツつ?」ツ??・ツソツオテ」ツ?ョテ」ツ?淌」ツつ〕ong longテ」ツつ津ァツ板ィテ」ツ??」ツつ?
typedef long long ll;

ll dp[20001][256];
ll code[20], x[20000];
const ll INF = 1e15;

int main(){
  int n,m;
  while(cin >> n >> m, n){
    for(int i=0;i<m;i++)cin >> code[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;

    //DP
    for(int i=0;i<n;i++){
      for(int j=0;j<256;j++){
	for(int k=0;k<m;k++){
	  //テ・ツ可催」ツ?ョテ・ツ?、テ」ツ?桂テ」ツ?ァテ」ツ?づ」ツつ甘」ツ?〔テァツ閉ェテァツ崢ョテ」ツ?ョテ」ツつウテ」ツδシテ」ツδ嘉」ツδ姪」ツδε」ツつッテ」ツつ津、ツスツソテ」ツ??・ツ?エテ・ツ青暗」ツ?ョテ・ツ?、nxtテ」ツつ津ヲツアツづ」ツつ?」ツつ?
	  ll nxt = j+code[k];
	  if(nxt<0)nxt = 0;
	  if(nxt>255)nxt = 255;

	  //テ、ツコツ古、ツケツ療ィツェツ、テ・ツキツョテ」ツつ津ヲツアツづ」ツつ?」ツつ?
	  ll cost = (x[i]-nxt)*(x[i]-nxt);

	  //テヲツシツクテ・ツ個姪・ツシツ湘」ツ?ォテ・ツ淞コテ」ツ?・テ」ツ??」ツ?淌ィツィツ暗ァツョツ療」ツつ津ィツ。ツ古」ツ??
	  dp[i+1][nxt] = min(dp[i+1][nxt], dp[i][j] + cost);
	}
      }
    }

    ll ans = INF;
    //テヲツ慊?・ツセツ古」ツ?ョテ・ツ?、テ」ツ?ッテ」ツ??」ツ?湘」ツつ嘉」ツ?ァテ」ツ?づ」ツ?」テ」ツ?ヲテ」ツつづ」ツつ暗」ツ??」ツ?ョテ」ツ?ァテ」ツ??」ツ?凖」ツ?ケテ」ツ?ヲテ」ツ?ョテ、ツクツュテ」ツ?ョテヲツ慊?・ツーツ湘・ツ?、テ」ツつ津ヲツアツづ」ツつ?」ツつ?
    for(int i=0;i<256;i++)ans = min(ans, dp[n][i]);
    cout << ans << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define FOR(i,s,n) for(int i=s; i<n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

struct node {
	int cost;
	int ind;
	int val;
	node(int c, int i, int v) {
		cost = c;
		ind = i;
		val = v;
	}
};

bool checked[20001][256];

bool operator<(const node& lhs, const node& rhs) {
	if( lhs.cost == rhs.cost ) {
		return lhs.ind < rhs.ind;
	}
	return lhs.cost > rhs.cost;
}

int main() {
	int n, m;
	while(cin >> n >> m, n || m) {
		memset(checked, 0, sizeof(checked));
		vector<int> c(m+1), x(n+1);
		rep(i,m) cin >> c[i];
		rep(i,n) cin >> x[i];
		priority_queue<node> que;
		que.push(node(0, 0, 128));
		while(!que.empty()) {
			node nd = que.top();
			que.pop();
			if( nd.ind == n ) {
				cout << nd.cost << endl;
				break;
			}
			if( checked[nd.ind][nd.val] ) {
				continue;
			}
			checked[nd.ind][nd.val] = true;
			rep(i, m) {
				int nval = min(max(nd.val+c[i], 0), 255);
				if( !checked[nd.ind+1][nval] ) {
					que.push( node(nd.cost+(x[nd.ind]-nval)*(x[nd.ind]-nval), nd.ind+1, nval) );
				}
			}
		}
	}
	return 0;
}

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

int n,m;
int dp[20100][300];
int x[20100],c[20];

int main(){
  while(cin >> n >> m, n||m){
    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] = 1000000000;
    }

    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 tmp = j + c[k];
	  if(tmp<0)tmp = 0;
	  if(255<tmp)tmp = 255;
	  int sq = (x[i]-tmp)*(x[i]-tmp);
	  dp[i+1][tmp] = min(dp[i+1][tmp],dp[i][j]+sq);
	}
      }
    }

    int ans = 1000000000;
    for(int i=0;i<256;i++)ans = min(ans,dp[n][i]);
    cout << ans << endl;
  }
}

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

#define INF 1 << 28
#define SQUARE(n) ((n)*(n))
typedef long INT;
typedef vector <int> VI;
typedef vector <VI> VVI;

int main( void )
{
	int n, m; // N: üÍM, M: R[hubNÌ·³
	while ( cin >> n >> m && n && m )
	{
		// üÍ
		VI C(m);
		for ( int i = 0; i < m; i++ )
		{
			cin >> C[i];
		}
		VI x(n);
		for ( int i = 0; i < n; i++ )
		{
			cin >> x[i];
		}

		VVI  dp = VVI( n+1, VI( 256, INF ) );
		dp[0][128] = 0;

		for ( int k = 1; k <= n; k++ )
		{
			for ( int i = 0; i < 256; i++ )
			{
				for ( int j = 0; j < m; j++ )
				{
					int next = i + C[j];
					next = next < 0 ? 0 : next;
					next = next > 255 ? 255 : next;
					dp[k][next] = min( dp[k][next], dp[k-1][i] + SQUARE( next - x[k-1] ) );
				}
			}
		}

		int answer = INF;
		for ( int i = 0; i < 256; i++ ) answer = min( answer, dp[n][i] );
		cout << answer << endl;
	}
	return 0;
}