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