#include <bits/stdc++.h>
using namespace std;
int able[9][4];
char t;
int acc, fal;
void solve(int battery, char room, int dst) {
  if (able[room - 'A'][dst]) {
    if (dst == 0)
      room -= 3;
    else if (dst == 1)
      ++room;
    else if (dst == 2)
      room += 3;
    else
      --room;
  }
  --battery;
  if (battery == 0) {
    if (room == t)
      ++acc;
    else
      ++fal;
  } else {
    for (int i = 0; i < 4; ++i) solve(battery, room, i);
  }
}
int main() {
  int n;
  char s;
  while (scanf("%d", &n), n) {
    char b;
    scanf(" %c %c %c", &s, &t, &b);
    acc = fal = 0;
    for (int i = 0; i < 9; ++i) {
      for (int j = 0; j < 4; ++j) able[i][j] = 1;
      if (i < 3) able[i][0] = 0;
      if (i > 5) able[i][2] = 0;
      if (i % 3 == 0) able[i][3] = 0;
      if (i % 3 == 2) able[i][1] = 0;
    }
    b -= 'A';
    if (b >= 3) able[b - 3][2] = 0;
    if (b <= 5) able[b + 3][0] = 0;
    if (b % 3 != 0) able[b - 1][1] = 0;
    if (b % 3 != 2) able[b + 1][3] = 0;
    for (int i = 0; i < 4; ++i) solve(n, s, i);
    printf("%lf\n", (double)acc / (acc + fal));
  }
  return 0;
}
