#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 5;
const int G = 5;
const int GG = G * G;
const int SEQ = 3;

const int dxs[] = {1, 0, -1, 0};
const int dys[] = {0, 1, 0, -1};

/* typedef */

typedef set<int> si;

/* global variables */

int n;
int scs[G];
int maxsum = 0;

/* classes */

struct Game {
  int gs[GG];
  
  Game() {}

  int move(int pos, int d) {
    int x = (pos % G) + dxs[d], y = (pos / G) + dys[d];
    int pos1 = -1;
    if (x >= 0 && x < G && y >= 0 && y < G) {
      pos1 = y * G + x;
      gswap(pos, pos1);
    }
    //printf("gmove(%d,%d)->%d\n", pos, d, pos1);
    return pos1;
  }

  void gswap(int pos0, int pos1) {
    //printf("gswap(%d,%d)\n", pos0, pos1);
    int tmp = gs[pos0]; gs[pos0] = gs[pos1]; gs[pos1] = tmp;
  }

  int calc() {
    //puts("calc() in");
    int sum = 0;
    static int cgs[GG];

    copy(gs, gs + GG, cgs);

    for (int bn = 1;; bn++) {
      si vset;
      for (int pos0 = 0; pos0 < GG; pos0 += G)
	for (int d = 0; d < G;) {
	  int g0 = cgs[pos0 + d];
	  if (g0 < 0) {
	    d++;
	    continue;
	  }
	  int d1;
	  for (d1 = d + 1; d1 < G && cgs[pos0 + d1] == g0; d1++);
	  if (d1 - d >= SEQ)
	    for (; d < d1; d++) vset.insert(pos0 + d);
	  else d = d1;
	}
      for (int pos0 = 0; pos0 < G; pos0++)
	for (int d = 0; d < GG;) {
	  int g0 = cgs[pos0 + d];
	  if (g0 < 0) {
	    d += G;
	    continue;
	  }
	  int d1;
	  for (d1 = d + G; d1 < GG && cgs[pos0 + d1] == g0; d1 += G);
	  if (d1 - d >= SEQ * G)
	    for (; d < d1; d += G) vset.insert(pos0 + d);
	  else d = d1;
	}

      if (vset.size() == 0) break;
      
      for (si::iterator sit = vset.begin(); sit != vset.end(); sit++) {
	//printf("%d ", *sit);
	sum += scs[cgs[*sit]] * bn, cgs[*sit] = -1;
      }
      //putchar('\n');
      
      for (int pos0 = G * (G - 1); pos0 < GG; pos0++) {
	int pos1, pos2;
	for (pos1 = pos2 = pos0; pos2 >= 0; pos2 -= G)
	  if (cgs[pos2] >= 0) {
	    cgs[pos1] = cgs[pos2];
	    pos1 -= G;
	  }
	for (; pos1 >= 0; pos1 -= G) cgs[pos1] = -1;
      }

      //print(cgs);
    }
    
    //puts("calc() out");
    return sum;
  }

  void print(const int *gs0) {
    for (int pos = 0; pos < GG; pos++) {
      printf("%2d", gs0[pos]);
      if ((pos + 1) % G == 0) putchar('\n');
    }
    putchar('\n');
  }
};

/* subroutines */

void rec(int k, int prv, int pos, Game g) {
  //printf("k=%d,prv=%d,pos=%d\n", k, prv, pos);
  //g.print(g.gs);
  
  for (int d = 0; d < 4; d++) {
    //printf("  k=%d,d=%d\n", k,d);
    int pos0 = g.move(pos, d);
    if (pos0 >= 0) {
      //printf("  pos0=%d\n", pos0);
      if (pos0 != prv) {
	int sum = g.calc();
	if (maxsum < sum) maxsum = sum;
	
	if (k < n) rec(k + 1, pos, pos0, g);
      }

      //printf("  k=%d,prv=%d,pos=%d\n", k, prv, pos);
      //printf("  2:gswap(%d,%d)\n", pos, pos0);
      g.gswap(pos, pos0);
    }
  }
}

int check(Game g) {
  maxsum = 0;

  int sum = g.calc();
  if (maxsum < sum) maxsum = sum;

  if (n > 0)
    for (int pos = 0; pos < GG; pos++) rec(1, -1, pos, g);

  return maxsum;
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n < 0) break;

    Game g;

    for (int pos = 0; pos < GG; pos++) cin >> g.gs[pos], g.gs[pos]--;
    for (int i = 0; i < G; i++) cin >> scs[i];
    //for (int i = 0; i < G; i++) printf("%d ", scs[i]); putchar('\n');
    
    cout << check(g) << endl;
  }
  
  return 0;
}

Measured memory_bytes: 1196