#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string A[20];
int cnt[20];
int ans, n;
void DFS(const string & a, int prev)
{
int len = a.size(), total = prev + len;
ans = max(ans, total);
for (int i = 1; i <= len; i++)
{
for (int j = 0; j < n; j++)
{
if (cnt[j] < 2 && A[j].size() > i && a.compare(len - i, i, A[j], 0, i) == 0)
{
cnt[j]++;
DFS(A[j], total - i);
cnt[j]--;
}
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> A[i];
char start;
cin >> start;
for (int i = 0; i < n; i++)
{
if (A[i][0] == start)
DFS(A[i], 0);
}
cout << ans;
return 0;
}