#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;

#define p(x) (x)*(x)

int round(int x)
{
  if(x > 255) return 255;
  else if(x < 0) return 0;
  else return x;
}

int main()
{
  while(1){
    int N, M;
    cin >> N >> M;
    if(N == 0 && M == 0) break;
    int C[M], x[N];
    for(int i = 0; i < M; i++)
      cin >> C[i];
    for(int i = 0; i < N; i++)
      cin >> x[i];
    int a[256];
    fill(a,a+256,INT_MAX);
    for(int i = 0; i < M; i++){
      int n = round(128+C[i]);
      a[n] = p(n-x[0]);
    }
    int b[256];
    for(int t = 1; t < N; t++){
      fill(b,b+256,INT_MAX);
      for(int i = 0; i < 256; i++){
        if(a[i] == INT_MAX) continue;
        for(int j = 0; j < M; j++){
          int n = round(i+C[j]);
          b[n] = min(b[n],a[i]+p(n-x[t]));
        }
      }
      copy(b,b+256,a);
    }
    cout << *min_element(a,a+256) << endl;
  }
}