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 setBit(int n, int pos) { return n = n | (1 << pos); }
int resetBit(int n, int pos) { return n = n & ~(1 << pos); }
bool checkBit(int n, int pos) { return (bool)(n & (1 << pos)); }
const int MAX = 1010;
const int INF = 2000000000;
int cost[5];
char str[5][MAX];
int ara[MAX];
int make[20][5][5];
int n;
int dp[MAX][16][16][16];
int call(int pos, int m1, int m2, int m3) {
  if (pos > n) return 0;
  if (dp[pos][m1][m2][m3] != -1) return dp[pos][m1][m2][m3];
  int ret = INF;
  int lala[5][5];
  int nm1 = m1, nm2 = m2, nm3 = m3, nm4 = ara[pos + 3], C;
  for (int i = 0; i <= 4; i++) {
    lala[4][1] = nm1, lala[4][2] = nm2, lala[4][3] = nm3, lala[4][4] = nm4;
    C = 0;
    if (pos + i - 1 <= n) C += cost[i];
    if (i == 1) {
      nm1 = make[nm1][0][1];
    } else if (i == 2 && pos + 1 <= n) {
      nm1 = make[nm1][0][2];
      nm2 = make[nm2][0][2];
    } else if (i == 3 && pos + 2 <= n) {
      nm1 = make[nm1][0][3];
      nm2 = make[nm2][0][3];
      nm3 = make[nm3][0][3];
    } else if (i == 4 && pos + 3 <= n) {
      nm1 = make[nm1][0][4];
      nm2 = make[nm2][0][4];
      nm3 = make[nm3][0][4];
      nm4 = make[nm4][0][4];
    }
    for (int j = 0; j <= 3; j++) {
      lala[3][1] = nm1, lala[3][2] = nm2, lala[3][3] = nm3;
      if (pos + j - 1 <= n) C += cost[j];
      if (j == 1) {
        nm1 = make[nm1][1][1];
      } else if (j == 2 && pos + 1 <= n) {
        nm1 = make[nm1][1][2];
        nm2 = make[nm2][1][2];
      } else if (j == 3 && pos + 2 <= n) {
        nm1 = make[nm1][1][3];
        nm2 = make[nm2][1][3];
        nm3 = make[nm3][1][3];
      }
      for (int k = 0; k <= 2; k++) {
        lala[2][1] = nm1, lala[2][2] = nm2;
        if (pos + k - 1 <= n) C += cost[k];
        if (k == 1) {
          nm1 = make[nm1][2][1];
        } else if (k == 2 && pos + 1 <= n) {
          nm1 = make[nm1][2][2];
          nm2 = make[nm2][2][2];
        }
        for (int l = 0; l <= 1; l++) {
          lala[1][1] = nm1;
          if (pos + l - 1 <= n) C += cost[l];
          if (l == 1) {
            nm1 = make[nm1][3][1];
          }
          if (nm1 == 0) ret = min(ret, C + call(pos + 1, nm2, nm3, nm4));
          if (pos + l - 1 <= n) C -= cost[l];
          nm1 = lala[1][1];
        }
        if (pos + k - 1 <= n) C -= cost[k];
        nm1 = lala[2][1], nm2 = lala[2][2];
      }
      if (pos + j - 1 <= n) C -= cost[j];
      nm1 = lala[3][1], nm2 = lala[3][2], nm3 = lala[3][3];
    }
    if (pos + i - 1 <= n) C -= cost[i];
    nm1 = lala[4][1], nm2 = lala[4][2], nm3 = lala[4][3], nm4 = lala[4][4];
  }
  return dp[pos][m1][m2][m3] = ret;
}
int main() {
  scanf("%d", &n);
  cost[0] = 0;
  for (int i = 1; i <= 4; i++) scanf("%d", &cost[i]);
  for (int i = 0; i < 4; i++) scanf("%s", str[i]);
  for (int col = 0; col < n; col++) {
    for (int row = 0; row < 4; row++) {
      if (str[row][col] == '*') ara[col + 1] = setBit(ara[col + 1], row);
    }
  }
  int x;
  for (int i = 0; i < 16; i++) {
    for (int start = 0; start < 4; start++) {
      x = i;
      for (int sz = 1; start + sz - 1 < 4; sz++) {
        x = resetBit(x, start + sz - 1);
        make[i][start][sz] = x;
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  printf("%d\n", call(1, ara[1], ara[2], ara[3]));
  return 0;
}
