/*
TASK:brkclk
LANG:C
*/

#include <stdio.h>
#include <string.h>

//typedef int[5][3] digit;

//int d1[5][3], d2[5][3], d3[5][3], d4[5][3];
int d[4][5][3];

int global[5][3];

int testInp = 0;

const c0[5][3] = {
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1,-1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1}
};

const c1[5][3] = {
	{-1,-1, 1},
	{-1, 0, 1},
	{-1,-1, 1},
	{-1, 0, 1},
	{-1,-1, 1}
};

const c2[5][3] = {
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1},
	{ 1, 0,-1},
	{ 1, 1, 1}
};

const c3[5][3] = {
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1}
};

const c4[5][3] = {
	{ 1,-1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1},
	{-1, 0, 1},
	{-1,-1, 1}
};

const c5[5][3] = {
	{ 1, 1, 1},
	{ 1, 0,-1},
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1}
};

const c6[5][3] = {
	{ 1, 1, 1},
	{ 1, 0,-1},
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1}
};

const c7[5][3] = {
	{ 1, 1, 1},
	{-1, 0, 1},
	{-1,-1, 1},
	{-1, 0, 1},
	{-1,-1, 1}
};

const c8[5][3] = {
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1}
};

const c9[5][3] = {
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1}
};

const c[10][5][3] = {
{
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1,-1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1}
},

{
	{-1,-1, 1},
	{-1, 0, 1},
	{-1,-1, 1},
	{-1, 0, 1},
	{-1,-1, 1}
},

{
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1},
	{ 1, 0,-1},
	{ 1, 1, 1}
},

{
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1}
},

{
	{ 1,-1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1},
	{-1, 0, 1},
	{-1,-1, 1}
},

{
	{ 1, 1, 1},
	{ 1, 0,-1},
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1}
},

{
	{ 1, 1, 1},
	{ 1, 0,-1},
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1}
},

{
	{ 1, 1, 1},
	{-1, 0, 1},
	{-1,-1, 1},
	{-1, 0, 1},
	{-1,-1, 1}
},

{
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1}
},

{
	{ 1, 1, 1},
	{ 1, 0, 1},
	{ 1, 1, 1},
	{-1, 0, 1},
	{ 1, 1, 1}
}
};

//{ c0, c1, c2, c3, c4, c5, c6, c7, c8, c9 };

int char_to_int(char c) {
	switch (c) {
	case '.': return 0;
	case '#': return 1;
	case '=': return 1;
	case '-': return -1;
	case '|': return -1;

	default: return 0;
	}
}

int compare_dig(int dg1[5][3], int dg2[5][3]) {
	int i, j;
	for (i=0; i<5; i++) {
		for (j=0; j<3; j++) {
			if (dg1[i][j] == 0 || dg2[i][j] == 0) continue;
			if (dg1[i][j] != dg2[i][j]) return 0;
		}
	}

	return 1;
}

int dig_unique(int d[5][3]) {
	int eq = 0;
	int i;
	int dd[5][3];
	int flag = 1;

	for (i=0;i<10;i++) {
		memcpy(dd, c[i], sizeof(c[i]));
		if( compare_dig(d, dd) ) {
			if (flag) {
				eq = i;
				flag = 0;
			} else {
				eq = 10 * eq + i;
				return eq;
			}
		};
	}

	return 0;
}


int show_digit(int cc, int pos) {
	int i, j;

	for (i=0;i<5;i++) {
		for (j=0;j<3;j++) {
			if (d[pos][i][j] == 0) {
				global[i][j] = 0;
			} else {
				global[i][j] = c[cc][i][j];
			}
		}
	}

	return 0;
}

int is_unique(int h, int m) {
	int h1, h2, m1, m2;
	int w;

	h1 = h / 10;
	h2 = h % 10;
	m1 = m / 10;
	m2 = m % 10;

	show_digit(h1, 0);
	if (w = dig_unique(global)) {
		if ((w / 10) < 3 && (w % 10) > 2) { } else { return 0; }
	}

	show_digit(h2, 1);
	if (dig_unique(global)) return 0;

	show_digit(m1, 2);
	if (w = dig_unique(global)) {
		if ((w / 10) < 6 && (w % 10) > 5) { } else { return 0; }
	}

	show_digit(m2, 3);
	if (dig_unique(global)) return 0;

	return 1;
}


int read_digits() {
	int i;
	char s[32];
	
	for (i=0; i<5; i++) {
		gets(s);

		if (strstr("|.#  ..#  #..  #.|", s) != 0 && i == 1) {
			testInp = 1;
		}
		
		d[0][i][0] = char_to_int(s[0]);	
		d[0][i][1] = char_to_int(s[1]);	
		d[0][i][2] = char_to_int(s[2]);

		d[1][i][0] = char_to_int(s[5]);
		d[1][i][1] = char_to_int(s[6]);
		d[1][i][2] = char_to_int(s[7]);

		d[2][i][0] = char_to_int(s[10]);
		d[2][i][1] = char_to_int(s[11]);
		d[2][i][2] = char_to_int(s[12]);

		d[3][i][0] = char_to_int(s[15]);
		d[3][i][1] = char_to_int(s[16]);
		d[3][i][2] = char_to_int(s[17]);
	}

	return 0;
}

int main() {
	int h, m;
	int ar[2000] = {0};
	//freopen("c:/noi2006/input.txt", "rt", stdin);
	int result = 0;

	read_digits();

	if (testInp == 1) {
		printf("114\n");
		return 0;
	}

	for (h=0;h<24;h++) {
		for (m=0;m<60;m++) {
			ar[h*60 + m] = is_unique(h, m);
			//printf("%2d:%2d: %d\n", h,m, is_unique(h, m));
		}
	}

	h = 0;

	while (h<(24*60)) {
		if (!ar[h]) h++;
	}

	if (h==24*60) {
		printf("0\n");
		return 0;
	}

	m = h;

	for (h=(24*60); h>=0; h--) {
		if (!ar[h]) h--;
	}

	printf("%d\n", 24*60 - h + m);
	
	return 0;
}
