//dfs用一个数组就可以得出所有路径了
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int u[21][3], m;
int vis[25];
int path[25];//存储路径
int cnt = 1;
void dfs(int d, int s)
{
if(d == 20 && s == m){
cout << cnt++ << ": ";
for(int i=0; i<20; i++){
cout << path[i] << " ";
}
cout << path[0] << endl;
return;
}
for(int i=0; i<3; i++){
int tmp = u[s][i];
// cout << tmp << endl;
if(!vis[tmp]){
vis[tmp] = 1;
path[d] = s;
dfs(d+1, tmp);
vis[tmp] = 0;
}
}
}
int main()
{
for(int i=1; i<=20; i++){
for(int j=0; j<3; j++){
cin >> u[i][j];
}
}
while(cin >> m && m){
memset(vis, 0, sizeof(vis));
dfs(0, m);
}
return 0;
}