#include <bits/stdc++.h>
using namespace std;
template <typename T>
void FILL_(void* ptr, size_t size, T value) {
  std::fill((T*)ptr, (T*)ptr + size, value);
}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
double dp[16][9];
int dir[4] = {1, 3, -1, -3};
int main() {
  int n;
  while (cin >> n, n) {
    fill((int*)dp, (int*)(dp + (sizeof(dp) / sizeof(*(dp)))), 0);
    char temp;
    int s, t, b, x, y;
    cin >> temp;
    s = temp - 'A';
    cin >> temp;
    t = temp - 'A';
    cin >> temp;
    b = temp - 'A';
    dp[0][s] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 9; j++) {
        if (dp[i][j] == 0) continue;
        x = j % 3;
        y = j / 3;
        for (int k = 0; k < 4; k++) {
          if (0 <= x + dx[k] && x + dx[k] < 3 && 0 <= y + dy[k] &&
              y + dy[k] < 3 && j + dir[k] != b)
            dp[i + 1][j + dir[k]] += dp[i][j] * 0.25;
          else
            dp[i + 1][j] += dp[i][j] * 0.25;
        }
      }
    }
    printf("%.8f", dp[n][t]);
  }
  return 0;
}
