#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <set>
#include <numeric>
#include <queue>
#include <map>

using namespace std;

int dfs(int cur, int goal ,int x,int y){
    queue<tuple<int,int>> que;
    que.push(make_tuple(cur,0));
    int visited[10] = {};
    while(!que.empty()){
        tuple<int,int> elem = que.front();
        que.pop();
        if(get<0>(elem) == goal && get<1>(elem)!=0){
            return get<1>(elem);
        }
        //cout << get<0>(elem);
        if(!visited[get<0>(elem)]){
            //cout << "test2" << endl;
            que.push(make_tuple((get<0>(elem)+x) %10,get<1>(elem)+1));
            que.push(make_tuple((get<0>(elem)+y)%10,get<1>(elem)+1));
            //cout << "test" << endl;
        }
        visited[get<0>(elem)] = true;

        //cout << "test" << endl;
    }

    return 100;

}

int main(){
    string line;
    cin >> line;
    int n = line.size();
    for(int x=0;x<10;x++){
        for(int y = 0;y<10;y++){
            long long int result = 0;
            int last = line[0]-'0';
            bool possible = true;
            int dp[10][10];
            for(int i = 0;i<10;i++){
                for(int j = 0;j<10;j++){

                    dp[i][j] = dfs(i,j,x,y);
                }
            }
            for(int i = 1;i<n;i++){
                int total = dp[last][line[i]-'0'];
                //cout << total << endl;
                if(total == 100){
                    possible = false;
                }else{
                    result += total-1;
                }
                last = line[i]-'0';
            }

            if(possible){
                cout << result << " ";
            }else{
                cout << -1 << " ";
            }
        }
        cout << endl;
    }

}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <set>
#include <numeric>
#include <queue>
#include <map>

using namespace std;

int dfs(int cur, int goal ,int x,int y){
    queue<tuple<int,int>> que;
    que.push(make_tuple(cur,0));
    int visited[10] = {};
    while(!que.empty()){
        tuple<int,int> elem = que.front();
        que.pop();
        if(get<0>(elem) == goal){
            if(get<1>(elem) != 0){
            return get<1>(elem);
            }
        }else{
            visited[get<0>(elem)] = true;
        }
        //cout << get<0>(elem);
        if(!visited[get<0>(elem)]){
            //cout << "test2" << endl;
            que.push(make_tuple((get<0>(elem)+x) %10,get<1>(elem)+1));
            que.push(make_tuple((get<0>(elem)+y)%10,get<1>(elem)+1));
            //cout << "test" << endl;
        }

        //cout << "test" << endl;
    }

    return 100;

}

int main(){
    string line;
    cin >> line;
    int n = line.size();
    for(int x=0;x<10;x++){
        for(int y = 0;y<10;y++){
            long long int result = 0;
            int last = 0;
            bool possible = true;
            int dp[10][10];
            for(int i = 0;i<10;i++){
                for(int j = 0;j<10;j++){

                    dp[i][j] = dfs(i,j,x,y);
                }
            }
            for(int i = 0;i<n;i++){
                int total = dp[last][line[i]-'0'];
                if(total == 100){
                    possible = false;
                }else{
                    result += total-1;
                }
                last = line[i]-'0';
            }

            if(possible){
                cout << result << " ";
            }else{
                cout << -1 << " ";
            }
        }
        cout << endl;
    }

}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <set>
#include <numeric>
#include <queue>
#include <map>

using namespace std;

int dfs(int cur, int goal ,int x,int y){
    queue<tuple<int,int>> que;
    que.push(make_tuple(cur,0));
    int visited[10] = {};
    while(!que.empty()){
        tuple<int,int> elem = que.front();
        que.pop();
        if(get<0>(elem) == goal){
            if(get<1>(elem != 0)){
            return get<1>(elem);
            }
        }else{
            visited[get<0>(elem)] = true;
        }
        //cout << get<0>(elem);
        if(!visited[get<0>(elem)]){
            //cout << "test2" << endl;
            que.push(make_tuple((get<0>(elem)+x) %10,get<1>(elem)+1));
            que.push(make_tuple((get<0>(elem)+y)%10,get<1>(elem)+1));
            //cout << "test" << endl;
        }

        //cout << "test" << endl;
    }

    return 100;

}

int main(){
    string line;
    cin >> line;
    int n = line.size();
    for(int x=0;x<10;x++){
        for(int y = 0;y<10;y++){
            long long int result = 0;
            int last = 0;
            bool possible = true;
            int dp[10][10];
            for(int i = 0;i<10;i++){
                for(int j = 0;j<10;j++){

                    dp[i][j] = dfs(i,j,x,y);
                }
            }
            for(int i = 0;i<n;i++){
                int total = dp[last][line[i]-'0'];
                if(total == 100){
                    possible = false;
                }else{
                    result += total-1;
                }
                last = line[i]-'0';
            }

            if(possible){
                cout << result << " ";
            }else{
                cout << -1 << " ";
            }
        }
        cout << endl;
    }

}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define inf 1000000009
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define pii pair<int, int>

int mat[10][10][10][10];

void gen()
{
	memset(mat, -1, sizeof mat);

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {

			queue <int> q;
			q.push(0);
			while(!q.empty()) {
				int u = q.front();
				if(mat[i][j][u][(u+i)%10] == -1) mat[i][j][u][(u+i)%10] = 1, q.push((u+i)%10);
				if(mat[i][j][u][(u+j)%10] == -1) mat[i][j][u][(u+j)%10] = 1, q.push((u+j)%10);
				q.pop();
			}

//			for(int l = 0; l < 10; l++) {
//				for(int m = 0; m < 10; m++) {
//					cout<<mat[i][j][l][m]<<" ";
//				} cout<<endl;
//			}

			for(int k = 0; k < 10; k++) {
				for(int l = 0; l < 10; l++) {
					for(int m = 0; m < 10; m++) {
						if(mat[i][j][l][k] == -1 || mat[i][j][k][m] == -1) continue;

						int sm = mat[i][j][l][k]+mat[i][j][k][m];

						if((sm < mat[i][j][l][m]) | (mat[i][j][l][m] == -1)) {
							mat[i][j][l][m] = mat[i][j][l][k] + mat[i][j][k][m];
						}
					}
				}
			}

		}
	}

//	for(int i = 0; i < 10; i++) {
//		for(int j = 0; j < 10; j++) {
//			cout<<mat[1][1][i][j]<<" ";
//		} cout<<endl;
//	}
}

int main()
{
	gen();

	string s;
	cin>>s;

	bool tf;
	int x, y, cst;

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {
			tf = true, cst = 0;

			for(int l = 1; l < s.size(); l++) {
				x = s[l-1]-'0';
				y = s[l]-'0';

				if(mat[i][j][x][y] > 0) cst += mat[i][j][x][y]-1;
				if(mat[i][j][x][y] < 0) {
					tf = false;
					break;
				}
			}

			if(tf) cout<<cst<<" ";
			else cout<<-1<<" ";
		}
		cout<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define inf 100000009
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define pii pair<int, int>

int mat[10][10][10][10];

void gen()
{
	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {
			for(int k = 0; k < 10; k++) {
				for(int l = 0; l < 10; l++) {
					mat[i][j][k][l] = inf;
				}
			}
		}
	}

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {

			queue <int> q;
			q.push(0);
			while(!q.empty()) {
				int u = q.front();
				if(mat[i][j][u][(u+i)%10] == inf) mat[i][j][u][(u+i)%10] = 1, q.push((u+i)%10);
				if(mat[i][j][u][(u+j)%10] == inf) mat[i][j][u][(u+j)%10] = 1, q.push((u+j)%10);
				q.pop();
			}

//			for(int l = 0; l < 10; l++) {
//				for(int m = 0; m < 10; m++) {
//					cout<<mat[i][j][l][m]<<" ";
//				} cout<<endl;
//			}

			for(int k = 0; k < 10; k++) {
				for(int l = 0; l < 10; l++) {
					for(int m = 0; m < 10; m++) {
						int sm = mat[i][j][l][k]+mat[i][j][k][m];

						if(sm < mat[i][j][l][m]) {
							mat[i][j][l][m] = sm;
						}
					}
				}
			}

		}
	}

//	for(int i = 0; i < 10; i++) {
//		for(int j = 0; j < 10; j++) {
//			cout<<mat[1][1][i][j]<<" ";
//		} cout<<endl;
//	}
}

int main()
{
	gen();

	string s;
	cin>>s;

	bool tf;
	int x, y, cst;

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {
			tf = true, cst = 0;

			for(int l = 1; l < s.size(); l++) {
				x = s[l-1]-'0';
				y = s[l]-'0';

				if(mat[i][j][x][y] == inf) {
					tf = false;
					break;
				}
				else cst += mat[i][j][x][y]-1;
			}

			if(tf) cout<<cst<<" ";
			else cout<<-1<<" ";
		}
		cout<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define inf 1000000009
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define pii pair<int, int>

int mat[10][10][10][10];

void gen()
{
	memset(mat, -1, sizeof mat);

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {

			queue <int> q;
			q.push(0);
			while(!q.empty()) {
				int u = q.front();
				if(mat[i][j][u][(u+i)%10] == -1) mat[i][j][u][(u+i)%10] = 1, q.push((u+i)%10);
				if(mat[i][j][u][(u+j)%10] == -1) mat[i][j][u][(u+j)%10] = 1, q.push((u+j)%10);
				q.pop();
			}

			for(int k = 0; k < 10; k++) {
				for(int l = 0; l < 10; l++) {
					for(int m = 0; m < 10; m++) {
						if(mat[i][j][l][k] == -1 || mat[i][j][k][m] == -1) continue;

						int sm = mat[i][j][l][k]+mat[i][j][k][m];

						if((sm < mat[i][j][l][m]) | (mat[i][j][l][m] == -1)) {
							mat[i][j][l][m] = mat[i][j][l][k] + mat[i][j][k][m];
						}
					}
				}
			}

		}
	}
}

