#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-8;
const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};
char stage[3][3];
int gAll = 0, gOk = 0;
int gCount[3][3][16];
void dfs(int sx, int sy, int gx, int gy, int cost) {
  if (cost == 0) {
    if (sx == gx && sy == gy) {
      gOk++;
    }
    return;
  }
  for (int i = 0; i < 4; i++) {
    int dx = sx + tx[i];
    int dy = sy + ty[i];
    if (dx < 0 || dx >= 3 || dy < 0 || dy >= 3) {
      gCount[sy][sx][cost - 1]++;
      if (gCount[sy][sx][cost - 1] <= 1) dfs(sx, sy, gx, gy, cost - 1);
    } else if (stage[dy][dx] != 'B') {
      gCount[dy][dx][cost - 1]++;
      if (gCount[dy][dx][cost - 1] <= 1) dfs(dx, dy, gx, gy, cost - 1);
    } else if (stage[dy][dx] == 'B') {
      gCount[sy][sx][cost - 1]++;
      if (gCount[sy][sx][cost - 1] <= 1) dfs(sx, sy, gx, gy, cost - 1);
    }
  }
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0) break;
    char s, t, b;
    memset(stage, '.', sizeof(stage));
    memset(gCount, 0, sizeof(gCount));
    cin >> s >> t >> b;
    int sx = (s - 'A') % 3;
    int sy = (s - 'A') / 3;
    int gx = (t - 'A') % 3;
    int gy = (t - 'A') / 3;
    stage[(b - 'A') / 3][(b - 'A') % 3] = 'B';
    gOk = 0;
    gAll = 0;
    dfs(sx, sy, gx, gy, n);
    for (int x = 0; x < 3; x++) {
      for (int y = 0; y < 3; y++) {
        gAll += gCount[y][x][0];
      }
    }
    printf("%lf\n", (double)gOk / (double)gAll);
  }
}
