#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int n;
double dp[20][3][3];
int dx[] = {-1,1,0,0};
int dy[] = {0,0,1,-1};

int main(){
  while(cin>>n,n){
    char a,b,c;
    cin>>a>>b>>c;
    int sx = (a - 'A') % 3;
    int sy = (a - 'A') / 3;
    int gx = (b - 'A') % 3;
    int gy = (b - 'A') / 3;
    int tx = (c - 'A') % 3;
    int ty = (c - 'A') / 3;

    memset(dp,0,sizeof(dp));
    dp[0][sx][sy] = 1;

    rep(i,n){
      rep(j,3){
        rep(k,3){
          if(dp[i][j][k] == 0) continue;
          rep(d,4){
            int nx = j + dx[d];
            int ny = k + dy[d];
            if(3>nx && nx>=0 && 3>ny && ny>=0 && (nx!=tx || ny!=ty)){
              dp[i+1][nx][ny] += dp[i][j][k] / 4;
            }
            else{
              dp[i+1][j][k] += dp[i][j][k] / 4;
            }
          }
        }
      }
    }

    printf("%.8f\n",dp[n][gx][gy]);
  }
}