int main()
{
	gen();

	string s;
	cin>>s;

	bool tf;
	int x, y, cst;

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {
			tf = true, cst = 0;

			for(int l = 1; l < s.size(); l++) {
				x = s[l-1]-'0';
				y = s[l]-'0';

				if(mat[i][j][x][y] > 0) cst += mat[i][j][x][y]-1;
				if(mat[i][j][x][y] < 0) {
					tf = false;
					break;
				}
			}

			if(tf) cout<<cst<<" ";
			else cout<<-1<<" ";
		}
		cout<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define inf 1000000009
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define pii pair<int, int>

int mat[10][10][10][10];

void gen()
{
	memset(mat, -1, sizeof mat);

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {

			queue <int> q;
			q.push(0);
			while(!q.empty()) {
				int u = q.front();
				if(mat[i][j][u][(u+i)%10] == -1) mat[i][j][u][(u+i)%10] = 1, q.push((u+i)%10);
				if(mat[i][j][u][(u+j)%10] == -1) mat[i][j][u][(u+j)%10] = 1, q.push((u+j)%10);
				q.pop();
			}

//			for(int l = 0; l < 10; l++) {
//				for(int m = 0; m < 10; m++) {
//					cout<<mat[i][j][l][m]<<" ";
//				} cout<<endl;
//			}

			mat[i][j][0][0] = 0;
			for(int k = 0; k < 10; k++) {
				for(int l = 0; l < 10; l++) {
					for(int m = 0; m < 10; m++) {
						int sm = mat[i][j][l][k]+mat[i][j][k][m];

						if(mat[i][j][l][k] == -1 || mat[i][j][k][m] == -1) continue;

						if((sm < mat[i][j][l][m]) | (mat[i][j][l][m] == -1)) {
							mat[i][j][l][m] = mat[i][j][l][k] + mat[i][j][k][m];
						}
					}
				}
			}

		}
	}

//	for(int i = 0; i < 10; i++) {
//		for(int j = 0; j < 10; j++) {
//			cout<<mat[0][8][i][j]<<" ";
//		} cout<<endl;
//	}
}

int main()
{
	gen();

	string s;
	cin>>s;

	bool tf;
	int x, y, cst;

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {
			tf = true, cst = 0;

			for(int l = 1; l < s.size(); l++) {
				x = s[l-1]-'0';
				y = s[l]-'0';

				if(mat[i][j][x][y] > 1) cst += mat[i][j][x][y]-1;
				if(mat[i][j][x][y] < 0) {
					tf = false;
					break;
				}
			}

			if(tf) cout<<cst<<" ";
			else cout<<-1<<" ";
		}
		cout<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
#define inf 1000000009
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define pii pair<int, int>

int mat[10][10][10][10];

void gen()
{
	memset(mat, -1, sizeof mat);

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {

			queue <int> q;
			q.push(0);
			while(!q.empty()) {
				int u = q.front();
				if(mat[i][j][u][(u+i)%10] == -1) mat[i][j][u][(u+i)%10] = 1, q.push((u+i)%10);
				if(mat[i][j][u][(u+j)%10] == -1) mat[i][j][u][(u+j)%10] = 1, q.push((u+j)%10);
				q.pop();
			}

//			for(int l = 0; l < 10; l++) {
//				for(int m = 0; m < 10; m++) {
//					cout<<mat[i][j][l][m]<<" ";
//				} cout<<endl;
//			}

			mat[i][j][0][0] = 0;
			for(int k = 0; k < 10; k++) {
				for(int l = 0; l < 10; l++) {
					for(int m = 0; m < 10; m++) {
						if(mat[i][j][l][k] == -1 || mat[i][j][k][m] == -1) continue;

						int sm = mat[i][j][l][k]+mat[i][j][k][m];

						if((sm < mat[i][j][l][m]) | (mat[i][j][l][m] == -1)) {
							mat[i][j][l][m] = mat[i][j][l][k] + mat[i][j][k][m];
						}
					}
				}
			}

		}
	}

//	for(int i = 0; i < 10; i++) {
//		for(int j = 0; j < 10; j++) {
//			cout<<mat[1][1][i][j]<<" ";
//		} cout<<endl;
//	}
}

int main()
{
	gen();

	string s;
	cin>>s;

	bool tf;
	int x, y, cst;

	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 10; j++) {
			tf = true, cst = 0;

			for(int l = 1; l < s.size(); l++) {
				x = s[l-1]-'0';
				y = s[l]-'0';

				if(mat[i][j][x][y] > 0) cst += mat[i][j][x][y]-1;
				if(mat[i][j][x][y] < 0) {
					tf = false;
					break;
				}
			}

			if(tf) cout<<cst<<" ";
			else cout<<-1<<" ";
		}
		cout<<endl;
	}
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <list>
#include <queue>
#include <utility>
#define MAXN 2000010
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
int n=0,f[10][10][10][10],pre=0;
LL ans[11][11];
char s[MAXN];
queue<int> que;

void Bfs(int len1,int len2)
{
	while(!que.empty()) que.pop();
	f[len1][len2][0][0]=0;
	que.push(0);
	while(!que.empty())
	{
		int num=que.front();
		que.pop();
		if(f[len1][len2][0][(num+len1)%10]==inf)
			f[len1][len2][0][(num+len1)%10]=f[len1][len2][0][num]+1,que.push((num+len1)%10);
		if(f[len1][len2][0][(num+len2)%10]==inf)
			f[len1][len2][0][(num+len2)%10]=f[len1][len2][0][num]+1,que.push((num+len2)%10);
	}
	f[len1][len2][0][0]=inf;
	if(len1==0||len2==0)
	{
		f[len1][len2][0][0]=1;
		return;
	}
	for(int i=1;i<=9;i++)
		f[len1][len2][0][0]=min((LL)f[len1][len2][0][0],(LL)f[len1][len2][0][i]+f[len1][len2][0][10-i]);
}

LL Calc(int len1,int len2)
{
	LL res=pre;
	for(int i=2;i<=n;i++)
	{
		if(f[len1][len2][s[i-1]-'0'][s[i]-'0']==inf) return -1;
		res+=f[len1][len2][s[i-1]-'0'][s[i]-'0'];
	}
	return res;
}

int main()
{
	memset(f,0x3f,sizeof(f));
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i>j) continue;
			Bfs(i,j);
			for(int u=1;u<=9;u++)
			{
				f[i][j][u][0]=f[i][j][u-1][9];
				for(int v=1;v<=9;v++) f[i][j][u][v]=f[i][j][u-1][v-1];
			}
		}
	}
	scanf("%s",s+1);
	n=strlen(s+1);
	if(s[1]!='0')
	{
		for(int i=n;i>=1;i--) s[i+1]=s[i];
		s[1]='0';n++;
		pre=1;
	}
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i<=j)
			{
				ans[i][j]=Calc(i,j);
				if(ans[i][j]!=-1) ans[i][j]-=n-1;
			}
			if(i<=j) printf("%d ",ans[i][j]);
			else printf("%d ",ans[j][i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <list>
#include <queue>
#include <utility>
#define MAXN 2000010
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
int n=0,f[10][10][10][10],pre=0;
LL ans[11][11];
char s[MAXN];
queue<int> que;

void Bfs(int len1,int len2)
{
	while(!que.empty()) que.pop();
	f[len1][len2][0][0]=0;
	que.push(0);
	while(!que.empty())
	{
		int num=que.front();
		que.pop();
		if(f[len1][len2][0][(num+len1)%10]==inf)
			f[len1][len2][0][(num+len1)%10]=f[len1][len2][0][num]+1,que.push((num+len1)%10);
		if(f[len1][len2][0][(num+len2)%10]==inf)
			f[len1][len2][0][(num+len2)%10]=f[len1][len2][0][num]+1,que.push((num+len2)%10);
	}
	f[len1][len2][0][0]=inf;
	if(len1==0||len2==0)
	{
		f[len1][len2][0][0]=1;
		return;
	}
	for(int i=1;i<=9;i++)
		f[len1][len2][0][0]=min((LL)f[len1][len2][0][0],(LL)f[len1][len2][0][i]+f[len1][len2][0][10-i]);
}

LL Calc(int len1,int len2)
{
	LL res=pre;
	for(int i=2;i<=n;i++)
	{
		if(f[len1][len2][s[i-1]-'0'][s[i]-'0']==inf) return -1;
		res+=f[len1][len2][s[i-1]-'0'][s[i]-'0'];
	}
	return res;
}

int main()
{
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
	memset(f,0x3f,sizeof(f));
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i>j) continue;
			Bfs(i,j);
			for(int u=1;u<=9;u++)
			{
				f[i][j][u][0]=f[i][j][u-1][9];
				for(int v=1;v<=9;v++) f[i][j][u][v]=f[i][j][u-1][v-1];
			}
		}
	}
	scanf("%s",s+1);
	n=strlen(s+1);
	if(s[1]!='0')
	{
		for(int i=n;i>=1;i--) s[i+1]=s[i];
		s[1]='0';n++;
		pre=1;
	}
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i<=j)
			{
				ans[i][j]=Calc(i,j);
				if(ans[i][j]!=-1) ans[i][j]-=n-1;
			}
			if(i<=j) printf("%d ",ans[i][j]);
			else printf("%d ",ans[j][i]);
		}
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <list>
#include <queue>
#include <utility>
#define MAXN 2000010
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
int n=0,f[10][10][10][10],ans[11][11],pre=0;
char s[MAXN];
queue<int> que;

void Bfs(int len1,int len2)
{
	while(!que.empty()) que.pop();
	f[len1][len2][0][0]=0;
	que.push(0);
	while(!que.empty())
	{
		int num=que.front();
		que.pop();
		if(f[len1][len2][0][(num+len1)%10]==inf)
			f[len1][len2][0][(num+len1)%10]=f[len1][len2][0][num]+1,que.push((num+len1)%10);
		if(f[len1][len2][0][(num+len2)%10]==inf)
			f[len1][len2][0][(num+len2)%10]=f[len1][len2][0][num]+1,que.push((num+len2)%10);
	}
	f[len1][len2][0][0]=inf;
	if(len1==0&&len2==0)
	{
		f[len1][len2][0][0]=1;
		return;
	}
	for(int i=1;i<=9;i++)
		f[len1][len2][0][0]=min((LL)f[len1][len2][0][0],(LL)f[len1][len2][0][i]+f[len1][len2][0][10-i]);
}

int Calc(int len1,int len2)
{
	int res=pre;
	for(int i=2;i<=n;i++)
	{
		if(f[len1][len2][s[i-1]-'0'][s[i]-'0']==inf) return -1;
		res+=f[len1][len2][s[i-1]-'0'][s[i]-'0'];
	}
	return res;
}

int main()
{
	memset(f,0x3f,sizeof(f));
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i>j) continue;
			Bfs(i,j);
			for(int u=1;u<=9;u++)
			{
				f[i][j][u][0]=f[i][j][u-1][9];
				for(int v=1;v<=9;v++) f[i][j][u][v]=f[i][j][u-1][v-1];
			}
		}
	}
	scanf("%s",s+1);
	n=strlen(s+1);
	if(s[1]!='0')
	{
		for(int i=n;i>=1;i--) s[i+1]=s[i];
		s[1]='0';n++;
		pre=1;
	}
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i<=j)
			{
				ans[i][j]=Calc(i,j);
				if(ans[i][j]!=-1) ans[i][j]-=n-1;
			}
			if(i<=j) printf("%d",ans[i][j]);
			else printf("%d",ans[j][i]);
			printf("%c",j<9?' ':'\n');
		}
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <list>
#include <queue>
#include <utility>
#define MAXN 2000010
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
int n=0,f[10][10][10][10],ans[11][11],pre=0;
char s[MAXN];
queue<int> que;

