#include <bits/stdc++.h>
using namespace std;

int n, *l; char **a;
int mrg[15][15];
int total;
bool ind[15];
string str[15];

int contained(string a, string b) {
    if (a.size() > b.size()) return 0;
    int ret = 0;
    int an = a.size(), bn = b.size();
    for (int p = 0; p < bn - an + 1; p++) {
        bool ok = true;
        for (int k = 0; k < an; k++) {
            if (a[k] != b[k+p])
                {ok = false; break;}
        }
        if (ok) ret++;
        if (ret > 1) return ret;
    }
    return ret;
}

void findMerge() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j) continue;
            for (int d = min(l[i], l[j]); d >= 1; d--) {
                bool ok = true;
                for (int p = 1; p <= d; p++) {
                    if (a[j][p] != a[i][l[i]-d+p]) {
                        ok = false; break;
                    }
                }
                if (ok) {
                    mrg[i][j] = d;
                    break;
                }
            }
        }
    }
}

void findInd() {
    memset(ind, true, sizeof(ind));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j || l[i] > l[j]) continue;
            if (contained(str[i], str[j]))
                ind[i] = false;
        }
    }
    for (int i = 1; i <= n; i++)
        total += l[i] * ind[i];
}

string construct(vector<int> &p) {
    string s;
    for (int i = 0; i < p.size()-1; i++) {
        if (!ind[p[i]]) continue;
        int k = 1; while (i+k < p.size() && !ind[p[i+k]]) k++;
        for (int j = 1; j <= l[p[i]] - (i+k>=p.size()?0:mrg[p[i]][p[i+k]]); j++)
            s.push_back(a[p[i]][j]);
        if (i+k > n) return s;
        i += k-1;
    }
    if (ind[p.back()])
    for (int j = 1; j <= l[p.back()]; j++)
        s.push_back(a[p.back()][j]);
    return s;
}

void debug() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << mrg[i][j] << ' ';
        }
        cout << '\n';
    }
    cout << total << '\n';
}

bool check(string s) {
    for (int i = 1; i <= n; i++) {
        if (contained(str[i], s) != 1)
            return false;
    }
    return true;
}

void put(string s, char *S) {
    for (int i = 0; i < s.length(); i++) {
        S[i+1] = s[i];
    }
}

int Resi(int N, int K, int* L, char** A, char* S) {
	n = N; l = L; a = A;

	for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= l[i]; j++) {
            str[i].push_back(a[i][j]);
        }
	}

	findMerge();
	findInd();

	vector<int> prm;
	for (int i = 0; i < n; i++)
        prm.push_back(i+1);

    int ans = 1e9;
    do {
        string s = construct(prm);
        if (!check(s)) continue;
        if (s.length() < ans) {
            ans = s.length();
            put(s, S);
        }
    } while (next_permutation(prm.begin(), prm.end()));

	return ans==1e9?-1:ans;

}