#include <bits/stdc++.h>
using namespace std;
int room[3][3];
int ok;
int all;
void rec(int x, int y, int but) {
  static int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  if (but == 0) {
    if (room[y][x] == 1) ok++;
    all++;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + d[i][0];
    int ny = y + d[i][1];
    if (nx < 0 || nx >= 3)
      rec(x, y, but - 1);
    else if (ny < 0 || nx >= 3)
      rec(x, y, but - 1);
    else if (room[ny][nx] == 2)
      rec(x, y, but - 1);
    else
      rec(nx, ny, but - 1);
  }
}
int main() {
  while (1) {
    fill(&room[0][0], &room[2][2] + 1, 0);
    int n;
    cin >> n;
    if (n == 0) break;
    char s, t, b;
    cin >> s >> t >> b;
    int temp = (int)s - (int)'A';
    int sx = temp % 3;
    int sy = temp / 3;
    temp = (int)t - (int)'A';
    room[temp / 3][temp % 3] = 1;
    temp = (int)b - (int)'A';
    room[temp / 3][temp % 3] = 2;
    ok = all = 0;
    rec(sx, sy, n);
    printf("%.8f\n", (double)ok / all);
  }
  return 0;
}
