#include <bits/stdc++.h>

using namespace std;
string makstr = "x";

int brPuta(string txt, string s)
{
    int start = 0, cnt = 0;
    int ind = txt.find(s);
    while(ind != -1)
    {
        cnt++;
        start = ind+1;
        ind = txt.find(s, start);
    }
    return cnt;
}

string preklopi(string a, string b)
{
    int l1 = a.length(), l2 = b.length();
    int i2 = 0;
    for(int i = 0; i < l1; i++)
    {
        if(a[i] == b[i2]) i2++;
        else
        {
            i2 = 0;
            if(b[0] == a[i]) i2++;
        }
    }

    for(int i = i2; i < l2; i++) a += b[i];
    return a;
}


map<pair<string, string>, string> memo;

string Resi2(string aa, string bb)
{
    if(memo.find({aa, bb}) != memo.end()) return memo[{aa, bb}];
    if(memo.find({bb, aa}) != memo.end()) return memo[{bb, aa}];
    if(bb.length() > aa.length()) swap(aa, bb);
    string rez;
    int br = brPuta(aa, bb);
    if(br == 1)
        rez = aa;
    else if(br > 1)
        rez = makstr;
    else
    {
        string p1 = preklopi(aa, bb);
        string p2 = preklopi(bb, aa);
        if(p1.length() > p2.length()) rez = p2;
        else rez = p1;
    }
    memo[{aa, bb}] = rez;
    memo[{bb, aa}] = rez;
    return rez;
}

bool cmpDuz(string a, string b)
{
    return a.length() > b.length();
}

bool valid(string r, string ss[], int n)
{
    for(int i = 0; i < n; i++)
        if(brPuta(r, ss[i]) != 1) return false;
    return true;
}

string ResiN(string ss[], int n)
{
    //cout << "Resavam ";
    //for(int i = 0; i < n; i++) cout << ss[i] << " ";
    //cout << endl;
    if(n == 1) return ss[0];
    else if(n == 2) return Resi2(ss[0], ss[1]);
    string minR = makstr;
    for(int i = 0; i < n; i++)
    {
        string noviSs[n-1];
        bool bio = false;
        for(int j = 0; j < n; j++)
        {
            if(j == i) bio = true;
            else noviSs[j-bio] = ss[j];
        }

        string ost =  ResiN(noviSs, n-1);
        if(ost == makstr) continue;
        string r = Resi2(ss[i], ost);
        if(valid(r, ss, n) && minR.length() > r.length()) minR = r;
    }
    //if(minR != makstr) cout << "Resenje je " << minR << endl;
    //else cout << "Nema resenja" << endl;
    return minR;
}



int Resi(int n, int k, int* l, char** a, char* s)
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= l[i]; j++)
        {
            if(a[i][j] - 'a' + 1 > k)
                return -1;
            makstr += 'x';
        }

    if(n == 2)
    {
        string aa, bb;
        for(int i = 1; i <= l[1]; i++) aa += a[1][i];
        for(int i = 1; i <= l[2]; i++) bb += a[2][i];
        string rez = Resi2(aa, bb);
        if(rez != makstr)
        {
            for(int i = 1; i <= rez.length(); i++)
                s[i] = rez[i-1];
            return rez.length();
        }
        return -1;
    }
    /*else if(n == 3)
    {
        string ss[3];
        for(int j = 1; j <= n; j++)
            for(int i = 1; i <= l[j]; i++) ss[j-1] += a[j][i];
        string minR = ResiN(ss, n);
        if(minR == makstr) return -1;
        for(int i = 1; i <= minR.length(); i++)
            s[i] = minR[i-1];
        return minR.length();
    }
    else if(n == 3)
    {
        string ss[3];
        for(int j = 1; j <= n; j++)
            for(int i = 1; i <= l[j]; i++) ss[j-1] += a[j][i];
        sort(ss, ss+3, cmpDuz);
        int br1 = brPuta(ss[0], ss[1]), br2 = brPuta(ss[0], ss[2]), br3 = brPuta(ss[1], ss[2]);
        string minR = makstr, p, r;
        if(br1 > 1 || br2 > 1 || br3 > 1) return -1;
        else if(br1 == 1 && br2 == 1)
        {
            minR = ss[0];
        }
        else if(br1 == 1)
        {
            r = Resi2(ss[0], ss[2]);
            if(r.length() < minR.length() && valid(r, ss, n)) minR = r;
        }
        else if(br2 == 1 || br3 == 1)
        {
            r = Resi2(ss[0], ss[1]);
            if(r.length() < minR.length() && valid(r, ss, n)) minR = r;
        }
        else
        {
            // preklapanja
            for(int i = 0; i <= 2; i++)
            {
                for(int j = i+1; j <= 2; j++)
                {
                    p = preklopi(ss[i], ss[j]);
                    r = Resi2(p, ss[3-i-j]);
                    if(r.length() < minR.length() && valid(r, ss, n)) minR = r;
                    p = preklopi(ss[j], ss[i]);
                    r = Resi2(p, ss[3-i-j]);
                    if(r.length() < minR.length() && valid(r, ss, n)) minR = r;
                    p = ss[i] + ss[j];
                    r = Resi2(p, ss[3-i-j]);
                    if(r.length() < minR.length() && valid(r, ss, n)) minR = r;
                    p = ss[j] + ss[i];
                    r = Resi2(p, ss[3-i-j]);
                    if(r.length() < minR.length() && valid(r, ss, n)) minR = r;
                }
            }
        }
        if(minR == makstr) return -1;
        for(int i = 1; i <= minR.length(); i++)
            s[i] = minR[i-1];
        return minR.length();
    }*/
    else if(k == 2)
    {
        string ss[n];
            for(int j = 1; j <= n; j++)
                for(int i = 1; i <= l[j]; i++) ss[j-1] += a[j][i];
        for(int len = 1; len <= 18; len++)
        {
            for(int mask = 0; mask < (1 << len); mask++)
            {
                string str;
                for(int j = len-1; j >= 0; j--)
                    if((mask & (1 << j)) != 0) str += 'a';
                    else str += 'b';
                if(valid(str, ss, n))
                {
                    for(int i = 1; i <= str.length(); i++)
                        s[i] = str[i-1];
                    return str.length();
                }
            }
        }
        return -1;
    }
    else
    {
        string ss[n];
        bool potreban[n];
        int novoN = 0, ind = 0;
        for(int j = 1; j <= n; j++)
        {
            potreban[j-1] = true;
            for(int i = 1; i <= l[j]; i++) ss[j-1] += a[j][i];
        }
        sort(ss, ss+n, cmpDuz);
        for(int i = n-1; i >= 0; i--)
        {
            for(int j = 0; j < i; j++)
            {
                int br = brPuta(ss[j], ss[i]);
                if(br == 1) potreban[i] = false;
                else if(br > 1) return -1;
            }
        }
        string novoSs[14];
        for(int i = 0; i < n; i++)
        {
            if(potreban[i])
            {
                novoSs[ind] = ss[i];
                ind++;
            }
        }
        string minR = ResiN(novoSs, ind);
        if(minR == makstr) return -1;
        for(int i = 1; i <= minR.length(); i++)
            s[i] = minR[i-1];
        return minR.length();
    }
    return -1;
}