#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename T>
class IdGen
{
	map<T, int> v2id_;
	vector<T>   id2v_;
public:
	int v2id(const T& v) {
		if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); }
		return v2id_[v];
	}
	const T& id2v(int i) const { return id2v_[i]; }
	int size() const { return id2v_.size(); }
};

template<typename Vert>
class SCC
{
public:
	IdGen<Vert> idgen;
	vector< vector<int> > G;

public:
	void addVert( Vert s_ )
	{
		int s = idgen.v2id(s_);
		if( s >= G.size() ) G.resize(s+1);
	}

	void addEdge( Vert s_, Vert t_ )
	{
		int s = idgen.v2id(s_), t = idgen.v2id(t_);
		if( max(s,t) >= G.size() ) G.resize(max(s,t)+1);
		G[s].push_back(t);
	}

	void scc()
	{
		int N = G.size();
		dfs_no.assign(N, 0);
		dfs_lo.assign(N, 0);
		pending = stack<int>();
		scc_id.assign(N, -1);
		scc_list.clear();
		scc_children.clear();
		for(int v=0; v<N; ++v)
			dfs(v);
	}

	vector<int> scc_id; // which scc does the vert belong to?
	vector< vector<int> > scc_list;     // list of nodes in the scc
	vector< vector<int> > scc_children; // forest relationship of sccs

private:
	int no;
	vector<int> dfs_no; // 1-origin dfs-visit ID
	vector<int> dfs_lo; // the least ID in the vert's scc
	stack<int> pending; // current unclassigied verts

	void dfs(int v)
	{
		// visit v if not yet
		if( dfs_no[v] )
			return;
		dfs_no[v] = dfs_lo[v] = ++no;
		pending.push(v);

		// visit children
		for(int i=0; i<G[v].size(); ++i) {
			int u = G[v][i];
			dfs( u );
			if( scc_id[u] == -1 )
				dfs_lo[v] = min( dfs_lo[v], dfs_lo[u] );
		}

		// when v is the representative of scc
		if( dfs_no[v] == dfs_lo[v] ) {
			vector<int> scc;
			for(;;) {
				int w = pending.top(); pending.pop();
				scc.push_back(w);
				scc_id[w] = scc_list.size();
				if( w == v ) break;
			}
			scc_list.push_back(scc);

			set<int> children;
			for(int j=0; j<scc.size(); ++j)
				for(int i=0; i<G[scc[j]].size(); ++i)
					children.insert( scc_id[G[scc[j]][i]] );
			children.erase(scc_id[v]);
			scc_children.push_back( vector<int>(children.begin(), children.end()) );
		}
	}
};

class ImpossibleGame { public:
	static LL C(LL n, LL k) {
		LL v = 1;
		for(int i=1; i<=k; ++i)
			v = v * (n+1-i) / i;
		return v;
	}

	static LL weight(LL a, LL b, LL c, LL d) {
		return C(a+b+c+d,a) * C(b+c+d,b) * C(c+d,c);
	}

	long long getMinimum(int k, vector <string> before, vector <string> after) 
	{
		vector< pair< vector<int>, vector<int> > > dif;
		for(int i=0; i<after.size(); ++i)
		{
			vector<int> be(4), af(4);
			for(int j=0; j<before[i].size(); ++j) be[before[i][j]-'A'] ++;
			for(int j=0; j< after[i].size(); ++j) af[ after[i][j]-'A'] ++;
			dif.push_back( make_pair(be,af) );
		}

		SCC< vector<int> > G;

		vector<int> v(4);
		for(v[0]=0; v[0]<=k; ++v[0])
		for(v[1]=0; v[0]+v[1]<=k; ++v[1])
		for(v[2]=0; v[0]+v[1]+v[2]<=k; ++v[2])
		{
			v[3] = k - v[0] - v[1] - v[2];
			G.addVert(v);
			for(int i=0; i<dif.size(); ++i)
			{
				bool dame = false;

				vector<int> u = v;
				for(int k=0; k<4; ++k)
				{
					if( (u[k]-=dif[i].first[k]) < 0 )
						dame = true;
					u[k] += dif[i].second[k];
				}
				if( !dame )
					G.addEdge(v, u);
			}
		}

		G.scc();

		LL ans = 0;
		for(int v=0; v<G.scc_list.size(); ++v)
			ans = max(ans, rec(v, G));
		return ans;
	}

	map<int, LL> memo;
	LL rec(int v, SCC< vector<int> >& G)
	{
		if( memo.count(v) )
			return memo[v];
		LL mx = 0;
		for(int i=0; i<G.scc_children[v].size(); ++i)
			mx = max(mx, rec(G.scc_children[v][i], G));
		for(int i=0; i<G.scc_list[v].size(); ++i) {
			const vector<int>& str = G.idgen.id2v(G.scc_list[v][i]);
			mx += weight(str[0], str[1], str[2], str[3]);
		}
		return memo[v] = mx;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, ImpossibleGame().getMinimum(k, before, after));}
int main(){

CASE(0)
	int k = 1; 
	string before_[] = { "A" };
	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_)); 
	string after_[] = { "B" };
	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_)); 
	long long _ = 2LL; 
END
CASE(1)
	int k = 2; 
	string before_[] = { "A", "A", "D" };
	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_)); 
	string after_[] = { "B", "C", "D" };
	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_)); 
	long long _ = 5LL; 
END
CASE(2)
	int k = 2; 
	string before_[] = { "B", "C", "D" };
	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_)); 
	string after_[] = { "C", "D", "B" };
	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_)); 
	long long _ = 9LL; 
END
CASE(3)
	int k = 6; 
	string before_[] = { "AABBC", "AAAADA", "AAACA", "CABAA", "AAAAAA", "BAAAA" };
	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_)); 
	string after_[] = { "AACCB", "DAAABC", "AAAAD", "ABCBA", "AABAAA", "AACAA" };
	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_)); 
	long long _ = 499LL; 
END
CASE(4)
	int k = 7;
	string before_[] = {"BDBBB", "ACBBBBC", "BBBBC", "CCACCCB", "CC", "BBBBBBD", "BBDBBAB", "DBBCBB", "DBABABB", "BBBBABB", "BBBBB", "BBABB", "BBBBBDB", "ABDC", "BDBBDCB", "BBBBBAC", "BBBDBC", "BBBBBBA", "BBBBBBB", "BACCBB", "ABBDABB", "DC", "ABBDBB", "ACBCABB"};
	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_)); 
	string after_[] = {"BADBC", "ACDBCBB", "BADCB", "BBBDBBC", "BD", "BBACCBD", "BBADBDB", "BBBBCC", "BBBCBBB", "BCBDCBD", "ABBBA", "DDBAB", "BCBDBDB", "BBCB", "BBCBBBB", "BABCBDB", "BBCCBD", "BBBBBCB", "DBCBADC", "BBBACA", "ABBABBC", "DB", "DBBCBB", "BBDBBBB"};
	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_)); 
	long long _ = 7112LL; 
END
/*
CASE(5)
	int k = ; 
	string before_[] = ;
	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_)); 
	string after_[] = ;
	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_)); 
	long long _ = LL; 
END
*/
}
// END CUT HERE
