#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>

using namespace std;

const int MAX = 16;
const double per = 1.0/4.0;
char s,t,b;
int n;
int nex[9][9] = {{1,3,0,0},{0,2,4,1},{1,5,2,2},{0,4,6,3},{1,3,5,7},{2,4,8,5},{3,7,6,6},{4,6,8,7},{5,7,8,8}};
double dp[MAX][9];

void init(char c){
  memset(dp,0,sizeof(dp));
  dp[0][c-'A'] = 1.0;
}

vector<int> get(int pos){
  vector<int> V;

  for(int i = 0; i < 4; i++)
    if(nex[pos][i] == b-'A') V.push_back(pos);
    else V.push_back(nex[pos][i]);

  return V;
}

void solve(){
  for(int i = 0; i < n; i++)
    for(int j = 0; j < 9; j++){
      vector<int> V = get(j);
      for(int k = 0; k < V.size(); k++)
	dp[i+1][V[k]] += dp[i][j]*per;
    } 
}

int main(){
  while(cin >> n && n){
    cin >> s >> t >> b;
    init(s);
    solve();
    printf("%.8f\n",dp[n][t-'A']);
  }
  return 0;
}