#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-5;
double p[5][5];
double ne[5][5];
char s, t, b;
double pp(int x, int y) {
  if (b / 3 + 1 == x && b % 3 + 1 == y)
    return 0;
  else
    return p[x][y];
}
int main() {
  int n;
  while (cin >> n) {
    if (n == 0) break;
    cin >> s >> t >> b;
    s -= 'A';
    t -= 'A';
    b -= 'A';
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        p[i][j] = 0;
        ne[i][j] = 0;
      }
    }
    p[s / 3 + 1][s % 3 + 1] = 1;
    while (n) {
      n--;
      for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
          ne[i][j] =
              (pp(i + 1, j) + pp(i - 1, j) + pp(i, j + 1) + pp(i, j - 1)) / 4;
          if (i == b / 3 + 1 && j == b % 3 + 1) ne[i][j] = 0;
        }
      }
      memcpy(p, ne, sizeof(ne));
    }
    printf("%.8f\n", pp(t / 3 + 1, t % 3 + 1));
  }
}
