#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int main(void) {
  int n, id = 0;
  string idx = "ABCDEFGHI";
  char second, t, d;
  map<char, pair<int, int> > mp;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      mp[idx[id++]] = pair<int, int>(j, i);
    }
  }
  while (cin >> n, n) {
    cin >> second >> t >> d;
    pair<int, int> S, T, D;
    S.first = mp[second].first, S.second = mp[second].second;
    T.first = mp[t].first, T.second = mp[t].second;
    D.first = mp[d].first, D.second = mp[d].second;
    double dp[16][3][3];
    for (int k = 0; k < 16; k++)
      for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) dp[k][i][j] = 0;
    dp[0][S.second][S.first] = 1;
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          for (int l = 0; l < 4; l++) {
            int nx = j + dx[l], ny = i + dy[l];
            if (nx < 0 || ny < 0 || 2 < nx || 2 < ny ||
                (nx == D.second && ny == D.first))
              nx = j, ny = i;
            dp[k + 1][ny][nx] += dp[k][i][j] / 4;
          }
        }
      }
    }
    printf("%.8f", dp[n][T.second][T.first]);
    cout << endl;
  }
  return 0;
}
