#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
int sx, sy, gx, gy, n, bx, by;
double P[3][3][20];
bool check(int x, int y) { return (0 <= x && x < 3 && 0 <= y && y < 3); }
void solve() {
  P[sy][sx][n] = 1;
  queue<int> qx, qy, rem;
  qx.push(sx), qy.push(sy), rem.push(n);
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    int y = qy.front();
    qy.pop();
    int r = rem.front();
    rem.pop();
    if (r == 0) continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (!check(nx, ny) || (nx == bx && ny == by)) {
        P[y][x][r - 1] += P[y][x][r] / 4;
        qx.push(x), qy.push(y), rem.push(r - 1);
      } else if (check(nx, ny)) {
        P[ny][nx][r - 1] += P[y][x][r] / 4;
        qx.push(nx), qy.push(ny), rem.push(r - 1);
      }
    }
  }
  printf("%.8f\n", P[gy][gx][0]);
}
int main() {
  char s, t, b;
  while (cin >> n, n) {
    memset(P, 0, sizeof(P));
    cin >> s >> t >> b;
    int ss = s - 'A', tt = t - 'A', bb = b - 'A';
    sx = ss % 3;
    sy = ss / 3;
    gx = tt % 3;
    gy = tt / 3;
    bx = bb % 3;
    by = bb / 3;
    solve();
  }
  return 0;
}
