#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<drugi.size()-cnt;i++) s2+=drugi[i];
            for (int i=0;i<prvi.size();i++) s2+=prvi[i];

            if (s1.size()<s2.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];

            return min(s1.size(),s2.size());

        }

	}
	else {
        for (int i=1;i<=L[1];i++) S[i]=A[1][i];
        return L[1];
	}
}