#include <iostream>
using namespace std;

#define int long long

int dp[20001][256];
int C[17];

signed main() {
    int n, m;
    while(cin>>n>>m, n) {
        for(int i=0; i<20001; ++i)
          for(int j=0; j<256; ++j) dp[i][j] = 1e+9;
        dp[0][128] = 0;
        for(int i=0; i<16; ++i) C[i] = 0;
        for(int i=0; i<m; ++i) cin >> C[i];
        for(int i=1; i<=n; ++i) {
            int x;
            cin >> x;
            for(int j=0; j<256; ++j) {
                for(int k=0; k<m; ++k) {
                    int x2 = j + C[k];
                    if(255<=x2) x2=255;
                    if(x2<=0) x2=0;
                    int diff = (x2-x) * (x2-x);
                    dp[i][x2] = min(dp[i][x2], dp[i-1][j]+diff);
                }
            }
        }

        int ans = 1e+9;
        for(int i=0; i<256; ++i) ans = min(ans, dp[n][i]);
        cout << ans << endl;
    }
}