#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
#define mmt(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define N 1010
using namespace std;
typedef long long LL;
typedef long double LD;
char s[N][11];int L[11],c[11][N],n,lat;
LL f[N];
bool move(char aa,char bb)
{
int a=aa-'A',b=bb-'A';
if (c[a][L[a]]<c[b][L[b]]&&L[a]&&c[a][L[a]]!=lat)
{lat=c[b][++L[b]]=c[a][L[a]],L[a]--;return 1;}
return 0;
}
int F(int n)
{
int res,i;lat=-1;
L[0]=n;L[1]=L[2]=0;c[0][0]=c[1][0]=c[2][0]=1e9;
FOR(i,1,n) c[0][i]=n-i+1;
for(res=0;!(L[1]==n||L[2]==n);res++)
FOR(i,0,5)
if (move(s[i][0],s[i][1])) {break;}
return res;
}
int main()
{
ios::sync_with_stdio(false);
scanf("%d",&n);int i;
FOR(i,0,5) scanf("%s",s[i]);
if (n<=5) printf("%d\n",F(n));
else
{
int f3=F(3),f4=F(4),f5=F(5);
int a=(f5-f4)/(f4-f3);
int b=f4-a*f3;
f[3]=f3;f[4]=f4;f[5]=f5;
FOR(i,6,n) f[i]=a*f[i-1]+b;
printf("%lld\n",f[n]);
}
return 0;
}