#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;

#define For(i,a,b) for (int i = a; i < b; i++)

vector<string> shapes[100];

void output(vector<string>& v)
{
	For (i, 0, v.size())
	{	
		For (j, 0, v[i].size())
			cout << v[i][j];
		cout << endl;
	}
	cout << endl;
}

vector<string> rotate(vector<string> & v)
{
	vector<string> ret(v[0].size(), string(v.size(), '.'));
	For (i, 0, v.size())
		For (j, 0, v[0].size())
			ret[j][v.size()-i-1] = v[i][j];
	return ret;
}

void trim(vector<string>& v)
{
	For (i, 0, v.size())
		if (v[i].find('#') == -1)
			v.erase(v.begin() + i--);
		else
			break;
			
	for (int i = v.size()-1; i >= 0; i--)
		if (v[i].find('#') == -1)
			v.erase(v.begin()+i);
		else
			break;
	
	if (v.empty()) return;
	
	while (v[0].size())
	{
		For (j, 0, v.size())
			if (v[j][0] == '#') goto fail1;
		For (j, 0, v.size())
			v[j].erase(0, 1);
	}
	
	fail1:;
	
	while (v[0].size())
	{
		For (j, 0, v.size())
			if (v[j][v[j].size()-1] == '#') goto fail2;
		For (j, 0, v.size())
			v[j].erase(v[j].size()-1, 1);
	}
	
	fail2:;
	
	//output(v);
}

map<pair<vector<string>, int>,int> mem;
int N;

bool place(int r, int c, vector<string>& board, vector<string>& shape, vector<string>& ret)
{
	ret = board;
	
	//output(board); output(shape);
	
	if (r + shape.size() > 5 || c + shape[0].size() > 5) return false;
	For (i, 0, shape.size())
		For (j, 0, shape[i].size())
			if (shape[i][j] == '#' && board[i+r][j+c] == '#') return false;
			else if (shape[i][j] == '#') ret[i+r][j+c] = '#';
			
	return true;
}

clock_t start;
int ans = INT_MAX;
void go(vector<string> board, int mas, int x)
{
	if (x >= ans) return;
	if (mas == 0) {ans = min(ans, x); return; }
	
	if (double(clock()-start) / CLOCKS_PER_SEC > 1) return;
	
	//output(board);
	//cout << x << endl;
	
	bool ok = false;
	For (i, 0, N)
		if (mas & (1<<i))
		{
			vector<string> temp;
			For (rot, 0, 4)
			{
				For (j, 0, 5)
					For (k, 0, 5)
						if (place(j,k,board, shapes[i], temp))
						{
							ok = true;
							go(temp, mas ^ (1<<i), x);
							break;
						}
				shapes[i] = rotate(shapes[i]);
			}
		}
	
	if (!ok) go(vector<string>(5, string(5, '.')), mas, x+1);
	
	//output(board);cout << ret << endl;cout << mas << endl;
}

int main()
{
	freopen("DATA5.txt", "r", stdin);
	freopen("OUT5.txt", "w", stdout);

	For (T, 0, 5)
	{	
		cin >> N; cin.ignore();
		For (i, 0, N)
		{
			string s;
			shapes[i].clear();
			while (getline(cin,s) && s != "")
				shapes[i].push_back(s);
			trim(shapes[i]);
			if (shapes[i].empty()) { i--; N--; }
		}
		
		start = clock();
		vector<string> v(5, string(5, '.'));
		ans = INT_MAX;
		go(v, (1<<N)-1, 0);
		cout << ans+1 << endl;
	}
	
	//system("pause");
	return 0;
}
