#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define MS 45
#define MAX 1037471823
#define Q 103
using namespace std;
int n, g[5][MS];
long long f[5][MS];
char s[12][12];
int main()
{
scanf("%d", &n);
rep(i, 1, 6) scanf("%s", s[i]);
f[1][1] = f[2][1] = f[3][1] = 1;
down(i, 6, 1)
{
if (s[i][0] == 'A') g[1][1] = s[i][1]-'A'+1;
if (s[i][0] == 'B') g[2][1] = s[i][1]-'A'+1;
if (s[i][0] == 'C') g[3][1] = s[i][1]-'A'+1;
}
rep(j, 2, n) rep(i, 1, 3)
if (g[g[i][j-1]][j-1] == i) g[i][j] = g[i][j-1], f[i][j] = f[i][j-1]+f[g[i][j-1]][j-1]+f[i][j-1]+2;
else g[i][j] = g[g[i][j-1]][j-1], f[i][j] = f[i][j-1]+f[g[i][j-1]][j-1]+1;
printf("%lld\n", f[1][n]);
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGZzdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxxdWV1ZT4KCiNkZWZpbmUgcmVwKGksIGwsIHIpIGZvcihpbnQgaSA9IGw7IGkgPD0gcjsgaSsrKQojZGVmaW5lIGRvd24oaSwgbCwgcikgZm9yKGludCBpID0gbDsgaSA+PSByOyBpLS0pCiNkZWZpbmUgTVMgNDUKI2RlZmluZSBNQVggMTAzNzQ3MTgyMwojZGVmaW5lIFEgMTAzCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG4sIGdbNV1bTVNdOwpsb25nIGxvbmcgZls1XVtNU107CmNoYXIgc1sxMl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