void Bfs(int len1,int len2)
{
	while(!que.empty()) que.pop();
	f[len1][len2][0][0]=0;
	que.push(0);
	while(!que.empty())
	{
		int num=que.front();
		que.pop();
		if(f[len1][len2][0][(num+len1)%10]==inf)
			f[len1][len2][0][(num+len1)%10]=f[len1][len2][0][num]+1,que.push((num+len1)%10);
		if(f[len1][len2][0][(num+len2)%10]==inf)
			f[len1][len2][0][(num+len2)%10]=f[len1][len2][0][num]+1,que.push((num+len2)%10);
	}
}

int Calc(int len1,int len2)
{
	int res=pre;
	for(int i=2;i<=n;i++)
	{
		if(f[len1][len2][s[i-1]-'0'][s[i]-'0']==inf) return -1;
		res+=f[len1][len2][s[i-1]-'0'][s[i]-'0'];
	}
	return res;
}

int main()
{
	memset(f,0x3f,sizeof(f));
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i>j) continue;
			Bfs(i,j);
			for(int u=1;u<=9;u++)
			{
				f[i][j][u][0]=f[i][j][u-1][9];
				for(int v=1;v<=9;v++) f[i][j][u][v]=f[i][j][u-1][v-1];
			}
		}
	}
	scanf("%s",s+1);
	n=strlen(s+1);
	if(s[1]!='0')
	{
		for(int i=n;i>=1;i--) s[i+1]=s[i];
		s[1]='0';n++;
		pre=1;
	}
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i<=j)
			{
				ans[i][j]=Calc(i,j);
				if(ans[i][j]!=-1) ans[i][j]-=n-1;
			}
			if(i<=j) printf("%d",ans[i][j]);
			else printf("%d",ans[j][i]);
			printf("%c",j<9?' ':'\n');
		}
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <list>
#include <queue>
#include <utility>
#define MAXN 2000010
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
int n=0,f[10][10][10][10],ans[11][11];
char s[MAXN];
queue<int> que;

void Bfs(int len1,int len2)
{
	while(!que.empty()) que.pop();
	f[len1][len2][0][0]=0;
	que.push(0);
	while(!que.empty())
	{
		int num=que.front();
		que.pop();
		if(f[len1][len2][0][(num+len1)%10]==inf)
			f[len1][len2][0][(num+len1)%10]=f[len1][len2][0][num]+1,que.push((num+len1)%10);
		if(f[len1][len2][0][(num+len2)%10]==inf)
			f[len1][len2][0][(num+len2)%10]=f[len1][len2][0][num]+1,que.push((num+len2)%10);
	}
}

int Calc(int len1,int len2)
{
	int res=0;
	for(int i=2;i<=n;i++)
	{
		if(f[len1][len2][s[i-1]-'0'][s[i]-'0']==inf) return -1;
		res+=f[len1][len2][s[i-1]-'0'][s[i]-'0'];
	}
	return res;
}

int main()
{
	memset(f,0x3f,sizeof(f));
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i>j) continue;
			Bfs(i,j);
			for(int u=1;u<=9;u++)
			{
				f[i][j][u][0]=f[i][j][u-1][9];
				for(int v=1;v<=9;v++) f[i][j][u][v]=f[i][j][u-1][v-1];
			}
		}
	}
	scanf("%s",s+1);
	n=strlen(s+1);
	for(int i=0;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i<=j)
			{
				ans[i][j]=Calc(i,j);
				if(ans[i][j]!=-1) ans[i][j]-=n-1;
			}
			if(i<=j) printf("%d",ans[i][j]);
			else printf("%d",ans[j][i]);
			printf("%c",j<9?' ':'\n');
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  map<int,int> d;
  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++){
       int D=dist(v[i],v[j]);
       if(D==0) return false;
       d[D]++;
    }
  auto it=d.begin();
  if(d.size()!=2) return false;
  return (d.begin()->second==4);
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<5;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<5;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<5;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  map<int,int> d;
  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++){
       int D=dist(v[i],v[j]);
       if(D==0) return false;
       d[D]++;
    }
  return (d.begin()->second>=4);
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<4;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<4;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<4;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  set<ii> igual;
//  for(ii x: v) igual.insert(x);
//  if(igual.size()!=4) return false;
  set<int> lin,col;
  for(ii x: v) lin.insert(x.first);
  for(ii x: v) col.insert(x.second);
  map<int,int> d;

  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++)
      d[dist(v[i],v[j])]++;

  if(d.size()!=2) return false;
  auto it=d.begin();
  if(it->second!=4) return false;
  return (lin.size()==2 and col.size()==2);
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<5;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<5;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<5;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  set<ii> igual;
  for(ii x: v) igual.insert(x);
  if(igual.size()!=4) return false;
  set<int> lin,col;
  for(ii x: v) lin.insert(x.first);
  for(ii x: v) col.insert(x.second);
  map<int,int> d;

  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++)
      d[dist(v[i],v[j])]++;

  //if(d.size()!=2) return false;
  auto it=d.begin();
  if(it->second!=4) return false;
  return (lin.size()==2 and col.size()==2);
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<5;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<5;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<5;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  map<int,int> d;
  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++){
       int D=dist(v[i],v[j]);
//       if(D==0) return false;
       d[D]++;
    }
  return (d.begin()->second==4);
  return true;
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<4;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<4;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<4;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  map<int,int> d;
  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++){
       int D=dist(v[i],v[j]);
       if(D==0) return false;
       d[D]++;
    }
  if(d.size()!=2) return false;
  return (d.begin()->second==4);
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<4;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<4;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<4;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
bool equadrado(vii v){
  set<ii> igual;
  for(ii x: v) igual.insert(x);
  if(igual.size()!=4) return false;
  set<int> lin,col;
  for(ii x: v) lin.insert(x.first);
  for(ii x: v) col.insert(x.second);
  return (lin.size()==2 and col.size()==2);
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<4;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<4;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<4;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<4;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  map<int,int> d;
  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++){
       int D=dist(v[i],v[j]);
       //if(D==0) return false;
       d[D]++;
    }
  auto it=d.begin();
  if(d.size()!=2) return false;
  return (d.begin()->second==2 or d.begin()->second==4);
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<4;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<4;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<4;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  map<int,int> d;
  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++){
       int D=dist(v[i],v[j]);
       if(D==0) return false;
       d[D]++;
    }
  if(d.size()!=2) return false;
  int a=d.begin()->first;
  auto it=d.begin();
  it++;
  if(a+a==it->first) return true;
  return false;
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<5;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<5;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<5;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scs(a) scanf("%s", a)
#define pri(x) printf("%d\n", x)
#define prie(x) printf("%d ", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define db(x) cerr << #x << " == " << x << endl
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ii> vii;
typedef vector< vii> vvii;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld pi = acos(-1);
const int MOD = 1e9 + 9;
int dist(ii a, ii b){
  ii delta;
  delta.first=a.first-b.first;
  delta.second=a.second-b.second;
  return delta.first*delta.first+delta.second*delta.second;
}
bool equadrado(vii v){
  map<int,int> d;
  for(int i=0;i<v.size();i++)
    for(int j=i+1;j<v.size();j++){
       int D=dist(v[i],v[j]);
       if(D==0) return false;
       d[D]++;
    }
  //if(d.size()!=2) return false;
  return (d.begin()->second==4);
}
ii roda(ii x, ii base, int quantos){
  int s=1,c=0;
  while(quantos--){
    ii aux=x;
    aux.first=c*(x.first-base.first) - s*(x.second-base.second)+ base.first;
    aux.second=s*(x.first-base.first) + c*(x.second-base.second) + base.second;
    x=aux;
  }
  return x;
}

