#include <bits/stdc++.h>
using namespace std;
char T[3], U[10] = "ABCDEFGHI";
int n, c[3][2], x[5][5];
double p[100][5][5];
void A(int E, int B, int C, double D, int F, int G) {
  if (x[B][C] == 0) {
    p[E][B][C] += D;
  } else {
    p[E][F][G] += D;
  }
}
int main() {
  while (true) {
    cin >> n;
    if (!n) {
      break;
    }
    cin >> T[0] >> T[1] >> T[2];
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 9; j++) {
        if (U[j] == (char)T[i]) {
          c[i][0] = j / 3 + 1;
          c[i][1] = j % 3 + 1;
        }
      }
    }
    p[n][c[0][0]][c[0][1]] = 1;
    for (int i = 0; i < 25; i++) {
      x[i / 5][i % 5] = 1;
    }
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        if (i != c[2][0] || j != c[2][1]) {
          x[i][j] = 0;
        }
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 1; j <= 3; j++) {
        for (int k = 1; k <= 3; k++) {
          if (p[i + 1][j][k] > 0) {
            A(i, j - 1, k, 1.0 * p[i + 1][j][k] / 4.0, j, k);
            A(i, j + 1, k, 1.0 * p[i + 1][j][k] / 4.0, j, k);
            A(i, j, k + 1, 1.0 * p[i + 1][j][k] / 4.0, j, k);
            A(i, j, k - 1, 1.0 * p[i + 1][j][k] / 4.0, j, k);
          }
        }
      }
    }
    cout << fixed << setprecision(15) << p[0][c[1][0]][c[1][1]] << endl;
  }
}
