#include <bits/stdc++.h>
using namespace std;
double dp[16][9];
int main() {
  int n;
  char s, t, b;
  while (scanf("%d", &n), n) {
    scanf(" %c %c %c", &s, &t, &b);
    memset(dp, 0, sizeof(dp));
    dp[0][s - 'A'] = 1.0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 9; j++) {
        if (j - 3 >= 0 && b - 'A' != j - 3)
          dp[i + 1][j - 3] += dp[i][j] / 4.0;
        else
          dp[i + 1][j] += dp[i][j] / 4.0;
        if (j - 1 >= 0 && b - 'A' != j - 1)
          dp[i + 1][j - 1] += dp[i][j] / 4.0;
        else
          dp[i + 1][j] += dp[i][j] / 4.0;
        if (j + 1 < 9 && b - 'A' != j + 1)
          dp[i + 1][j + 1] += dp[i][j] / 4.0;
        else
          dp[i + 1][j] += dp[i][j] / 4.0;
        if (j + 3 < 9 && b - 'A' != j + 3)
          dp[i + 1][j + 3] += dp[i][j] / 4.0;
        else
          dp[i + 1][j] += dp[i][j] / 4.0;
      }
    }
    printf("%.7lf\n", dp[n][t - 'A']);
  }
  return 0;
}
