#include <bits/stdc++.h>
using namespace std;
int ans;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void solve(char x, char y, char n, char room[3][3]) {
  if (n == 0) {
    if (room[y][x] == 2) {
      ans++;
    }
    return;
  } else {
    for (int i = 0; i < 4; i++) {
      int mx = x + dx[i];
      int my = y + dy[i];
      if (mx == -1) mx = 0;
      if (mx == 3) mx = 2;
      if (my == -1) my = 0;
      if (my == 3) my = 2;
      if (room[my][mx] == 1) {
        mx -= dx[i];
        my -= dy[i];
      }
      solve(mx, my, n - 1, room);
    }
  }
}
int main() {
  char fx[256] = {0}, fy[256] = {0};
  char n, s, t, b;
  for (int i = 0; i < 9; i++) {
    fx[i + 'A'] = i % 3;
    fy[i + 'A'] = i / 3;
  }
  while (scanf("%d", &n), n) {
    scanf(" %c %c %c", &s, &t, &b);
    char room[3][3] = {0};
    char x = fx[s];
    char y = fy[s];
    room[fy[t]][fx[t]] = 2;
    room[fy[b]][fx[b]] = 1;
    ans = 0;
    if (n % 2 == 1 && (y * 3 + x) % 2 == (fy[t] * 3 + fx[t]) % 2) {
      ;
    } else if (n % 2 == 0 && (y * 3 + x) % 2 != (fy[t] * 3 + fx[t]) % 2) {
      ;
    } else {
      solve(x, y, n, room);
    }
    printf("%.8f\n", ans / pow(4, n));
  }
}
