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;
const int MAX_COL = 1e3;
int cost[5];
bool e[5][MAX_COL + 10];
int nColumns;
int fullBits(int k) { return (1 << k) - 1; }
bool done(int mask) { return !((mask ^ fullBits(4)) & fullBits(4)); }
int startCleaner(int k) {
  int col = fullBits(k);
  int cleaner = 0;
  for (int j = 1; j <= k; j++) {
    cleaner += col << (4 * (j - 1));
  }
  return cleaner;
}
int moveDown(int cleaner) { return (cleaner << 1); }
int skipColumn(int mask) { return (mask >> 4); }
int merge(int cur, int next) { return (cur + (next << 4)); }
bool effective(int cleaner, int mask) {
  return (((cleaner | mask) ^ mask) & fullBits(4));
}
int download(int col) {
  int mask = 0;
  int shift = 0;
  for (int row = 1; row <= 4; row++) {
    mask += (e[row][col] << shift);
    shift++;
  }
  return mask;
}
void clean(int sum, int mask, set<pair<int, int> > &space) {
  if (done(mask)) {
    space.emplace(sum, skipColumn(mask));
  } else {
    for (int k = 1; k <= 3; k++) {
      int cleaner = startCleaner(k);
      for (int i = k; i <= 4; i++) {
        if (effective(cleaner, mask)) {
          clean(sum + cost[k], cleaner | mask, space);
        }
        cleaner = moveDown(cleaner);
      }
    }
  }
}
int memo[MAX_COL + 10][1 << 8];
int solve(int j, int mask) {
  if (memo[j][mask] == -1) {
    if (j > nColumns) {
      memo[j][mask] = 0;
    } else if (done(mask)) {
      memo[j][mask] = solve(j + 1, merge(skipColumn(mask), download(j + 2)));
    } else {
      memo[j][mask] =
          cost[4] + solve(j + 4, merge(download(j + 4), download(j + 5)));
      set<pair<int, int> > space;
      clean(0, mask + (download(j + 2) << 2 * 4), space);
      for (pair<int, int> sol : space) {
        memo[j][mask] =
            min(memo[j][mask], sol.first + solve(j + 1, sol.second));
      }
    }
  }
  return memo[j][mask];
}
int main() {
  cin >> nColumns;
  for (int i = 1; i <= 4; i++) {
    cin >> cost[i];
  }
  memset(e, true, sizeof(e));
  for (int i = 1; i <= 4; i++) {
    string row;
    cin >> row;
    for (int j = 1; j <= nColumns; j++) {
      e[i][j] = (row[j - 1] == '.' ? true : false);
    }
  }
  memset(memo, -1, sizeof(memo));
  cout << solve(1, merge(download(1), download(2))) << endl;
}
