#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]=
{
	{
		"+---+",
		"|   |",
		"|   |",
		"+   +",
		"|   |",
		"|   |",
		"+---+",
	},
	{
		"    +",
		"    |",
		"    |",
		"    +",
		"    |",
		"    |",
		"    +",
	},
	{
		"+---+",
		"    |",
		"    |",
		"+---+",
		"|    ",
		"|    ",
		"+---+",
	},
	{
		"+---+",
		"    |",
		"    |",
		"+---+",
		"    |",
		"    |",
		"+---+",
	},
	{
		"+   +",
		"|   |",
		"|   |",
		"+---+",
		"    |",
		"    |",
		"    +",
	},
	{
		"+---+",
		"|    ",
		"|    ",
		"+---+",
		"    |",
		"    |",
		"+---+",
	},
	{
		"+---+",
		"|    ",
		"|    ",
		"+---+",
		"|   |",
		"|   |",
		"+---+",
	},
	{
		"+---+",
		"    |",
		"    |",
		"    +",
		"    |",
		"    |",
		"    +",
	},
	{
		"+---+",
		"|   |",
		"|   |",
		"+---+",
		"|   |",
		"|   |",
		"+---+",
	},
	{
		"+---+",
		"|   |",
		"|   |",
		"+---+",
		"    |",
		"    |",
		"+---+",
	},
};

int main()
{
	string x;
	while(getline(cin,x),x!="end")
	{
		string ss[7];
		ss[0]=x;
		REP(i,6) getline(cin,ss[i+1]);
		getline(cin,x);
		getline(cin,x);
		int ret=0;
		int h1,h2,m1,m2;
		h1=h2=m1=m2=0;
		REP(h,24) REP(m,60)
		{
			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,SZ(ss[0]))
			{
				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;
				m1=c;
				m2=d;
				ret++;
			}
		}
		if (ret!=1) puts("ambiguous");
		else cout<<h1<<h2<<":"<<m1<<m2<<endl;
	}
	puts("end");
	return 0;
}
