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;
long long n;
vector<string> v;
long long c[5];
long long dp[1000][8][8][8][8];
long long solve(long long curr, long long mask0, long long mask1,
                long long mask2, long long mask3) {
  if (curr == n) {
    if (mask1 == 0 && mask2 == 0 && mask3 == 0 && mask0 == 0)
      return 0;
    else
      return INT_MAX;
  }
  long long &ans = dp[curr][mask0][mask1][mask2][mask3];
  if (ans != -1) return ans;
  vector<long long> masks(4);
  masks[0] = mask0;
  masks[1] = mask1;
  masks[2] = mask2;
  masks[3] = mask3;
  long long mask_needed = 0;
  for (long long i = 0; i < 4; i++)
    if (v[curr][i] == '*' && ((masks[i] & 1) == 0)) mask_needed |= (1 << i);
  for (long long i = 0; i < 4; i++) masks[i] >>= 1;
  vector<long long> mp(4);
  mp[0] = 0, mp[1] = 1, mp[2] = 3, mp[3] = 7;
  ans = LLONG_MAX;
  for (long long i = 0; i <= 4; i++) {
    for (long long j = 0; j <= 3; j++) {
      for (long long k = 0; k <= 2; k++) {
        for (long long l = 0; l <= 1; l++) {
          long long cost = c[i] + c[j] + c[k] + c[l];
          vector<long long> new_masks = masks;
          long long curr_mask = 0;
          if (i > 0) {
            for (long long x = 0; x < 4; x++) {
              if (x <= i - 1) new_masks[x] |= mp[i - 1];
              if (x < i) curr_mask |= (1 << x);
            }
          }
          if (j > 0)
            for (long long x = 1; x < 4; x++) {
              if (x <= 1 + (j - 1)) new_masks[x] |= mp[j - 1];
              if (x < 1 + j) curr_mask |= (1 << x);
            }
          if (k > 0)
            for (long long x = 2; x < 4; x++) {
              if (x <= 2 + (k - 1)) new_masks[x] |= mp[k - 1];
              if (x < 2 + k) curr_mask |= (1 << x);
            }
          if (l > 0)
            for (long long x = 3; x < 4; x++) {
              if (x <= 2 + (l - 1)) new_masks[x] |= mp[l - 1];
              if (x < 3 + l) curr_mask |= (1 << x);
            }
          if ((curr_mask | mask_needed) != curr_mask) continue;
          ans = min(ans, cost + solve(curr + 1, new_masks[0], new_masks[1],
                                      new_masks[2], new_masks[3]));
        }
      }
    }
  }
  return ans;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  c[0] = 0;
  for (long long i = 1; i <= 4; i++) cin >> c[i];
  string s[4];
  for (long long i = 0; i < 4; i++) {
    cin >> s[i];
  }
  v.resize(n);
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < 4; j++) {
      v[i].push_back(s[j][i]);
    }
  }
  cout << solve(0, 0, 0, 0, 0) << endl;
  return 0;
}
