#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
#define miz(x,y) x=min(x,y)
typedef long long ll;
ll inf=1e16;
ll dp[20001][256];
int main(){
	while(true){
		int N,M,c[16],x[20000];
		cin>>N>>M;
		if(N==0) break;
		rep(i,M) cin>>c[i];
		rep(i,N) cin>>x[i];
		rep(i,N+1) rep(j,256) dp[i][j]=inf;
		dp[0][128]=0;
		rep(i,N){
			rep(j,256) if(dp[i][j]!=inf){
				rep(k,M){
					int s=min(255,max(0,j+c[k]));
					miz(dp[i+1][s],dp[i][j]+(s-x[i])*(s-x[i]));
				}
			}
		}
		ll ans=inf;
		rep(j,256) miz(ans,dp[N][j]);
		cout<<ans<<endl;
	}
}