int menor(vector< pair<ii,ii> > &v){
  int ret=INF;
  for(int i=0;i<5;i++){
    ii p1=roda(v[0].second,v[0].first,i);
    for(int j=0;j<5;j++){
      ii p2=roda(v[1].second,v[1].first,j);
      for(int k=0;k<5;k++){
        ii p3=roda(v[2].second,v[2].first,k);
        for(int l=0;l<5;l++){
          ii p4=roda(v[3].second,v[3].first,l);
          vii aux;
          aux.pb(p1);
          aux.pb(p2);
          aux.pb(p3);
          aux.pb(p4);
          if(equadrado(aux)) ret=min(ret,i+j+k+l);
        }
      }
    }
  }
  if(ret==INF) ret=-1;
  return ret;
}
int main()
{
  BUFF;
  int n;
  cin>>n;
  while(n--){
    int cont=4;
    vector <pair<ii,ii> > v;
    while(cont--){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      v.pb(mp(mp(c,d),mp(a,b)));
    }
    pri(menor(v));
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

//Made with <3 by MinumCoklatPanas
typedef long long ll;
const int INF = 1e9;
const ll INFF = 1e18;
const int MAXN = 510;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double INFD = 1E9;
const double EPS = 1e-6;

double between_d(double x,double l,double r) {
	return (min(l,r)<=x+EPS && x<=max(l,r)+EPS);
}

double same_d(double x,double y) {
	return between_d(x,y,y);
}

struct point {
	double x,y;
	point() {
		x=y=0.0;
	}
	point(double _x,double _y) {
		x=_x; y=_y;
	}
	bool operator< (point other) const {
        if (same_d(x,other.x))
        {
            return other.y - y > -EPS;
            // return y < other.y;/
        }
        else
            return other.x - x > -EPS;
            // return x < other.x;
	}
	bool operator== (point other) const {
		return same_d(x,other.x)&&same_d(y,other.y);
	}
};

double e_dist(point P1,point P2) {
	return hypot(P1.x-P2.x,P1.y-P2.y);
}

point rotate(double deg,point p,point pivot)
{
    double x,y;
    point ret;
    x = p.x - pivot.x;
    y = p.y - pivot.y;
    if (deg == 0.0)
    {
        ret.x = x + pivot.x;
        ret.y = y + pivot.y;
    }
    else
    if (deg == 90.0)
    {
        ret.x = -y + pivot.x;
        ret.y = x + pivot.y;
    }
    else
    if (deg == 180.0)
    {
        ret.x = -x + pivot.x;
        ret.y = -y + pivot.y;
    }
    else
    {
        ret.x = y + pivot.x;
        ret.y = -x + pivot.y;
    }
    return ret;
}

double toRad(double x)
{
    return (PI / 180.0) * x;
}

bool valid(vector< point > tab)
{
    // for (auto it : tab)
    //     cout << it.x << " " << it.y << endl;
    // cout << endl;
    vector< double > dist;
    for (int i = 1 ; i < 4 ; i++)
    {
        dist.push_back(e_dist(tab[0],tab[i]));
        if (dist.back() == 0.0) return false;
    }
    sort(dist.begin(),dist.end());
    if (!same_d(dist[0],dist[1])) return false;
    if (!same_d(dist[0] * sqrt(2.0),dist[2])) return false;
    // cout << "yo\n";
    for (int i = 1 ; i < 4 ; i++)
    {
        vector< double > tmp;
        for (int j = 0 ; j < 4 ; j++) if (i != j)
        {
            tmp.push_back(e_dist(tab[i],tab[j]));
            if (dist.back() == 0.0) return false;
        }
        sort(tmp.begin(),tmp.end());
        if (tmp != dist) return false;
    }
    return true;
}

int rec(int ix,vector< point > tab,vector< point > pivot)
{
    if (ix == 4)
    {
        if (valid(tab))
        {
            return 0;
        }
        else
            return INF;
    }
    point tmp = tab[ix];
    int ret = INF;
    for (int i = 0 ; i < 4 ; i++)
    {
        vector< point > tmp2 = tab;
        tmp2[ix] = rotate((90.0 * i),tmp,pivot[ix]);
        ret = min(ret,i + rec(ix + 1,tmp2,pivot));
        // break;
        // if (ix != 1)
        //     break;
        // else
        // {
        //     if (i == 1)
        //         break;
        // }
    }
    tab[ix] = tmp;
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        vector< point > tab(4),pivot(4);
        for (int i = 0 ; i < 4 ; i++)
        {
            cin >> tab[i].x >> tab[i].y >> pivot[i].x >> pivot[i].y;
        }
        // sort(tab.begin(),tab.end());
        // for (int i = 0 ; i < tab.size() ; i++)
        //     cout << tab[i].x << " " << tab[i].y << endl;
        // return 0;
        // point tmp = rotate(toRad(90.0),tab[1],pivot[1]);
        // cout << tmp.x << " " << tmp.y << endl;
        int ans = rec(0,tab,pivot);
        if (ans >= INF)
            cout << -1 << endl;
        else
            cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int n;

typedef struct p
{
    ll x,y;
}point ;
point arr[4][4],h[4];
ll dis[10];

ll dist(ll x1,ll y1,ll x2, ll y2)
{
    return (x1-x2)*(x1-x2)+(y2-y1)*(y2-y1);
}
void solve()
{
    for(int i=0;i<4;i++)
    {
        for(int j=1;j<4;j++)
        {
            arr[i][j].x = h[i].x+h[i].y-arr[i][j-1].y;
            arr[i][j].y = -h[i].x+h[i].y+arr[i][j-1].x;
        }
    }
    int ans = 16;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<4;k++)
            {
                for(int l=0;l<4;l++)
                {
                    memset(dis,0,sizeof dis);
                    dis[0] = dist(arr[0][i].x,arr[0][i].y,arr[1][j].x,arr[1][j].y);
                    dis[1] = dist(arr[1][j].x,arr[1][j].y,arr[2][k].x,arr[2][k].y);
                    dis[2] = dist(arr[2][k].x,arr[2][k].y,arr[3][l].x,arr[3][l].y);
                    dis[3] = dist(arr[3][l].x,arr[3][l].y,arr[0][i].x,arr[0][i].y);
                    dis[4] = dist(arr[0][i].x,arr[0][i].y,arr[2][k].x,arr[2][k].y);
                    dis[5] = dist(arr[1][j].x,arr[1][j].y,arr[3][l].x,arr[3][l].y);
                    sort(dis,dis+6);
                    if (dis[0]==dis[1] && dis[1]==dis[2] && dis[2]==dis[3] && dis[3]==dis[0] && 2*dis[0]==dis[5] && dis[5]==dis[4] && dis[0])
                    ans=min(ans,i+j+k+l);
                }
            }
        }
    }
    if(ans!=16)
        cout<<ans<<endl;
    else
        cout<<-1<<endl;
}

int main()
{

        cin>>n;
        while(n--)
        {
            memset(arr,0,sizeof arr);
            memset(h,0,sizeof h);
            for(int i=0;i<4;i++)
                cin>>arr[i][0].x>>arr[i][0].y>>h[i].x>>h[i].y;
            solve();

        }
        return 0;
}
//hello//hello


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int n;

typedef struct p
{
    ll x,y;
}point ;
point arr[4][4],h[4];
ll dis[10];

ll dist(ll x1,ll y1,ll x2, ll y2)
{
    return (x1-x2)*(x1-x2)+(y2-y1)*(y2-y1);
}
void solve()
{
    for(int i=0;i<4;i++)
    {
        for(int j=1;j<4;j++)
        {
            arr[i][j].x = h[i].x+h[i].y-arr[i][j-1].y;
            arr[i][j].y = -h[i].x+h[i].y+arr[i][j-1].x;
        }
    }
    int ans = 16;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<4;k++)
            {
                for(int l=0;l<4;l++)
                {
                    memset(dis,0,sizeof dis);
                    dis[0] = dist(arr[0][i].x,arr[0][i].y,arr[1][j].x,arr[1][j].y);
                    dis[1] = dist(arr[1][j].x,arr[1][j].y,arr[2][k].x,arr[2][k].y);
                    dis[2] = dist(arr[2][k].x,arr[2][k].y,arr[3][l].x,arr[3][l].y);
                    dis[3] = dist(arr[3][l].x,arr[3][l].y,arr[0][i].x,arr[0][i].y);

                    sort(dis,dis+6);
                    if (dis[0]==dis[1] && dis[1]==dis[2] && dis[2]==dis[3] && dis[3]==dis[0] &&  dis[0])
                    ans=min(ans,i+j+k+l);
                }
            }
        }
    }
    if(ans!=16)
        cout<<ans<<endl;
    else
        cout<<-1<<endl;
}

int main()
{

        cin>>n;
        while(n--)
        {
            memset(arr,0,sizeof arr);
            memset(h,0,sizeof h);
            for(int i=0;i<4;i++)
                cin>>arr[i][0].x>>arr[i][0].y>>h[i].x>>h[i].y;
            solve();

        }
        return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int n;

typedef struct p
{
    ll x,y;
}point ;
point arr[4][4],h[4];
ll dis[10];

ll dist(ll x1,ll y1,ll x2, ll y2)
{
    return (x1-x2)*(x1-x2)+(y2-y1)*(y2-y1);
}
void solve()
{
    for(int i=0;i<4;i++)
    {
        for(int j=1;j<4;j++)
        {
            arr[i][j].x = h[i].x+h[i].y-arr[i][j-1].y;
            arr[i][j].y = -h[i].x+h[i].y+arr[i][j-1].x;
        }
    }
    int ans = 16;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<4;k++)
            {
                for(int l=0;l<4;l++)
                {
                    memset(dis,0,sizeof dis);
                    dis[0] = dist(arr[0][i].x,arr[0][i].y,arr[1][j].x,arr[1][j].y);
                    dis[1] = dist(arr[1][j].x,arr[1][j].y,arr[2][k].x,arr[2][k].y);
                    dis[2] = dist(arr[2][k].x,arr[2][k].y,arr[3][l].x,arr[3][l].y);
                    dis[3] = dist(arr[3][l].x,arr[3][l].y,arr[0][i].x,arr[0][i].y);

                    sort(dis,dis+4);
                    if (dis[0]==dis[1] && dis[1]==dis[2] && dis[2]==dis[3] && dis[3]==dis[0] &&  dis[0])
                    ans=min(ans,i+j+k+l);
                }
            }
        }
    }
    if(ans!=16)
        cout<<ans<<endl;
    else
        cout<<-1<<endl;
}

int main()
{

        cin>>n;
        while(n--)
        {
            memset(arr,0,sizeof arr);
            memset(h,0,sizeof h);
            for(int i=0;i<4;i++)
                cin>>arr[i][0].x>>arr[i][0].y>>h[i].x>>h[i].y;
            solve();

        }
        return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int n;

typedef struct p
{
    ll x,y;
}point ;
point arr[4][4],h[4];
ll dis[10];

ll dist(ll x1,ll y1,ll x2, ll y2)
{
    return (x1-x2)*(x1-x2)+(y2-y1)*(y2-y1);
}
void solve()
{
    for(int i=0;i<4;i++)
    {
        for(int j=1;j<4;j++)
        {
            arr[i][j].x = h[i].x+h[i].y-arr[i][j-1].y;
            arr[i][j].y = -h[i].x+h[i].y+arr[i][j-1].x;
        }
    }
    int ans = 16;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<4;k++)
            {
                for(int l=0;l<4;l++)
                {
                    memset(dis,0,sizeof dis);
                    dis[0] = dist(arr[0][i].x,arr[0][i].y,arr[1][j].x,arr[1][j].y);
                    dis[1] = dist(arr[1][j].x,arr[1][j].y,arr[2][k].x,arr[2][k].y);
                    dis[2] = dist(arr[2][k].x,arr[2][k].y,arr[3][l].x,arr[3][l].y);
                    dis[3] = dist(arr[3][l].x,arr[3][l].y,arr[0][i].x,arr[0][i].y);
                    dis[4] = dist(arr[0][i].x,arr[0][i].y,arr[2][k].x,arr[2][k].y);
                    dis[5] = dist(arr[1][j].x,arr[1][j].y,arr[3][l].x,arr[3][l].y);
                    sort(dis,dis+6);
                    if (dis[0]==dis[1] && dis[1]==dis[2] && dis[2]==dis[3] && dis[3]==dis[0]  && dis[5]==dis[4] && dis[0])
                    ans=min(ans,i+j+k+l);
                }
            }
        }
    }
    if(ans!=16)
        cout<<ans<<endl;
    else
        cout<<-1<<endl;
}

