#include <bits/stdc++.h>
using namespace std;
int tx, ty, bx, by;
int tox(char c) { return (c - 'A') % 3; }
int toy(char c) { return (c - 'A') / 3; }
double cal(int r, int y, int x, double p) {
  if (y == by && x == bx) {
    return 0;
  } else if (r == 0) {
    return (y == ty && x == tx) ? p : 0;
  } else {
    double a = 0;
    for (int i = 0; i < 5; i++) {
      int d[] = {0, 1, 0, -1, 0};
      int nx = x + d[i];
      int ny = y + d[i + 1];
      if (0 <= nx && nx < 3 && 0 <= ny && ny < 3) {
        a += cal(r - 1, ny, nx, p / 4);
      }
    }
    return a;
  }
}
int main() {
  for (int n; cin >> n, n;) {
    char s, t, b;
    cin >> s >> t >> b;
    tx = tox(t);
    ty = toy(t);
    bx = tox(b);
    by = toy(b);
    cout << fixed << setprecision(6) << cal(n, toy(s), tox(s), 1) << endl;
  }
  return 0;
}
