1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 #include<cmath>
6 #include<ctime>
7 #include<algorithm>
8 #include<algorithm>
9 using namespace std;
10 #define INF 99999999
11 char ch[6][2];
12 int n,stack[5][32],top[5];
13 long long f[32];
14 inline int read()
15 {
16 int x=0,f=1; char ch=getchar();
17 while(!isdigit(ch)) {if(ch==‘-‘) f=-1; ch=getchar();}
18 while(isdigit(ch)) {x=x*10+ch-‘0‘; ch=getchar();}
19 return x*f;
20 }
21 bool check(){return (!top[1]&&!top[2])||(!top[1]&&!top[3]);}
22 void work(int x)
23 {
24 memset(stack,0,sizeof(stack));
25 memset(top,0,sizeof(top));
26 for(int i=x;i;i--) stack[1][++top[1]]=i;
27 int last=0;
28 stack[1][0]=stack[2][0]=stack[3][0]=INF;
29 while(!check())
30 {
31 for(int i=0;i<6;i++)
32 {
33 int xx=ch[i][0]-‘A‘+1,yy=ch[i][1]-‘A‘+1;
34 if(stack[xx][top[xx]]<stack[yy][top[yy]]&&stack[xx][top[xx]]!=last)
35 {
36 stack[yy][++top[yy]]=stack[xx][top[xx]];
37 last=stack[xx][top[xx]]; top[xx]--;
38 break;
39 }
40 }
41 f[x]++;
42 }
43 }
44 int main()
45 {
46 //freopen("cin.in","r",stdin);
47 //freopen("cout.out","w",stdout);
48 n=read();
49 for(int i=0;i<6;i++) scanf("%c%c ",&ch[i][0],&ch[i][1]);
50 f[1]=1;
51 work(2);
52 work(3);
53 int a=(f[3]-f[2])/(f[2]-f[1]),b=f[2]-a*f[1];
54 for(int i=4;i<=n;i++) f[i]=f[i-1]*a+b;
55 printf("%lld\n",f[n]);
56 return 0;
57 }