You are given a matrix f with 4 rows and n columns. Each element of the matrix is either an asterisk (*) or a dot (.).

You may perform the following operation arbitrary number of times: choose a square submatrix of f with size k × k (where 1 ≤ k ≤ 4) and replace each element of the chosen submatrix with a dot. Choosing a submatrix of size k × k costs ak coins.

What is the minimum number of coins you have to pay to replace all asterisks with dots?

Input

The first line contains one integer n (4 ≤ n ≤ 1000) — the number of columns in f.

The second line contains 4 integers a1, a2, a3, a4 (1 ≤ ai ≤ 1000) — the cost to replace the square submatrix of size 1 × 1, 2 × 2, 3 × 3 or 4 × 4, respectively.

Then four lines follow, each containing n characters and denoting a row of matrix f. Each character is either a dot or an asterisk.

Output

Print one integer — the minimum number of coins to replace all asterisks with dots.

Examples

Input

4
1 10 8 20
***.
***.
***.
...*


Output

9


Input

7
2 1 8 2
.***...
.***..*
.***...
....*..


Output

3


Input

4
10 10 1 10
***.
*..*
*..*
.***


Output

2

Note

In the first example you can spend 8 coins to replace the submatrix 3 × 3 in the top-left corner, and 1 coin to replace the 1 × 1 submatrix in the bottom-right corner.

In the second example the best option is to replace the 4 × 4 submatrix containing columns 2 – 5, and the 2 × 2 submatrix consisting of rows 2 – 3 and columns 6 – 7.

In the third example you can select submatrix 3 × 3 in the top-left corner and then submatrix 3 × 3 consisting of rows 2 – 4 and columns 2 – 4.
#include <bits/stdc++.h>
using namespace std;
int read() {
  char ch = getchar();
  int x = 0, ff = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return ff * x;
}
void write(int z) {
  if (z < 0) {
    putchar('-');
    z = -z;
  }
  if (z >= 10) {
    write(z / 10);
  }
  putchar(z % 10 + '0');
}
int n, inf;
int val[5], jl[1005], shu[16];
int f[1005][16][16][16];
char mp[1005];
int main() {
  memset(f, 0x3f3f3f3f, sizeof(f));
  inf = f[0][0][0][0];
  n = read();
  for (int i = 1; i <= 4; ++i) val[i] = read();
  for (int i = 1; i <= 4; ++i) {
    scanf("%s", mp + 1);
    for (int j = 1; j <= n; ++j)
      if (mp[j] == '*') jl[j] |= (1 << (i - 1));
  }
  f[0][0][0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int a = 0; a < (i <= 3 ? 1 : 16); ++a)
      for (int b = 0; b < (i <= 2 ? 1 : 16); ++b)
        for (int c = 0; c < (i <= 1 ? 1 : 16); ++c)
          if (f[i - 1][a][b][c] != inf) {
            int d = jl[i], tmp = f[i - 1][a][b][c];
            if (i > 3) f[i][0][0][0] = min(f[i][0][0][0], tmp + val[4]);
            if (a != 0) continue;
            f[i][b][c][d] = tmp;
            int bb = b & 8, cc = c & 8, dd = d & 8;
            if (i > 2) f[i][bb][cc][dd] = min(f[i][bb][cc][dd], tmp + val[3]);
            if (i > 2) dd = 0;
            f[i][bb][cc][dd] = min(f[i][bb][cc][dd], tmp + val[3] + val[1]);
            if (i > 2) cc = 0;
            f[i][bb][cc][dd] = min(f[i][bb][cc][dd], tmp + val[3] + val[2]);
            if (i > 2) bb = 0;
            f[i][bb][cc][dd] = min(f[i][bb][cc][dd], tmp + val[3] + val[3]);
            bb = b & 1, cc = c & 1, dd = d & 1;
            if (i > 2) f[i][bb][cc][dd] = min(f[i][bb][cc][dd], tmp + val[3]);
            if (i > 2) dd = 0;
            f[i][bb][cc][dd] = min(f[i][bb][cc][dd], tmp + val[3] + val[1]);
            if (i > 2) cc = 0;
            f[i][bb][cc][dd] = min(f[i][bb][cc][dd], tmp + val[3] + val[2]);
            if (i > 1)
              f[i][b][0][0] = min(f[i][b][0][0], tmp + val[2] + val[2]);
            cc = c & 3, dd = d & 3;
            if (i > 1) f[i][b][cc][dd] = min(f[i][b][cc][dd], tmp + val[2]);
            if (i > 1)
              f[i][b][cc][dd & 1] =
                  min(f[i][b][cc][dd & 1], tmp + val[2] + val[1]);
            if (i > 1)
              f[i][b][cc][dd & 2] =
                  min(f[i][b][cc][dd & 2], tmp + val[2] + val[1]);
            if (i > 1)
              dd = 0, f[i][b][cc][dd] =
                          min(f[i][b][cc][dd], tmp + val[2] + val[1] + val[1]);
            cc = c & 9, dd = d & 9;
            if (i > 1) f[i][b][cc][dd] = min(f[i][b][cc][dd], tmp + val[2]);
            if (i > 1)
              f[i][b][cc][dd & 1] =
                  min(f[i][b][cc][dd & 1], tmp + val[2] + val[1]);
            if (i > 1)
              f[i][b][cc][dd & 8] =
                  min(f[i][b][cc][dd & 8], tmp + val[2] + val[1]);
            if (i > 1)
              dd = 0, f[i][b][cc][dd] =
                          min(f[i][b][cc][dd], tmp + val[2] + val[1] + val[1]);
            cc = c & 12, dd = d & 12;
            if (i > 1) f[i][b][cc][dd] = min(f[i][b][cc][dd], tmp + val[2]);
            if (i > 1)
              f[i][b][cc][dd & 4] =
                  min(f[i][b][cc][dd & 4], tmp + val[2] + val[1]);
            if (i > 1)
              f[i][b][cc][dd & 8] =
                  min(f[i][b][cc][dd & 8], tmp + val[2] + val[1]);
            if (i > 1)
              dd = 0, f[i][b][cc][dd] =
                          min(f[i][b][cc][dd], tmp + val[2] + val[1] + val[1]);
            for (int l = 1; l < 16; ++l) {
              shu[l] = shu[l ^ (l & -l)] + 1;
              dd = d - (d & l);
              f[i][b][c][dd] = min(f[i][b][c][dd], tmp + val[1] * shu[l]);
            }
          }
  }
  printf("%d", f[n][0][0][0]);
  return 0;
}
