#include <bits/stdc++.h>
int di[] = {-3, -1, 1, 3};
int main() {
  int n, s, t, b;
  char cs, ct, cb;
  while (scanf("%d", &n)) {
    if (n == 0) break;
    getchar();
    scanf("%c %c %c", &cs, &ct, &cb);
    s = cs - 'A', t = ct - 'A', b = cb - 'A';
    double p[9] = {0};
    p[s] = 1.0;
    while (n--) {
      double q[9] = {0};
      for (int i = 0; i < 9; ++i)
        for (int d = 0; d < 4; ++d) {
          if ((0 <= (i + di[d]) && (i + di[d]) < 9) &&
              !(i % 3 == 0 && di[d] == -1) && !(i % 3 == 2 && di[i] == 1) &&
              i + di[d] != b)
            q[i + di[d]] += p[i] / 4;
          else
            q[i] += p[i] / 4;
        }
      for (int i = 0; i < 9; ++i) p[i] = q[i];
    }
    printf("%.8lf\n", p[t]);
  }
  return 0;
}
