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;
inline char gc() {
  static char buf[1 << 14], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 14, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read(int &x) {
  x = 0;
  bool f = 1;
  char c = gc();
  for (; !isdigit(c); c = gc()) {
    if (c == '-') f ^= 1;
    if (c == -1) return -1;
  }
  for (; isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c ^ 48);
  x = f ? x : -x;
  return 1;
}
inline void wr(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) wr(x / 10);
  putchar(x % 10 | 48);
}
inline bool sxd() {
  char c = gc();
  for (; c != '.' && c != '*'; c = gc())
    ;
  return c == '*';
}
const int N = 2010;
int n, b[5], a[N], s[5][N], f[N][16][16][16];
inline void Min(int &x, int y) {
  if (y < x) x = y;
}
inline int num(int x) {
  int ret = 0;
  for (; x; x -= x & -x) ++ret;
  return ret;
}
int main() {
  read(n);
  for (int i = 1; i <= 4; ++i) read(b[i]);
  for (int i = 1; i <= 4; ++i)
    for (int j = 1; j <= n; ++j) s[i][j] = sxd();
  for (int i = 1; i <= n; ++i)
    a[i] = s[1][i] * 1 + s[2][i] * 2 + s[3][i] * 4 + s[4][i] * 8;
  memset(f, 0x3f, sizeof f);
  f[0][0][0][0] = 0;
  n += 4;
  for (int i = 1; i <= n; ++i) {
    for (int j = (i - 3 > 0 ? a[i - 3] : 0);;
         j = (j - 1) & (i - 3 > 0 ? a[i - 3] : 0)) {
      for (int k = (i - 2 > 0 ? a[i - 2] : 0);;
           k = (k - 1) & (i - 2 > 0 ? a[i - 2] : 0)) {
        for (int l = (i - 1 > 0 ? a[i - 1] : 0);;
             l = (l - 1) & (i - 1 > 0 ? a[i - 1] : 0)) {
          if (!j) Min(f[i][k][l][a[i]], f[i - 1][j][k][l]);
          Min(f[i][0][0][0], f[i - 1][j][k][l] + b[4]);
          if ((j & 1) == 0)
            Min(f[i][k & 1][l & 1][a[i]], f[i - 1][j][k][l] + b[3]);
          if ((j & 8) == 0)
            Min(f[i][k & 8][l & 8][a[i]], f[i - 1][j][k][l] + b[3]);
          Min(f[i][0][0][a[i]], f[i - 1][j][k][l] + b[3] * 2);
          if ((j & 3) == 0) Min(f[i][k & 3][l][a[i]], f[i - 1][j][k][l] + b[2]);
          if ((j & 9) == 0) Min(f[i][k & 9][l][a[i]], f[i - 1][j][k][l] + b[2]);
          if ((j & 12) == 0)
            Min(f[i][k & 12][l][a[i]], f[i - 1][j][k][l] + b[2]);
          Min(f[i][0][l][a[i]], f[i - 1][j][k][l] + b[2] * 2);
          Min(f[i][k][l][a[i]], f[i - 1][j][k][l] + b[1] * num(j));
          Min(f[i][k & 1][l & 1][a[i]], f[i - 1][j][k][l] + b[3] + b[1]);
          Min(f[i][k & 8][l & 8][a[i]], f[i - 1][j][k][l] + b[3] + b[1]);
          Min(f[i][0][l & 1][a[i]], f[i - 1][j][k][l] + b[3] + b[2]);
          Min(f[i][0][l & 8][a[i]], f[i - 1][j][k][l] + b[3] + b[2]);
          if ((j & 1) == 0)
            Min(f[i][k & 3][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1]);
          if ((j & 2) == 0)
            Min(f[i][k & 3][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1]);
          Min(f[i][k & 3][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1] * 2);
          if ((j & 1) == 0)
            Min(f[i][k & 9][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1]);
          if ((j & 8) == 0)
            Min(f[i][k & 9][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1]);
          Min(f[i][k & 9][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1] * 2);
          if ((j & 4) == 0)
            Min(f[i][k & 12][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1]);
          if ((j & 8) == 0)
            Min(f[i][k & 12][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1]);
          Min(f[i][k & 12][l][a[i]], f[i - 1][j][k][l] + b[2] + b[1] * 2);
          if (!l) break;
        }
        if (!k) break;
      }
      if (!j) break;
    }
  }
  wr(f[n][0][0][0]);
  return 0;
}
