#include <bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int n, s, t, b, sum = 0;
    int ai[16][3][3] = {0};
    char a, d, c;
    scanf("%d", &n);
    if (n == 0) break;
    cin >> a >> d >> c;
    s = a - 'A';
    t = d - 'A';
    b = c - 'A';
    ai[0][s / 3][s % 3] = 1;
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < 3; ++j) {
        for (int l = 0; l < 3; ++l) {
          if (j - 1 >= 0 && (j - 1) * 3 + l != b)
            ai[i][j - 1][l] += ai[i - 1][j][l];
          else
            ai[i][j][l] += ai[i - 1][j][l];
          if (j + 1 < 3 && (j - 1) * 3 + l != b)
            ai[i][j + 1][l] += ai[i - 1][j][l];
          else
            ai[i][j][l] += ai[i - 1][j][l];
          if (l - 1 >= 0 && j * 3 + l - 1 != b)
            ai[i][j][l - 1] += ai[i - 1][j][l];
          else
            ai[i][j][l] += ai[i - 1][j][l];
          if (l + 1 < 3 && j * 3 + l + 1 != b)
            ai[i][j][l + 1] += ai[i - 1][j][l];
          else
            ai[i][j][l] += ai[i - 1][j][l];
        }
      }
    }
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        sum += ai[n][i][j];
      }
    }
    printf("%f\n", (double)ai[n][t / 3][t % 3] / sum);
  }
}
