#include<bits/stdc++.h>
using namespace std;
vector<int>s,v[105];
int n,m,in[105],rin[105];
int topo()
{
queue<int>q;
int c=0;
for(int i=1;i<=n;i++)
{
rin[i]=in[i];
if(in[i]==0)
{
q.push(i);
c++;
}
}
if(c==0) return -1;
if(c>1) return 1;
while(!q.empty())
{
int f=q.front();
q.pop();
s.push_back(f);
c=0;
for(auto i : v[f])
{
if(--in[i]==0)
{
q.push(i);
c++;
}
}
if(c>1) return 1;
}
if(s.size()!=n) return -1;
return 0;
}
int main()
{
int r;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
char t1,t2,t0;
cin>>t1>>t0>>t2;
if(t1==t2) goto end;
v[t1-'A'+1].push_back(t2-'A'+1);
in[t2-'A'+1]++;
r=topo();
if(r==-1)
{
end:
printf("Inconsistency found after %d relations.",i);
return 0;
}
else if(r==0)
{
printf("Sorted sequence determined after %d relations: ",i);
for(auto j : s) printf("%c",j+'A'-1);
printf(".");
return 0;
}
for(int j=1;j<=n;j++) in[j]=rin[j];
s.clear();
}
printf("Sorted sequence cannot be determined.");
return 0;
}
WA on #4,#5,#6,#7,#10,#11