#include <iostream>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

const int big = 100000000;
int main(){
  while(1){
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    vector<int> c(m);
    vector<int> x(n);
    rep(i, m) cin >> c[i];
    rep(i, n) cin >> x[i];
    vector<vector<int>> dp(n+1, vector<int>(256, big));
    dp[0][128] = 0;
    int y;
    rep(i, n)rep(j, 256)rep(k, m){
      y = j + c[k];
      if (y < 0) y = 0;
      if (y > 255) y = 255;
      dp[i+1][y] = min(dp[i+1][y], dp[i][j] + (y - x[i]) * (y - x[i]));
    }
    int ans = big;
    rep(i, 256) if(ans > dp[n][i]) ans = dp[n][i];
    cout << ans << endl;
  }
  
  return 0;
}
