#include <bits/stdc++.h>
using namespace std;
int dp[3][3][20];
int mx[] = {-1, 1, 0, 0}, my[] = {0, 0, -1, 1};
char abc[3][4] = {"ABC", "DEF", "GHI"};
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) return 0;
    char s, t, b;
    scanf(" %c %c %c", &s, &t, &b);
    memset(dp, 0, sizeof(dp));
    int sx, sy, rx, ry;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (abc[i][j] == s) {
          sx = j;
          sy = i;
        } else if (abc[i][j] == t)
          dp[i][j][0] = 1;
        else if (abc[i][j] == b) {
          rx = j;
          ry = i;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int y = 0; y < 3; y++) {
        for (int x = 0; x < 3; x++) {
          if (dp[y][x][i] == 0) continue;
          for (int j = 0; j < 4; j++) {
            int cx = x + mx[j], cy = y + my[j];
            if (cx < 0 || 3 < cx || cy < 0 || 3 < cy) {
              dp[y][x][i + 1] += dp[y][x][i];
            } else if (cx == rx && cy == ry) {
              dp[y][x][i + 1] += dp[y][x][i];
            } else {
              dp[cy][cx][i + 1] += dp[y][x][i];
            }
          }
        }
      }
    }
    printf("%.15lf\n", static_cast<double>(dp[sy][sx][n]) /
                           static_cast<double>(1 << 2 * n));
  }
}
