#include <bits/stdc++.h>
using namespace std;
long double dp[15][3][3] = {};
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int bx, by;
int tx, ty;
long double rec(int rest, int x, int y) {
  if (x < 0 || x > 2 || y < 0 || y > 2) return 0;
  if (dp[rest][x][y] > 0) return dp[rest][x][y];
  if (rest == 0) {
    if (x == tx && y == ty)
      return 1;
    else
      return 0;
  }
  long double sum = 0;
  for (int i = 0; i < 4; i++) {
    if (((x + dx[i] == bx) && (y + dy[i] == by)) || x + dx[i] < 0 ||
        x + dx[i] > 2 || y + dy[i] > 2 || y + dy[i] < 0) {
      sum += rec(rest - 1, x, y);
    } else {
      sum += rec(rest - 1, x + dx[i], y + dy[i]);
    }
  }
  sum = sum / 4.0;
  dp[rest][x][y] = sum;
  return sum;
}
void topoint(char a, int &x, int &y) {
  int tmp = a - 'A';
  x = tmp / 3;
  y = tmp % 3;
  return;
}
int main() {
  int N;
  cout.precision(8);
  cout.setf(ios::fixed);
  while (cin >> N, N) {
    char s, t, b;
    cin >> s >> t >> b;
    int sx, sy;
    topoint(s, sx, sy);
    topoint(t, tx, ty);
    topoint(b, bx, by);
    cout << rec(N, sx, sy) << endl;
    for (int i = 0; i < 15; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          dp[i][j][k] = 0;
        }
      }
    }
  }
  return 0;
}
