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

int n,m;
int dp[20100][300];
int x[20100],c[20];

int main(){
  while(cin >> n >> m, n||m){
    for(int i=0;i<m;i++)cin >> c[i];
    for(int i=0;i<n;i++)cin >> x[i];

    for(int i=0;i<=n;i++){
      for(int j=0;j<256;j++)dp[i][j] = 1000000000;
    }

    dp[0][128] = 0;

    for(int i=0;i<n;i++){
      for(int j=0;j<256;j++){
	for(int k=0;k<m;k++){
	  int tmp = j + c[k];
	  if(tmp<0)tmp = 0;
	  if(255<tmp)tmp = 255;
	  int sq = (x[i]-tmp)*(x[i]-tmp);
	  dp[i+1][tmp] = min(dp[i+1][tmp],dp[i][j]+sq);
	}
      }
    }

    int ans = 1000000000;
    for(int i=0;i<256;i++)ans = min(ans,dp[n][i]);
    cout << ans << endl;
  }
}
    