#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(),(a).end()
#define ms(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10) 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge{ int from, to, cost; };

int n, m;
int c[20]; int x[20005];
int memo[20005][300];

int dp(int y, int i)
{
	if (i == n) return 0;
	if (memo[i][y] != INF) return memo[i][y];
	int res = INF;
	REP(j, m)
	{
		int tmp = y;
		tmp += c[j];
		if (tmp > 255) tmp = 255;
		if (tmp < 0) tmp = 0;
		res = min(res, dp(tmp,i+1)+(tmp-x[i])*(tmp-x[i]));
	}
	return memo[i][y] = res;
}

int main()
{
	while (cin >> n >> m, n)
	{
		REP(i, 20005)REP(j, 300) memo[i][j] = INF;
		REP(i, m) cin >> c[i];
		REP(i, n) cin >> x[i];
		cout << dp(128, 0) << endl;
	}
	return 0;
}