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 int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 1005;
const int MSK = (1 << 3) + 1;
const int D = 8;
char str[6][N];
int dp[N][MSK][MSK][MSK][MSK];
int n, a1, a2, a3, a4;
vector<int> create(int idx, int mr0, int mr1, int mr2, int mr3) {
  if (str[0][idx] == '.') mr0 |= 4;
  if (str[1][idx] == '.') mr1 |= 4;
  if (str[2][idx] == '.') mr2 |= 4;
  if (str[3][idx] == '.') mr3 |= 4;
  vector<int> m;
  m.push_back(mr0);
  m.push_back(mr1);
  m.push_back(mr2);
  m.push_back(mr3);
  return m;
}
int solve(int idx, int mr0, int mr1, int mr2, int mr3) {
  if (idx == n) return 0;
  int &ret = dp[idx][mr0][mr1][mr2][mr3];
  if (~ret) return ret;
  ret = solve(idx + 1, 7, 7, 7, 7) + a4;
  vector<int> m = create(idx, mr0, mr1, mr2, mr3);
  mr0 = m[0];
  mr1 = m[1];
  mr2 = m[2];
  mr3 = m[3];
  int cnt = 0;
  for (int i = 0; i <= 3; i++)
    if (str[i][idx] == '*')
      if (!(m[i] & 4)) cnt++;
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7),
                       ((mr2 * 2) & 7), ((mr3 * 2) & 7)) +
                     cnt * a1);
  if ((m[2] & 4) && (m[3] & 4))
    ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 4, ((mr1 * 2) & 7) | 4,
                         ((mr2 * 2) & 7), ((mr3 * 2) & 7)) +
                       a2);
  if ((m[2] & 4) || (m[3] & 4))
    ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 4, ((mr1 * 2) & 7) | 4,
                         ((mr2 * 2) & 7), ((mr3 * 2) & 7)) +
                       a2 + a1);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 4, ((mr1 * 2) & 7) | 4,
                       ((mr2 * 2) & 7), ((mr3 * 2) & 7)) +
                     a2 + a1 + a1);
  if ((m[0] & 4) && (m[1] & 4))
    ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7),
                         ((mr2 * 2) & 7) | 4, ((mr3 * 2) & 7) | 4) +
                       a2);
  if ((m[0] & 4) || (m[1] & 4))
    ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7),
                         ((mr2 * 2) & 7) | 4, ((mr3 * 2) & 7) | 4) +
                       a2 + a1);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7),
                       ((mr2 * 2) & 7) | 4, ((mr3 * 2) & 7) | 4) +
                     a2 + a1 + a1);
  if ((m[0] & 4) && (m[3] & 4))
    ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7) | 4,
                         ((mr2 * 2) & 7) | 4, ((mr3 * 2) & 7)) +
                       a2);
  if ((m[0] & 4) || (m[3] & 4))
    ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7) | 4,
                         ((mr2 * 2) & 7) | 4, ((mr3 * 2) & 7)) +
                       a2 + a1);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7) | 4,
                       ((mr2 * 2) & 7) | 4, ((mr3 * 2) & 7)) +
                     a2 + a1 + a1);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 4, ((mr1 * 2) & 7) | 4,
                       ((mr2 * 2) & 7) | 4, ((mr3 * 2) & 7) | 4) +
                     a2 + a2);
  if ((m[3] & 4))
    ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 6, ((mr1 * 2) & 7) | 6,
                         ((mr2 * 2) & 7) | 6, ((mr3 * 2) & 7)) +
                       a3);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 6, ((mr1 * 2) & 7) | 6,
                       ((mr2 * 2) & 7) | 6, ((mr3 * 2) & 7) | 4) +
                     a3 + a2);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 6, ((mr1 * 2) & 7) | 6,
                       ((mr2 * 2) & 7) | 6, ((mr3 * 2) & 7)) +
                     a3 + a1);
  if ((m[0] & 4))
    ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7) | 6,
                         ((mr2 * 2) & 7) | 6, ((mr3 * 2) & 7) | 6) +
                       a3);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 4, ((mr1 * 2) & 7) | 6,
                       ((mr2 * 2) & 7) | 6, ((mr3 * 2) & 7) | 6) +
                     a3 + a2);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7), ((mr1 * 2) & 7) | 6,
                       ((mr2 * 2) & 7) | 6, ((mr3 * 2) & 7) | 6) +
                     a3 + a1);
  ret = min(ret, solve(idx + 1, ((mr0 * 2) & 7) | 6, ((mr1 * 2) & 7) | 6,
                       ((mr2 * 2) & 7) | 6, ((mr3 * 2) & 7) | 6) +
                     a3 + a3);
  return ret;
}
int main() {
  n = read();
  a1 = read();
  a2 = read();
  a3 = read();
  a4 = read();
  for (int i = 0; i <= n - 1; i++) scanf("%s", str[i]);
  memset(dp, -1, sizeof dp);
  printf("%d\n", solve(0, 0, 0, 0, 0));
  return 0;
}
