#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;
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] =
              (p[i + 1][j] + p[i - 1][j] + p[i][j + 1] + p[i][j - 1]) / 4;
        }
      }
      ne[b / 3 + 1][b % 3 + 1] = 0;
      memcpy(p, ne, sizeof(ne));
    }
    printf("%.8f\n", ne[t / 3 + 1][t % 3 + 1]);
  }
}
