#include <iostream>
#include <fstream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<n;i++)
#define pi pair<int,int>
#define pb push_back
#define sec second
#define sh(i) (1LL<<i)
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int n,dy[]={0,1,0,-1},dx[]={1,0,-1,0};
char s,b,j;
map<pair<int,char>,double> M;

char code(int y,int x){
	return 'A'+y*3+x;
}

pi dec(char c){
	return pi((c-'A')/3,(c-'A')%3);
}

double f(int n,char c){
	if(n==0)return c==b;
	if(M.find(pi(n,c))!=M.end())return M[pi(n,c)];
	double& res=M[pi(n,c)]=0;
	pi p=dec(c);
	int y=p.fir,x=p.sec;
	rep(i,4){
		int ny=y+dy[i],nx=x+dx[i];
		if(ny<0||2<ny||nx<0||2<nx||code(ny,nx)==j)res+=f(n-1,c)/4;
		else res+=f(n-1,code(ny,nx))/4;
	}
	return res;
}

int main(){
	while(cin>>n&&n){
		M.clear();
		cin>>s>>b>>j;
		cout<<f(n,s)<<endl;
	}
}