#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, 1, -1, 0};
int main(void) {
  int n;
  char s, t, b;
  int dp[5][5];
  int i1, i2, i3, i4;
  double ans;
  while (1) {
    cin >> n;
    if (n == 0) break;
    cin >> s >> t >> b;
    memset(dp, -1, sizeof(dp));
    for (i1 = 1; i1 <= 3; i1++) {
      for (i2 = 1; i2 <= 3; i2++) {
        dp[i1][i2] = 0;
      }
    }
    dp[((s - 'A') / 3) + 1][((s - 'A') % 3) + 1] = 1;
    dp[((b - 'A') / 3) + 1][((b - 'A') % 3) + 1] = -1;
    for (i1 = 0; i1 < n; i1++) {
      for (i2 = 1; i2 <= 3; i2++) {
        for (i3 = 1; i3 <= 3; i3++) {
          if (dp[i2][i3] == 1) {
            for (i4 = 0; i4 < 4; i4++) {
              if (dp[i2 + dy[i4]][i3 + dx[i4]] == -1)
                dp[i2][i3]++;
              else
                dp[i2 + dy[i4]][i3 + dx[i4]]++;
            }
          }
        }
      }
    }
    ans = double(dp[((t - 'A') / 3) + 1][((t - 'A') % 3) + 1]) / pow(4.0, n);
    printf("%.8lf\n", ans);
  }
  return 0;
}
