#include <bits/stdc++.h>
int main() {
  int n, s, t, b, i, j, k, x[9][4] = {{1, 3, 0, 0}, {0, 2, 4, 1}, {1, 5, 2, 2},
                                      {0, 4, 6, 3}, {1, 3, 5, 7}, {2, 4, 8, 6},
                                      {3, 7, 0, 0}, {4, 6, 8, 7}, {5, 7, 8, 8}};
  char _[9];
  while (scanf("%d", &n), n) {
    double a[101][9] = {0};
    getchar(), fgets(_, 9, stdin);
    s = _[0] - 'A', t = _[2] - 'A', b = _[4] - 'A';
    a[0][s] = 1;
    for (i = 0; i < n; ++i)
      for (j = 0; j < 9; ++j)
        for (k = 0; k < 4; ++k)
          if (x[j][k] != b)
            a[i + 1][x[j][k]] += a[i][j];
          else
            a[i + 1][j] += a[i][j];
    printf("%.8f\n", a[n][t] / pow(4.0, n));
  }
  return 0;
}
