#include <bits/stdc++.h>
using namespace std;
int dp[16][9];
const int m[] = {-3, -1, 1, 3};
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) return 0;
    char s, t, b;
    cin >> s >> t >> b;
    int ss = s - 'A', bb = b - 'A', tt = t - 'A';
    memset(dp, 0, sizeof(dp));
    dp[0][tt] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 9; j++) {
        if (dp[i][j] == 0) continue;
        for (int k = 0; k < 4; k++) {
          if (j + m[k] < 0 || 9 <= j + m[k])
            dp[i + 1][j] += dp[i][j];
          else if (bb == j + m[k])
            dp[i + 1][j] += dp[i][j];
          else
            dp[i + 1][j + m[k]] += dp[i][j];
        }
      }
    }
    printf("%.15lf\n", (double)dp[n][ss] / pow(4, n));
  }
}
