#include <bits/stdc++.h>
using namespace std;
int n;
char s, t, d;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
void solve() {
  int goal[3][3] = {{}};
  map<char, pair<int, int> > tmp;
  tmp['A'] = pair<int, int>(0, 0);
  tmp['B'] = pair<int, int>(0, 1);
  tmp['C'] = pair<int, int>(0, 2);
  tmp['D'] = pair<int, int>(1, 0);
  tmp['E'] = pair<int, int>(1, 1);
  tmp['F'] = pair<int, int>(1, 2);
  tmp['G'] = pair<int, int>(2, 0);
  tmp['H'] = pair<int, int>(2, 1);
  tmp['I'] = pair<int, int>(2, 2);
  queue<pair<int, pair<int, int> > > que;
  que.push(make_pair(n, tmp[s]));
  while (!que.empty()) {
    pair<int, pair<int, int> > p = que.front();
    que.pop();
    if (p.first == 0) {
      goal[p.second.first][p.second.second]++;
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int nx = dx[i] + p.second.first, ny = dy[i] + p.second.second;
      if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3 &&
          tmp[d] != pair<int, int>(nx, ny)) {
        que.push(make_pair(p.first - 1, pair<int, int>(nx, ny)));
      } else {
        que.push(make_pair(p.first - 1,
                           pair<int, int>(p.second.first, p.second.second)));
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) sum += goal[i][j];
  }
  printf("%.7f\n", (double)goal[tmp[t].first][tmp[t].second] / (double)sum);
}
int main() {
  while (cin >> n, n) {
    cin >> s >> t >> d;
    solve();
  }
}
