#include <bits/stdc++.h>
//#include <code.h>
//#include "code.h"

using namespace std;



struct TrieNode {
    struct TrieNode *children[26];
    bool endofword;
};

struct TrieNode *GetNode(void){
    struct TrieNode *exist = new TrieNode();

    for (int i = 0; i < 26; i++){
        exist->children[i]=NULL;
    }

    exist->endofword = false;

    return exist;
}

string voicewords[14];

int K1 = 26;

void FilmInsert(struct TrieNode *node, int wordchoice, int howfar, bool lit){

    //cout << "we are inserting word: " << voicewords[wordchoice] << "\n";
    if (howfar == voicewords[wordchoice].size()){
        node->endofword = true;
        //cout << "ACTION: we have ended a word! " << voicewords[wordchoice] << "\n";
    }
    else {
        bool dimlylit = false;
        if (node->endofword && !lit){
            dimlylit = true;
            //cout << "ACTION: we have dimly lit the word " << voicewords[wordchoice] << "\n";
        }

        if (dimlylit || lit){
            if (!node->children[voicewords[wordchoice][howfar] - 'a']){
                node->children[voicewords[wordchoice][howfar] - 'a'] = GetNode();
                FilmInsert(node->children[voicewords[wordchoice][howfar] - 'a'], wordchoice, howfar+1, true);
            }
        }

        for (int i = 0; i < K1 && !lit; i++){
            if (node->children[i]){
                if ('a' + i == voicewords[wordchoice][howfar]){
                    FilmInsert(node->children[i], wordchoice, howfar+1, false);
                }
                else {
                    FilmInsert(node->children[i], wordchoice, 0, false);
                }
            }
        }

    }
}

pair<string, int> FilmSearch(struct TrieNode *node, int N, string sofar, string occurences){
    //node->children[0] = NULL;

    //occurences = "muahahha";

    for (int i = 0; i < N && node->endofword; i++){
        if (sofar.size() >= voicewords[i].size() && sofar.size() > 0){
            //cout << "sup we are at " << voicewords[i] << " with the comparison of " << sofar.substr(sofar.size()-voicewords[i].size(), voicewords[i].size()) << "\n";
            if (sofar.substr(sofar.size()-voicewords[i].size(), voicewords[i].size()) == voicewords[i]){
                //cout << "times we\n";
                if(occurences[i] == '1'){
                    //cout << "oh fuck, overflow\n";
                    return {"", 999999999};
                }
                else {
                    //cout << "in here, adding 1\n";
                    occurences[i] = '1';
                }
            }
        }
    }

    pair<string, int> notgood = {"", 999999999};
    bool hasbeen = false;
    for (int i = 0; i<K1; i++){
        if (node->children[i]){
            hasbeen = true;
            string addition = "";
            addition += 'a' + i;
            pair<string,int> nextpair = FilmSearch(node->children[i], N, sofar+addition, occurences);
            if (nextpair.second < notgood.second){
                notgood = nextpair;
            }
        }
    }

    if(!hasbeen){
        bool danger = false;
        for (int i=0; i<N && !danger; i++){
            if(occurences[i] == '0'){
                //cout << "DANGER AT "<< i <<"! " << occurences << "\n";
                danger = true;
            }
        }
        if (!danger)
            return {sofar, sofar.size()};
        else

            return {"", 999999999};
    }
    else {
        return notgood;
    }

    //return {"", 1};
}

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

    K1 = K;

    struct TrieNode *root = GetNode();
    root->endofword = true;

    for (int i = 0; i < N; i++){
        string voiceword = "";
        for (int j = 0; j < L[i+1]; j++){
            voiceword += A[i+1][j+1];
        }
    }


    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            FilmInsert(root, j, 0, false);
        }
    }


    pair<string, int> death = FilmSearch(root, N, "", "00000000000000000000000000");

    if (death.second > 1000000){
        return -1;
    }

    for (int i = 0; i < death.second; i++){
        S[i+1] = death.first[i];
    }
    return death.second;

}