#include<bits/stdc++.h>
using namespace std;
queue<int> q;
vector<int> g[30];
bool vis[30],certain;
int ind[30],temp[30],topo[30];
int n,m;
bool toposort(int cnt)
{
memset(temp,0,sizeof(temp));
memset(topo,0,sizeof(topo));
while(!q.empty()) q.pop();
certain = 1;
for(int i = 0; i < n; i ++) temp[i] = ind[i];
for(int i = 0; i < n; i ++){
if(!temp[i]){
q.push(i);
}
}
int depth = 0;
while(!q.empty())
{
int t = 0;
int fr = q.front(); q.pop();
depth ++;
topo[depth] = fr;
for(int i = 0; i < (int)g[fr].size(); i ++){
int v = g[fr][i];
if(--temp[v] == 0){
t ++;
q.push(v);
}
}
if(t > 1) certain = 0;
}
if(depth < cnt) return 0;
else return 1;
}
int main()
{
int cnt = 0;
cin >> n >> m;
for(int i = 1; i <= m; i ++){
string s;
cin >> s;
char a = s[0], b = s[2];
int ta = a-'A', tb = b-'A';
g[ta].push_back(tb);
ind[tb] ++;
if(!vis[ta]){
vis[ta] = 1;
cnt ++;
}
if(!vis[tb]){
vis[tb] = 1;
cnt ++;
}
if(!toposort(cnt)){
cout << "Inconsistency found after " << i << " relations.";
exit(0);
}
else{
if(cnt == n && certain){
cout << "Sorted sequence determined after " << i << " relations: ";
for (int i = 1; i <= n; ++i) cout << (char)(topo[i] + 'A');
cout << '.';
exit(0);
}
}
}
cout << "Sorted sequence cannot be determined.";
return 0;
}