#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 20

int get_int(void) {
  int num;
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return 0;
  sscanf(line, "%d", &num);
#else
#error
#endif
  return num;
}

int get_int2(int *a1, int *a2) {
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return -1;
  sscanf(line, "%d %d", a1, a2);
#else
#error
#endif
  return 0;
}

#define SET_MAX 16
#define VAL_MAX 255
#define SAMPLE_MAX 20000
#define ANS_MAX 10000
#define INIT_SAMPLE 128
#define min(a,b) ((a) > (b) ? (b) : (a))
// 522133279
// This is within INT_MAX condidering the worst case;
// // (N: 20000) * (diff_max: 255)*diff_max = 20000 * 65535 <= 1400000000
#define INF 0x1f1f1f1f
#include <limits.h>

int main(void) {
    int ss, ts;
    int ans[ANS_MAX];
    int ans_idx = 0;
    int i, j;
    static int dp[SAMPLE_MAX+1][VAL_MAX+1];
    while(1) {
        get_int2(&ss, &ts);
        if(ss+ts == 0) break;
        int table[SET_MAX];
        int samples[SAMPLE_MAX+1];
        for(i = 0; i < ts; i++) {
            table[i] = get_int();
        }
        for(i = 1; i <= ss; i++) {
            samples[i] = get_int();
        }

        // initialization
        memset(dp, 0x1f, (SAMPLE_MAX+1)*(VAL_MAX+1)*sizeof(int));
        dp[0][128] = 0;
        int prev;
        for(i = 1; i <= ss; i++) {
            int target = samples[i];
            for(j = 0; j < ts; j++) {
                int code = table[j];
                for(prev = 0; prev <= VAL_MAX; prev++) {
                    int cur = prev+code;
                    if(cur < 0) cur = 0;
                    if(cur > VAL_MAX) cur = VAL_MAX;
                    int diff2 = (target - cur)*(target-cur);
                    dp[i][cur] = min(dp[i-1][prev] + diff2, dp[i][cur]);
                }
            }
        }
        int res = INT_MAX;
        for(i = 0; i <= VAL_MAX; i++) {
            res = min(res, dp[ss][i]);
        }
        ans[ans_idx++] = res;
    }
    for(i = 0; i < ans_idx; i++) {
        printf("%d\n", ans[i]);
    }
    return 0;
}
