#include <bits/stdc++.h>
 
#define loop(n, i) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()

using namespace std;
typedef long long ll;
int dp[20010][260];

int main()
{
    while (1) {
        int n, m; cin >> n >> m;
        if (!n) break;
        vector<int> C(m), x(n+1, 128);
        loop (m, i) cin >> C[i];
        loop (n, i) cin >> x[i+1];
        fill(dp[0], dp[20009]+260, 1<<29);
        loop (256, i) dp[n+1][i] = 0;

        for (int i = n; i >= 0; i--) {
            for (int j = 0; j < 256; j++) {
                int d = (x[i] - j) * (x[i] - j);
                for (int c : C) {
                    int k = j + c;
                    k = max(k, 0);
                    k = min(k, 255);
                    dp[i][j] = min(dp[i][j], dp[i+1][k] + d);
                }
            }
        }
        cout << dp[0][128] << endl;
    }
    return 0;
}