#include <bits/stdc++.h>
double data[3][3];
int cost[3][3];
int sx, sy, tx, ty, bx, by;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void bfs() {
  std::queue<std::pair<int, int> > que;
  que.push(std::pair<int, int>(sx, sy));
  while (!que.empty()) {
    std::pair<int, int> p = que.front();
    que.pop();
    if (cost[p.second][p.first] > 0) {
      for (int i = 0; i < 4; i++) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];
        if (0 <= nx && nx < 3 && 0 <= ny && ny < 3) {
          data[ny][nx] += data[p.second][p.first] / 4;
          cost[ny][nx] = cost[p.second][p.first] - 1;
          if (nx == bx && ny == by) nx = p.first, ny = p.second;
          que.push(std::pair<int, int>(nx, ny));
        }
      }
      cost[p.second][p.first]--;
    }
  }
}
int main() {
  int n;
  while (std::cin >> n, n) {
    memset(data, 0, sizeof(data));
    memset(cost, 0, sizeof(cost));
    char s, t, b;
    std::cin >> s >> t >> b;
    sx = (s - 'A') % 3;
    sy = (s - 'A') / 3;
    tx = (t - 'A') % 3;
    ty = (t - 'A') / 3;
    bx = (b - 'A') % 3;
    by = (b - 'A') / 3;
    data[sy][sx] = 1;
    cost[sy][sx] = n;
    bfs();
    printf("%.8f\n", data[ty][tx]);
  }
  return 0;
}
