#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;
void simulate(int cx, int cy, int e, int& trial, int& success) {
  if (e == 0) {
    if (room[cx + cy * 3] == t) success++;
    trial++;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int xx = cx + off[i][0];
    int yy = cy + off[i][1];
    int index = xx + yy * 3;
    if (room[index] == b || xx < 0 || yy < 0 || xx >= 3 || yy >= 3)
      simulate(x, y, e - 1, trial, success);
    else
      simulate(xx, yy, e - 1, trial, success);
  }
}
double solve() {
  int trial = 0;
  int success = 0;
  simulate(x, y, n, trial, success);
  return (double)success / (double)trial;
}
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;
}
