#include <bits/stdc++.h>
using namespace std;
const int rub[] = {0, 1, 51, 1911, 65535};
const int N = 1000 + 7, K = 1 << 16;
int n, c[5], f[N][K], a[N];
map<int, int> cost;
vector<pair<int, int> > type;
int bfs() {
  memset(f, -1, sizeof(f));
  queue<pair<int, int> > Q;
  int mask = a[4], i, ans = 1e9, next;
  for (int i = 3, loop = 1; i >= loop; --i) mask = (mask << 4) | a[i];
  f[0][mask] = 0;
  Q.push(make_pair(0, mask));
  while (!Q.empty()) {
    i = Q.front().first;
    mask = Q.front().second;
    Q.pop();
    if (i == n) {
      ans = min(ans, f[i][mask]);
      continue;
    }
    for (auto tmp : type)
      if (((tmp.first | mask) & 15) == 15) {
        next = ((tmp.first | mask) >> 4) | (a[i + 5] << 12);
        if (f[i + 1][next] > f[i][mask] + tmp.second || f[i + 1][next] == -1) {
          if (f[i + 1][next] == -1) Q.push(make_pair(i + 1, next));
          f[i + 1][next] = f[i][mask] + tmp.second;
        }
      }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio();
  cin.tie(0);
  cin >> n;
  for (int i = 1, loop = 4; i <= loop; ++i) cin >> c[i];
  for (int mask = 0, loop = 5 * 5 * 5 * 5 - 1; mask <= loop; ++mask) {
    int tmp = mask, valid = 1, m = 0, s = 0;
    for (int i = 0, loop = 3; i <= loop; ++i) {
      if (tmp % 5 + i > 4) {
        valid = 0;
        break;
      }
      m |= rub[tmp % 5] << i;
      s += c[tmp % 5];
      tmp /= 5;
    }
    if (valid && (!cost.count(m) || cost[m] > s)) cost[m] = s;
  }
  for (auto tmp : cost) type.push_back(tmp);
  for (int i = 0, loop = 3; i <= loop; ++i)
    for (int j = 1, loop = n; j <= loop; ++j) {
      char ch;
      cin >> ch;
      if (ch == '.') a[j] |= 1 << i;
    }
  cout << bfs();
}
