#include <bits/stdc++.h>
using namespace std;
int r[9];
int deno;
int num;
void rc(int n, int i) {
  if (n == 0) {
    deno++;
    if (r[i] == 1) num++;
  } else {
    --n;
    if (i < 3 || r[i - 3] == 2)
      rc(n, i);
    else
      rc(n, i - 3);
    if (i % 3 == 2 || r[i + 1] == 2)
      rc(n, i);
    else
      rc(n, i + 1);
    if (i > 5 || r[i + 3] == 2)
      rc(n, i);
    else
      rc(n, i + 3);
    if (i % 3 == 0 || r[i - 1] == 2)
      rc(n, i);
    else
      rc(n, i - 1);
  }
}
int main() {
  int n;
  char s, t, b;
  while (cin >> n && n) {
    cin >> s >> t >> b;
    for (int i = 0; i < 9; i++) r[i] = 0;
    r[t - 'A'] = 1;
    r[b - 'A'] = 2;
    deno = 0;
    num = 0;
    rc(n, s - 'A');
    double ans = (double)num / deno;
    printf("%lf\n", ans);
  }
  return 0;
}
