#include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int main(){
  string s[3];
  int n;
  double a[16][3][3];
  while(cin>>n,n){
    memset(a,0,sizeof(a));
    r(i,3)cin>>s[i];
    r(i,15)a[i][(s[2][0]-'A')/3][(s[2][0]-'A')%3]=-1;
    a[0][(s[0][0]-'A')/3][(s[0][0]-'A')%3]=1;
    r(k,n)r(i,3)r(j,3)if(a[k][i][j]>0)r(l,4){
      int y=i+dy[l],x=j+dx[l];
      if(y<0||y>=3||x<0||x>=3)a[k+1][i][j]+=a[k][i][j]/4;
      else if(a[k][y][x]==-1)a[k+1][i][j]+=a[k][i][j]/4;
      else a[k+1][y][x]+=a[k][i][j]/4;
    }
    printf("%.9f\n",a[n][(s[1][0]-'A')/3][(s[1][0]-'A')%3]);
  }
}