#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
typedef long long ll;

int n;
int sx, sy, gx, gy, bx, by; 
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double dp[3][3][16];

// 残りk回動けて今x, yにいるとき最後充電スペースにいける確率
double dfs(int x, int y, int k) {
    if (dp[x][y][k] >= 0) return dp[x][y][k];
    if (k == 0) {
        if (x == gx && y == gy) return 1;
        else return 0;
    }
    double ret = 0;
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) ret += dfs(x, y, k-1) / 4;
        else if (nx == bx && ny == by) ret += dfs(x, y, k-1) / 4;
        else ret += dfs(nx, ny, k-1) / 4;
    }
    return dp[x][y][k] = ret;
}

int main(void) {
    while (1) {
        cin >> n;
        if (n == 0) break;
        char s, t, b;
        cin >> s >> t >> b;
        sx = (int)(s-'A') % 3; sy = (int)(s-'A')/3;
        gx = (int)(t-'A') % 3; gy = (int)(t-'A')/3;
        bx = (int)(b-'A') % 3; by = (int)(b-'A')/3;
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                for (int k = 0; k < 16; k++)
                    dp[i][j][k] = -1;
        printf("%.10f\n", dfs(sx, sy, n));
    }
    return 0;
}