#include <bits/stdc++.h>
using namespace std;
;
int n, out, can, dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
int gox, goy, mox, moy, stx, sty;
void func(int y, int x, int bat) {
  if (bat == 0) {
    if (y == goy && x == gox) can++;
    out++;
    return;
  }
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    ny = dy[i] + y;
    nx = dx[i] + x;
    if (!(ny >= 0 && ny < 3 && nx >= 0 && nx < 3) || (nx == mox && ny == moy))
      func(y, x, bat - 1);
    else
      func(ny, nx, bat - 1);
  }
}
int main() {
  while (cin >> n, n) {
    char a[3];
    can = 0;
    out = 0;
    int sx[3], sy[3];
    cin >> a[0] >> a[1] >> a[2];
    for (int i = 0; i < 3; i++) {
      if (a[i] == 'A') {
        a[i] = 0;
        sx[i] = 0;
      } else if (a[i] == 'B') {
        sy[i] = 0;
        sx[i] = 1;
      } else if (a[i] == 'C') {
        sy[i] = 0;
        sx[i] = 2;
      } else if (a[i] == 'D') {
        sy[i] = 1;
        sx[i] = 0;
      } else if (a[i] == 'E') {
        sy[i] = 1;
        sx[i] = 1;
      } else if (a[i] == 'F') {
        sy[i] = 1;
        sx[i] = 2;
      } else if (a[i] == 'G') {
        sy[i] = 2;
        sx[i] = 0;
      } else if (a[i] == 'H') {
        sy[i] = 2;
        sx[i] = 1;
      } else if (a[i] == 'I') {
        sy[i] = 2;
        sx[i] = 2;
      }
    }
    sty = sy[0];
    stx = sx[0];
    goy = sy[1];
    gox = sx[1];
    moy = sy[2];
    mox = sx[2];
    func(sty, stx, n);
    printf("%.8lf\n", (double)can / out);
  }
  return 0;
}
