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

#define rep(i,x,y) for(int (i)=(x);i<(y);(i)++)
#define INF 1000000000

int main()
{
  while(true){
    int N,M;
    cin>>N>>M;
    if(N==0&&M==0){
      break;
    }

    vector<int> C,x;
    int temp;
    rep(i,0,M){
      cin>>temp;
      C.push_back(temp);
    }
    rep(i,0,N){
      cin>>temp;
      x.push_back(temp);
    }

    vector<vector<int>> dp(N+1,vector<int>(256,INF));
    dp[0][128]=0;
    rep(i,0,N){
      rep(j,0,256){
        if(dp[i][j]==INF){
          continue;
        }
        rep(k,0,M){
          int yn=j+C[k];
          yn=min(255,yn);
          yn=max(0,yn);
          int sum=(x[i]-yn)*(x[i]-yn);
          dp[i+1][yn]=min(dp[i+1][yn],dp[i][j]+sum);
        }
      }
    }

    int ans=dp[N][0];
    rep(i,0,256){
      ans=min(ans,dp[N][i]);
    }
    cout<<ans<<endl;
  }

  return 0;
}