#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1){
    int n,m;
    cin>>n>>m;
    if(!n)break;
    vector<int> codes(m);
    REP(i,m)cin>>codes[i];
    vector<int> data(n);
    REP(i,n)cin>>data[i];
    vector<int64_t> dp(256,INT64_C(1000000000000000));
    dp[128] = 0;
    REP(i,n){
      vector<int64_t> tmp(256,INT64_C(1000000000000000));
      REP(j,256){
        REP(k,m){
          int newv = min(255, max(0, j+codes[k]));
          int diff = data[i] - newv;
          int sqd = diff*diff;
          tmp[newv] = min(tmp[newv], dp[j] + sqd);
        }
      }
      dp.swap(tmp);
    }
    cout << *min_element(begin(dp),end(dp)) << endl;
  }
  return 0;
}