#include <bits/stdc++.h>
char from, battery, to;
double prob[16][16];
double getProb(int lim, int start) {
  int i;
  double ans;
  double possi;
  if (prob[lim][start] >= 0) {
    return (prob[lim][start]);
  }
  if (start == battery && lim == 0) {
    return (1.0);
  }
  if (lim == 0) {
    return (0);
  }
  ans = 0.0;
  if ((start + 1) % 3 && start + 1 != to) {
    ans += 0.25 * getProb(lim - 1, start + 1);
  } else {
    ans += 0.25 * getProb(lim - 1, start);
  }
  if (start % 3 && start - 1 != to) {
    ans += 0.25 * getProb(lim - 1, start - 1);
  } else {
    ans += 0.25 * getProb(lim - 1, start);
  }
  if (start + 3 < 9 && start + 3 != to) {
    ans += 0.25 * getProb(lim - 1, start + 3);
  } else {
    ans += 0.25 * getProb(lim - 1, start);
  }
  if (start - 3 >= 0 && start - 3 != to) {
    ans += 0.25 * getProb(lim - 1, start - 3);
  } else {
    ans += 0.25 * getProb(lim - 1, start);
  }
  return (prob[lim][start] = ans);
}
int main(void) {
  int left;
  int i, j;
  while (scanf("%d", &left) && left) {
    getchar();
    scanf("%c %c %c", &from, &battery, &to);
    from -= 'A';
    battery -= 'A';
    to -= 'A';
    for (i = 0; i < 16; i++) {
      for (j = 0; j < 16; j++) {
        prob[i][j] = -1;
      }
    }
    printf("%.8lf\n", getProb(left, from));
  }
  return (0);
}
