#include <stdio.h>

int patterns = 0;
int goals = 0;
/* typedef enum { */
/* 	A = 0, */
/* 	B, */
/* 	C, */
/* 	D, */
/* 	E, */
/* 	F, */
/* 	G, */
/* 	H, */
/* 	I, */
/* } ROOMS; */
void cleaning(char lroom, char groom, char broom, int cnt);
void subcleaning(char lroom, char groom, char broom, int cnt, char next);

void subcleaning(char lroom, char groom, char broom, int cnt, char next) {
//	printf("%c ", next);
	
	if (next == broom || next == 'N') {
		cleaning(lroom, groom, broom, --cnt);
	} else {
		cleaning(next, groom, broom, --cnt);
	}
}

void cleaning(char lroom, char groom, char broom, int cnt) {
//	++patterns;
	if (0 == cnt) {
		++patterns;
		if (lroom == groom) {
			++goals;
		}
		return;
	}
	
	switch(lroom) {
	case 'A':
		subcleaning(lroom, groom, broom, cnt, 'B');
		subcleaning(lroom, groom, broom, cnt, 'D');
		subcleaning(lroom, groom, broom, cnt, 'N');
		subcleaning(lroom, groom, broom, cnt, 'N');
		break;
	case 'B':
		subcleaning(lroom, groom, broom, cnt, 'A');
		subcleaning(lroom, groom, broom, cnt, 'E');
		subcleaning(lroom, groom, broom, cnt, 'C');
		subcleaning(lroom, groom, broom, cnt, 'N');
		break;
	case 'C':
		subcleaning(lroom, groom, broom, cnt, 'B');
		subcleaning(lroom, groom, broom, cnt, 'F');
		subcleaning(lroom, groom, broom, cnt, 'N');
		subcleaning(lroom, groom, broom, cnt, 'N');
		break;
	case 'D':
		subcleaning(lroom, groom, broom, cnt, 'A');
		subcleaning(lroom, groom, broom, cnt, 'E');
		subcleaning(lroom, groom, broom, cnt, 'G');
		subcleaning(lroom, groom, broom, cnt, 'N');
		break;
	case 'E':
		subcleaning(lroom, groom, broom, cnt, 'B');
		subcleaning(lroom, groom, broom, cnt, 'D');
		subcleaning(lroom, groom, broom, cnt, 'F');
		subcleaning(lroom, groom, broom, cnt, 'H');
		break;
	case 'F':
		subcleaning(lroom, groom, broom, cnt, 'C');
		subcleaning(lroom, groom, broom, cnt, 'E');
		subcleaning(lroom, groom, broom, cnt, 'I');
		subcleaning(lroom, groom, broom, cnt, 'N');
		break;
	case 'G':
		subcleaning(lroom, groom, broom, cnt, 'D');
		subcleaning(lroom, groom, broom, cnt, 'H');
		subcleaning(lroom, groom, broom, cnt, 'N');
		subcleaning(lroom, groom, broom, cnt, 'N');
		break;
	case 'H':
		subcleaning(lroom, groom, broom, cnt, 'G');
		subcleaning(lroom, groom, broom, cnt, 'E');
		subcleaning(lroom, groom, broom, cnt, 'I');
		subcleaning(lroom, groom, broom, cnt, 'N');
		break;
	case 'I':
		subcleaning(lroom, groom, broom, cnt, 'F');
		subcleaning(lroom, groom, broom, cnt, 'I');
		subcleaning(lroom, groom, broom, cnt, 'N');
		subcleaning(lroom, groom, broom, cnt, 'N');
		break;
	}
	return;
}

int main(void) {
	int n;
	char s, t, b; 
	while(1) {
		scanf("%d", &n);
		if (n == 0) {
			break;
		}
		scanf(" %c %c %c", &s, &t, &b);
//		printf("%c %c %c\n", s, t, b);
		patterns = 0;
		goals = 0;
		cleaning(s, t, b, n);
		printf("goals = %d\n", goals);
		printf("patterns = %d\n", patterns);
		printf("%lf\n", (double)goals/patterns);
	}
	return 0;
}