#include <iostream>
#include <map>

using namespace std;
typedef pair<int, int> pii;

map<pii, double> memo;
double recur(int n, int room, int goal, int unused){
	if(n == 0){ return (room == goal) ? 1.0 : 0.0; }
	pii key(n, room);
	if(memo.find(key) != memo.end()){ return memo[key]; }
	double answer = 0.0;
	answer += recur(n - 1, (room % 3 == 0 || room - 1 == unused) ? room : (room - 1), goal, unused);
	answer += recur(n - 1, (room % 3 == 2 || room + 1 == unused) ? room : (room + 1), goal, unused);
	answer += recur(n - 1, (room / 3 == 0 || room - 3 == unused) ? room : (room - 3), goal, unused);
	answer += recur(n - 1, (room / 3 == 2 || room + 3 == unused) ? room : (room + 3), goal, unused);
	answer /= 4;
	memo.insert(make_pair(key, answer));
	return answer;
}
int main(){
	while(true){
		int n;
		cin >> n;
		if(n == 0){ break; }
		char s, t, b;
		cin >> s >> t >> b;
		memo.clear();
		cout << recur(n, s - 'A', t - 'A', b - 'A') << endl;
	}
	return 0;
}