#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;


class DP{
public:
	int cost, n, x;
	
	DP(){}
	DP(int xx, int nn, int cc){
		cost = cc;x=xx;n=nn;
	}
	
	bool operator< (const DP &opp) const {
		return cost>opp.cost;
	}
};
int C[20];
int x[20001];
bool visit[20001][256];
int N, M;
int main(){
	int i,j;
	while(cin >> N >> M, N){
		memset(visit, 0, sizeof(visit));
		for(i=0;i<M;i++) cin >> C[i];
		for(i=0;i<N;i++) cin >> x[i];
		priority_queue<DP> dp;
		DP S;
		dp.push(DP(128, 0, 0));
		while(!dp.empty()){
			S = dp.top();
			dp.pop();

			if(S.n == N) break;
			for(i=0;i<M;i++){
				DP T=S;
				T.x += C[i];
				if(T.x > 255) T.x = 255;
				if(T.x < 0) T.x = 0;
				
				T.cost += (x[T.n]-T.x)*(x[T.n]-T.x);
				T.n ++;
				if(visit[T.n][T.x]) continue;
				visit[T.n][T.x] = true;
				dp.push(T);
			}
		}
		cout << S.cost << endl;
	}
	return 0;
}