#include <iostream>
#include <string>
using namespace std;
#include <climits>
bool val[26];
int raz = 0;
int Resi(int N, int K, int* L, char** A, char* S)
{
    if(N == 1)
    {
        S = A[1];
        return L[1];
    }
    if(N == 2)
    {
        char* a = A[1];
        char* b = A[2];
        int la, lb;
        la = L[1];
        lb = L[2];
        if(L[1] < L[2])
        {
            char* p = b;
            b = a;
            a = p;
            la = L[2];
            lb = L[1];
        }
        for(int i = 1; i <= la; i++)
        {
            if(!val[a[i] - 'a'])
            {
                val[a[i] - 'a'] = 1;
                raz++;
                if(raz > K) return -1;
            }
        }
        int in = 0;
        for(int i = 1; i <= la - lb + 1; i++)
        {
            int j;
            for(j = 1; j <= lb && a[i + j - 1] == b[j]; j++);
            if(j == lb + 1) in++;
            if(in == 2) return -1;
        }
        if(in == 1)
        {
            for(int i = 1; i <= la; i++) S[i] = a[i];
            return la;
        }
        int m = INT_MAX;
        for(int i = la - lb + 2; i <= la; i++)
        {
            int j;
            for(j = 1; i + j - 1 <= la && a[i + j - 1] == b[j]; j++);
            if(j == la + 2 - i)
            {
                for(int k = 1; k <= la; k++) S[k] = a[k];
                for(int k = 1; j + k - 1 <= lb; k++) S[la + k] = b[j + k - 1];
                m = la + lb + 1 - j;
                i = la + 1;
            }
        }
        for(int i = lb - 1; i >= 1; i--)
        {
            int j;
            for(j = 1; j <= i && a[j] == b[lb - i + j]; j++);
            if(j == i + 1)
            {
                if(la + lb - i < m)
                {
                    for(int k = 1; k <= lb; k++) S[k] = b[k];
                    for(int k = 1; k <= la - i; k++) S[lb + k] = a[i + k];
                    m = la + lb - i;
                }
                i = 0;
            }
        }
        if(m != INT_MAX) return m;
        for(int i = 1; i <= la; i++) S[i] = a[i];
        for(int i = 1; i <= lb; i++) S[la + i] = b[i];
        return la + lb;
    }
    return -1;
}