#include <iostream>
#include <algorithm>
#define inf 1e9

using namespace std;

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

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

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

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

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

