/*
TASK: EXPRES
LANG: C++
*/

#define NDEBUG

#include <stdio.h>
#include <string.h>
#include <assert.h>

//const int DEBUG_MODE = 1;
const int DEBUG_MODE = 0;

const int MAX_CHANGES = 40 + 1;
const int MAX_GLYPH_COUNT = 40 + 40 - 1 + 1;
const int MAX_LINE_LENGTH = MAX_GLYPH_COUNT*3 + 1;
const int MAX_ONES = 600;
const int INF = 1000000;
const int CODE_PLUS = 10;
const int CODE_MINUS = 11;

// font[glyph][line][column]
const char font[12][5][4] = {
	{
		"111",
		"101",
		"101",
		"101",
		"111",
	},
	{
		"001",
		"001",
		"001",
		"001",
		"001",
	},
	{
		"111",
		"001",
		"111",
		"100",
		"111",
	},
	{
		"111",
		"001",
		"111",
		"001",
		"111",
	},
	{
		"101",
		"101",
		"111",
		"001",
		"001",
	},
	{
		"111",
		"100",
		"111",
		"001",
		"111",
	},
	{
		"111",
		"100",
		"111",
		"101",
		"111",
	},
	{
		"111",
		"001",
		"001",
		"001",
		"001",
	},
	{
		"111",
		"101",
		"111",
		"101",
		"111",
	},
	{
		"111",
		"101",
		"111",
		"001",
		"111",
	},
	{
		"000",
		"010",
		"111",
		"010",
		"000",
	},
	{
		"000",
		"000",
		"111",
		"000",
		"000",
	}	
};

// font[glyph][line][column]
const int count_o_ones[12] = {12,5,11,11,9,11,12,7,13,12,5,3};

// flip_count[source][dest]
const int flip_count[12][12] = 
{{0,7,3,3,5,3,2,5,1,2,13,11},
{7,0,8,6,4,8,9,2,8,7,8,6},
{3,8,0,2,6,4,3,6,2,3,10,8},
{3,6,2,0,4,2,3,4,2,1,10,8},
{5,4,6,4,0,4,5,4,4,3,8,6},
{3,8,4,2,4,0,1,6,2,1,10,8},
{2,9,3,3,5,1,0,7,1,2,11,9},
{5,2,6,4,4,6,7,0,6,5,10,8},
{1,8,2,2,4,2,1,6,0,1,12,10},
{2,7,3,1,3,1,2,5,1,0,11,9},
{13,8,10,10,8,10,11,10,12,11,0,2},
{11,6,8,8,6,8,9,8,10,9,2,0}
};

struct Move {
	int new_digit;
	int new_sign;

	Move()
	{
		set(-1,-1);
	}
	Move(int new_digit,int new_sign)
	{
		set(new_digit,new_sign);
	}
	void set(int new_digit,int new_sign)
	{
		this->new_digit = new_digit;
		this->new_sign = new_sign;
	}
};

//input[line][char]
char input[5][MAX_LINE_LENGTH]; 

int line_length;
int digit_count;
int glyph_count;
int max_changes;
int input_ones;
char expression[MAX_GLYPH_COUNT];
char result_expression[MAX_GLYPH_COUNT];
int result_expr_cur_pos = 0;

// max_sum[a][b][c] = the largest achievable sum, 
// using the first a glyphs of expr 
// achievable with at most b flips
// having a total of exactly c ones
int max_sum[MAX_GLYPH_COUNT][MAX_CHANGES][MAX_ONES]; 

Move best_move[MAX_GLYPH_COUNT][MAX_CHANGES][MAX_ONES]; 
bool solved[MAX_GLYPH_COUNT][MAX_CHANGES][MAX_ONES]; 

/*
void compute_flip_count_matrix()
{
	for (int source=0;source<12;source++) {
		for (int dest=0;dest<12;dest++)
			for (int y=0;y<5;y++)
				for (int x=0;x<3;x++)
					if (font[source][y][x] != font[dest][y][x])
						flip_count[source][dest]++;		
	}
}
*/

void print_flip_count_matrix()
{
	FILE *fout;
	//fout = fopen("flip_count_matrix.txt","w");
	fout = stdout;

	fprintf(fout,"{");
	for (int source=0;source<12;source++) {
		fprintf(fout,"{");
		for (int dest=0;dest<12;dest++) {
			fprintf(fout,"%d",flip_count[source][dest]);
			if (dest != 11)
				fprintf(fout,",");
		}
		fprintf(fout,"}");		
		if (source != 11)
			fprintf(fout,",");
		fprintf(fout,"\n");
	}
	fprintf(fout,"}\n");
}

