#include <bits/stdc++.h>

using namespace std;

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;
}

int Resi(int n, int k, int* l, char a[100][100], char* s)
{
    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];
        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.length();
        }
        else if(br > 1) return -1;
        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.length();
        }
    }
    return -1;
}