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

int main(){
  while(true) {
    int N, M, C[16];
    cin >> N >> M;
    if(N == 0 && M == 0) break;
    REP(i, M) {
      cin >> C[i];
    }
    int sum[256], nsum[256];
    REP(i, 256) {
      sum[i] = i == 128 ? 0 : (1 << 30);
    }
    REP(i, N) { 
      int ty;
      cin >> ty;
      REP(j, 256) {
        nsum[j] = 9999999999;
      }
      REP(y, 256) {
        REP(k, M) {
          int ny = y + C[k];
          if(ny < 0) ny = 0;
          if(ny > 255) ny = 255;
          int d = ny - ty;
          int tsum = sum[y] + d * d;
          if(tsum < nsum[ny]) nsum[ny] = tsum;
        }
      }
      REP(j, 256) {
        sum[j] = nsum[j];
      }
    }
    int minsum = 999999999;
    REP(i, 256) {
      if(minsum > sum[i]) minsum = sum[i];
    }
    cout << minsum << endl;
  }
}
