#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	int table[5][5],rest;
};

int N,ans;
int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0},score[6];

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= 4 && col >= 0 && col <= 4)return true;
	else{
		return false;
	}
}

int calc(int table[5][5]){

	int ret = 0;

	int work[5][5],delete_num[6];
	bool delete_table[5][5];

	for(int row = 0; row < 5; row++){
		for(int  col = 0; col < 5; col++)work[row][col] = table[row][col];
	}

	int bonus = 1,delete_sum,tmp,count,tmp_row,tmp_col;

	while(true){

		delete_sum = 0;
		for(int i = 1; i <= 5; i++)delete_num[i] = 0;

		for(int row = 0; row < 5; row++){
			for(int col = 0; col < 5; col++)delete_table[row][col] = false;
		}

		for(int row = 0; row <= 4; row++){
			for(int col = 0; col <= 2; col++){
				if(delete_table[row][col] == true || work[row][col] == 0)continue;

				tmp_col = col;
				for(count = 0; tmp_col <= 4 && work[row][col] == work[row][tmp_col]; count++,tmp_col++);
				if(count >= 3){
					for(int i = 0; i < count; i++){
						delete_table[row][col+i] = true;
					}
				}
			}
		}

		for(int col = 0; col <= 4; col++){
			for(int row = 0; row <= 2; row++){
				if(delete_table[row][col] == true || work[row][col] == 0)continue;

				tmp_row = row;
				for(count = 0; tmp_row <= 4 && work[row][col] == work[tmp_row][col]; count++,tmp_row++);
				if(count >= 3){
					for(int i = 0; i < count; i++){
						delete_table[row+i][col] = true;
					}
				}
			}
		}

		for(int row = 0; row < 5; row++){
			for(int col = 0; col < 5; col++){
				if(delete_table[row][col]){
					delete_num[work[row][col]]++;
					delete_sum++;
					work[row][col] = 0;
				}
			}
		}

		for(int col = 0; col < 5; col++){
			for(int row = 4; row >= 1; row--){
				if(work[row][col] == 0){
					for(tmp = row-1; tmp >= 0; tmp--){
						if(work[tmp][col] != 0)break;
					}
					if(tmp == -1)break;

					work[row][col] = work[tmp][col];
					work[tmp][col] = 0;
				}
			}
		}

		if(delete_sum == 0)break;

		for(int i = 1; i <= 5; i++){
			ret += delete_num[i]*score[i]*bonus;
		}

		bonus++;
	}

	return ret;
}


void recursive(Info info,int row,int col){

	ans = max(ans,calc(info.table));

	if(info.rest == 0)return;

	int adj_row,adj_col;
	for(int i = 0; i < 4; i++){
		adj_row = row+diff_row[i];
		adj_col = col+diff_col[i];

		if(!rangeCheck(adj_row,adj_col))continue;

		Info new_info;
		for(int row = 0; row < 5; row++){
			for(int col = 0; col < 5; col++)new_info.table[row][col] = info.table[row][col];
		}
		swap(new_info.table[adj_row][adj_col],new_info.table[row][col]);
		new_info.rest = info.rest-1;
		recursive(new_info,adj_row,adj_col);
	}
}


void func(){

	Info first;
	for(int row = 0; row < 5; row++){
		for(int col = 0; col < 5; col++)scanf("%d",&first.table[row][col]);
	}

	for(int i = 1; i <= 5; i++)scanf("%d",&score[i]);

	first.rest = N;

	ans = 0;

	for(int row = 0; row < 5; row++){
		for(int col = 0; col < 5; col++)recursive(first,row,col);
	}

	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == -1)break;

		func();
	}

	return 0;
}

Measured memory_bytes: 3192