int main()
{

        cin>>n;
        while(n--)
        {
            memset(arr,0,sizeof arr);
            memset(h,0,sizeof h);
            for(int i=0;i<4;i++)
                cin>>arr[i][0].x>>arr[i][0].y>>h[i].x>>h[i].y;
            solve();

        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n)  for(int  i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 105;
struct Point{
   int x, y;
   Point(){ x = y = 0; }
   Point(int _x,int _y) { x = _x;y = _y; }
   void input(){
      scanf("%d %d",&x,&y);
      //cout<< x << " " << y << endl;
   }
   Point operator + (const Point & a) const{
      return Point(x + a.x, y + a.y);
   }
   Point operator - (const Point & a) const{
       return Point(x - a.x, y - a.y);
   }
   Point Rotate(const Point & a) const{///counter clockwise 90 degree
       return Point (-a.y, a.x);
   }
   long long modul(void) const{
      return 1LL * x + 1LL * y;
   }
   bool operator < (const Point & a) const{
      return (x == a.x ? y < a.y : x < a.x);
   }
   bool operator == (const Point &a) const{
      return (x == a.x && y==a.y);
   }
};

Point rotation(const Point &o,const Point &a,int t){
   Point res = a;
   while(t--){
     res = o + o.Rotate(res - o);
   }
   return res;
}
#define pb push_back

bool isok(vector<Point> u){
   if( (u[1] - u[0]).modul()!= (u[2] - u[3]).modul()) return 0;
   if( (u[2] - u[1]).modul()!= (u[3] - u[0]).modul() ) return 0;
   if( (u[3] - u[0]) == u[3].Rotate(u[1] - u[0]) ) return 1;
   if(u[1] - u[0] == u[3].Rotate(u[3] - u[0])) return 1;
   return false;
}

bool isSquare(vector<Point> & u){
   vector<Point> mi = u;
   sort(mi.begin(), mi.end());
   forn(i, 3) if(mi[i] == mi[i + 1]) return false;
   do {if(isok(mi)) return 1; } while(next_permutation(mi.begin(), mi.end()));
   return 0;
}
void run(){
   Point pos[5], home[5];

   for1(i, 4){
      pos[i].input();
      home[i].input();
   }
   int ans = 200;
   forn(a, 4) forn(b, 4) forn(c, 4) forn(d, 4){
      vector<Point> lu;
      lu.pb(rotation(home[1], pos[1], a));
      lu.pb(rotation(home[2], pos[2], b));
      lu.pb(rotation(home[3], pos[3], c));
      lu.pb(rotation(home[4], pos[4], d));
      if(isSquare(lu)) ans = min(ans, a + b + c + d);
   }
   if(ans == 200) cout<< -1 << endl;
   else cout<< ans << endl;
}
int main(){
   // freopen("Point.inp","r",stdin);
   int n;
   scanf("%d",&n);
   //cout<< n << endl;
   while(n--){
     run();
   }
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n)  for(int  i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 105;
struct Point{
   int x, y;
   Point(){ x = y = 0; }
   Point(int _x,int _y) { x = _x;y = _y; }
   void input(){
      scanf("%d %d",&x,&y);
      //cout<< x << " " << y << endl;
   }
   Point operator + (const Point & a) const{
      return Point(x + a.x, y + a.y);
   }
   Point operator - (const Point & a) const{
       return Point(x - a.x, y - a.y);
   }
   Point Rotate(const Point & a) const{///counter clockwise 90 degree
       return Point (-a.y, a.x);
   }
   long long modul(void) const{
      return 1LL * x + 1LL * y;
   }
   bool operator < (const Point & a) const{
      return (x == a.x ? y < a.y : x < a.x);
   }
   bool operator == (const Point &a) const{
      return (x == a.x && y==a.y);
   }
};

Point rotation(const Point &o,const Point &a,int t){
   Point res = a;
   while(t--){
     res = o + o.Rotate(res - o);
   }
   return res;
}
#define pb push_back

bool isok(vector<Point> u){
   if( (u[1] - u[0]).modul()!= (u[2] - u[3]).modul()) return 0;
   if( (u[2] - u[1]).modul()!= (u[3] - u[0]).modul() ) return 0;
   if( (u[3] - u[0]) == u[3].Rotate(u[1] - u[0]) ) return 1;
   if(u[1] - u[0] == u[3].Rotate(u[3] - u[0])) return 1;
   return false;
}

bool isSquare(vector<Point> & u){
   vector<Point> mi = u;
   sort(mi.begin(), mi.end());
   forn(i, 3) if(mi[i] == mi[i - 1]) return false;
   do {if(isok(mi)) return 1; } while(next_permutation(mi.begin(), mi.end()));
   return 0;
}
void run(){
   Point pos[5], home[5];

   for1(i, 4){
      pos[i].input();
      home[i].input();
   }
   int ans = 200;
   forn(a, 4) forn(b, 4) forn(c, 4) forn(d, 4){
      vector<Point> lu;
      lu.pb(rotation(home[1], pos[1], a));
      lu.pb(rotation(home[2], pos[2], b));
      lu.pb(rotation(home[3], pos[3], c));
      lu.pb(rotation(home[4], pos[4], d));
      if(isSquare(lu)) ans = min(ans, a + b + c + d);
   }
   if(ans == 200) cout<< -1 << endl;
   else cout<< ans << endl;
}
int main(){
   // freopen("Point.inp","r",stdin);
   int n;
   scanf("%d",&n);
   //cout<< n << endl;
   while(n--){
     run();
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i, n)  for(ll  i = 0;i<n;i++)
#define for1(i, n) for(ll i = 1;i<=n;i++)
const ll N = 105;
struct Poll{
   ll x, y;
   Poll(){ x = y = 0; }
   Poll(ll _x,ll _y) { x = _x;y = _y; }
   void input(){
      cin >> x >> y;
      //cout<< x << " " << y << endl;
   }
   Poll operator + (const Poll & a) const{
      return Poll(x + a.x, y + a.y);
   }
   Poll operator - (const Poll & a) const{
       return Poll(x - a.x, y - a.y);
   }
   Poll Rotate(const Poll & a) const{///counter clockwise 90 degree
       return Poll (-a.y, a.x);
   }
   long long modul(void) const{
      return 1LL * x + 1LL * y;
   }
   bool operator < (const Poll & a) const{
      return (x == a.x ? y < a.y : x < a.x);
   }
   bool operator == (const Poll &a) const{
      return (x == a.x && y==a.y);
   }
};

Poll rotation(const Poll &o,const Poll &a,ll t){
   Poll res = a;
   while(t--){
     res = o + o.Rotate(res - o);
   }
   return res;
}
#define pb push_back
ll dis(Poll u,Poll v){
    ll dx = u.x - v.x;
    ll dy = u.y - v.y;
    return dx * dx + dy * dy;
}

bool isSquare(vector<Poll> & u){
   vector<Poll> mi = u;
   sort(mi.begin(), mi.end());
   vector<ll> d;
   forn(i, 4){
     for(ll j = i + 1;j<4;j++) d.push_back(dis(mi[i], mi[j]));
   }
   sort(d.begin(), d.end());
   if(d[0] == d[3] && d[4] == d[5] && d[0]) return 1;
   return 0;
}
void run(){
   Poll pos[5], home[5];

   for1(i, 4){
      pos[i].input();
      home[i].input();
   }
   ll ans = 200;
   forn(a, 4) forn(b, 4) forn(c, 4) forn(d, 4){
      vector<Poll> lu;
      lu.pb(rotation(home[1], pos[1], a));
      lu.pb(rotation(home[2], pos[2], b));
      lu.pb(rotation(home[3], pos[3], c));
      lu.pb(rotation(home[4], pos[4], d));
      if(isSquare(lu)) ans = min(ans, a + b + c + d);
   }
   if(ans == 200) cout<< -1 << endl;
   else cout<< ans << endl;
}
int main(){
    //freopen("Point.inp","r",stdin);
    ios_base:: sync_with_stdio(false);
   ll n;
   cin >> n;
   //cout<< n << endl;
   while(n--){
     run();
   }
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n)  for(int  i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 105;
struct Point{
   int x, y;
   Point(){ x = y = 0; }
   Point(int _x,int _y) { x = _x;y = _y; }
   void input(){
      scanf("%d %d",&x,&y);
      //cout<< x << " " << y << endl;
   }
   Point operator + (const Point & a) const{
      return Point(x + a.x, y + a.y);
   }
   Point operator - (const Point & a) const{
       return Point(x - a.x, y - a.y);
   }
   Point Rotate(const Point & a) const{///counter clockwise 90 degree
       return Point (-a.y, a.x);
   }
   long long modul(void) const{
      return 1LL * x + 1LL * y;
   }
   bool operator < (const Point & a) const{
      return (x == a.x ? y < a.y : x < a.x);
   }
   bool operator == (const Point &a) const{
      return (x == a.x && y==a.y);
   }
};

Point rotation(const Point &o,const Point &a,int t){
   Point res = a;
   while(t--){
     res = o + o.Rotate(res - o);
   }
   return res;
}
#define pb push_back

bool isok(vector<Point> u){
   if( (u[1] - u[0]).modul()!= (u[2] - u[3]).modul()) return 0;
   if( (u[2] - u[1]).modul()!= (u[3] - u[0]).modul() ) return 0;
   if( (u[3] - u[0]) == u[3].Rotate(u[1] - u[0]) ) return 1;
   return false;
}

bool isSquare(vector<Point> & u){
   vector<Point> mi = u;
   sort(mi.begin(), mi.end());
   forn(i, 3) if(mi[i] == mi[i - 1]) return false;
   do {if(isok(mi)) return 1; } while(next_permutation(mi.begin(), mi.end()));
   return 0;
}
void run(){
   Point pos[5], home[5];

   for1(i, 4){
      pos[i].input();
      home[i].input();
   }
   int ans = 200;
   forn(a, 4) forn(b, 4) forn(c, 4) forn(d, 4){
      vector<Point> lu;
      lu.pb(rotation(home[1], pos[1], a));
      lu.pb(rotation(home[2], pos[2], b));
      lu.pb(rotation(home[3], pos[3], c));
      lu.pb(rotation(home[4], pos[4], d));
      if(isSquare(lu)) ans = min(ans, a + b + c + d);
   }
   if(ans == 200) cout<< -1 << endl;
   else cout<< ans << endl;
}
int main(){
   // freopen("Point.inp","r",stdin);
   int n;
   scanf("%d",&n);
   //cout<< n << endl;
   while(n--){
     run();
   }
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n)  for(int  i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 105;
struct Point{
   int x, y;
   Point(){ x = y = 0; }
   Point(int _x,int _y) { x = _x;y = _y; }
   void input(){
      scanf("%d %d",&x,&y);
      //cout<< x << " " << y << endl;
   }
   Point operator + (const Point & a) const{
      return Point(x + a.x, y + a.y);
   }
   Point operator - (const Point & a) const{
       return Point(x - a.x, y - a.y);
   }
   Point Rotate(const Point & a) const{///counter clockwise 90 degree
       return Point (-a.y, a.x);
   }
   long long modul(void) const{
      return 1LL * x + 1LL * y;
   }
   bool operator < (const Point & a) const{
      return (x == a.x ? y < a.y : x < a.x);
   }
   bool operator == (const Point &a) const{
      return (x == a.x && y==a.y);
   }
};

Point rotation(const Point &o,const Point &a,int t){
   Point res = a;
   while(t--){
     res = o + o.Rotate(res - o);
   }
   return res;
}
#define pb push_back
int dis(Point u,Point v){
    int dx = u.x - v.x;
    int dy = u.y - v.y;
    return dx * dx + dy * dy;
}

bool isSquare(vector<Point> & u){
   vector<Point> mi = u;
   sort(mi.begin(), mi.end());
   vector<int> d;
   forn(i, 4){
     for(int j = i + 1;j<4;j++) d.push_back(dis(mi[i], mi[j]));
   }
   sort(d.begin(), d.end());
   if(d[0] == d[3] && d[4] == d[5] && d[0]) return 1;
   return 0;
}
void run(){
   Point pos[5], home[5];

   for1(i, 4){
      pos[i].input();
      home[i].input();
   }
   int ans = 200;
   forn(a, 4) forn(b, 4) forn(c, 4) forn(d, 4){
      vector<Point> lu;
      lu.pb(rotation(home[1], pos[1], a));
      lu.pb(rotation(home[2], pos[2], b));
      lu.pb(rotation(home[3], pos[3], c));
      lu.pb(rotation(home[4], pos[4], d));
      if(isSquare(lu)) ans = min(ans, a + b + c + d);
   }
   if(ans == 200) cout<< -1 << endl;
   else cout<< ans << endl;
}
int main(){
   // freopen("Point.inp","r",stdin);
   int n;
   scanf("%d",&n);
   //cout<< n << endl;
   while(n--){
     run();
   }
}


//
//  main.cpp
//  C
//
//  Created by Ali Keramatipour on 7/9/15.
//  Copyright (c) 2015 Ali Keramatipour. All rights reserved.
//

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std ;
int m ;
typedef pair<int,int> pii;

bool check(pii x , pii y , pii a , pii b){
    if ( abs(x.first - y.first) == abs(x.second - y.second)
    && abs(a.first - b.first ) == abs(a.second - b.second)
    && abs(a.first - b.first ) != 0
    && abs(x.second - y.second) != 0
    )
    {
        if ( (a.first == x.first) && (b.first == y.first) && (a.second == y.second) && (b.second == x.second) )
            return 1 ;
        if ( (a.second == x.second) && (b.second == y.second) && (a.first == y.first) && (b.first == x.first) )
            return 1 ;
    }
    return 0 ;
}

bool isSq(pii x , pii y , pii a , pii b )
{
    if (check(x , y , a , b)
    || check(x , y , b , a )
    || check(x , a , y , b )
    || check(x , a , b , y )
    || check(x , b , a , y )
    || check(x , b , y , a )

    || check(y , x , a , b )
    || check(y , x , b , a )
    || check(y , a , x , b )
    || check(y , a , b , x )
    || check(y , b , a , x )
    || check(y , b , x , a )

    || check(a , x , y , b )
    || check(a , x , b , y )
    || check(a , y , x , b )
    || check(a , y , b , x )
    || check(a , b , y , x )
    || check(a , b , x , y )

    || check(b , x , y , a )
    || check(b , x , a , y )
    || check(b , y , x , a )
    || check(b , y , a , x )
    || check(b , a , y , x )
    || check(b , a , x , y ) )
        return 1 ;
    return 0 ;

}

void rott(int &x , int &y , int a , int b){

    int lx = x - a;
    int ly = y - b;
    swap(lx,ly);
    lx *= -1;
    lx += a;
    ly += b;
    x = lx;
    y = ly;

}

int main(int argc, const char * argv[]) {

    int n , x , y , a , b ;
    cin >> n ;
    for (int k = 0 ; k < n ; k++){
    vector< pair< pair<int,int> , int > > v[4] ;

    for (int i = 0 ; i < 4 ; i++){
        cin >> x >> y >> a >> b ;
        v[i].push_back({ {x,y},0 }) ;
        //cout << x << " " << y << endl ;
        for (int q = 1 ; q < 4 ; q++){
            rott( x , y , a , b ) ;
            //cout << x << " " << y << endl ;
            v[i].push_back({ {x,y},q }) ;
        }

    }
    m = 1e9 + 5 ;
        /*
        for (int i = 1 ; i <= 1 ; i++)
        {
            for (int j = 0 ; j < v[i].size() ; j++)
                cout << v[i][j].first.first << " " << v[i][j].first.second << endl ;
            cout << endl << endl ;
        }*/

    for (int i1 = 0 ; i1 < 4 ; i1++)
    for (int i2 = 0 ; i2 < 4 ; i2++)
    for (int i3 = 0 ; i3 < 4 ; i3++)
    for (int i4 = 0 ; i4 < 4 ; i4++){
        int s = i1 + i2 + i3 + i4 ;/*
        cout << i1 << " " << i2 << " " << i3 << " " << i4 << endl ;
        cout << v[0][i1].first.first << " " << v[0][i1].first.second << endl ;
        cout << v[1][i2].first.first << " " << v[1][i2].first.second << endl ;
        cout << v[2][i3].first.first << " " << v[2][i3].first.second << endl ;
        cout << v[3][i4].first.first << " " << v[3][i4].first.second << endl ;
        cout << endl ;*/

        if ( isSq(v[0][i1].first , v[1][i2].first , v[2][i3].first , v[3][i4].first) ){
            m = min (m , s );

        }
    }

    if ( m >= 1e9 + 5)
        cout << -1 ;
    else
        cout << m ;

    cout << endl;
    }
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by Ali Keramatipour on 7/9/15.
//  Copyright (c) 2015 Ali Keramatipour. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std ;
int m ;
typedef pair<int,int> pii;

bool check(pii x , pii y , pii a , pii b){
    if ( abs(x.first - y.first) == abs(x.second - y.second)
    && abs(a.first - b.first ) == abs(a.second - b.second)
    && abs(a.first - b.first ) != 0
    && abs(x.second - y.second) != 0
    )
    {
        if ( (a.first == x.first) && (b.first == y.first) && (a.second == y.second) && (b.second == x.second) )
            return 1 ;
        if ( (a.second == x.second) && (b.second == y.second) && (a.first == y.first) && (b.first == x.first) )
            return 1 ;
    }
    return 0 ;
}

bool isSq(pii x , pii y , pii a , pii b )
{
    if (check(x , y , a , b)
    || check(x , y , b , a )
    || check(x , a , y , b )
    || check(x , a , b , y )
    || check(x , b , a , y )
    || check(x , b , y , a )

    || check(y , x , a , b )
    || check(y , x , b , a )
    || check(y , a , x , b )
    || check(y , a , b , x )
    || check(y , b , a , x )
    || check(y , b , x , a )

    || check(a , x , y , b )
    || check(a , x , b , y )
    || check(a , y , x , b )
    || check(a , y , b , x )
    || check(a , b , y , x )
    || check(a , b , x , y )

    || check(b , x , y , a )
    || check(b , x , a , y )
    || check(b , y , x , a )
    || check(b , y , a , x )
    || check(b , a , y , x )
    || check(b , a , x , y ) )
        return 1 ;
    return 0 ;

}

void rott(int &x , int &y , int a , int b){

    int lx = x - a;
    int ly = y - b;
    swap(lx,ly);
    lx *= -1;
    lx += a;
    ly += b;
    x = lx;
    y = ly;

}

int main(int argc, const char * argv[]) {

    int n , x , y , a , b ;
    cin >> n ;
    for (int k = 0 ; k < n ; k++){
    vector< pair< pair<int,int> , int >> v[4] ;

    for (int i = 0 ; i < 4 ; i++){
        cin >> x >> y >> a >> b ;
        v[i].push_back({ {x,y},0 }) ;
        //cout << x << " " << y << endl ;
        for (int q = 1 ; q < 4 ; q++){
            rott( x , y , a , b ) ;
            //cout << x << " " << y << endl ;
            v[i].push_back({ {x,y},q }) ;
        }

    }
    m = 1e9 + 5 ;
        /*
        for (int i = 1 ; i <= 1 ; i++)
        {
            for (int j = 0 ; j < v[i].size() ; j++)
                cout << v[i][j].first.first << " " << v[i][j].first.second << endl ;
            cout << endl << endl ;
        }*/

    for (int i1 = 0 ; i1 < 4 ; i1++)
    for (int i2 = 0 ; i2 < 4 ; i2++)
    for (int i3 = 0 ; i3 < 4 ; i3++)
    for (int i4 = 0 ; i4 < 4 ; i4++){
        int s = i1 + i2 + i3 + i4 ;/*
        cout << i1 << " " << i2 << " " << i3 << " " << i4 << endl ;
        cout << v[0][i1].first.first << " " << v[0][i1].first.second << endl ;
        cout << v[1][i2].first.first << " " << v[1][i2].first.second << endl ;
        cout << v[2][i3].first.first << " " << v[2][i3].first.second << endl ;
        cout << v[3][i4].first.first << " " << v[3][i4].first.second << endl ;
        cout << endl ;*/

        if ( isSq(v[0][i1].first , v[1][i2].first , v[2][i3].first , v[3][i4].first) ){
            m = min (m , s );

        }
    }

    if ( m >= 1e9 + 5)
        cout << -1 ;
    else
        cout << m ;

    cout << endl;
    }
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by Ali Keramatipour on 7/9/15.
//  Copyright (c) 2015 Ali Keramatipour. All rights reserved.
//

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std ;
int m ;
typedef pair<int,int> pii;

int dis(pii x , pii y)
{
    int xx = x.first - y.first ;
    int yy = x.second - y.second ;
    //cout << xx << " " << yy << endl ;
    return xx*xx + yy*yy ;
}

bool check(pii a , pii b , pii c , pii d){

    pii p[4] = {a,b,c,d} ;
    for (int i = 0 ; i < 4 ; i++)
        for (int j = 0 ; j < 4 ; j++)
            if ( i != j && p[i] == p[j])
                return 0 ;

    int diag = -1 , side = -1 ;
    if ( (dis(a,b) == dis(a,c)) &&  (dis(a,c) == dis(c,d)) && (dis(c,d) == dis(b,d)) ){
        if ( dis(b,c) == dis(a,d) ){
            diag = dis(b,c) ;
            side = dis(a,b) ;
        }
    } else if ( (dis(a,c) == dis(a,d)) &&(dis(a,d) == dis(b,c) ) && (dis(b,c) == dis(b,d)) ){
        if ( dis(c,d) == dis(a,b) ){
            diag = dis(c,d) ;
            side = dis(a,c) ;
        }
    }else if ( (dis(a,b) == dis(a,d)) && ( dis(a,d) == dis(c,b) ) && (dis(c,b) == dis(c,d) )){
        if ( dis(b,d) == dis(a,c)){
            diag = dis(b,d) ;
            side = dis(a,b) ;
        }
    }

    if ( diag == -1 || side == -1)
        return 0 ;

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

        int dg = 0 , sd = 0 ;
        for (int j = 0 ; j < 4 ; j++)
        {
            if ( i != j ){

                int dist = dis(p[i],p[j]);

                if ( dist == diag )
                    dg++ ;
                if ( dist == side )
                    sd++ ;
            }
        }
        if (!(dg == 1 && sd == 2) )
            return 0 ;

    }
    return 1 ;
}

void rott(int &x , int &y , int a , int b){

    int lx = x - a;
    int ly = y - b;
    swap(lx,ly);
    lx *= -1;
    lx += a;
    ly += b;
    x = lx;
    y = ly;

}

int main(int argc, const char * argv[]) {

    int n , x , y , a , b ;
    cin >> n ;

    for (int k = 0 ; k < n ; k++){
        vector< pair<int,int> > v[4] ;

        for (int i = 0 ; i < 4 ; i++){
            cin >> x >> y >> a >> b ;
            v[i].push_back({x,y}) ;
            for (int q = 1 ; q < 4 ; q++){
                rott( x , y , a , b ) ;
                v[i].push_back( {x,y} ) ;
            }

        }
        m = 1e9 + 5 ;
        for (int i1 = 0 ; i1 < 4 ; i1++)
        for (int i2 = 0 ; i2 < 4 ; i2++)
        for (int i3 = 0 ; i3 < 4 ; i3++)
        for (int i4 = 0 ; i4 < 4 ; i4++){
            int s = i1 + i2 + i3 + i4 ;
            if ( check(v[0][i1] , v[1][i2] , v[2][i3] , v[3][i4] ) )
                m = min (m , s );
        }

        if ( m >= 1e9 + 5)
            cout << -1 ;
        else
            cout << m ;

        cout << endl;
    }
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by Ali Keramatipour on 7/9/15.
//  Copyright (c) 2015 Ali Keramatipour. All rights reserved.
//

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std ;
int m ;
typedef pair<int,int> pii;

int dis(pii x , pii y)
{
    int xx = x.first - y.first ;
    int yy = x.second - y.second ;
    //cout << xx << " " << yy << endl ;
    return xx*xx + yy*yy ;
}

bool check(pii a , pii b , pii c , pii d){
    //cout << "1 " << endl ;
    //cout << a.first << a.second << " " << b.first << b.second << " " << c.first << c.second << " " << d.first << d.second << endl ;
    //cout << dis(a,b) << endl ;
    pii p[4] = {a,b,c,d} ;
    for (int i = 0 ; i < 4 ; i++)
        for (int j = 0 ; j < 4 ; j++)
            if ( i != j && p[i] == p[j])
                return 0 ;
    int diag = -1 , side = -1 ;
    if ( (dis(a,b) == dis(a,c)) &&  (dis(a,c) == dis(c,d)) && (dis(c,d) == dis(b,d)) ){
        if ( dis(b,c) == dis(a,d) ){
            diag = dis(b,c) ;
            side = dis(a,b) ;
            //cout << diag << " " << side << endl;
            //cout << "her1e" << endl ;
        }
    } else if ( (dis(a,c) == dis(a,d)) &&(dis(a,d) == dis(b,c) ) && (dis(b,c) == dis(b,d)) ){
        if ( dis(c,d) == dis(a,b) ){
            diag = dis(c,d) ;
            side = dis(a,c) ;
            //cout << diag << " " << side << endl;
            //cout << "here1" << endl ;
        }
    }else if ( (dis(a,b) == dis(a,d)) && ( dis(a,d) == dis(c,b) ) && (dis(c,b) == dis(c,d) )){
        if ( dis(b,d) == dis(a,c)){
            diag = dis(b,d) ;
            side = dis(a,b) ;
            //cout << "here1" << endl ;
        }
    }
    if ( diag == -1 || side == -1)
        return 0 ;

    //cout << p[0].first << " " << p[0].second << endl ;
    //cout << "here" << endl ;
    for (int i = 0 ; i < 4 ; i++){
        //cout << p[i].first << " " << p[i].second << endl ;
        int dg = 0 , sd = 0 ;
        for (int j = 0 ; j < 4 ; j++)
        {
            if ( i != j ){
                int dist = dis(p[i],p[j]);
                if ( dist == diag )
                    dg++ ;
                if ( dist == side )
                    sd++ ;
            }
        }
        if (!(dg == 1 && sd == 2) ){
            //cout << "inja" << " " << dg << " " << sd << " " << i << endl ;
            return 0 ;
        }

    }
    return 1 ;
}

bool isSq(pii x , pii y , pii a , pii b )
{
    if (check(x , y , a , b))
        return 1 ;
    return 0 ;
     /*
      || check(x , y , b , a )
      || check(x , a , y , b )
      || check(x , a , b , y )
      || check(x , b , a , y )
      || check(x , b , y , a )

      || check(y , x , a , b )
      || check(y , x , b , a )
      || check(y , a , x , b )
      || check(y , a , b , x )
      || check(y , b , a , x )
      || check(y , b , x , a )

      || check(a , x , y , b )
      || check(a , x , b , y )
      || check(a , y , x , b )
      || check(a , y , b , x )
      || check(a , b , y , x )
      || check(a , b , x , y )

      || check(b , x , y , a )
      || check(b , x , a , y )
      || check(b , y , x , a )
      || check(b , y , a , x )
      || check(b , a , y , x )
      || check(b , a , x , y )*/
}

void rott(int &x , int &y , int a , int b){

    int lx = x - a;
    int ly = y - b;
    swap(lx,ly);
    lx *= -1;
    lx += a;
    ly += b;
    x = lx;
    y = ly;

}

int main(int argc, const char * argv[]) {
    /*
    cout << check({1,1},{-1,-1},{1,-1},{-1,1});
    return 0 ;*/
    int n , x , y , a , b ;
    cin >> n ;
    for (int k = 0 ; k < n ; k++){
    vector< pair< pair<int,int> , int > > v[4] ;

    for (int i = 0 ; i < 4 ; i++){
        cin >> x >> y >> a >> b ;
        v[i].push_back({ {x,y},0 }) ;
        //cout << x << " " << y << endl ;
        for (int q = 1 ; q < 4 ; q++){
            rott( x , y , a , b ) ;
            //cout << x << " " << y << endl ;
            v[i].push_back({ {x,y} , q}) ;
        }

    }
    m = 1e9 + 5 ;
        /*
        for (int i = 0 ; i < 4 ; i++)
        {
            for (int j = 0 ; j < v[i].size() ; j++)
                cout << v[i][j].first.first << " " << v[i][j].first.second << endl ;
            cout << endl << endl ;
        }*/

    for (int i1 = 0 ; i1 < 4 ; i1++)
    for (int i2 = 0 ; i2 < 4 ; i2++)
    for (int i3 = 0 ; i3 < 4 ; i3++)
    for (int i4 = 0 ; i4 < 4 ; i4++){
        int s = i1 + i2 + i3 + i4 ;
        /*
        cout << i1 << " " << i2 << " " << i3 << " " << i4 << endl ;
        cout << v[0][i1].first.first << " " << v[0][i1].first.second << endl ;
        cout << v[1][i2].first.first << " " << v[1][i2].first.second << endl ;
        cout << v[2][i3].first.first << " " << v[2][i3].first.second << endl ;
        cout << v[3][i4].first.first << " " << v[3][i4].first.second << endl ;
        cout << endl ;*/

        if ( isSq(v[0][i1].first , v[1][i2].first , v[2][i3].first , v[3][i4].first) ){
            m = min (m , s );
            //cout << i1 << " " << i2 << " " << i3 << " " << i4 << endl ;
            //cout << v[0][i1].first.first << " " << v[0][i1].first.second << endl ;
            //cout << v[1][i2].first.first << " " << v[1][i2].first.second << endl ;
            //cout << v[2][i3].first.first << " " << v[2][i3].first.second << endl ;
            //cout << v[3][i4].first.first << " " << v[3][i4].first.second << endl ;
            //cout << endl ;
        }
    }

    if ( m >= 1e9 + 5)
        cout << -1 ;
    else
        cout << m ;

    cout << endl;
    }
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by Ali Keramatipour on 7/9/15.
//  Copyright (c) 2015 Ali Keramatipour. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std ;
int m ;
typedef pair<int,int> pii;

bool check(pii x , pii y , pii a , pii b){
    if ( abs(x.first - y.first) == abs(x.second - y.second)
    && abs(a.first - b.first ) == abs(a.second - b.second)
    && abs(a.first - b.first ) != 0
    && abs(x.second - y.second) != 0
    )
    {
        if ( (a.first == x.first) && (b.first == y.first) && (a.second == y.second) && (b.second == x.second) )
            return 1 ;
        if ( (a.second == x.second) && (b.second == y.second) && (a.first == y.first) && (b.first == x.first) )
            return 1 ;
    }
    return 0 ;
}

bool isSq(pii x , pii y , pii a , pii b )
{
    if (check(x , y , a , b)
    || check(x , y , b , a )
    || check(x , a , y , b )
    || check(x , a , b , y )
    || check(x , b , a , y )
    || check(x , b , y , a )

    || check(y , x , a , b )
    || check(y , x , b , a )
    || check(y , a , x , b )
    || check(y , a , b , x )
    || check(y , b , a , x )
    || check(y , b , x , a )

    || check(a , x , y , b )
    || check(a , x , b , y )
    || check(a , y , x , b )
    || check(a , y , b , x )
    || check(a , b , y , x )
    || check(a , b , x , y )

    || check(b , x , y , a )
    || check(b , x , a , y )
    || check(b , y , x , a )
    || check(b , y , a , x )
    || check(b , a , y , x )
    || check(b , a , x , y ) )
        return 1 ;
    return 0 ;

}

void rott(int &x , int &y , int a , int b){

    int lx = x - a;
    int ly = y - b;
    swap(lx,ly);
    lx *= -1;
    lx += a;
    ly += b;
    x = lx;
    y = ly;

}

int main(int argc, const char * argv[]) {

    int n , x , y , a , b ;
    cin >> n ;
    for (int k = 0 ; k < n ; k++){
    vector< pair< pair<int,int> , int > > v[4] ;

    for (int i = 0 ; i < 4 ; i++){
        cin >> x >> y >> a >> b ;
        v[i].push_back({ {x,y},0 }) ;
        //cout << x << " " << y << endl ;
        for (int q = 1 ; q < 4 ; q++){
            rott( x , y , a , b ) ;
            //cout << x << " " << y << endl ;
            v[i].push_back({ {x,y},q }) ;
        }

    }
    m = 1e9 + 5 ;
        /*
        for (int i = 1 ; i <= 1 ; i++)
        {
            for (int j = 0 ; j < v[i].size() ; j++)
                cout << v[i][j].first.first << " " << v[i][j].first.second << endl ;
            cout << endl << endl ;
        }*/

    for (int i1 = 0 ; i1 < 4 ; i1++)
    for (int i2 = 0 ; i2 < 4 ; i2++)
    for (int i3 = 0 ; i3 < 4 ; i3++)
    for (int i4 = 0 ; i4 < 4 ; i4++){
        int s = i1 + i2 + i3 + i4 ;/*
        cout << i1 << " " << i2 << " " << i3 << " " << i4 << endl ;
        cout << v[0][i1].first.first << " " << v[0][i1].first.second << endl ;
        cout << v[1][i2].first.first << " " << v[1][i2].first.second << endl ;
        cout << v[2][i3].first.first << " " << v[2][i3].first.second << endl ;
        cout << v[3][i4].first.first << " " << v[3][i4].first.second << endl ;
        cout << endl ;*/

        if ( isSq(v[0][i1].first , v[1][i2].first , v[2][i3].first , v[3][i4].first) ){
            m = min (m , s );

        }
    }

    if ( m >= 1e9 + 5)
        cout << -1 ;
    else
        cout << m ;

    cout << endl;
    }
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by Ali Keramatipour on 7/9/15.
//  Copyright (c) 2015 Ali Keramatipour. All rights reserved.
//

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std ;
int m ;
typedef pair<int,int> pii;

int dis(pii x , pii y)
{
    int xx = x.first - y.first ;
    int yy = x.second - y.second ;
    //cout << xx << " " << yy << endl ;
    return xx*xx + yy*yy ;
}

bool check(pii a , pii b , pii c , pii d){
    //cout << "1 " << endl ;
    //cout << a.first << a.second << " " << b.first << b.second << " " << c.first << c.second << " " << d.first << d.second << endl ;
    //cout << dis(a,b) << endl ;
    int diag = -1 , side = -1 ;
    if ( (dis(a,b) == dis(a,c)) &&  (dis(a,c) == dis(c,d)) && (dis(c,d) == dis(b,d)) ){
        if ( dis(b,c) == dis(a,d) ){
            diag = dis(b,c) ;
            side = dis(a,b) ;
            //cout << diag << " " << side << endl;
            //cout << "her1e" << endl ;
        }
    } else if ( (dis(a,c) == dis(a,d)) &&(dis(a,d) == dis(b,c) ) && (dis(b,c) == dis(b,d)) ){
        if ( dis(c,d) == dis(a,b) ){
            diag = dis(c,d) ;
            side = dis(a,c) ;
            //cout << diag << " " << side << endl;
            //cout << "here1" << endl ;
        }
    }else if ( (dis(a,b) == dis(a,d)) && ( dis(a,d) == dis(c,b) ) && (dis(c,b) == dis(c,d) )){
        if ( dis(b,d) == dis(a,c)){
            diag = dis(b,d) ;
            side = dis(a,b) ;
            //cout << "here1" << endl ;
        }
    }
    if ( diag == -1 || side == -1)
        return 0 ;
    pii p[4] = {a,b,c,d} ;
    //cout << p[0].first << " " << p[0].second << endl ;
    //cout << "here" << endl ;
    for (int i = 0 ; i < 4 ; i++){
        //cout << p[i].first << " " << p[i].second << endl ;
        int dg = 0 , sd = 0 ;
        for (int j = 0 ; j < 4 ; j++)
        {
            if ( i != j ){
                int dist = dis(p[i],p[j]);
                if ( dist == diag )
                    dg++ ;
                if ( dist == side )
                    sd++ ;
            }
        }
        if (!(dg == 1 && sd == 2) ){
            //cout << "inja" << " " << dg << " " << sd << " " << i << endl ;
            return 0 ;
        }

    }

    return 1 ;
}

bool isSq(pii x , pii y , pii a , pii b )
{
    if (check(x , y , a , b))
        return 1 ;
    return 0 ;
     /*
      || check(x , y , b , a )
      || check(x , a , y , b )
      || check(x , a , b , y )
      || check(x , b , a , y )
      || check(x , b , y , a )

      || check(y , x , a , b )
      || check(y , x , b , a )
      || check(y , a , x , b )
      || check(y , a , b , x )
      || check(y , b , a , x )
      || check(y , b , x , a )

      || check(a , x , y , b )
      || check(a , x , b , y )
      || check(a , y , x , b )
      || check(a , y , b , x )
      || check(a , b , y , x )
      || check(a , b , x , y )

      || check(b , x , y , a )
      || check(b , x , a , y )
      || check(b , y , x , a )
      || check(b , y , a , x )
      || check(b , a , y , x )
      || check(b , a , x , y )*/
}

void rott(int &x , int &y , int a , int b){

    int lx = x - a;
    int ly = y - b;
    swap(lx,ly);
    lx *= -1;
    lx += a;
    ly += b;
    x = lx;
    y = ly;

}

int main(int argc, const char * argv[]) {
    /*
    cout << check({1,1},{-1,-1},{1,-1},{-1,1});
    return 0 ;*/
    int n , x , y , a , b ;
    cin >> n ;
    for (int k = 0 ; k < n ; k++){
    vector< pair< pair<int,int> , int > > v[4] ;

    for (int i = 0 ; i < 4 ; i++){
        cin >> x >> y >> a >> b ;
        v[i].push_back({ {x,y},0 }) ;
        //cout << x << " " << y << endl ;
        for (int q = 1 ; q < 4 ; q++){
            rott( x , y , a , b ) ;
            //cout << x << " " << y << endl ;
            v[i].push_back({ {x,y} , q}) ;
        }

    }
    m = 1e9 + 5 ;
        /*
        for (int i = 0 ; i < 4 ; i++)
        {
            for (int j = 0 ; j < v[i].size() ; j++)
                cout << v[i][j].first.first << " " << v[i][j].first.second << endl ;
            cout << endl << endl ;
        }*/

    for (int i1 = 0 ; i1 < 4 ; i1++)
    for (int i2 = 0 ; i2 < 4 ; i2++)
    for (int i3 = 0 ; i3 < 4 ; i3++)
    for (int i4 = 0 ; i4 < 4 ; i4++){
        int s = i1 + i2 + i3 + i4 ;
        /*
        cout << i1 << " " << i2 << " " << i3 << " " << i4 << endl ;
        cout << v[0][i1].first.first << " " << v[0][i1].first.second << endl ;
        cout << v[1][i2].first.first << " " << v[1][i2].first.second << endl ;
        cout << v[2][i3].first.first << " " << v[2][i3].first.second << endl ;
        cout << v[3][i4].first.first << " " << v[3][i4].first.second << endl ;
        cout << endl ;*/

        if ( isSq(v[0][i1].first , v[1][i2].first , v[2][i3].first , v[3][i4].first) ){
            m = min (m , s );
            //cout << i1 << " " << i2 << " " << i3 << " " << i4 << endl ;
            //cout << v[0][i1].first.first << " " << v[0][i1].first.second << endl ;
            //cout << v[1][i2].first.first << " " << v[1][i2].first.second << endl ;
            //cout << v[2][i3].first.first << " " << v[2][i3].first.second << endl ;
            //cout << v[3][i4].first.first << " " << v[3][i4].first.second << endl ;
            //cout << endl ;
        }
    }

    if ( m >= 1e9 + 5)
        cout << -1 ;
    else
        cout << m ;

    cout << endl;
    }
    return 0;
}


//Bismillahir Rahmanir Rahim
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cassert>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<string>
#include<sstream>
#include<algorithm>
#include<vector>
#include<stack>
#include<list>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<bitset>
#include<ctime>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int

#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)

#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)

#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFL(z) printf("%I64d",z)
#define PFS(z) printf("%s",z)

#define MI map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define SI set<int>
#define SS set<string>
#define SMI multiset<int>
#define VI vector<int>
#define VI2 vector < vector < int > >
#define QU queue<int>
#define PQU priority_queue<int>
#define STK stack<int>
#define pb push_back

#define inf 2000000000
#define pi  acos(-1.0)
#define MAX 1000007
#define MOD 1000000007
#define eps 1e-9

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];

vector<int>edges,cost;
int l=lower_bound(a+1,a+5,4)-a;
///amake 1 theke 5 tomo index porjnto search dibe. jdi match khay taile oi index i or na match khele 1 add kore dibe.
int u=upper_bound(a+1,a+5,18)-a;
///amake 1 theke 5 tomo index porjnto search dibe. match khakor na khak 1 add kore dibe.
//***last e ekta extra largest element both khetre add korte hbe.
printf("%s\n",s.c_str());
//MOD er khetre jdi t kokhono negative hoye jay then ekta extra MOD add kore neya better.
*/

/******************* Code Starts here *******************/
struct D
{
    int xx,yy;
    bool operator<(const D& p)
    const
       {
           if(xx<p.xx) return true;
           else if(xx==p.xx && yy<=p.yy) return true;
           else return false;
        }
}dd[6];

int n,res,x[1000],y[1000],a[1000],b[1000],g[3][5],g1[3][5],g2[3][5],g3[3][5];

int X(int id, int num)
{
    if(num==1)
    {
        return a[id]-(y[id]-b[id]);
    }
    else if(num==2)
    {
        return a[id]-(x[id]-a[id]);
    }
    else if(num==3)
    {
        return a[id]+(y[id]-b[id]);
    }
    else return a[id]+(x[id]-a[id]);
}

int Y(int id, int num)
{
    if(num==1)
    {
        return x[id]-a[id]+b[id];
    }
    else if(num==2)
    {
        return b[id]-(y[id]-b[id]);
    }
    else if(num==3)
    {
        return b[id]-(x[id]-a[id]);
    }
    else return b[id]+(y[id]-b[id]);
}

bool isSquare(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
   dd[1].xx=x1; dd[1].yy=y1;
   dd[2].xx=x2; dd[2].yy=y2;
   dd[3].xx=x3; dd[3].yy=y3;
   dd[4].xx=x4; dd[4].yy=y4;
   sort(dd+1,dd+5);

if(dd[1].xx!=dd[2].xx || dd[3].xx!=dd[4].xx || dd[1].yy==dd[2].yy || dd[3].yy==dd[4].yy || dd[1].xx==dd[3].xx || dd[2].xx==dd[4].xx) return false;
   int d1=abs(dd[1].xx-dd[3].xx);
   int d2=abs(dd[2].xx-dd[4].xx);
   int d3=abs(dd[1].yy-dd[2].yy);
   int d4=abs(dd[3].yy-dd[4].yy);
   if(d1==d2 && d2==d3 && d3==d4 && d1>0) return true;
   return false;
}

int main()
{
    //fin("A-small-attempt0.in");
   // fout("A_output.txt");
    int x1,y1,x2,y2,x3,y3,x4,y4,tamp;
    S(n);
    while(n--)
    {
        res=2000000000;

        repc(i,1,4)
        {
          sc("%d %d %d %d",&x[i],&y[i],&a[i],&b[i]);
        }
        repc(i,1,4)
        {
            g[1][i]=X(1,i); g[2][i]=Y(1,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g1[1][i]=X(2,i); g1[2][i]=Y(2,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g2[1][i]=X(3,i); g2[2][i]=Y(3,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g3[1][i]=X(4,i); g3[2][i]=Y(4,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
        repc(i,1,4)
        {
            x1=g[1][i]; y1=g[2][i];
            repc(j,1,4)
            {
                x2=g1[1][j];y2=g1[2][j];
                repc(k,1,4)
                {
                    x3=g2[1][k];y3=g2[2][k];

                    repc(l,1,4)
                    {
                       x4=g3[1][l];y4=g3[2][l];
                      // pf("gp\n");
                       if(isSquare(x1,y1,x2,y2,x3,y3,x4,y4))
                       {

                           tamp=i%4+j%4+k%4+l%4;
                         // pf("%d %d %d %d %d %d %d %d ----- %d %d\n",x1,y1,x2,y2,x3,y3,x4,y4,res,tamp);
                           res=min(res,tamp);
                       }
                    }
                }
            }
        }

       // pfn;

       // pfn;
        if(res<2000000000) PF(res);
        else pf("-1");
        pfn;
    }
    return 0;
}