#include <iostream>
#include <vector>
 
using namespace std;
 
int main() {
 int n,m;
 while(cin >> n >> m, n) {
  vector<int> code(m), value(n);
  for (int i = 0; i < m; i++)
   cin >> code[i];
  for (int i = 0; i < n; i++)
   cin >> value[i];
  vector<vector<int> > dp(256, vector<int>(n + 1, 1000000000));
  dp[0][128] = 0;
  for (int i = 0; i < n; i++) {
   for (int j = 0; j < 256; j++) {
    for (int k = 0; k < m; k++) {
     int val = min(255,max(0,j+code[k]));
     dp[i+1][val] = min(dp[i+1][val], dp[i][j] + (val -value[i]) * (val - value[i]));
    }
   }
  }
  int ans = 1000000000;
  for (int i = 0; i < 256; i++) {
   ans = min(ans, dp[n][val]);
  }
  cout << ans << endl;
 }
 return 0;
}