#include <bits/stdc++.h>
using namespace std;
char cs, ct, cb;
int n, s, t, b;
long double dp[16][5][5];
int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
int main() {
  while (cin >> n && n) {
    scanf("%c", &cs);
    cin >> cs >> ct >> cb;
    s = cs - 'A';
    t = ct - 'A';
    b = cb - 'A';
    int sx, sy, tx, ty, bx, by;
    sx = s % 3 + 1;
    sy = s / 3 + 1;
    tx = t % 3 + 1;
    ty = t / 3 + 1;
    bx = b % 3 + 1;
    by = b / 3 + 1;
    for (int i = 0; i < 16; i++)
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < 5; k++) dp[i][j][k] = 0.0;
    dp[n][sy][sx] = 1.0;
    for (; n > 0; n--) {
      for (int y = 1; y <= 3; y++) {
        for (int x = 1; x <= 3; x++) {
          for (int k = 0; k < 4; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (nx == bx && ny == by) {
              nx = x;
              ny = y;
            }
            dp[n - 1][ny][nx] += dp[n][y][x] * 0.25;
          }
        }
      }
    }
    printf("%.8Lf\n", dp[0][ty][tx]);
  }
}
