#include <bits/stdc++.h>
using namespace std;
char fie[4][4] = {"ABC", "DEF", "GHI"};
int N;
char s, t, b;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
double dp[16][4][4];
int used[16][4][4];
double solve(int no, int x, int y) {
  if (no == N) {
    if (fie[x][y] == t) return 1.0f;
    return 0.0f;
  }
  if (used[no][x][y]) return dp[no][x][y];
  double ret = 0.0f;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || ny < 0 || nx >= 3 || ny >= 3 || fie[ny][nx] == b) {
      ret += solve(no + 1, x, y) / 4.0f;
    } else {
      ret += solve(no + 1, nx, ny) / 4.0f;
    }
  }
  used[no][x][y] = 1;
  return dp[no][x][y] = ret;
}
int main() {
  while (1) {
    int x, y;
    cin >> N;
    if (!N) break;
    memset(dp, 0, sizeof(dp));
    memset(used, 0, sizeof(used));
    cin >> s >> t >> b;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (fie[i][j] == s) {
          x = j;
          y = i;
        }
    printf("%.9lf\n", solve(0, x, y));
  }
}
