#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  while (cin >> N, N) {
    char s, t, b;
    cin >> s >> t >> b;
    int sx, sy;
    sx = (s - 'A') / 3;
    sy = (s - 'A') % 3;
    int tx, ty;
    tx = (t - 'A') / 3;
    ty = (t - 'A') % 3;
    int bx, by;
    bx = (b - 'A') / 3;
    by = (b - 'A') % 3;
    vector<vector<vector<int> > > dp(
        N + 1, vector<vector<int> >(3, vector<int>(3, 0)));
    dp[0][sx][sy] = 1;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < N; i++) {
      for (int x = 0; x < 3; x++) {
        for (int y = 0; y < 3; y++) {
          if (!dp[i][x][y]) continue;
          if (x == tx && y == ty) continue;
          if (x == bx && y == by) continue;
          for (int j = 0; j < 4; j++) {
            int nx = x + dx[j];
            int ny = y + dy[j];
            if (0 <= nx && nx < 3 && 0 <= ny && ny < 3) {
              dp[i + 1][nx][ny] += dp[i][x][y];
            }
          }
        }
      }
    }
    double ans = 0.;
    double p = 1. / 4.;
    for (int i = 1; i <= N; i++) {
      ans += p * dp[i][tx][ty];
      p /= 4.;
    }
    printf("%0.9f\n", ans);
  }
  return 0;
}
