#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-10;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
  int n;
  int t = 0;
  while (cin >> n, n) {
    ++t;
    long double heya[3][3];
    for (int i = 0; i < (int)3; ++i)
      for (int j = 0; j < (int)3; ++j) heya[i][j] = 0.0;
    char s, t, b;
    cin >> s >> t >> b;
    s -= 'A';
    t -= 'A';
    b -= 'A';
    heya[s / 3][s % 3] = 1.0;
    while (n--) {
      if (n < 0) break;
      long double next[3][3];
      for (int i = 0; i < (int)3; ++i)
        for (int j = 0; j < (int)3; ++j) next[i][j] = 0;
      for (int i = 0; i < (int)9; ++i) {
        int cx = i / 3, cy = i % 3;
        for (int j = 0; j < (int)4; ++j) {
          int nx = cx + dx[j], ny = cy + dy[j];
          if (nx < 0 || 2 < nx || ny < 0 || 2 < nx || nx * 3 + ny == b)
            next[cx][cy] += heya[cx][cy] / 4;
          else
            next[nx][ny] += heya[cx][cy] / 4;
        }
      }
      memcpy(heya, next, sizeof(next));
    }
    printf("%.8Lf\n", heya[t / 3][t % 3]);
  }
}
