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;
template <class T>
struct rge {
  T b, e;
};
template <class T>
rge<T> range(T i, T j) {
  return rge<T>{i, j};
}
struct debug {
  template <class T>
  debug& operator<<(const T&) {
    return *this;
  }
};
using ll = long long;
template <typename T>
void min_self(T& a, T b) {
  a = min(a, b);
}
template <typename T>
void max_self(T& a, T b) {
  a = max(a, b);
}
const int mxN = 1005;
int memo[mxN][1 << 4][1 << 4][1 << 4][1 << 4];
int grid[4][mxN], cost[5], n;
const int inf = 1e9 + 5;
int msb(int b) { return 31 - __builtin_clz(b); }
int dp(int col, int mask[]) {
  int& ans = memo[col][mask[0]][mask[1]][mask[2]][mask[3]];
  if (ans != -1) {
    return ans;
  }
  ans = inf;
  auto Transition = [&]() {
    int x = -1, y = -1;
    for (int j = 0; j < 4; j++) {
      for (int i = 0; i < 4; i++) {
        if (y == -1 && (mask[i] >> j & 1)) {
          y = 3 - j;
          x = i;
        }
      }
    }
    assert(x != -1 && y != -1);
    for (int type = 1; type <= 4; type++) {
      for (int r = 0; r <= 4 - type; r++) {
        for (int c = 0; c <= 4 - type; c++) {
          if (r <= x && x < r + type && c <= y && y < c + type) {
            int new_mask[4];
            for (int i = 0; i < 4; i++) {
              new_mask[i] = mask[i];
              if (r <= i && i < r + type) {
                int s = (1 << type) - 1;
                while (msb(s) != 3 - c) {
                  s <<= 1;
                }
                s = ~s;
                new_mask[i] &= s;
              }
            }
            min_self(ans, dp(col, new_mask) + cost[type]);
          }
        }
      }
    }
  };
  if (col == 0) {
    bool all_zero = true;
    for (int i = 0; i < 4; i++) {
      if (mask[i]) {
        all_zero = false;
      }
    }
    if (all_zero) {
      return ans = 0;
    }
    Transition();
  } else {
    bool all_even = true;
    for (int i = 0; i < 4; i++) {
      if (mask[i] & 1) {
        all_even = false;
      }
    }
    if (all_even) {
      for (int i = 0; i < 4; i++) {
        mask[i] >>= 1;
        mask[i] |= grid[i][col - 1] << 3;
      }
      return ans = dp(col - 1, mask);
    }
    Transition();
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  memset(memo, -1, sizeof memo);
  cin >> n;
  for (int i = 1; i <= 4; i++) {
    cin >> cost[i];
  }
  for (int i = 0; i < 4; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      grid[i][j] = s[j] == '*';
    }
  }
  int mask[4];
  for (int i = 0; i < 4; i++) {
    mask[i] = 0;
    for (int j = n - 4; j < n; j++) {
      mask[i] |= grid[i][j] << (n - j - 1);
    }
  }
  cout << dp(n - 4, mask) << "\n";
}
