A browser-based puzzle game called "Bubble Puzzle" is now popular on the Internet.

The puzzle is played on a 4 × 4 grid, and initially there are several bubbles in the grid squares. Each bubble has a state expressed by a positive integer, and the state changes when the bubble gets stimulated. You can stimulate a bubble by clicking on it, and this will increase the bubble's state by 1. You can also click on an empty square. In this case, a bubble with state 1 is newly created in the square.

When the state of a bubble becomes 5 or higher, the bubble blows up and disappears, and small waterdrops start flying in four directions (up, down, left and right). These waterdrops move one square per second. At the moment when a bubble blows up, 4 waterdrops are in the square the bubble was in, and one second later they are in the adjacent squares, and so on.

A waterdrop disappears either when it hits a bubble or goes outside the grid. When a waterdrop hits a bubble, the state of the bubble gets increased by 1. Similarly, if a bubble is hit by more than one water drop at the same time, its state is increased by the number of the hitting waterdrops. Please note that waterdrops do not collide with each other.

<image>

As shown in the figure above, waterdrops created by a blow-up may cause other blow-ups. In other words, one blow-up can trigger a chain reaction. You are not allowed to click on any square while waterdrops are flying (i.e., you have to wait until a chain reaction ends). The goal of this puzzle is to blow up all the bubbles and make all the squares empty as quick as possible.

Your mission is to calculate the minimum number of clicks required to solve the puzzle.



Input

The input consists of 4 lines, each contains 4 nonnegative integers smaller than 5. Each integer describes the initial states of bubbles on grid squares. 0 indicates that the corresponding square is empty.

Output

Output the minimum number of clicks to blow up all the bubbles on the grid in a line. If the answer is bigger than 5, output -1 instead. No extra characters should appear in the output.

Examples

Input

4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4


Output

1


Input

2 4 4 1
2 4 4 1
2 4 4 1
2 4 4 1


Output

5


Input

2 4 3 4
2 2 4 4
3 3 2 2
2 3 3 3


Output

3
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 4
#define LIMIT 5

enum DIR{
	North,
	East,
	South,
	West,
};
DIR dir_array[4] = {North,East,South,West};
int diff_row[4] = {-1,0,1,0},diff_col[4] = {0,1,0,-1};


struct Info{
	int table[NUM][NUM],max_value,click_count;
};

struct Data{
	Data(){
		row = col = 0;
		dir = North;
	}

	Data(int arg_row,int arg_col,DIR arg_dir){
		row = arg_row;
		col = arg_col;
		dir = arg_dir;
	}
	int row,col;
	DIR dir;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= NUM-1 && col >= 0 && col <= NUM-1)return true;
	else{
		return false;
	}
}

int getMaxValue(int table[NUM][NUM]){

	int ret = 0;

	for(int row = 0; row < NUM; row++){
		for(int col = 0; col < NUM; col++){
			ret = max(ret,table[row][col]);
		}
	}

	return ret;
}

void copyInfo(Info& to,Info from){

	for(int row = 0; row < NUM; row++){
		for(int col = 0; col < NUM; col++){
			to.table[row][col] = from.table[row][col];
		}
	}

	to.click_count = from.click_count+1;
}

void calc(Info& info,int first_row,int first_col){

	info.table[first_row][first_col] = 0;

	queue<Data> Q,NEXT;

	int adj_row,adj_col;

	for(int i = 0; i < 4; i++){
		adj_row = first_row+diff_row[i];
		adj_col = first_col+diff_col[i];

		if(!rangeCheck(adj_row,adj_col))continue;

		Q.push(Data(adj_row,adj_col,dir_array[i]));
	}

	Data tmp;
	int next_row,next_col;

	while(true){

		while(!Q.empty()){
			tmp = Q.front();
			Q.pop();

			if(info.table[tmp.row][tmp.col] == 0){

				switch(tmp.dir){
				case North:
					next_row = tmp.row-1;
					next_col = tmp.col;
					break;
				case East:
					next_row = tmp.row;
					next_col = tmp.col+1;
					break;
				case South:
					next_row = tmp.row+1;
					next_col = tmp.col;
					break;
				case West:
					next_row = tmp.row;
					next_col = tmp.col-1;
					break;
				}

				if(rangeCheck(next_row,next_col)){
					NEXT.push(Data(next_row,next_col,tmp.dir));
				}

			}else{
				info.table[tmp.row][tmp.col]++;
			}
		}

		for(int row = 0; row < NUM; row++){
			for(int col = 0; col < NUM; col++){
				if(info.table[row][col] >= 5){
					info.table[row][col] = 0;

					for(int i = 0; i < 4; i++){

						adj_row = row+diff_row[i];
						adj_col = col+diff_col[i];

						if(!rangeCheck(adj_row,adj_col))continue;

						NEXT.push(Data(adj_row,adj_col,dir_array[i]));
					}
				}
			}
		}

		if(NEXT.empty())break;
		while(!NEXT.empty()){
			Q.push(NEXT.front());
			NEXT.pop();
		}
	}
}


int main(){

	Info first;
	for(int row = 0; row < NUM; row++){
		for(int col = 0; col < NUM; col++)scanf("%d",&first.table[row][col]);
	}

	first.max_value = getMaxValue(first.table);
	first.click_count = 0;

	queue<Info> Q;
	Q.push(first);

	bool cleard = false;

	while(!Q.empty()){

		if(Q.front().max_value == 0){
			printf("%d\n",Q.front().click_count);
			cleard = true;
			break;
		}else if(Q.front().click_count == LIMIT){
			Q.pop();
		}else if(LIMIT-Q.front().click_count < LIMIT-Q.front().max_value){
			Q.pop();
		}else{

			for(int row = 0; row < NUM; row++){
				for(int col = 0; col < NUM; col++){

					Info next_info;
					copyInfo(next_info,Q.front());
					next_info.table[row][col]++;

					if(next_info.table[row][col] == 5){
						calc(next_info,row,col);
					}
					next_info.max_value = getMaxValue(next_info.table);
					Q.push(next_info);				}
			}

			Q.pop();
		}
	}

	if(!cleard){
		printf("-1\n");
	}

	return 0;
}

