//49
#include<iostream>
#include<iomanip>
#include<cstring>

using namespace std;

#define tox(c) (((c)-'A')%3)
#define toy(c) (((c)-'A')/3)

int main(){
  for(int n;cin>>n,n;){
    char s,t,b;
    double p[3][3]={};
    cin>>s>>t>>b;
    p[toy(s)][tox(s)]=1;
    while(n--){
      double pt[3][3]={};
      for(int y=0;y<3;y++){
	for(int x=0;x<3;x++){
	  int d[]={0,1,0,-1,0};
	  for(int i=0;i<4;i++){
	    int ny=y+d[i];
	    int nx=x+d[i+1];
	    if(0<=ny&&ny<3&&0<=nx&&nx<3&&!(ny==toy(b)&&nx==tox(b))){
	      pt[ny][nx]+=p[y][x]/4;
	    }else{
	      pt[y][x]+=p[y][x]/4;
	    }
	  }
	}
      }
      memcpy(p,pt,sizeof(p));
    }
    cout<<fixed<<setprecision(6)<<p[toy(t)][tox(t)]<<endl;
  }
  return 0;
}