#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void solve() {
  int n;
  while (cin >> n, n) {
    long double dp[16][5][5] = {0.0};
    char s, t, b;
    cin >> s >> t >> b;
    int sx, sy, gx, gy, bx, by;
    sy = (s - 'A') / 3 + 1, sx = (s - 'A') % 3 + 1;
    gy = (t - 'A') / 3 + 1, gx = (t - 'A') % 3 + 1;
    by = (b - 'A') / 3 + 1, bx = (b - 'A') % 3 + 1;
    char Map[5][5];
    for (int i = 0; i < 5; ++i) {
      for (int j = 0; j < 5; ++j) {
        Map[i][j] = 'X';
      }
    }
    for (int i = 1; i < 4; ++i) {
      for (int j = 1; j < 4; ++j) {
        if (i != bx || j != by) {
          Map[i][j] = ' ';
        }
      }
    }
    dp[n][sy][sx] = 1.0;
    while (n) {
      for (int i = 1; i < 4; ++i) {
        for (int j = 1; j < 4; ++j) {
          for (int k = 0; k < 4; ++k) {
            int tx = j + dx[k];
            int ty = i + dy[k];
            if (Map[ty][tx] == 'X') {
              tx = j;
              ty = i;
            }
            dp[n - 1][ty][tx] += dp[n][i][j] * 0.25;
          }
        }
      }
      n--;
    }
    printf("%.8lf\n", dp[0][gy][gx]);
  }
}
int main() {
  solve();
  return (0);
}
