#include<iostream>
#include<vector>
#include<climits>
#include<algorithm>
using namespace std;

int main()
{
    int n, m;
    while(cin>>n>>m, n|m) {
        vector<long long int> c(m), x(n);
        for(int i=0; i<m; i++) cin>>c[i];
        for(int i=0; i<n; i++) cin>>x[i];
        vector< vector<long long int> > dp(n+1, vector<long long int>(255+1, LONG_LONG_MAX/2));
        dp[0][128] = 0;
        for(int i=0; i<n; i++) {
            for(int j=0; j<=255; j++) {
                for(int k=0; k<m; k++) {
                    int l = j+c[k];
                    if(l < 0) l = 0;
                    if(255 < l) l = 255;
                    dp[i+1][l] = min(dp[i+1][l], dp[i][j]+(x[i]-l)*(x[i]-l));
                }
            }
        }
        cout<<*(min_element(dp[n].begin(), dp[n].end()))<<endl;
    }
}