#include <bits/stdc++.h>
using namespace std;
double dp[3][3][16];
char map[3][3] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
int main() {
  int n;
  char s, t, d;
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  while (true) {
    cin >> n;
    if (n == 0) break;
    cin >> s >> t >> d;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k <= n; k++) dp[i][j][k] = 0.0;
        if (map[i][j] == s) dp[i][j][0] = 1.0;
      }
    }
    for (int k = 1; k <= n; k++) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          for (int l = 0; l < 4; l++) {
            if (j + dx[l] < 0 || j + dx[l] == 3 || i + dy[l] < 0 ||
                i + dy[l] == 3)
              dp[i][j][k] += dp[i][j][k - 1];
            else {
              if (map[j + dx[l]][i + dy[l]] == d)
                dp[i][j][k] += dp[i][j][k - 1];
              else
                dp[i][j][k] += dp[i + dy[l]][j + dx[l]][k - 1];
            }
          }
          dp[i][j][k] /= 4.0;
        }
      }
    }
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (map[i][j] == t)
          cout << fixed << setprecision(10) << dp[i][j][n] << endl;
  }
  return 0;
}
