#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    long double room[3][3][n + 1];
    memset(room, 0, sizeof(room));
    char s, t, b;
    cin >> s >> t >> b;
    int y = (s - 'A') / 3;
    int x = (s - 'A') % 3;
    int by = (b - 'A') / 3;
    int bx = (b - 'A') % 3;
    int gy = (t - 'A') / 3;
    int gx = (t - 'A') % 3;
    room[y][x][0] = 1.0;
    int dx[] = {-1, 0, 0, 1};
    int dy[] = {0, -1, 1, 0};
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < 3; k++) {
        for (int l = 0; l < 3; l++) {
          for (int j = 0; j < 4; j++) {
            int nx = l + dx[j];
            int ny = k + dy[j];
            if (nx < 0 || 2 < nx || ny < 0 || 2 < ny || (bx == l && by == k)) {
              room[k][l][i + 1] += room[k][l][i] * 0.25;
            } else {
              room[ny][nx][i + 1] += room[k][l][i] * 0.25;
            }
          }
        }
      }
    }
    cout.precision(9);
    cout << fixed << room[gy][gx][n] << endl;
  }
  return 0;
}
