#include<bits/stdc++.h>
using namespace std;

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define INF 1876543210

int main()
{
  int n,m;cin>>n>>m;
  while(n!=0&&m!=0)
  {
    vector<int> c(m);
    vector<int> x(n);
    int i,j,k;
    rep(i,m)cin>>c[i];
    rep(i,n)cin>>x[i];
    vector<vector<int>> dp(n+1,vector<int>(256,INF));
    dp[0][128]=0;
    rep(i,n)rep(j,256)rep(k,m)
    {
      int aft=j+c[k];
      if(aft>255)aft=255;
      if(aft<0)aft=0;
      int add=(x[i]-aft);
      dp[i+1][aft]=min(dp[i+1][aft],dp[i][j]+add*add);
    }
    int ans=INF;
    rep(i,256)ans=min(ans,dp[n][i]);
    cout<<ans<<endl;
    cin>>n>>m;
  }
}

