#include<iostream>
#include<algorithm>
#include<cmath>
#define inf (1<<29)
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;
int N,M;
int C[17];
int x[20010];
int dp[20010][256];

int main()
{
  while(cin >> N >> M,N|M)
    {
      REP(i,1,M+1)cin >> C[i];
      REP(i,1,N+1)cin >> x[i];
      rep(i,20010)rep(j,256)dp[i][j] = inf;
      dp[0][128] = 0;

      rep(i,N+1)
	{
	  REP(j,1,M+1)
	    {
	      rep(k,256)
		{
		  if(dp[i][k] == inf)continue;
		  int value = k+C[j];
		  if(value < 0)value = 0;
		  else if(value > 255)value = 255;
		  dp[i+1][value] = min(dp[i+1][value],
				       dp[i][k]+(x[i+1]-value)*(x[i+1]-value));
		}
	    }
	}

      int men = inf;
      rep(i,256)men = min(men,dp[N][i]);
      cout << men << endl;
    }
  return 0;
}