import std.stdio, std.conv, std.string, std.format, std.algorithm;

void main()
{
  long[][] dp = new long[][]( 20001, 256 );
  immutable long INF = 1UL << 62;
  while ( true ) {
	string input = readln;
	int N, M;
	formattedRead( input, "%d %d\n", &N, &M );
	if ( N == 0 && M == 0 ) break;

	int[] cs = new int[]( M );
	foreach ( i; 0 .. M ) {
	  cs[ i ] = to!int( strip( readln ) );
	}
	int[] xs = new int[]( N );
	foreach ( i; 0 .. N ) {
	  xs[ i ] = to!int( strip( readln ) );
	}
	
	foreach ( ary; dp[ 0 .. N + 1 ] ) ary[] = INF;
	dp[ 0 ][ 128 ] = 0;
	foreach ( i; 0 .. N ) {
	  foreach ( j; 0 .. 256 ) {
		foreach ( c; cs ) {
		  immutable int next = min( 255, max( 0, j + c ) );
		  dp[ i + 1 ][ next ] = min( dp[ i + 1 ][ next ], dp[ i ][ j ] + ( xs[ i ] - next ) ^^ 2 );
		}
	  }
	}
	writeln( reduce!min( dp[ N ] ) );
  }  
}