#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int main() {
  int n;
  char s, t, b;
  while (cin >> n, n) {
    cin >> s >> t >> b;
    double dp[16][3][3] = {};
    int sn = s - 'A', tn = t - 'A', bn = b - 'A';
    dp[0][sn / 3][sn % 3] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 4; l++) {
            int x = j + dx[l], y = k + dy[l];
            if (x >= 0 && x < 3 && y >= 0 && y < 3 && x * 3 + y != bn) {
              dp[i + 1][x][y] += dp[i][j][k] / 4.0;
            }
          }
        }
      }
    }
    printf("%.9f\n", dp[n][tn / 3][tn % 3]);
  }
  return 0;
}
