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;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

const double EPS = 1e-9;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

ll f(ll x){
    return max((ll)0,min((ll)255,x));
}

int main(void){
    ll N,M;
    cin >> N >> M;
    while(N!=0 || M!=0){
        vector<ll> C(M); REP(i,M) cin >> C[i];

        vector<ll> x(N); REP(i,N) cin >> x[i];
        ll dp[2][256]; REP(i,256) dp[0][i] = LL_INF;
        dp[0][128] = 0;

        REP(i,N){
            int a,b;
            if(i%2 == 0){
                a = 1;
                b = 0;
            }
            else{
                a = 0;
                b = 1;
            }
            REP(j,256) dp[a][j] = LL_INF;
            REP(j,256){
                if(dp[b][j] == LL_INF) continue;
                REP(k,M){
                    ll t = f(j + C[k]);
                    chmin(dp[a][t], dp[b][j] + (t-x[i])*(t-x[i]));
                }
            }
        }

        ll ans = LL_INF;
        int a; if(N%2 == 0) a = 0; else a = 1;
        REP(i,256) chmin(ans, dp[a][i]);
        cout << ans << en;
        cin >> N >> M;
    }
    return 0;
}

C++: //#include "bits/stdc++.h"

#include <iostream>
#include <cassert>
#include <vector>
// #include <map>
#include <string>
#include <set>
#include <queue>
// #include <algorithm>
// #include <numeric>
// #include <limits>

using namespace std;

struct Node;
struct Seeker;

const int first_signal_log = 128;
int n;
int m;
vector<short> book; // code book // TODO short
vector<short> seq; // sequence // TODO short
priority_queue<Seeker> que;
set<Node> close_list;

template <class T>
void print(const vector<T>& vec) {
	for (auto v : vec) {
		cout << " " << v;
	}
	cout << endl;
}

struct Node {
	string k = "";
	void push_back(int key) {
		assert(0 <= key);
		assert(key <= 16);
		k.push_back((char) key + 65);
	}

	void print() const {
		cout << "(";
		for (auto c : k) {
			cout << (int) c - 65 << " ";
		}
		cout << k;
		cout << ") ";
	}
};

bool operator < (const Node& a, const Node& b) {
	return a.k < b.k;
}

bool operator == (const Node& a, const Node& b) {
	return a.k == b.k;
}

struct Seeker {
	Node node;
	uint cost = 0; // square sum
	int y = first_signal_log; // TODO short

	Seeker(Node _node) : node(_node) {
	}

	void print() const {
		node.print();
		cout << y << " " << cost << endl;
	}

	void change(int code) {
		y += code;
		y = max(0, y);
		y = min(y, 255);
	}

};

bool operator < (const Seeker& a, const Seeker& b) {
	return a.cost > b.cost;
}

void seek(Seeker s, size_t key) {
	s.node.push_back(key);
	if (close_list.count(s.node) > 0) {
		return;
	}
	close_list.insert(s.node);
	size_t time = s.node.k.size();
	auto code = book[key];
	s.change(code); // s.y += code;
	auto diff = s.y - seq[time];
	s.cost += diff * diff;
	que.push(s);
	// cout << "seek s "; s.print(); // debug
}

bool isGoal(const Seeker& s) {
	int time = s.node.k.size();
	return n == time;
}

uint solve() {
	Node start_node;
	Seeker start(start_node);
	que.push(start);
	while (que.empty() == false) {
		Seeker s = que.top();
		// cout << "top s print "; s.print(); // debug
		if (isGoal(s)) {
			return s.cost;
		}
		que.pop();
		for (size_t key = 0; key < book.size(); key++ ) {
			seek(s, key);
		}
	}
	return -1;
}	

void clearQue() {
	while (que.empty() == false) {
		que.pop();
	}
}

void input() {
	clearQue();
	close_list.clear();
	book.resize(m);
	for (auto& val : book) {
		cin >> val;
	}
	seq.resize(n + 1);
	seq[0] = first_signal_log;
	for (int i = 1; i < n + 1; i++) {
		cin >> seq[i];
	}
	// std::cout << "debug book"; print(book);
	// std::cout << "debug seq"; print(seq);
}

int main() {
	while (true) {
		cin >> n >> m;
		if (n == 0 and m == 0) {
			break;
		}
		input();
		cout << solve() << endl;

	}

	// std::cout << "\e[38;5;0m\e[48;5;40m --- end ---  \e[m" << std::endl; // debug
	return 0;
}