#include <iostream>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
#define INF 100000000
using namespace std;
typedef long long ll;

int N, M, C[16], X[20000];
ll dp[20001][256];

int main(void) {
  while(cin >> N >> M, N || M) {
    REP(i, 0, M) cin >> C[i];
    REP(i, 0, N) cin >> X[i];

    REP(i, 0, N + 1) REP(j, 0, 256) dp[i][j] = INF;
    dp[0][128] = 0;

    REP(i, 0, N) REP(j, 0, 256) if(dp[i][j] < INF) REP(k, 0, M) {
      ll y = max(0, min(255, j + C[k]));
      dp[i + 1][y] = min(dp[i + 1][y], dp[i][j] + (y - X[i]) * (y - X[i]));
    }

    ll ans = INF;
    REP(i, 0, 256) ans = min(ans, dp[N][i]);
    cout << ans << endl;
  }
}