#include <bits/stdc++.h>
int di[] = {-3, -1, 1, 3};
int main() {
  using namespace std;
  int n, s, t, b;
  char cs, ct, cb;
  while (cin >> n) {
    if (n == 0) break;
    cin >> cs >> ct >> cb;
    s = cs - 'A';
    t = ct - 'A';
    b = cb - 'A';
    double p[9] = {0};
    p[s] = 1;
    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[d] == 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("%.8f\n", p[t]);
  }
  return 0;
}
