#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
    int a = x.size();
    int b = x[0].size();
    int c = y[0].size();
    vector<vector<T> > z(a, vector<T>(c, 0));
    for(int i=0; i<a; ++i){
        for(int j=0; j<c; ++j){
            for(int k=0; k<b; ++k){
                z[i][j] += x[i][k] * y[k][j];
            }
        }
    }
    return z;
}

template <class T>
vector<vector<T> > matrixPower(const vector<vector<T> >& x, int k)
{
    int n = x.size();
    vector<vector<T> > y(n, vector<T>(n, 0));
    for(int i=0; i<n; ++i)
        y[i][i] = 1;

    vector<vector<T> > z = x;
    while(k > 0){
        if(k & 1)
            y = matrixProduct(y, z);
        z = matrixProduct(z, z);
        k >>= 1;
    }
    return y;
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;
        char s, t, b;
        cin >> s >> t >> b;

        vector<vector<double> > mat(9, vector<double>(9, 0.0));
        for(int i=0; i<9; ++i){
            if(i > 2 && i-3 != b-'A'){
                mat[i][i-3] = 0.25;
            }else{
                mat[i][i] += 0.25;
            }
            if(i < 6 && i+3 != b-'A'){
                mat[i][i+3] = 0.25;
            }else{
                mat[i][i] += 0.25;
            }
            if(i % 3 != 0 && i-1 != b-'A'){
                mat[i][i-1] = 0.25;
            }else{
                mat[i][i] += 0.25;
            }
            if(i % 3 != 2 && i+1 != b-'A'){
                mat[i][i+1] = 0.25;
            }else{
                mat[i][i] += 0.25;
            }
        }

        mat = matrixPower(mat, n);
        printf("%.10f\n", mat[s-'A'][t-'A']);
    }
}