#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int INF = (int)10e9;

int main(){
  int n, m;
  while(scanf("%d%d", &n, &m) && (n|m)){
    vector<int> C(m);
    for(int i=0; i<m; i++) scanf("%d", &C[i]);
    
    vector<int> dp(256, INF);
    vector<int> next(256);
    dp[128] = 0;
    while(n--) {
      fill(next.begin(), next.end(), INF);
      int in; scanf("%d", &in);
      for(int i=0; i<256; i++){
	if(dp[i] == INF) continue;
	for(int j=0; j<m; j++){
	  int conv = i + C[j];
	  conv = min(max(conv, 0), 255);
	  int diff = (in - conv) * (in - conv);
	  next[conv] = min(next[conv], dp[i] + diff);
	}
      }
      dp = next;
    }
    
    printf("%d\n", *min_element(dp.begin(), dp.end()));
  }
  return 0;
}