#include "testlib.h"
using namespace std;
int MAX=100000, MAX_LEN=15;
char errMsg[1000];
void fail()
{
    quit(_fail, errMsg);
}
string read_word()
{
    string res="";
    while(true)
    {
        char c=inf.readChar();
        if(('a'<=c) && (c<='z')) res+=c; else
        {
            inf.unreadChar(c);
            break;
        }
    }
    if((res.length()<1) || (res.length()>MAX_LEN))
        sprintf(errMsg, "Invalid token '%s'", res.c_str()), fail();
    return res;
}
bool sorted(vector<string>& v)
{
    for(int i=0; i<v.size()-1; ++i)
        if(v[i]>v[i+1]) return false;
    return true;
}
int main()
{
    registerValidation();
    int n=inf.readInt(1, 100000); inf.readEoln();
    vector<string> words, defenitions;
    for(int i=0; i<n; ++i)
    {
        words.push_back(read_word()); defenitions.clear();
        inf.readChar(' '); inf.readChar('-'); inf.readChar(' ');
        defenitions.push_back(read_word());
        while(!inf.eoln())
        {
            inf.readChar(','); inf.readChar(' ');
            defenitions.push_back(read_word());
        }
        if(defenitions.size()+1>MAX)
            sprintf(errMsg, "Too many words in input file!"), fail();
        MAX-=defenitions.size()+1;
        if(!sorted(defenitions))
            sprintf(errMsg, "Description %d is invalid: defenitions are not sorted!", i+1), fail();
    }
    if(!sorted(words))
        sprintf(errMsg, "Words are not sorted!"), fail();
    return 0;
}
