#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
pair<int, int> f(char c) {
  pair<int, int> ret;
  int a = c - 'A';
  ret.first = a / 3;
  ret.second = a % 3;
  return ret;
}
int main() {
  int n, bunshi, bunbo;
  char a, b, c;
  pair<int, int> s, t, m;
  pair<int, pair<int, int> > p;
  while (cin >> n && n > 0) {
    cin >> a >> b >> c;
    s = f(a);
    t = f(b);
    m = f(c);
    bunshi = 0;
    bunbo = 0;
    queue<pair<int, pair<int, int> > > q;
    q.push(pair<int, pair<int, int> >(n, s));
    while (!q.empty()) {
      p = q.front();
      q.pop();
      if (p.first == 0) {
        bunbo++;
        if (p.second == t) bunshi++;
        continue;
      }
      for (int i = 0; i < 4; i++) {
        pair<int, int> next(p.second.first + dx[i], p.second.second + dy[i]);
        if (next.first < 0 || next.first > 2 || next.second < 0 ||
            next.second > 2 || next == m)
          q.push(pair<int, pair<int, int> >(p.first - 1, p.second));
        else
          q.push(pair<int, pair<int, int> >(p.first - 1, next));
      }
    }
    printf("%.15lf\n", (double)bunshi / bunbo);
  }
}