inline int code(char c)
{
	assert((c >= '0' && c<='9') || ( c == '-' ) || ( c == '+'));
	if (c == '-')
		return 11;
	if (c == '+')
		return 10;
	return c - '0';
}

void count_input_ones()
{
	input_ones = 0;
	for (int glyph=0;glyph<glyph_count;glyph++)
		input_ones += count_o_ones[code(expression[glyph])];
}

void print_expression()
{
	for (int i=0;i<glyph_count;i++)
		printf("%c ",expression[i]);
	printf("\n");
}

void count_font_ones()
{
	for (int glyph=0;glyph<12;glyph++) {
		int count = 0;
		for (int y=0;y<5;y++)
			for (int x=0;x<3;x++)
				if (font[glyph][y][x] == '1')
					count++;
		printf("%d ",count);
	}
	printf("\n");
}

void parse_input()
{
	for (int glyph=0;glyph < glyph_count;glyph++) {
		int match;

		for (int font_glyph=0;font_glyph<12;font_glyph++) {
			match = font_glyph;
			for (int y=0;y<5;y++)
				for (int x=0;x<3;x++) 
					if (input[y][3*glyph + x] != font[font_glyph][y][x]) {
						match = -1;					
						goto bad_match;
					}
bad_match:				;
			if (match != -1)
				break;
		}
		if (match <= 9)
			expression[glyph] = '0' + match;
		else if (match == 10)
			expression[glyph] = '+';
		else
			expression[glyph] = '-';
	}
}

void print_input()
{
	for (int y=0;y<5;y++) {
		for (int x=0;x<line_length;x++)
			printf("%c",input[y][x]);
		printf("\n");
	}
}

void load()
{
	FILE *fin;

	if (DEBUG_MODE) {
		fin = fopen("express.in","r");
	} else {
		fin = stdin;
	}

	fscanf(fin,"%d%d",&digit_count,&max_changes);
	glyph_count = digit_count + digit_count -1;
	line_length = glyph_count * 3;	
	for (int y=0;y<5;y++) {
		// Yank the \n
		char dummy;
		fscanf(fin,"%c",&dummy);

		for (int x=0;x<line_length;x++)
			fscanf(fin,"%c",&(input[y][x]));		
	}
	
	fclose(fin);

	memset(solved,0,sizeof(solved));

	//print_input();
	//count_font_ones();
	
	parse_input();	

	//print_expression();	

	count_input_ones();

	//memset(flip_count,0,sizeof(flip_count));
	//compute_flip_count_matrix();
	//print_flip_count_matrix();
}

//inline int max(int i1,int i2)
//{
//	return i1 > i2 ? i1 : i2;
//}

inline int sign_code(int pos)
{
	if (pos == -1)
		return 1;

	int sign_code = code(expression[pos]);
	if (sign_code == CODE_PLUS)
		return 1;
	else if (sign_code == CODE_MINUS)
		return -1;
	else assert(false);
}

