#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<vector>
#include<deque>
#include<set>
#include<map>
#include<sstream>

using namespace std;

#define PB push_back()
#define SZ(x) ((int)x.size())
#define PI 3.141592653589
#define REP(i,N) for(int(i)=0;(i)<(int)(N);++(i))

string s[10][7]=
{
	{
		"+---+",
		"|   |",
		"|   |",
		"+   +",
		"|   |",
		"|   |",
		"+---+",
	},
	{
		"    +",
		"    |",
		"    |",
		"    +",
		"    |",
		"    |",
		"    +",
	},
	{
		"+---+",
		"    |",
		"    |",
		"+---+",
		"|    ",
		"|    ",
		"+---+",
	},
	{
		"+---+",
		"    |",
		"    |",
		"+---+",
		"    |",
		"    |",
		"+---+",
	},
	{
		"+   +",
		"|   |",
		"|   |",
		"+---+",
		"    |",
		"    |",
		"    +",
	},
	{
		"+---+",
		"|    ",
		"|    ",
		"+---+",
		"    |",
		"    |",
		"+---+",
	},
	{
		"+---+",
		"|    ",
		"|    ",
		"+---+",
		"|   |",
		"|   |",
		"+---+",
	},
	{
		"+---+",
		"    |",
		"    |",
		"    +",
		"    |",
		"    |",
		"    +",
	},
	{
		"+---+",
		"|   |",
		"|   |",
		"+---+",
		"|   |",
		"|   |",
		"+---+",
	},
	{
		"+---+",
		"|   |",
		"|   |",
		"+---+",
		"    |",
		"    |",
		"+---+",
	},
};

char x[100];
char ss[7][100];

int main()
{
	while(gets(x),strcmp(x,"end")!=0)
	{
		strcpy(ss[0],x);
		REP(i,6) gets(ss[i+1]);
		gets(x);
		gets(x);
		int ret=0,ret2=0;
		int h1,h2,m1,m2;
		h1=h2=m1=m2=0;
		REP(h,24)
		{
			int m=0;
			int a=h/10;
			int b=h%10;
			int c=m/10;
			int d=m%10;
			string r[7];
			REP(i,7)
			{
				r[i]="";
				r[i]+=s[a][i]+"  ";
				r[i]+=s[b][i]+"  ";
				if (i==2||i==4) r[i]+="o";
				else r[i]+=" ";
				r[i]+="  ";
				r[i]+=s[c][i]+"  ";
				r[i]+=s[d][i];
			}
			int ok=1;
			REP(i,12)
			{
				REP(j,7)
				{
					if (ss[j][i]!='.')
					{
						if (ss[j][i]!=r[j][i])
						{
							ok=0;
							goto dalsi;
						}
					}
				}
			}
dalsi:
			;
			if (ok)
			{
				h1=a;
				h2=b;
				ret++;
			}
		}
		if (ret==1)
		REP(m,60)
		{
			int h=0;
			int a=h/10;
			int b=h%10;
			int c=m/10;
			int d=m%10;
			string r[7];
			REP(i,7)
			{
				r[i]="";
				r[i]+=s[a][i]+"  ";
				r[i]+=s[b][i]+"  ";
				if (i==2||i==4) r[i]+="o";
				else r[i]+=" ";
				r[i]+="  ";
				r[i]+=s[c][i]+"  ";
				r[i]+=s[d][i];
			}
			int ok=1;
			for(int i=17;i<SZ(r[0]);++i)
			{
				REP(j,7)
				{
					if (ss[j][i]!='.')
					{
						if (ss[j][i]!=r[j][i])
						{
							ok=0;
							goto dalsi2;
						}
					}
				}
			}
dalsi2:
			;
			if (ok)
			{
				m1=c;
				m2=d;
				ret2++;
			}
		}
		if (ret!=1||ret2!=1) puts("ambiguous");
		else cout<<h1<<h2<<":"<<m1<<m2<<endl;
	}
	puts("end");
	return 0;
}
