#include <iostream>
#define INF 1e+18
#define int long long
using namespace std;

int dp[20001][256];

signed main(){
	int n,m,c[16];
	while(cin >> n >> m,n){
		for(int i = 0;i <= n;i++){
			for(int j = 0;j < 256;j++) {
				if(i == 0 && j == 128) dp[i][j] = 0;
				else dp[i][j] = INF;
			}
		}
		for(int i = 0;i < m;i++) cin >> c[i];
		for(int i = 0;i < n;i++){
			int x;
			cin >> x;
			for(int j = 0;j < 256;j++){
				for(int k = 0;k < m;k++){
					int d = j + c[k];
					if(d < 0) d = 0;
					else if(d > 255) d = 255;
					dp[i + 1][d] = min(dp[i + 1][d],dp[i][j] + (x - d) * (x - d));
				}
			}
		}
		int mi = INF;
		for(int i = 0;i < 256;i++) mi = min(mi,dp[n][i]);
		cout << mi << endl;
	}
	return 0;
}