#include<stdio.h>
#include<algorithm>
#include<string>
#include<map>
#include<string.h>
#include<vector>
#define MAXN 1100
using namespace std;
int ptr[1100];
int next[110000];
int zu[110000];
int zeit, dis[MAXN], fin[MAXN], low[MAXN], par[MAXN], dep[MAXN];
int kodat[MAXN], koptr[MAXN + 1];
void dfsInfo(int u, int oy, int d) {
	dis[u] = low[u] = zeit++; par[u] = oy; dep[u] = d;
	int i, v;
	for (i = ptr[u]; ~i; i = next[i]) if ((v = zu[i]) != oy) {
		if (!~dis[v]) {
			dfsInfo(v, u, d + 1);
			low[u] = min(low[u], low[v]);
		} else {
			low[u] = min(low[u], dis[v]);
		}
	}
	fin[u] = zeit++;
}
int n;
void dfsInfos() {
	
	memset(dis, ~0, n * 4); zeit = 0;
	for (int u = 0; u < n; ++u) if (!~dis[u]) dfsInfo(u, -1, 0);
	for (int u = 0; u < n; ++u) {
		int &j = koptr[u + 1] = koptr[u];
		for (int i = ptr[u]; ~i; i = next[i]) if (u == par[zu[i]]) kodat[j++] = zu[i];
	}
}
bool produce(int u, int v) {
	return (dis[u] <= dis[v] && fin[u] >= fin[v]);
}
int related(int u, int v) {
	int s = koptr[u], e = koptr[u + 1], h;
	for (; s + 1 < e; ) {
		h = (s + e) >> 1;
		(dis[kodat[h]] <= dis[v]) ? s = h : e = h;
	}
	return kodat[s];
}
bool isBridge(int u, int v) {
	if (dis[u] > dis[v]) swap(u, v);
	return (u == par[v] && dis[v] <= low[v]);
}
bool isFatalEdge(int u, int v, int a, int b) {
	if (dis[u] > dis[v]) swap(u, v);
	return (u == par[v] && dis[v] <= low[v] && produce(v, a) != produce(v, b));
}
bool isFatalPoint(int u, int a, int b) {
	bool ua = produce(u, a), ub = produce(u, b);
	if (!ua && !ub) {
		return 0;
	} else if (ua && ub) {
		int ra = related(u, a), rb = related(u, b);
		return (ra != rb && (dis[u] <= low[ra] || dis[u] <= low[rb]));
	} else {
		if (ub) swap(a, b);
		return (dis[u] <= low[related(u, a)]);
	}
}
int UF[110];
char in[20];
vector<int>g[110];
vector<int>g2[110];
int FIND(int a){
	if(UF[a]<0)return a;
	return UF[a]=FIND(UF[a]);
}
void UNION(int a,int b){
	a=FIND(a);b=FIND(b);if(a==b)return;UF[a]+=UF[b];UF[b]=a;
}
int dp[2][110][110];
int ts[110];
int dp2[110][110][110];
void solve(int a,int b){
	int t=-UF[FIND(a)];
	for(int i=0;i<g2[a].size();i++){
		if(b!=g2[a][i]){
			solve(g2[a][i],a);
			t+=ts[g2[a][i]];
		}
	}
	for(int i=0;i<g2[a].size()+1;i++)for(int j=0;j<=t;j++)
		for(int k=0;k<=t;k++)dp2[i][j][k]=-999999999;
	dp2[0][0][0]=0;
	for(int i=0;i<g2[a].size();i++){
		if(g2[a][i]==b){
			for(int j=0;j<=t;j++){
				for(int k=0;k<=t;k++){
					dp2[i+1][j][k]=dp2[i][j][k];
				}
			}
			continue;
		}
		for(int j=0;j<=t;j++)for(int k=0;k<=t;k++){
			if(dp2[i][j][k]<0)continue;
			for(int l=0;l<=ts[g2[a][i]];l++){
				dp2[i+1][j+l][k]=max(dp2[i+1][j+l][k],dp2[i][j][k]+dp[0][g2[a][i]][l]);
				dp2[i+1][j][k+l]=max(dp2[i+1][j][k+l],dp2[i][j][k]+dp[1][g2[a][i]][l]);
			}
		}
	}
	ts[a]=t;
	int z=g2[a].size();
	int v=-UF[FIND(a)];
	for(int i=0;i<=t;i++)for(int j=0;j<=t;j++){
		if(i>=v)dp[0][a][i]=max(dp[0][a][i],dp2[z][i-v][j]+(i)*(v+j));
		if(j>=v)dp[1][a][j]=max(dp[1][a][j],dp2[z][i][j-v]+(v+i)*(j));
	}
	//for(int i=0;i<=t;i++)printf("%d %d %d: %d\n",0,a,i,dp[0][a][i]);
	//for(int i=0;i<=t;i++)printf("%d %d %d: %d\n",1,a,i,dp[1][a][i]);
	
}
int main(){
	int a,b;scanf("%d%d",&a,&b);
	n=a;
	map<string,int>m;
	for(int i=0;i<a;i++){
		scanf("%s",in);
		string tmp=in;
		m[tmp]=i;
	}
	for(int i=0;i<1100;i++)ptr[i]=-1;
	for(int i=0;i<110000;i++)next[i]=-1;
	int sz=0;
	for(int i=0;i<b;i++){
		scanf("%s",in);
		string tmp=in;
		int p=m[tmp];
		scanf("%s",in);
		tmp=in;
		int q=m[tmp];
		g[p].push_back(q);
		g[q].push_back(p);
		next[sz]=ptr[p];
		zu[sz]=q;
		ptr[p]=sz++;
		next[sz]=ptr[q];
		zu[sz]=p;
		ptr[q]=sz++;
	}
	dfsInfos();
	for(int i=0;i<a;i++)UF[i]=-1;
	//for(int i=0;i<a;i++)printf("%d %d\n",dis[i],low[i]);
	for(int i=0;i<a;i++){
		for(int j=ptr[i];~j;j=next[j]){
			if(!isBridge(i,zu[j]))UNION(i,zu[j]);
		}
	}
	for(int i=0;i<a;i++){
		for(int j=ptr[i];~j;j=next[j]){
			if(isBridge(i,zu[j])){
				g2[FIND(i)].push_back(FIND(zu[j]));
			}
		}
	}
	//for(int i=0;i<a;i++)printf("%d ",FIND(i));
	solve(FIND(0),-1);
	int ret=0;
	for(int i=0;i<a;i++)for(int j=0;j<=a;j++)ret=max(ret,max(dp[0][i][j],dp[1][i][j]));
	printf("%d\n",ret);
}

Measured memory_bytes: 7316