#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int INF = 100000000;

string m = "012345678";
P getP(char c){
	int p = m[c-'A']-'0';
	return MP(p%3, p/3);
}

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

int main(){
	while(1){
		int n;
		char s, t, b;
		cin >> n;
		if(!n)break;
		cin >> s >> t >> b;

		P sp = getP(s);
		P tp = getP(t);
		P bp = getP(b);

		double p[16][3][3] = {};

		p[0][sp.Y][sp.X] = 1.0;

		for(int i = 1; i <= n; i++){
			rep(j, 3){
				rep(k, 3){
					if(MP(k, j) == bp) continue;
					int wallcnt = 0;
					rep(l, 4){
						int x = k+dx[l];
						int y = j+dy[l];
						if(x < 0 || x > 2 || y < 0 || y > 2 ||
							MP(x, y) == bp){
							wallcnt++;
							continue;
						}
						p[i][j][k] += p[i-1][y][x]/4;
					}
					p[i][j][k] += p[i-1][j][k]*wallcnt/4;
				}
			}
		}

		printf("%.8lf\n", p[n][tp.Y][tp.X]);
	}

	return 0;
}