#include <iostream>
#include <cstdio>
#define BAT 1
#define SOU 2
using namespace std;

int main(){
  int n, sum;
  char s,t,b;
  int mem[20][9];

  while( cin>>n && n ){
    cin >> s>>t>>b;
    int souko = b-'A';
    int goal= t-'A';
    
    for( int i=0;i<=n;i++ )
      for( int j=0;j<9;j++ )
	mem[i][j] = 0;

    mem[0][ s-'A' ]=1;
    for( int i=0;i<n;i++ ){
      for( int j=0;j<9;j++ ){	
	if( mem[i][j]==0 ) continue;
	int k=i+1;
	// up
	if( j<3 || j-3==souko )  mem[k][j]  +=mem[i][j];
	else                     mem[k][j-3]+=mem[i][j];
	// right
	if( j%3==2 || j+1==souko )  mem[k][j] +=mem[i][j];
	else                        mem[k][j+1]+=mem[i][j];
	// down
	if( j>5 || j+3==souko )  mem[k][j] += mem[i][j];
	else                     mem[k][j+3] +=mem[i][j];
	// left
	if( j%3==0 || j-1==souko )  mem[k][j] += mem[i][j];
	else                        mem[k][j-1]+=mem[i][j];
      }
    }
    /*
    for( int i=0;i<=n;i++ ){
      cout << "n="<<i << "|"<<endl ;
      for( int j=0;j<9;j++ ){
	cout << " "<<mem[i][j];
	if( j%3==2 ) cout << endl;
      }
      cout << endl;
    }
    */
    sum=0;
    for( int i=0;i<9;i++ )
      sum += mem[n][i];
    /*
    cout << "souko==" << souko << " goal="<<goal<<endl;
    cout << "dev " << mem[n][goal] << "/"<<sum <<endl;
    */
    printf("%lf\n",(double)mem[n][goal]/sum );
  }
  return 0;
}