#include <iostream>
#define INF 1000000000;
using namespace std;

int code[20001];
int num[17];
int N,M;
int minn;
int count = 0;

void saiki(int y,int sum,int kai){
  count++;
  //  cout <<"y="<< y <<" sum=" << sum << " kai="<< kai<< endl;
  if(kai == N && sum < minn) minn = sum;

  else if(kai < N && sum < minn ) {
    for(int i=0;i<M;i++) {
      int yn = y+code[i];
      if(yn < 0) yn = 0;
      if(yn > 255) yn = 255;
      saiki(yn,sum+(num[kai]-yn)*(num[kai]-yn),kai+1);
    }
  }
}

int main() {
  
  while(1) {   
    cin >> N >> M;
  if(N == 0 && M == 0) break;
  for(int i=0;i<M;i++) cin >> code[i];
  for(int i=0;i<N;i++) cin >> num[i];
  
  minn = INF;
  saiki(128,0,0);
  cout << minn << endl;
  //  cout << count << endl;
  }
  return 0;
  
}