#include<iostream>
#include<queue>
#define N 3
#define MAX 16
using namespace std;

double posib[N][N][MAX];
static const int di[] = {-1,0,1,0};
static const int dj[] = {0,1,0,-1};

bool isinside( int i, int j ){
  return 0<=i&&i<N && 0<=j&&j<N;
}

int main()
{
  while(true){
    int n;
    char s,t,b;
    int si,sj,ti,tj,bi,bj;

    cin >> n >> s >> t >> b;
    if( n == 0 ) break;
    // cout << s << ' ' << t << ' ' << b << endl;
    s -= 'A'; t -= 'A'; b -= 'A';
    si = s / N; sj = s % N;
    ti = t / N; tj = t % N;
    bi = b / N; bj = b % N;

    for(int i = 0; i < N; ++i){
      for(int j = 0; j < N; ++j){
	for(int k = 0; k < MAX; ++k){
	  posib[i][j][k] = 0.0;
	}
      }
    }
    posib[ti][tj][0] = 1.0;
    posib[bi][bj][0] = 0.0;

    for(int k = 1; k < MAX; ++k){
      //cout << "K = " << k << endl;
      for(int i = 0; i < N; ++i){
	for(int j = 0; j < N; ++j){

	  if( i == bi && j == bj ) continue;

	  int cnt = 4;

	  for(int l = 0; l < sizeof(di)/sizeof(*di); ++l){
	    int li = i+di[l];
	    int lj = j+dj[l];

	    if( li == bi && lj == bj || !isinside( li, lj ) ){
	      posib[i][j][k] += posib[i][j][k-1] / (double)cnt;
	    }else if( isinside( li, lj ) ){
	      posib[i][j][k] += posib[li][lj][k-1] / (double)cnt;
	    }

	  }
	  
	  //cout << posib[i][j][k] << "(CNT:"<<cnt<<") ";
	}
	//cout << endl;
      }
      //cout << endl;
    }
    printf("%.6lf\n", posib[si][sj][n]);
    //return 0;
  }
  return 0;
}