#include <bits/stdc++.h>

using namespace std;

const int dx[] = {-1, 0, 0, 1};
const int dy[] = { 0,-1, 1, 0};

int sx, sy, gx, gy, n, bx, by;
double P[3][3][20];

bool check(int x, int y){
  return (0 <= x && x < 3 && 0 <= y && y < 3);
}

void solve(){
  P[sy][sx][n] = 1;

  while(n){
    for(int i = 0 ; i < 3 ; i++){
      for(int j = 0 ; j < 3 ; j++){
        for(int k = 0 ; k < 4 ; k++){
          int nx = i + dx[k];
          int ny = j + dy[k];
          if(!check(nx, ny) || (bx == nx && by == ny)){
            P[j][i][n-1] += P[j][i][n] / 4;
          }else if(check(nx, ny)){
            P[ny][nx][n-1] += P[j][i][n] / 4;
          }
        }
      }
    }
    n--;
  }

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

int main(){
  char s, t, b;
  
  while(cin >> n, n){
    memset(P, 0, sizeof(P));

    cin >> s >> t >> b;
    int ss = s - 'A', tt = t - 'A', bb = b - 'A';
    sx = ss%3; sy = ss/3;
    gx = tt%3; gy = tt/3;
    bx = bb%3; by = bb/3;
    solve();
  }

  return 0;
}