/*
给定一组偏序关系,问最少第几步能确定次序
如果出现环,问第几步出现环
因为要求第几步确定次序或者第几步出现环,所以每次读入一个偏序关系就进行一次拓扑排序
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N = ;
int in[N]; //入度
bool Map[N][N]; //临接矩阵此题可以随便用
char out[N]; //拓扑排序输出数组 int topsort( int n ) {
priority_queue<int,vector<int>,greater<int> > q; //优先队列忠实粉丝
int flag = ,k;
int zeroflag = ;
int num = ,temp[N];
memcpy(temp,in,sizeof(in));//拷贝
for( int i = ; i < n; i++ ) {
if( !in[i] ) q.push(i);
}
while( !q.empty() ) {
if( q.size() > )
zeroflag = ;//要注意在while里面判断 因为此过程中也可能出现入度0不止一个的情况
k = q.top();
q.pop();
num++;
out[num] = k + 'A'; //num计数 以存到out里面
for(int i = ; i < n; i++ )
if(Map[k][i]== && --temp[i] == )
q.push(i);
}
if( num != n ) return ;
if( zeroflag == ) return ;//多个点入度为零
return -;
}
int main(){
int n,m,k;
int step; //记录操作数
int circleflag,orderflag;
char s[];
while( scanf("%d%d", &n, &m) != EOF && n ){
circleflag=;orderflag=;
memset(Map,,sizeof(Map));
memset(in,,sizeof(in));
for( int i = ; i <= m; i++ ){
scanf("%s", s);
if( circleflag == && orderflag == ){ //已经判出了 就不读了
if(Map[s[]-'A'][s[]-'A'] == ){
circleflag=; //有环了
printf("Inconsistency found after %d relations.\n", i);
continue ;
}
if(Map[s[]-'A'][s[]-'A'] == ){
Map[s[]-'A'][s[]-'A'] = ;
in[s[]-'A']++;
}
k = topsort(n);
if( k == ){
circleflag=;
printf("Inconsistency found after %d relations.\n", i);
}
else if( k == - ){
orderflag=;
step=i; //记录位置
}
}
}
if(circleflag == && orderflag == )
printf("Sorted sequence cannot be determined.\n");
else if(orderflag == ){
printf("Sorted sequence determined after %d relations: ", step);
for(int i=; i<=n; i++)
printf("%c", out[i]);
printf(".\n");
}
}
return ;
}