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

int n, *l; char **a;
int mrg[15][15];
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--) {
                if (str[i].substr(l[i]-d, d) == str[j].substr(0, d)) {
                    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) continue;
            if (contained(str[i], str[j]))
                ind[i] = false;
        }
    }
}

string construct(vector<int> &p) {
    vector<int> q;
    for (auto x : p)
        if (ind[x])
            q.push_back(x);
    string ret;
    int m = q.size();
    for (int i = 0; i < m-1; i++) {
        ret += str[q[i]].substr(0, l[q[i]] - mrg[q[i]][q[i+1]]);
    }
    ret += str[q.back()];
    return ret;
}

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;

}