//Differential Pulse Code Modulation
#include<bits/stdc++.h>
using namespace std;

#define INF 1000000000
int N, M;
int C[16], x[20000];
int dp[20000][257]; //dp[p] := i???????????°?????\????????\???????????¨?????????????????????????????¨?????????????????????????°????

//p:?????? w:?????¨?????????
int solve(int p, int w){
  if(p==N)return 0;
  if(dp[p][w]!=-1)return dp[p][w];

  int mn=INF;
  for(int i=0; i<M; i++){
    int nw = w+C[i];
    if(nw<0)nw=0;
    if(nw>255)nw=255;
    int tmp=solve(p+1, nw)+(nw-x[p])*(nw-x[p]);
    if(mn>tmp)mn=tmp;
  }
  return dp[p][w]=mn;
}

int main(){
  while(cin >> N >> M, !(N==0 && M==0)){
    //??\???
    for(int i=0; i<M; i++)
      cin >> C[i];
    for(int i=0; i<N; i++)
      cin >> x[i];

    memset(dp, -1, sizeof(dp));

    cout << solve(0, 128) << endl;
  }
  return 0;
}