#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 maksDuz = 0, l1 = a.length(), l2 = b.length();
    int minlen = min(l1, l2);
    for(int duz = 1; duz <= minlen; duz++)
        if(a.substr(l1 - duz) == b.substr(0, duz))
            maksDuz = duz;
    b = b.substr(maksDuz);
    return a + b;
}

string Resi2(int* l, string aa, string bb, char* s)
{
    if(l[2] > l[1]) swap(aa, bb);
    int br = brPuta(aa, bb);
    if(br == 1)
    {
        for(int i = 1; i <= aa.length(); i++)
            s[i] = aa[i-1];
        return aa;
    }
    else if(br > 1) return makstr;
    else
    {
        string p1 = preklopi(aa, bb);
        string p2 = preklopi(bb, aa);
        if(p1.length() > p2.length()) swap(p1, p2);
        for(int i = 1; i <= p1.length(); i++)
            s[i] = p1[i-1];
        return p1;
    }
}

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

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];
        return Resi2(l, aa, bb, s).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 = ss[0] + ss[1] + ss[2], p, r;
        if(br1 > 1 || br2 > 1 || br3 > 1) return -1;
        else if(br1 == 1)
        {
            r = Resi2(l, ss[0], ss[2], s);
            if(r.length() < minR.length()) minR = r;
        }
        else if(br2 == 1)
        {
            r = Resi2(l, ss[0], ss[1], s);
            if(r.length() < minR.length()) minR = r;
        }
        else if(br3 == 1)
        {
            r = Resi2(l, ss[0], ss[1], s);
            if(r.length() < minR.length()) minR = r;
        }
        else
        {

            p = preklopi(ss[0], ss[1]);
            r = Resi2(l, p, ss[2], s);
            if(r.length() < minR.length()) minR = r;
            p = preklopi(ss[1], ss[0]);
            r = Resi2(l, p, ss[2], s);
            if(r.length() < minR.length()) minR = r;
            p = preklopi(ss[0], ss[2]);
            r = Resi2(l, p, ss[1], s);
            if(r.length() < minR.length()) minR = r;
            p = preklopi(ss[2], ss[0]);
            r = Resi2(l, p, ss[1], s);
            if(r.length() < minR.length()) minR = r;
            p = preklopi(ss[1], ss[2]);
            r = Resi2(l, p, ss[0], s);
            if(r.length() < minR.length()) minR = r;
            p = preklopi(ss[2], ss[1]);
            r = Resi2(l, p, ss[0], s);
            if(r.length() < minR.length()) minR = r;
        }
        for(int i = 1; i <= minR.length(); i++)
            s[i] = minR[i-1];
        return minR.length();
    }
    return -1;
}