#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int n;
char s, t, b;
double p[2][3][3];
int getX(char c) { return (c - 'A') % 3; }
int getY(char c) { return (c - 'A') / 3; }
void solve() {
  for (int y = (0); y < (3); ++y)
    for (int x = (0); x < (3); ++x) p[0][y][x] = 0.;
  p[0][getY(s)][getX(s)] = 1.0;
  for (int i = (0); i < (n); ++i) {
    for (int y = (0); y < (3); ++y)
      for (int x = (0); x < (3); ++x) {
        if (y == getY(b) && x == getX(b)) continue;
        for (int d = (0); d < (4); ++d) {
          int nx = x + dx[d], ny = y + dy[d];
          if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue;
          p[1][y][x] += p[0][ny][nx] * 0.25;
        }
      }
    swap(p[0], p[1]);
  }
  cout << p[0][getY(t)][getX(t)] << endl;
}
int main() {
  while (cin >> n, n) {
    cin >> s >> t >> b;
    solve();
  }
}
