#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7, N = 1e3 + 5;
int dp[4][N][1 << 12], cost[4], add[4][4], n;
bool is[5][N];
int calc(int c, int r, int msk) {
  if (r == 4) return !(msk & 15) ? calc(c + 1, 0, (msk >> 4) | (15 << 8)) : 1e9;
  if (c >= n) return 0;
  int &ref = dp[r][c][msk];
  if (ref != -1) return ref;
  if (!is[r][c]) msk &= ~(1 << r);
  ref = calc(c, r + 1, msk);
  for (int i = 0; i < 4; i++)
    if (i < 3)
      ref = min(ref, calc(c, r + 1, (msk & ~add[r][i])) + cost[i]);
    else
      ref = min(ref, calc(c + 4, 0, (1 << 12) - 1) + cost[i]);
  return ref;
}
int main() {
  scanf("%d", &n);
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < 4; i++) scanf("%d", cost + i);
  for (int i = 0; i < 3; i++)
    for (int j = 0; j + i < 4; j++)
      for (int k = 0; k <= i; k++)
        for (int l = 0; l <= i; l++) add[j][i] |= 1 << (4 * k + j + l);
  for (int i = 0; i < 4; i++) {
    char c[N];
    scanf("%s", c);
    string s = c;
    for (int j = 0; j < s.size(); j++) is[i][j] = s[j] != '.';
  }
  printf("%d\n", calc(0, 0, (1 << 12) - 1));
}
