#include <bits/stdc++.h>

using namespace std;
string s[15];
int n;

bool proveri(string res)
{
    //cout<<"*"<<res<<endl;
    for(int i=0;i<n;i++)
    {
        //cout<<res<<" "<<s[i]<<endl;
        int br=0;
        //int br2=0;
        for(int j=0;j<=res.size()-s[i].size();j++)
        {
            if(res[j]==s[i][0])
            {
                //br2++;
                bool ok=true;
                int k;
                int poc=j;
                for(k=1;ok && k<s[i].size();k++)
                if(res[k+poc]!=s[i][k])
                        ok=false;

                if(ok) br++;
            }
        }
        //cout<<br2<<endl;
        //cout<<s[i]<<" ";
        //cout<<br<<endl;
        if(br>1)
            return false;
    }
    //cout<<"USO";
    return true;
}
bool proveri2(int i, int j)
{
    if(s[i].size()==s[j].size())
        return s[i]==s[j];
    string s1, s2;
    if(s[i].size()>s[j].size())
        s1=s[i], s2=s[j];
    else
        s1=s[j], s2=s[i];

    for(int i=0;i<=s1.size()-s2.size();i++)
    {
        if(s1[i]==s2[0])
        {
             bool ok=true;
             int k;
             int poc=j;
             for(k=1;ok && k+poc<s1.size();k++)
             if(s1[k+poc]!=s2[k])
                    ok=false;

             if(ok) return true;
        }
    }
    return false;
}

int Resi(int N, int K, int* L, char** A, char* S) {
    n=N;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=L[i];j++)
            s[i-1]+=A[i][j];

    //cout<<"OK";
    /*for(int i=0;i<n;i++)
        cout<<s[i].size();*/

    /*for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            if(proveri2(i,j))
                return -1;*/

    sort(s,s+n);


    int minsol=10000;
    string sol, tsol;

    do
    {
        tsol="";
        tsol=s[0];
        //cout<<sol<<endl;
        for(int i=1;i<n;i++)
        {
            bool ubacio=false;
            int p=(tsol.size()-s[i].size()+1);
            for(int j=0;j<tsol.size() && !ubacio;j++)
            {
                if(tsol[j]==s[i][0])
                {
                    bool ok =true;
                    int poc=j;
                    int k;
                    for(k=1;ok && k<s[i].size() && k+poc<tsol.size();k++)
                        if(tsol[k+poc]!=s[i][k])
                            ok=false;
                    if(ok)
                    {
                        //cout<<"USO "<<sol<<" "<<j<<endl;
                        while(k<s[i].size())
                        {
                            tsol+=s[i][k];
                            k++;
                        }
                        ubacio=true;
                    }
                }
            }
            if(!ubacio)
                tsol+=s[i];
        }
        if(proveri(tsol) && tsol.size()<minsol)
            sol=tsol, minsol=tsol.size();
        //cout<<sol<<endl;

        /*cout<<endl;
        for(int i=0;i<n;i++)
            cout<<s[i];*/
    }
    while(next_permutation(s,s+n));


    for(int i=0;i<sol.size();i++)
        S[i+1]=sol[i];

	//S[1] = 'a';
	if(minsol==10000)
        minsol=-1;
	return minsol;
}