#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)

int dp[20001][256];
const int inf = 1e9;

int main(){
    int n, m;
    while (cin >> n >> m && n){
        vector<int> c(m);
        rep(i,m) cin >> c[i];
        rep(i,n+1) rep(j,256) dp[i][j] = inf;
        dp[0][128] = 0;
        rep(i,n){
            int x; cin >> x;
            rep(j,256){
                rep(k,m){
                    int y = j + c[k];
                    if(y > 255) y = 255;
                    if(y < 0) y = 0;
                    int d = x - y;
                    dp[i+1][y] = min(dp[i+1][y], dp[i][j]+d*d);
                }
            }
        }
        cout << *min_element(dp[n], dp[n+1]) << endl;
    }
}