#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define pii pair<int,int>

int main() {
  int n;
  int dx[]={0,1,0,-1};
  int dy[]={-1,0,1,0};
  
  while(cin >> n, n) {
    char cs, ct, cb;
    cin >> cs >> ct >> cb;
    int s=cs-'A', t=ct-'A', b=cb-'A';
    int sx = s%3, sy = s/3;
    int tx = t%3, ty = t/3;
    int bx = b%3, by = b/3;

    double dp[3][3][n+1];

    for (int i=0; i<3;i++)
      for(int j=0; j<3; j++)
        for(int k=0; k<=n; k++)
          dp[i][j][k] = 0;
    
    dp[sx][sy][0] = 1;
    double p = 1;
    int dx[] = {0,1,0,-1};
    int dy[] = {-1,0,1,0};
    
    for (int i=1; i<=n; i++) {
      for (int x=0; x<3; x++) {
        for (int y=0; y<3; y++) {
          if (x==bx&&y==by) continue;
          for(int j=0; j<4; j++) {
            int xx = x+dx[j];
            int yy = y+dy[j];
            if (xx<0||3<=xx || yy<0||3<=yy || (xx==bx&&yy==by)) {
              xx = x;
              yy = y;
            }
            dp[x][y][i] += dp[xx][yy][i-1] * 0.25;
          }
        }
      }
    }

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