#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int INF = 1<<30;

int n, m, data[16], dp[2][256], in;

int main(){
  while(1){
    scanf("%d%d", &n, &m);
    if(!n && !m) break;
    for(int i=0;i<m;i++) scanf("%d", &data[i]);
    for(int i=0;i<256;i++) dp[0][i] = INF;
    dp[0][128] = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<256;j++) dp[(i+1)%2][j] = INF;
      scanf("%d", &in);
      for(int j=0;j<256;j++){
        if(dp[i%2][j] == INF) continue;
        for(int k=0;k<m;k++){
          int next = j + data[k];
          if(next < 0) next = 0;
          if(next > 255) next = 255;
          dp[(i+1)%2][next] = min(dp[(i+1)%2][next], abs(in-(next)) * abs(in-(next)) + dp[i%2][j]);
        }
      }
    }
    int ans = INF;
    for(int i=0;i<256;i++) ans = min(ans, dp[n%2][i]);
    printf("%d\n", ans);
  }
}