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

#define INF 1 << 28
#define SQUARE(n) ((n)*(n))
typedef long INT;
typedef vector <int> VI;
typedef vector <VI> VVI;

int main( void )
{
	int n, m; // N: üÍM, M: R[hubNÌ·³
	while ( cin >> n >> m && n && m )
	{
		// üÍ
		VI C(m);
		for ( int i = 0; i < m; i++ )
		{
			cin >> C[i];
		}
		VI x(n);
		for ( int i = 0; i < n; i++ )
		{
			cin >> x[i];
		}

		VVI  dp = VVI( n+1, VI( 256, INF ) );
		dp[0][128] = 0;

		for ( int k = 1; k <= n; k++ )
		{
			for ( int i = 0; i < 256; i++ )
			{
				for ( int j = 0; j < m; j++ )
				{
					int next = i + C[j];
					next = next < 0 ? 0 : next;
					next = next > 255 ? 255 : next;
					dp[k][next] = min( dp[k][next], dp[k-1][i] + SQUARE( next - x[k-1] ) );
				}
			}
		}

		int answer = INF;
		for ( int i = 0; i < 256; i++ ) answer = min( answer, dp[n][i] );
		cout << answer << endl;
	}
	return 0;
}