#include <bits/stdc++.h>
const int INF = 1001001001;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
double dp[3][3][16];
int n, s, t, b;
int toIndex(char l) { return l - 'A'; }
double rec(int x, int y, int battery) {
  if (dp[y][x][battery] > -1e-6) {
    return dp[y][x][battery];
  }
  if (x == t % 3 && y == t / 3 && battery == n) {
    return 1.0;
  }
  if (battery >= n) {
    return 0.0;
  }
  double res = 0.0;
  for (int i = (0); i < (4); i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 &&
        !(nx == b % 3 && ny == b / 3)) {
      res += 0.25 * rec(nx, ny, battery + 1);
    } else {
      res += 0.25 * rec(x, y, battery + 1);
    }
  }
  return res;
}
int main() {
  while (std::cin >> n, n) {
    std::fill(dp[0][0], dp[0][0] + 3 * 3 * 16, -1.0);
    char sl, tl, bl;
    std::cin >> sl >> tl >> bl;
    s = toIndex(sl), t = toIndex(tl), b = toIndex(bl);
    printf("%.7f\n", rec(s % 3, s / 3, 0));
  }
}
