#include <stdio.h>

int from, battery, to;
double prob[16][16]; //残量iで, 地点jからbatteryへいける確率

double getProb(int lim, int start)
{
    int k;
    double ans;
    
    if (prob[lim][start] >= 0){
        return (prob[lim][start]);
    }
    
    if (start == battery && lim == 0){
        return (1);
    }
    if (lim == 0){
        return (0);
    }
    
    ans = 0.0;
    k = 4;
    
    if ((start + 1) % 3 && start + 1 != to){
        ans += 0.25 * getProb(lim - 1, start + 1);
        k--;
    }
    
    if (start % 3 && start - 1 != to){
        ans += 0.25 * getProb(lim - 1, start - 1);
        k--;
    }
    
    if (start + 3 < 9 && start + 3 != to){
        ans += 0.25 * getProb(lim - 1, start + 3);
        k--;
    }
    
    if (start - 3 >= 0 && start - 3 != to){
        ans += 0.25 * getProb(lim - 1, start - 3);
        k--;
    }
    
    ans += 0.25 * k * getProb(lim - 1, start);
    
    return (prob[lim][start] = ans);
}

int main(void)
{
    int left;
    int i, j;
    
    while (1){
        scanf("%d", &left);
        getchar();
        if (left == 0){
            break;
        }
        scanf("%c %c %c", &from, &battery, &to);
        getchar();
        from -= 'A';
        battery -= 'A';
        to -= 'A';
        
        for (i = 0; i < 16; i++){
            for (j = 0; j < 16; j++){
                prob[i][j] = -1;
            }
        }
        
        printf("%.8lf\n", getProb(left, from));
    }
    
    return (0);
}