#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
const double PI = acos(-1);
int n;
char s, t, b;
pair<int, int> S, T, B;
double dp[16][3][3];
int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
int main() {
  while (cin >> n && n) {
    cin >> s >> t >> b;
    S = make_pair((s - 'A') / 3, (s - 'A') % 3);
    T = make_pair((t - 'A') / 3, (t - 'A') % 3);
    B = make_pair((b - 'A') / 3, (b - 'A') % 3);
    memset(dp, 0.0, sizeof(dp));
    dp[0][S.first][S.second] = 1;
    for (int i = 0; i < n; i++) {
      for (int y = 0; y < 3; y++) {
        for (int x = 0; x < 3; x++) {
          if (dp[i][y][x] == 0) continue;
          for (int d = 0; d < 4; d++) {
            int ny = y + dy[d], nx = x + dx[d];
            if (0 <= nx && nx < 3 && 0 <= ny && ny < 3 &&
                make_pair(ny, nx) != B)
              dp[i + 1][ny][nx] += dp[i][y][x] * 0.25;
            else
              dp[i + 1][y][x] += dp[i][y][x] * 0.25;
          }
        }
      }
    }
    cout << fixed << setprecision(10) << dp[n][T.first][T.second] << endl;
  }
}