int solve(int first,int changes,int ones)
{
	if (changes < 0)
		return -INF;		
	if (first < 0) {
		if ( ones == 0)
			return 0;
		else 
			return -INF;
	}	
	if (ones <= 0)
		return -INF;
	if (solved[first][changes][ones])
		return max_sum[first][changes][ones];

	solved[first][changes][ones] = true;		

	//printf("%d %d %d\n",first,changes,ones);
/*
	BAD CODE!!!!!

	Remnant of an old idea. 

	if (changes == 0) {
		best_move[first][changes][ones].set(code(expression[first]),
							    sign_code(first-1));

		
		int cur_sum = 0;
		int cur_ones = 0;

		while (first >= 0) {
			best_move[first][changes][ones].set(code(expression[first]),
							    sign_code(first-1));

 			cur_sum += code(expression[first]) * sign_code(first-1);
			cur_ones += count_o_ones[expression[first]];

			first -= 2;
		}

		if (cur_ones != ones)
			max_sum[first][changes][ones]=-INF;
		else
			max_sum[first][changes][ones]=cur_sum;

		return max_sum[first][changes][ones];
		
	}
*/
		
	int best_result = -INF;
	int cur_digit = code(expression[first]);
	int cur_digit_ones = count_o_ones[cur_digit];

	for (int new_digit=0;new_digit<10;new_digit++) {
		int new_digit_ones = count_o_ones[new_digit];
		int new_digit_flips = flip_count[cur_digit][new_digit];		
		int sign_ones;
		Move cur_move = Move(new_digit,sign_code(first-1));

		//if (new_digit == 7 && first == 4 && changes == 4) {
		//	printf("Ai!\n");
		//}
		if (new_digit_flips > changes)
			continue;

		int recursion_result;

		if (first == 0)
			sign_ones = 0;
		else 
			sign_ones = count_o_ones[code(expression[first-1])];
		

		recursion_result = solve(first - 2,
					 changes - new_digit_flips,
					 ones - new_digit_ones - sign_ones);
		
		int cur_result = recursion_result + new_digit * sign_code(first - 1);

		//best_result = max(best_result,cur_result);
		if (best_result < cur_result) {
			best_result = cur_result;
			best_move[first][changes][ones] = cur_move;
		}

		if (first != 0) {
			//try flipping the sign
			int cur_sign = code(expression[first - 1]);
			
			int new_sign_ones;
			int new_sign_flips;
			
			{ // Use a +		
				cur_move = Move(new_digit,1);
				new_sign_ones = count_o_ones[CODE_PLUS];
				new_sign_flips = flip_count[cur_sign][CODE_PLUS];

				if (new_digit_flips + new_sign_flips <= changes) {
					recursion_result = solve(first - 2,
								 changes - new_digit_flips - new_sign_flips,
								 ones - new_digit_ones - new_sign_ones);

					cur_result = recursion_result + new_digit;
					if (best_result < cur_result) {
						best_result = cur_result;
						best_move[first][changes][ones] = cur_move;
					}					
				}
			}
			{ // Use a -			
				cur_move = Move(new_digit,-1);
				new_sign_ones = count_o_ones[CODE_MINUS];
				new_sign_flips = flip_count[cur_sign][CODE_MINUS];

				if (new_digit_flips + new_sign_flips <= changes) {
					recursion_result = solve(first - 2,
								 changes - new_digit_flips - new_sign_flips,
								 ones - new_digit_ones - new_sign_ones);

					cur_result = recursion_result - new_digit;
					if (best_result < cur_result) {
						best_result = cur_result;
						best_move[first][changes][ones] = cur_move;
					}										
				}
			}
		}
	}

	max_sum[first][changes][ones] = best_result;	
	return best_result;		
}

void solve()
{
	solve(glyph_count-1,max_changes,input_ones);	
}

int sign_glyph_code(int sign)
{
	if (sign == 1)
		return CODE_PLUS;
	else if (sign == -1)
		return CODE_MINUS;
	else
		assert(false);
}

//compute solution in human readable form
int compute_sol_h(int first,int changes,int ones)
{
	if (first == 0) {
		result_expression[result_expr_cur_pos++] = '0' + best_move[first][changes][ones].new_digit;	
		
		return 0;
	}

	Move move = best_move[first][changes][ones];
	int cur_digit = code(expression[first]);
	int flips = flip_count[cur_digit][move.new_digit];
	int cur_sign = code(expression[first-1]);
	int new_sign = sign_glyph_code(move.new_sign);
	flips += flip_count[cur_sign][new_sign];

	int new_ones = count_o_ones[move.new_digit] + count_o_ones[new_sign];

	compute_sol_h(first-2,changes - flips,ones - new_ones);

	if (move.new_sign == 1) 
		result_expression[result_expr_cur_pos++]='+';		
	else if (move.new_sign == -1)
		result_expression[result_expr_cur_pos++]='-';		
	else 
		assert(false);
	result_expression[result_expr_cur_pos++] = '0' + move.new_digit;	
}


void print_sol_h()
{
	for (int i=0;i<result_expr_cur_pos;i++)
		printf("%c",result_expression[i]);
	printf("\n");	
}	

void print_sol_glyphs()
{
	for (int y=0;y<5;y++) {
		for (int glyph=0;glyph < glyph_count;glyph++) {
			for (int x=0;x<3;x++)
				printf("%c",font[code(result_expression[glyph])][y][x]);
		}
		printf("\n");
	}
}

void save()
{
	printf("%d\n",solve(glyph_count-1,max_changes,input_ones));
	compute_sol_h(glyph_count-1,max_changes,input_ones);

	//print_sol_h();		

	print_sol_glyphs();
}

int main()
{
	load();
	solve();
	save();

	return 0;
}