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;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 40;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

ll dp[22000][300];

int main() {
    ll N, M;
    while(cin >> N >> M) {
        if(N == 0 && M == 0) return 0;

        vector<ll> C(M), x(N);
        rep(i, M) cin >> C[i];
        rep(i, N) cin >> x[i];

        rep(i, 22000) rep(j, 300) dp[i][j] = INF;
        dp[0][128] = 0;

        rep(i, N) rep(j, 256) {
            rep(k, M) {
                if(j + C[k] < 0) chmin(dp[i+1][0], dp[i][j] + x[i]*x[i]);
                else if(j + C[k] > 255) chmin(dp[i+1][255], dp[i][j] + (255-x[i])*(255-x[i]));
                else chmin(dp[i+1][j+C[k]], dp[i][j] + (j+C[k]-x[i])*(j+C[k]-x[i]));
            }
        }
        ll ans = INF;
        rep(j, 256) chmin(ans, dp[N][j]);
        cout << ans << endl;
    }
}

C++:  
#include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using mii=map<int,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9

int main(){
	int n,m;
	while(cin>>n>>m,n){
        vi code(m);
        rep(i,m)cin>>code[i];
        vvi dp(2,vi(256,INF));
        dp[0][128]=0;
        range(i,1,n+1){
            int num; cin>>num;
            fill(all(dp[i%2]),INF);
            rep(j,256){
                rep(k,m){
                    int next=min(max(j+code[k],0),255);
                    int diff=num-next;
                    dp[i%2][next]=min(dp[i%2][next],dp[1-i%2][j]+diff*diff);
                }
            }
        }
        cout<<*min_element(all(dp[n%2]))<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
#define _(_1,_2,_3,N,...)N
#define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__)
template<C T>void pr1(T a){cout<<a;ln;}
template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  int n,m;
  while(cin >> n >> m && n) {
    int c[m],a[n];
    rep(i,m) R c[i];
    rep(i,n) R a[i];
    int dp[2][500];
    rep(i,2)rep(j,500)dp[i][j]=MAX;
    dp[0][128]=0;
    rep(i,n) {
      rep(j,500) {
        if(dp[0][j]!=MAX) {
          rep(k,m) {
            int x=j+c[k];
            if(x<0) x=0;
            if(x>255) x=255;
            dp[1][x]=min(dp[1][x],dp[0][j]+(a[i]-x)*(a[i]-x));
          }
        }
      }
      rep(j,500) {
        dp[0][j]=dp[1][j];
        dp[1][j]=MAX;
      }
    }
    int ans=MAX;
    rep(i,500) ans=min(ans,dp[0][i]);
    pr(ans);
  }
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}

C++: 
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <iomanip>
 
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define CLR(a) memset((a), 0 ,sizeof(a))
#define MCLR(a) memset((a), -1 ,sizeof(a))
#define SZ(a) (sizeof(a))
#define FILL(a,v) fill((int*)a,(int*)(a+(SZ(a)/SZ(*(a)))),v)

bool cheak(int x,int y,int xMax,int yMax)
{return x >= 0 && y >= 0 && xMax > x && yMax > y;}
const int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
const int INF = 2147483648/3;
typedef pair<int,int> P;
typedef long long ll;
typedef unsigned long long ull;

const int MAX = 20010;
int dp[MAX][256];

int main()
{
	int n,m;
	while(cin >> n >> m && (n || m))
	{
		int c[20],x[MAX];
		REP(i,m)cin >> c[i];
		REP(i,n)cin >> x[i];
		
		FILL(dp,INF);
		dp[0][128] = 0;

		REP(i,n)
		REP(j,256)
		if(dp[i][j] != INF)
		REP(k,m)
		{
			int tmp = j+c[k];
			int oldval = dp[i][j];

			if(tmp < 0)
				tmp = 0;
			if(tmp >= 256)
				tmp = 255;
			
			int val = pow(x[i]-tmp,2);
			if(val < dp[i+1][tmp])
				dp[i+1][tmp] = val+oldval;
		}

		int ans = INF;
		REP(j,256)
		{
			ans = min(dp[n][j],ans);
		}


		cout << ans << endl;
	}

	return 0;

}