#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>
#include<vector>
#include<map>
#include<iomanip>
using namespace std;
#define fr first
#define sc second
typedef pair<int,int> Pt;
int main(){
  map<char,Pt> tmp;
  for(int i = 0 ; i < 9 ; i++ ) tmp['A' + i] = Pt( i / 3 , i % 3 );

  char s,t,b;
  Pt st,gt,bt;
  int n,d[]={0,1,0,-1};
  while(cin >> n && n){
    double dp[16][4][4] = {{{}}};
    cin >> s >> t >> b;
    st = tmp[s] , gt = tmp[t] , bt = tmp[b];
    dp[0][st.fr][st.sc] = 1.0;
    for(int i = 0 ; i < n ; i++ ){
      for(int x = 0 ; x < 3 ; x++ ){
        for(int y = 0 ; y < 3 ; y++ ){
          for(int j = 0 ; j < 4 ; j++ ){
            int px = x + d[j] , py = y + d[3-j];
            if( px < 0 || px > 2 || py < 0 || py > 2 || bt == Pt(px,py)){
              px = x , py = y;
            }
            dp[i+1][px][py] += dp[i][x][y] / 4.0;
          }
        }
      }
    }
    cout << fixed << setprecision(8) << dp[n][gt.fr][gt.sc] << endl;
  }
}