#include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdarg>
#include <set>
#include <limits>
#include <iomanip>

using namespace std;
#define INF 2e9 + 9
#define MOD 1000000007
#define ALL(v) v.begin() , v.end()
#define Int int64_t
#define pll pair<Int,Int>
template<class T> using pql = priority_queue<T, vector<T>>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define ll(n) Int n; cin >> n;
#define LL(...) Int __VA_ARGS___; cin >> __VA_ARGS___;
#define OUT(...) cout << __VA_ARGS___;
#define RP(n, i, s) for(Int i = s ;i < n ;i++)
#define REP(n, i) for (Int i = 0; i < n; i++)
#define REPe(n, i) for (Int i = 1; i <= n; i++)
#define RPe(n, i, s) for (Int i = s; i <= n; i++)
#define upd_max(maxer, updater) if (maxer < updater) maxer = updater;

void ansYes(bool flag) {
    if (flag) cout << "Yes" << endl; else cout << "No" << endl;
}

void ansYES(bool flag) {
    if (flag) cout << "YES" << endl; else cout << "NO" << endl;
}

struct node {
    Int to;
    Int length;
};


vector<Int> divisor(Int n) {
    vector<Int> ret;
    for (Int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    return (ret);
}

/*
vector<vector<int>> to;
vector<int> flag;
vector<int> c;


Int dfs(int from, int mae) {
    flag[from] = 1;
    Int mini = 0;
    for (auto next : to[from]) {
        if (next == mae) continue;
        if (flag[next] == 1) {
            return -1;
        } else {
            mini = min(mini, dfs(next, from));
        }
    }
    return mini;
}*/

int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};

struct datas {
    Int max;
    Int size;
};

class ContainChecker {
protected:
    Int minx, miny, maxx, maxy;
public:
    ContainChecker(Int minx, Int miny, Int maxx, Int maxy) : minx(minx), miny(miny), maxx(maxx), maxy(maxy) {};

    bool in(Int x, Int y) {
        return (minx <= x && miny <= y && x <= maxx && y <= maxy);
    }
};


int main(void) {
    while(1) {
        Int n, m;
        cin >> n >> m;
        if (n == 0) break;
        vector<Int> code(m);
        vector<Int> in(n);
        vector<vector<Int>> dp(20001,vector<Int>(520,1e15));
        REP(m, i) {
            cin >> code.at(i);
        }

        REP(n, i) {
            cin >> in.at(i);
        }
        dp[0][128+255] = 0;
        REP(n, i) {
            for (Int k = -255; k <= 255; k++) {
                Int indexedk = k + 255;
                if (dp[i][indexedk] != 1e15) {
                    REP(m, p) {
                        Int nextm = min(max(k + code.at(p),(Int)0),(Int)255);
                        Int indexedm = nextm+255;
                        Int nextwa = dp[i][indexedk] + pow((nextm - in.at(i)),2);
                        if (dp[i+1][indexedm] > dp[i][indexedk] + (nextm - in.at(i)) * (nextm - in.at(i))) {
                            dp[i+1][indexedm] = dp[i][indexedk] + (nextm - in.at(i)) * (nextm - in.at(i));
                        }
                    }
                }
            }
        }
        Int maxer = 1e15;
        for (Int i = 0; i <= 510; i++){
            if (dp[n][i] != 1e15) maxer = min(maxer,dp[n][i]);
        }
/*        for(Int k = 0; k <= 510; k++){

            cout << k -255 << " : ";
            REP(n + 1, i){
                cout << dp[i][k] << " ";
            }
            cout << endl;
        }*/

        cout << maxer << endl;
    }

}





Measured memory_bytes: 85336