#include<cstdio>
//#include<string>
//using namespace std;

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

string prvi="",drugi="",temp="",temp2="";
int cnt;

int Resi(int N, int K, int* L, char** A, char* S) {

	if (N == 2) {

        for (int i=1;i<=N;i++) {
            for (int j=1;j<=L[i];j++) {
                if (i==1) prvi+=A[i][j];
                else drugi+=A[i][j];
            }
        }

        for (int i=0;i<prvi.size();i++) {
            if (prvi[i] > 97+K-1) return -1;
        }

        for (int i=0;i<drugi.size();i++) {
            if (drugi[i] > 97+K-1) return -1;
        }

        if (prvi.size() < drugi.size()) swap(prvi,drugi);

        cnt=0;
        for (int i=0;i<prvi.size()-drugi.size()+1;i++) {
            if (prvi.substr(i,drugi.size()) == drugi) cnt++;
        }

        if (cnt > 1) return -1;
        else if (cnt == 1) {
            for (int i=0;i<prvi.size();i++) S[i+1]=prvi[i];
            return prvi.size();
        }
        else {

            string s1="",s2="",s3="",s4="";

            cnt=0;
            temp="";
            for (int i=0;i<drugi.size();i++) {
                temp+=drugi[i];   /// stavlja se na kraj;

                if (prvi.substr(prvi.size()-temp.size(),temp.size()) == temp) cnt=temp.size();

            }

            for (int i=0;i<prvi.size();i++) s1+=prvi[i];
            for (int i=cnt;i<drugi.size();i++) s1+=drugi[i];


            cnt=0;
            temp="";
            temp2="";
            for (int i=drugi.size()-1;i>=0;i--) {
                temp2=drugi[i];   /// stavlja se na pocetak;
                temp2+=temp;
                temp=temp2;

                if (prvi.substr(0,temp.size()) == temp) cnt=temp.size();
            }

            if (cnt == 0) cnt=drugi.size();
            for (int i=0;i<cnt;i++) s2+=drugi[i];
            for (int i=0;i<prvi.size();i++) s2+=prvi[i];

//            s3 = prvi+drugi;
//            s4 = drugi+prvi;

            cnt=0;
            temp="";
            for (int i=0;i<prvi.size();i++) {
                temp+=prvi[i];

                if (temp.size() > drugi.size()) break;

                if (drugi.substr(drugi.size()-temp.size(),temp.size()) == temp) cnt=temp.size();
            }

            for (int i=0;i<drugi.size();i++) s3+=drugi[i];
            for (int i=cnt;i<prvi.size();i++) s3+=prvi[i];

            cnt=0;
            temp="";
            temp2="";
            for (int i=prvi.size()-1;i>=0;i--) {
                temp2=prvi[i];
                temp2+=temp;
                temp=temp2;

                if (temp.size() > drugi.size()) break;

                if (drugi.substr(0,temp.size()) == temp) cnt=temp.size();
            }

            if (cnt == 0) cnt=prvi.size();
            for (int i=0;i<cnt;i++) s4+=prvi[i];
            for (int i=0;i<drugi.size();i++) s4+=drugi[i];
//

            bool s1ne=false,s2ne=false,s3ne=false,s4ne=false;



            /// provera
            cnt=0;
            for (int i=0;i<s1.size()-prvi.size()+1;i++) {
                if (s1.substr(i,prvi.size()) == prvi) cnt++;
            }

            if (cnt > 1) s1ne=true;

            cnt=0;
            for (int i=0;i<s1.size()-drugi.size()+1;i++) {
                if (s1.substr(i,drugi.size()) == drugi) cnt++;
            }

            if (cnt > 1) s1ne=true;
            /// provera
            cnt=0;
            for (int i=0;i<s2.size()-prvi.size()+1;i++) {
                if (s2.substr(i,prvi.size()) == prvi) cnt++;
            }

            if (cnt > 1) s2ne=true;

            cnt=0;
            for (int i=0;i<s2.size()-drugi.size()+1;i++) {
                if (s2.substr(i,drugi.size()) == drugi) cnt++;
            }

            if (cnt > 1) s2ne=true;
            /// provera



            /// provera
            cnt=0;
            for (int i=0;i<s3.size()-prvi.size()+1;i++) {
                if (s3.substr(i,prvi.size()) == prvi) cnt++;
            }

            if (cnt > 1) s3ne=true;

            cnt=0;
            for (int i=0;i<s3.size()-drugi.size()+1;i++) {
                if (s3.substr(i,drugi.size()) == drugi) cnt++;
            }

            if (cnt > 1) s3ne=true;
            /// provera
            cnt=0;
            for (int i=0;i<s4.size()-prvi.size()+1;i++) {
                if (s4.substr(i,prvi.size()) == prvi) cnt++;
            }

            if (cnt > 1) s4ne=true;

            cnt=0;
            for (int i=0;i<s4.size()-drugi.size()+1;i++) {
                if (s4.substr(i,drugi.size()) == drugi) cnt++;
            }

            if (cnt > 1) s4ne=true;
            /// provera


            if (s1ne == true && s2ne == true && s3ne == true && s4ne == true) return -1;
            else if (s2ne == true && s3ne == true && s4ne == true) {
                cnt = s1.size();
                for (int i=0;i<s1.size();i++) S[i+1]=s1[i];
            }
            else if (s1ne == true && s3ne == true && s4ne == true) {
                cnt = s2.size();
                for (int i=0;i<s2.size();i++) S[i+1]=s2[i];
            }
            else if (s1ne == true && s1ne == true && s4ne == true) {
                cnt = s3.size();
                for (int i=0;i<s3.size();i++) S[i+1]=s3[i];
            }
            else if (s1ne == true && s2ne == true && s3ne == true) {
                cnt = s4.size();
                for (int i=0;i<s4.size();i++) S[i+1]=s4[i];
            }
            else if (s3ne == true && s4ne == true) {
                cnt = min(s1.size(),s2.size());
                if (cnt==s1.size()) for (int i=0;i<s1.size();i++) S[i+1]=s1[i];
                else for (int i=0;i<s2.size();i++) S[i+1]=s2[i];
            }
            else if (s2ne == true && s4ne == true) {
                cnt = min(s1.size(),s3.size());
                if (cnt==s1.size()) for (int i=0;i<s1.size();i++) S[i+1]=s1[i];
                else for (int i=0;i<s3.size();i++) S[i+1]=s3[i];
            }
            else if (s1ne == true && s4ne == true) {
                cnt = min(s2.size(),s3.size());
                if (cnt==s2.size()) for (int i=0;i<s2.size();i++) S[i+1]=s2[i];
                else for (int i=0;i<s3.size();i++) S[i+1]=s3[i];
            }
            else if (s2ne == true && s3ne == true) {
                cnt = min(s1.size(),s4.size());
                if (cnt==s1.size()) for (int i=0;i<s1.size();i++) S[i+1]=s1[i];
                else for (int i=0;i<s4.size();i++) S[i+1]=s4[i];
            }
            else if (s1ne == true && s3ne == true) {
                cnt = min(s2.size(),s4.size());
                if (cnt==s2.size()) for (int i=0;i<s2.size();i++) S[i+1]=s2[i];
                else for (int i=0;i<s4.size();i++) S[i+1]=s4[i];
            }
            else if (s1ne == true && s2ne == true) {
                cnt = min(s3.size(),s4.size());
                if (cnt==s3.size()) for (int i=0;i<s3.size();i++) S[i+1]=s3[i];
                else for (int i=0;i<s4.size();i++) S[i+1]=s4[i];
            }
            else if (s1ne == true) {
                cnt = min(s2.size(),min(s3.size(),s4.size()));
                if (cnt==s2.size()) for (int i=0;i<s2.size();i++) S[i+1]=s2[i];
                else if (cnt==s3.size()) for (int i=0;i<s3.size();i++) S[i+1]=s3[i];
                else for (int i=0;i<s4.size();i++) S[i+1]=s4[i];
            }
            else if (s2ne == true) {
                cnt = min(s1.size(),min(s3.size(),s4.size()));
                if (cnt==s1.size()) for (int i=0;i<s1.size();i++) S[i+1]=s1[i];
                else if (cnt==s3.size()) for (int i=0;i<s3.size();i++) S[i+1]=s3[i];
                else for (int i=0;i<s4.size();i++) S[i+1]=s4[i];
            }
            else if (s3ne == true) {
                cnt = min(s1.size(),min(s2.size(),s4.size()));
                if (cnt==s1.size()) for (int i=0;i<s1.size();i++) S[i+1]=s1[i];
                else if (cnt==s2.size()) for (int i=0;i<s2.size();i++) S[i+1]=s2[i];
                else for (int i=0;i<s4.size();i++) S[i+1]=s4[i];
            }
            else if (s4ne == true) {
                cnt = min(s1.size(),min(s2.size(),s3.size()));
                if (cnt==s1.size()) for (int i=0;i<s1.size();i++) S[i+1]=s1[i];
                else if (cnt==s2.size()) for (int i=0;i<s2.size();i++) S[i+1]=s2[i];
                else for (int i=0;i<s3.size();i++) S[i+1]=s3[i];
            }
            else {
                cnt = min(s1.size() , min(s2.size() , min(s3.size() , s4.size())));
                if (cnt==s1.size()) for (int i=0;i<s1.size();i++) S[i+1]=s1[i];
                else if (cnt==s2.size()) for (int i=0;i<s2.size();i++) S[i+1]=s2[i];
                else if (cnt==s3.size()) for (int i=0;i<s3.size();i++) S[i+1]=s3[i];
                else for (int i=0;i<s4.size();i++) S[i+1]=s4[i];
            }

            return cnt;


        }



	}

	return 1;
}