#include <bits/stdc++.h>
using namespace std;
double dp[16][9];
int k, x, y, z;
int b[9][4] = {{1, 3, -1, -1}, {0, 2, 4, -1}, {1, 5, -1, -1},
               {0, 4, 6, -1},  {1, 3, 5, 7},  {2, 4, 8, -1},
               {3, 7, -1, -1}, {4, 6, 8, -1}, {5, 7, -1, -1}};
int main() {
  while (cin >> k, k) {
    memset(dp, 0, sizeof(dp));
    char a, b, c;
    cin >> a >> b >> c;
    dp[0][a - 'A'] = 1;
    y = b - 'A';
    z = c - 'A';
    double S = 0;
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < 9; j++) {
        for (int l = 0; l < 4; l++) {
          if (::b[j][l] == -1 || ::b[j][l] == z) {
            dp[i + 1][j] += dp[i][j] * 0.25;
          } else {
            dp[i + 1][::b[j][l]] += dp[i][j] * 0.25;
          }
        }
      }
    }
    printf("%.10lf\n", dp[k][y]);
  }
}
