#include <bits/stdc++.h>
using namespace std;
int n;
char room[10] = "ABCDEFGHI";
char s, t, b;
int off[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int x, y;
double table[3][3];
double solve() {
  memset(&table[0][0], 0, sizeof(double) * 9);
  table[x][y] = 1.0;
  for (int k = 0; k < n; k++) {
    double temp[3][3];
    memset(&temp[0][0], 0, sizeof(double) * 9);
    for (int j = 0; j < 3; j++) {
      for (int i = 0; i < 3; i++) {
        if (table[j][i] != 0.0) {
          for (int m = 0; m < 4; m++) {
            int xx = i + off[m][0];
            int yy = j + off[m][1];
            int index = xx + yy * 3;
            xx = (xx >= 0 && xx < 3 && room[index] != b) ? xx : i;
            yy = (yy >= 0 && yy < 3 && room[index] != b) ? yy : j;
            temp[yy][xx] += table[j][i] / 4.0;
          }
        }
      }
    }
    memcpy(&table[0][0], &temp[0][0], sizeof(double) * 9);
  }
  for (int i = 0; i < 9; i++) {
    if (room[i] == t) return table[i / 3][i % 3];
  }
  return 0.0;
}
int main() {
  while (1) {
    cin >> n;
    if (n == 0) break;
    cin >> s >> t >> b;
    for (int i = 0; i < 9; i++) {
      if (room[i] == s) {
        x = i % 3;
        y = i / 3;
      }
    }
    printf("%.8f\n", solve());
  }
  return 0;
}
