#include <bits/stdc++.h>
using namespace std;
double prob(int n, int pos, int t, int d) {
  if (n == 0) {
    if (pos == t) {
      return 1;
    } else {
      return 0;
    }
  }
  double p = 0;
  if ((pos - 3 < 0) || (pos - 3 == d)) {
    p += prob(n - 1, pos, t, d) / 4.0;
  } else {
    p += prob(n - 1, pos - 3, t, d) / 4.0;
  }
  if ((pos % 3 == 0) || (pos - 1 == d)) {
    p += prob(n - 1, pos, t, d) / 4.0;
  } else {
    p += prob(n - 1, pos - 1, t, d) / 4.0;
  }
  if ((pos % 3 == 2) || (pos + 1 == d)) {
    p += prob(n - 1, pos, t, d) / 4.0;
  } else {
    p += prob(n - 1, pos + 1, t, d) / 4.0;
  }
  if ((pos + 3 > 8) || (pos + 3 == d)) {
    p += prob(n - 1, pos, t, d) / 4.0;
  } else {
    p += prob(n - 1, pos + 3, t, d) / 4.0;
  }
  return p;
}
int main() {
  vector<double> r;
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    char a[2], b[2], c[2];
    int s = 0, t = 0, d = 0;
    scanf("%s %s %s", a, b, c);
    s = a[0] - 'A';
    t = b[0] - 'A';
    d = c[0] - 'A';
    r.push_back(prob(n, s, t, d));
  }
  for (int i = 0; i < r.size(); i++) {
    cout << setprecision(10) << r[i] << endl;
  }
  return 0;
}
