#include <bits/stdc++.h>
using namespace std;
long cnt, as;
char s, t, b, field[3][3];
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
void search(int x, int y, int c) {
  int tx, ty;
  if (c == 0.0) {
    if (field[y][x] == 2) as++;
    cnt++;
    return;
  }
  for (int k = 0; k < 4; k++) {
    tx = x + dx[k];
    ty = y + dy[k];
    if (tx < 0 || ty < 0 || tx >= 3 || ty >= 3) {
      cnt++;
      continue;
    }
    if (field[ty][tx] == 1) {
      search(x, y, c - 1);
    } else {
      search(tx, ty, c - 1);
    }
  }
}
int main() {
  int n;
  for (; cin >> n, n;) {
    cin.ignore();
    cnt = as = 0;
    scanf(" %c %c %c ", &s, &t, &b);
    memset(field, 0, sizeof(field));
    int x = s == 'A' || s == 'D' || s == 'G'   ? 0
            : s == 'B' || s == 'E' || s == 'H' ? 1
                                               : 2;
    int y = s == 'A' || s == 'B' || s == 'C'   ? 0
            : s == 'D' || s == 'E' || s == 'F' ? 1
                                               : 2;
    int p = t == 'A' || t == 'D' || t == 'G'   ? 0
            : t == 'B' || t == 'E' || t == 'H' ? 1
                                               : 2;
    int q = t == 'A' || t == 'B' || t == 'C'   ? 0
            : t == 'D' || t == 'E' || t == 'F' ? 1
                                               : 2;
    field[q][p] = 2;
    int u = b == 'A' || b == 'D' || b == 'G'   ? 0
            : b == 'B' || b == 'E' || b == 'H' ? 1
                                               : 2;
    int t = b == 'A' || b == 'B' || b == 'C'   ? 0
            : b == 'D' || b == 'E' || b == 'F' ? 1
                                               : 2;
    field[t][u] = 1;
    search(x, y, n);
    printf("%.8lf\n", (double)as / pow(4.0, (double)n));
  }
  return 0;
}
