#include <bits/stdc++.h>
using namespace std;
int grd[3][3];
double dp[3][3][20];
int mx[] = {-1, 1, 0, 0}, my[] = {0, 0, -1, 1};
char abc[3][4] = {"ABC", "DEF", "GHI"};
double dfs(int x, int y, int n) {
  if (dp[y][x][n] != 0) return dp[y][x][n];
  if (n == 0) {
    if (grd[x][y] == 1)
      return dp[y][x][n] = 1.0;
    else
      return dp[y][x][n] = 0.0;
  }
  double ret = 0.0;
  for (int i = 0; i < 4; i++) {
    int cx = x + mx[i], cy = y + my[i];
    if (cx < 0 || 2 < cx || cy < 0 || 2 < cy) continue;
    if (grd[cy][cx] == 2)
      ret += dfs(x, y, n - 1) * 0.25;
    else
      ret += dfs(cx, cy, n - 1) * 0.25;
  }
  return dp[y][x][n] = ret;
}
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) return 0;
    char s, t, b;
    scanf(" %c %c %c", &s, &t, &b);
    memset(grd, 0, sizeof(grd));
    memset(dp, 0, sizeof(dp));
    int sx, sy;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (abc[i][j] == s) {
          sx = j;
          sy = i;
        } else if (abc[i][j] == t)
          grd[i][j] = 1;
        else if (abc[i][j] == b)
          grd[i][j] = 2;
      }
    }
    printf("%.15lf\n", dfs(sx, sy, n));
  }
}
