#include <iostream>
#include <algorithm>
using namespace std;

int pow(int x, int n) {
  int ans = 1;
  for(int i = 0; i < n; i++) {
    ans *= x;
  }
  return ans;
}
 
int main() {
  static int d[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
  while(1) {
    int room[3][3];
    fill(&room[0][0], &room[2][2]+1, 0);
    int n;
    cin >> n;
    if(n == 0) break;

    char s, t, b;
    cin >> s >> t >> b;
    int temp = (int)s-(int)'A';
    int sx = temp%3;
    int sy = temp/3;
    temp = (int)t-(int)'A';
    int gx = temp%3;
    int gy = temp/3;
    temp = (int)b-(int)'A';
    room[temp/3][temp%3] = 2;

    double value[3][3];
    fill(&value[0][0], &value[2][2]+1, 0);
    value[sy][sx] = 1;
    for(int t = 0; t < n; t++) {
      double newValue[3][3];
      fill(&newValue[0][0], &newValue[2][2]+1, 0);
      for(int i = 0; i < 3; i++) {
	for(int j = 0; j < 3; j++) {
	  for(int k = 0; k < 4; k++) {
	    int nx = j + d[k][0];
	    int ny = i + d[k][1];
	    if(nx < 0 || nx >= 3) nx = j;
	    if(ny < 0 || ny >= 3) ny = i;
	    if(room[ny][nx] == 2) {
	      nx = j;
	      ny = i;
	    }
	    newValue[ny][nx] += value[i][j]/4;
	  }
	}
      }
      for(int i = 0; i < 3; i++) {
	for(int j = 0; j < 3; j++) {
	  value[i][j] = newValue[i][j];
	}
      }
    }
    printf("%.8f\n", value[gy][gx]);
  }
  return 0;
}