#include <bits/stdc++.h>
using namespace std;
int xdir[4] = {0, 1, 0, -1}, ydir[4] = {1, 0, -1, 0};
int main() {
  int n, p, q;
  long double a[3][3][16];
  char s, t, b, alp[3][3];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      alp[i][j] = (char)('A' + (i * 3 + j));
    }
  }
  while (cin >> n && n) {
    cin >> s >> t >> b;
    fill(&a[0][0][0], &a[2][2][n], 0);
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (alp[i][j] == s) {
          p = i;
          q = j;
        }
      }
    }
    a[p][q][0] = 1.0;
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          for (int l = 0; l < 4; l++) {
            int dx = j + xdir[l], dy = i + ydir[l];
            if (0 <= dx && dx < 3 && 0 <= dy && dy < 3 && alp[dy][dx] != b) {
              a[dy][dx][k + 1] += a[i][j][k] / 4.0;
            } else {
              a[i][j][k + 1] += a[i][j][k] / 4.0;
            }
          }
        }
      }
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (alp[i][j] == t) {
          p = i;
          q = j;
        }
      }
    }
    printf("%.8Lf\n", a[p][q][n]);
  }
  return 0;
}
