#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>

typedef std::array<std::array<char, 5>, 5> State;
struct Coordinate {
	int x, y;
	Coordinate operator+(const Coordinate a) const {
		return { x + a.x, y + a.y };
	}
};

constexpr std::array<Coordinate, 4> neighbor = { Coordinate{1, 0}, Coordinate{-1,0}, Coordinate{0,1}, Coordinate{0,-1} };
void drop(State& state) {
	for (auto j = 0; j < 5; ++j) {
		int count = 0;
		for (auto i = 4; i >= 0; --i) {
			if (state[i][j] == -1) ++count;
			else if (count != 0) {
				state[i + count][j] = state[i][j];
				state[i][j] = -1;
			}
		}
	}
}
std::array<char, 5> erase_block(State & state) {
	std::stack<Coordinate> horizontal, vertical;
	for (auto i = 0; i < 5; ++i) {
		for (auto j = 0; j < 3; ++j) {
			if (state[i][j] != -1) {
				auto is_seq{ true };
				for (auto c = 1; c < 3; ++c) {
					if (state[i][j] != state[i][j + c]) is_seq = false;
				}
				if (is_seq) horizontal.push({ j, i });
			}
			if (state[j][i] != -1) {
				auto is_seq{ true };
				for (auto c = 1; c < 3; ++c) {
					if (state[j][i] != state[j + c][i]) is_seq = false;
				}
				if (is_seq) vertical.push({ i, j });
			}
		}
	}
	std::array<char, 5> result{ 0,0,0,0,0 };
	while (!horizontal.empty()) {
		auto top = horizontal.top(); horizontal.pop();
		for (auto c = 0; c < 3; ++c) {
			if (state[top.y][top.x + c] != -1) result[state[top.y][top.x + c] - 1]++;
			state[top.y][top.x + c] = -1;
		}
	}
	while (!vertical.empty()) {
		auto top = vertical.top(); vertical.pop();
		for (auto c = 0; c < 3; ++c) {
			if (state[top.y + c][top.x] != -1) result[state[top.y + c][top.x] - 1]++;
			state[top.y + c][top.x] = -1;
		}
	}
	return result;
}
bool is_valid(Coordinate position, int order, const int length) {
	auto next = position;
	for (auto i = 0; i < length; ++i) {
		next = next + neighbor[order % 4];
		if (next.y < 0 || next.y >= 5 || next.x < 0 || next.x >= 5) return false;
		order /= 4;
	}
	return true;
}
State swap(const State& state, Coordinate position, int order, const int length) {
	auto copy = state;
	for (auto i = 0; i < length; ++i) {
		auto next = position + neighbor[order % 4];
		auto temp = copy[position.y][position.x];
		copy[position.y][position.x] = copy[next.y][next.x];
		copy[next.y][next.x] = temp;
		position = next;
		order /= 4;
	}
	return copy;
}

int main() {
	while (true) {
		int n; std::cin >> n; if (n == -1) break;
		State state; for (auto& line : state) for (auto& a : line) {
			std::cin >> a;
			a -= '0';
		}
		std::array<int, 5> score; for (auto& s : score) std::cin >> s;
		int max_score = 0;
		for (auto length = 0; length <= n; ++length) {
			for (auto order = 0; order < (1 << (length * 2)); ++order) {
				for (auto y = 0; y < 5; ++y) for (auto x = 0; x < 5; ++x) if (is_valid({ x, y }, order, length)) {
					auto s = swap(state, { x, y }, order, length);
					int sum_score{ 0 };
					int bonus{ 1 };
					while (true) {
						auto erased = erase_block(s);
						if (std::count(erased.begin(), erased.end(), 0) == 5) break;
						for (auto i = 0; i < 5; ++i) {
							sum_score += score[i] * erased[i] * bonus;
						}
						++bonus;
						drop(s);
					}
					max_score = std::max(max_score, sum_score);
				}
			}
		}
		std::cout << max_score << std::endl;

	}
}


Measured memory_bytes: 3120