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 <vector>
#include <iostream>
#include <utility>
#include <cmath>

using namespace std;

const long long INF = 1e15;
const long long Y = 128;
int main(){
	long long n,m;
	while(cin>>n>>m,n){
		vector<long long> cod(m),signal(n+1);
		vector<vector<pair<long long,long long>>> dp(n+1,vector<pair<long long,long long>>(m,make_pair(INF,INF)));
		double ave_signal = 0;
		for(int i=0;i<m;i++){//code input
			cin >> cod[i];
		}
		for(int i=1;i<n+1;i++){//signal input
			cin >> signal[i];
			ave_signal += signal[i];
		}
		ave_signal /= n;
		
		for(int i=0;i<m;i++){//?????????input
			dp[0][i].first = Y;
			dp[0][i].second = 0;
			//cout << "(" << dp[0][i].first << ", " << dp[0][i].second << ")";
		}
			//cout << endl;

		for(int i=1;i<n+1;i++){//dp
			for(int j=0;j<m;j++){//??\????????±???
				int min_line = ave_signal;
				int max_line = signal[i];
				if(ave_signal > signal[i]) swap(min_line,max_line); 
					bool is_bad = true;
				for(int k=0;k<m;k++){//?????¶??±
					int tmp_signal = dp[i-1][k].first + cod[j];
					if(tmp_signal>255)tmp_signal = 255;
					else if(tmp_signal<0)tmp_signal = 0;
					if(max_line + 1 >= tmp_signal && min_line - 1 <= tmp_signal){
						if(dp[i-1][k].second + (signal[i]  - tmp_signal)*(signal[i]  - tmp_signal) < dp[i][j].second){
							is_bad = false;
							dp[i][j].second = dp[i-1][k].second + (signal[i]  - tmp_signal)*(signal[i]  - tmp_signal);
							dp[i][j].first = tmp_signal;
						}
					}else if(is_bad){
						dp[i][j].second = dp[i-1][k].second + (signal[i]  - tmp_signal)*(signal[i]  - tmp_signal);
						dp[i][j].first = tmp_signal;
					}
				}
//				cout << "(" << dp[i][j].first << ", " << dp[i][j].second << ")";
			}
			//cout << endl;
		}
		long long ans = INF;
		for(int i=0;i<m;i++)
			ans = min(ans,dp[n][i].second);
		cout << ans << endl;
	}
	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][260];
long long dfs(int i_,int y_) {
    if(i_ == N) return dp[i_][y_] = 0;
    if(dp[i_][y_]!=INF) 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));

    }
    //cout<<i_<<","<<y_<<","<<ret<<endl;
    return dp[i_][y_] = ret;

}
void solve() {
    rep(i,M) cin>>C[i];
    rep(i,N) cin>>X[i];
    /*
    ans = min(ans,dfs(0,128));
    */

    long long ans = INF;
    rep(i,20004) rep(j,260) dp[i][j] = INF;
    dp[0][128] = 0;
    rep(i,N) {
        rep(j,260) {
            rep(k,M) {
                int y = j + C[k];
                y = max(y,0);
                y = min(y,255);
                dp[i+1][y] = min(dp[i+1][y],dp[i][j]+(X[i]-y)*(X[i]-y));
            }
        }
    }
    rep(i,260) ans=min(ans,dp[N][i]);
    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<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



void f(int n,int m){
  int c[1000000];
  
  rep(i,m){
    cin>>c[i];
  }

  int dp[21000][300]={};
  rep(i,21000){
    rep(j,300){
      dp[i][j]=inf;
    }
  }

  dp[0][128]=0;

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

  cout<<ans<<endl;
}


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

  return 0;
}