#include <bits/stdc++.h>
using namespace std;
pair<int, int> get_pos(int x) { return pair<int, int>(x / 3, x % 3); }
bool inField(int x, int y) { return (0 <= x && x < 3 && 0 <= y && y < 3); }
int main() {
  int N;
  while (cin >> N, N) {
    string S = "ABCDEFGHI";
    char a[3];
    for (int i = 0; i < 3; i++) {
      cin >> a[i];
    }
    int s = S.find(a[0]);
    int t = S.find(a[1]);
    int b = S.find(a[2]);
    double dp[25][9] = {{}};
    pair<int, int> p = get_pos(b);
    dp[0][s] = 1;
    const int di[] = {-1, +0, +1, +0};
    const int dj[] = {+0, -1, +0, +1};
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 9; j++) {
        if (dp[i][j] == 0) continue;
        pair<int, int> n = get_pos(j);
        for (int k = 0; k < 4; k++) {
          int ni = n.first + di[k];
          int nj = n.second + dj[k];
          if (!inField(nj, ni) || (pair<int, int>(ni, nj) == p)) {
            dp[i + 1][j] += dp[i][j];
          } else {
            dp[i + 1][ni * 3 + nj] += dp[i][j] / 4;
          }
        }
      }
    }
    printf("%.10f\n", dp[N][t]);
  }
  return 0;
}
