#include <bits/stdc++.h>
using namespace std;
int n, b1, b2, b3, dr[4] = {-3, -1, 1, 3};
char c1, c2, c3;
double dp[19][9];
int main() {
  while (cin >> n, n) {
    cin >> c1 >> c2 >> c3;
    b1 = c1 - 65, b2 = c2 - 65, b3 = c3 - 65;
    memset(dp, 0, sizeof(dp));
    dp[0][b1] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 9; j++) {
        int r[4] = {0}, c = 0;
        for (int k = 0; k < 4; k++) {
          int u = j + dr[k];
          if (u < 0 || u > 8 || j % 3 + u % 3 == 2 || u == b3) continue;
          r[c++] = u + 1;
        }
        for (int k = 0; k < c; k++) {
          dp[i][r[k] - 1] += dp[i - 1][j] / c;
        }
      }
    }
    cout << fixed << setprecision(15) << dp[n][b2] << endl;
  }
}
