#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); ++i)
#define all(c) (c).begin(), (c).end()
const int inf = 1 << 28;

template<typename T> inline void chmin(T& t, T f){if(t > f)t = f;}
template<typename T> inline T sqr(T x){return x * x;}

int N, M;

int solve(vector<int>& Cs, vector<int>& xs){
    vector<int> dp(256, inf); dp[128] = 0;
    rep(i, N){
        vector<int> nxt(256, inf);
        rep(y, 256)if(dp[y] != inf){
            rep(j, M){
                int ny = max(0, min(y + Cs[j], 255));
                chmin(nxt[ny], dp[y] + sqr(xs[i] - ny));
            }
        }
        dp.swap(nxt);
    }
    return *min_element(all(dp));
}

int main(){
    while(cin >> N >> M, N|M){
        vector<int> Cs(M), xs(N);
        rep(i, M)cin >> Cs[i];
        rep(i, N)cin >> xs[i];
        cout << solve(Cs, xs) << '\n';
    }
    return 0;
}