// Author : Bony Roopchandani

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

typedef pair<double, double> Point;
Point home[4];
bool ok;

double dist(Point x, Point y) {
	return hypot(y.first-x.first, y.second-x.second);
}

Point rotate90byOrigin(Point a, Point v) {
	a.first -= v.first;
    a.second -= v.second;
    return Point(-a.second + v.first, a.first + v.second);
}

bool isSquare(Point a, Point b, Point c, Point d)
{
    if(a==b or a==c or a==d or b==c or b==d or c==d) return false;
    Point x[4];
    x[0]=a, x[1]=b, x[2]=c, x[3]=d;
    int r[4] = {0,1,2,3};
    do {
        a = x[r[0]];
        b = x[r[1]];
        c = x[r[2]];
        d = x[r[3]];
        if(rotate90byOrigin(b,a) == d && rotate90byOrigin(c,b) == a) return true;

    } while(next_permutation(r, r+4));
    return false;
}

Point rotate(Point a, int p) {
	double aa=home[p].first-(a.second-home[p].second);
	double bb=home[p].second+(a.first-home[p].first);
	return make_pair(aa, bb);
}

int solve(Point a, Point b, Point c, Point d, int cntA, int cntB, int cntC, int cntD) {
	if(cntA==4 or cntB==4 or cntC==4 or cntD==4) return 0;
	if(isSquare(a, b, c, d)) {
		ok=true;
		return 0;
	}
	int aa=solve(rotate(a, 0), b, c, d, cntA+1, cntB, cntC, cntD);
	int bb=solve(a, rotate(b, 1), c, d, cntA, cntB+1, cntC, cntD);
	int cc=solve(a, b, rotate(c, 2), d, cntA, cntB, cntC+1, cntD);
	int dd=solve(a, b, c, rotate(d, 3), cntA, cntB, cntC, cntD+1);
	return 1+min(aa, min(bb, min(cc, dd)));
}

int main(void) {
	ios_base::sync_with_stdio(false);
	int testCase;
	for(cin>>testCase; testCase; testCase--) {
		double x, y, hx, hy;
		Point a, b, c, d;
		cin>>x>>y>>hx>>hy;
		a={x, y}; home[0]={hx, hy};
		cin>>x>>y>>hx>>hy;
		b={x, y}; home[1]={hx, hy};
		cin>>x>>y>>hx>>hy;
		c={x, y}; home[2]={hx, hy};
		cin>>x>>y>>hx>>hy;
		d={x, y}; home[3]={hx, hy};
		ok=false;
		int res=solve(a, b, c, d, 0, 0, 0, 0);
		cout<<(ok?res:-1)<<"\n";
	}
	return (0);
}


// Author : Bony Roopchandani

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

typedef pair<double, double> Point;
Point home[4], s[4];
bool ok;
int res, ans;

Point rotate(Point a, Point v) {
	a.first -= v.first;
    a.second -= v.second;
    return Point(-a.second + v.first, a.first + v.second);
}

bool isSquare(Point a, Point b, Point c, Point d)
{
    if(a==b or a==c or a==d or b==c or b==d or c==d) return false;
    Point x[4];
    x[0]=a, x[1]=b, x[2]=c, x[3]=d;
    int r[4] = {0,1,2,3};
    do {
        a = x[r[0]];
        b = x[r[1]];
        c = x[r[2]];
        d = x[r[3]];
        if(rotate(b,a) == d && rotate(c,b) == a) return true;

    } while(next_permutation(r, r+4));
    return false;
}

void solve(int pos) {
	if(pos==4) {
		if(isSquare(s[0], s[1], s[2], s[3])) {
			ok=true;
			res=min(res, ans);
		}
		return;
	}
	solve(pos+1);
	ans+=1;
	s[pos]=rotate(s[pos], home[pos]);
	solve(pos+1);
	ans+=1;
	s[pos]=rotate(s[pos], home[pos]);
	solve(pos+1);
	ans+=1;
	s[pos]=rotate(s[pos], home[pos]);
	solve(pos+1);
	ans-=3;
}

int main(void) {
	ios_base::sync_with_stdio(false);
	int testCase;
	for(cin>>testCase; testCase; testCase--) {
		double x, y, hx, hy;
		cin>>x>>y>>hx>>hy;
		s[0]={x, y}; home[0]={hx, hy};
		cin>>x>>y>>hx>>hy;
		s[1]={x, y}; home[1]={hx, hy};
		cin>>x>>y>>hx>>hy;
		s[2]={x, y}; home[2]={hx, hy};
		cin>>x>>y>>hx>>hy;
		s[3]={x, y}; home[3]={hx, hy};
		ok=false;
		ans=0;
		res=12345678;
		solve(0);
		cout<<(ok?res:-1)<<"\n";
	}
	return (0);
}


//CppSpoj <CERC07K , Key Task>
/**Milan Topalovic
*Faculty of Computing Belgrade
*/
//#include <unordered_map>
//#include <unordered_set>
#include <map>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
//#include <deque>
#include <limits.h>
//#include <climits>
#include <float.h> //DBL_MAX
#include <string>
#include <cstring>
//#include <cstdlib>
//#include <time.h>
#include <utility>
#include <iomanip>

using namespace std;

#define endl '\n'
#define dendl "\n\n"
#define cti(chr) (((chr) - ('0'))) //char to int
#define itc(inte) (((inte) + ('0')))
#define isd(chr) (((chr) >= ('0') && (chr) <= ('9')))
#define is_upper(chr) (((chr) >= ('A') && (chr) <= ('Z')))
#define isin(x , y , lx , ly , bx , by) (((x) >= (lx)) && ((x) < (bx)) && ((y) >= (ly))  && ((y) < (by)))

typedef long long int ll;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef priority_queue<int , vector<int> , less<int> > maxqueue;
typedef priority_queue<int , vector<int> , greater<int> > minqueue;
long long  mod = 1e9 + 7;
long long  mod2 = 1e9 + 9;
double eps = 1e-9;
const int MAX_SIZE = 1e7 + 1;

int n , t , m , q , k , p;

pii rotate90(int &l1 , int &l2 , int a , int b)
{
	swap(l1 , l2);
	l1 *= -1;
	return {a + l1 , b + l2};
}

vector<pii> rotate(int x , int y , int a , int b)
{
	int lx = x - a;
	int ly = y - b;

	vector<pii> pos = {{x , y}};
	pos.push_back(rotate90(lx , ly , a , b));
	pos.push_back(rotate90(lx , ly , a , b));
	pos.push_back(rotate90(lx , ly , a , b));

	return pos;
}

bool check(vector<pii> points)
{
	sort(points.begin() , points.end());

	if( points[0].first != points[1].first ||
		points[2].first != points[3].first ||
		points[0].second != points[2].second ||
		points[1].second != points[3].second )
		return false;

	int lx = points[2].first - points[0].first;
	int ly = points[1].second - points[0].second;

	return (ly == lx && ly);
}

int main()
{
	ios_base::sync_with_stdio(0);

	cin >> n;

	for(int i = 0 ; i < n ; ++i)
	{
		vector<vector<pii> > moles;
		for(int j = 0 ; j < 4 ; ++j)
		{
			int xi , yi , ai , bi; cin >> xi >> yi >> ai >> bi;
			moles.push_back(rotate(xi , yi , ai , bi));
		}

		int ans = 100;
		for(int j = 0 ; j < 4 ; ++j)
			for(int z = 0 ; z < 4 ; ++z)
				for(int k = 0 ; k < 4 ; ++k)
					for(int g = 0 ; g < 4 ; ++g)
						if(check({
							moles[0][j] ,
							moles[1][z] ,
							moles[2][k] ,
							moles[3][g]
						})) ans = min(ans , j + z + k + g);

		if(ans == 100) ans = -1;
		cout << ans << endl;

	}

	cin >> n;
	//scanf("%d" , &t);
	//printf("%.10lf\n" , bs());
}


//CppSpoj <CERC07K , Key Task>
/**Milan Topalovic
*Faculty of Computing Belgrade
*/
//#include <unordered_map>
//#include <unordered_set>
#include <map>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
//#include <deque>
#include <limits.h>
//#include <climits>
#include <float.h> //DBL_MAX
#include <string>
#include <cstring>
//#include <cstdlib>
//#include <time.h>
#include <utility>
#include <iomanip>

using namespace std;

#define endl '\n'
#define dendl "\n\n"
#define cti(chr) (((chr) - ('0'))) //char to int
#define itc(inte) (((inte) + ('0')))
#define isd(chr) (((chr) >= ('0') && (chr) <= ('9')))
#define is_upper(chr) (((chr) >= ('A') && (chr) <= ('Z')))
#define isin(x , y , lx , ly , bx , by) (((x) >= (lx)) && ((x) < (bx)) && ((y) >= (ly))  && ((y) < (by)))

typedef long long int ll;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef priority_queue<int , vector<int> , less<int> > maxqueue;
typedef priority_queue<int , vector<int> , greater<int> > minqueue;
long long  mod = 1e9 + 7;
long long  mod2 = 1e9 + 9;
double eps = 1e-9;
const int MAX_SIZE = 1e7 + 1;

int n , t , m , q , k , p;

pii rotate90(int &l1 , int &l2 , int a , int b)
{
	swap(l1 , l2);
	l1 *= -1;
	return {a + l1 , b + l2};
}

vector<pii> rotate(int x , int y , int a , int b)
{
	int lx = x - a;
	int ly = y - b;

	vector<pii> pos = {{x , y}};
	pos.push_back(rotate90(lx , ly , a , b));
	pos.push_back(rotate90(lx , ly , a , b));
	pos.push_back(rotate90(lx , ly , a , b));

	return pos;
}

ll square_dist(pii p1 , pii p2)
{
	return (ll)(p1.first - p2.first) * (ll)(p1.first - p2.first)
	+ (ll)(p1.second - p2.second) * (ll)(p1.second - p2.second);
}

bool check(vector<pii> points)
{
	vector<ll> sides;
	for(int i = 0 ; i < 4 ; ++i)
		for(int j = i + 1 ; j < 4 ; ++j)
			sides.push_back(square_dist(points[i] , points[j]));

	sort(sides.begin() , sides.end());
	return (sides[0] && sides[0] == sides[3] && sides[4] == sides[5]);
}

int main()
{
	ios_base::sync_with_stdio(0);

	cin >> n;

	for(int i = 0 ; i < n ; ++i)
	{
		vector<vector<pii> > moles;
		for(int j = 0 ; j < 4 ; ++j)
		{
			int xi , yi , ai , bi; cin >> xi >> yi >> ai >> bi;
			moles.push_back(rotate(xi , yi , ai , bi));
		}

		int ans = 100;
		for(int j = 0 ; j < 4 ; ++j)
			for(int z = 0 ; z < 4 ; ++z)
				for(int k = 0 ; k < 4 ; ++k)
					for(int g = 0 ; g < 4 ; ++g)
						if(check({
							moles[0][j] ,
							moles[1][z] ,
							moles[2][k] ,
							moles[3][g]
						})) ans = min(ans , j + z + k + g);

		if(ans == 100) ans = -1;
		cout << ans << endl;

	}

	cin >> n;
	//scanf("%d" , &t);
	//printf("%.10lf\n" , bs());
}


//CppSpoj <CERC07K , Key Task>
/**Milan Topalovic
*Faculty of Computing Belgrade
*/
//#include <unordered_map>
//#include <unordered_set>
#include <map>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
//#include <deque>
#include <limits.h>
//#include <climits>
#include <float.h> //DBL_MAX
#include <string>
#include <cstring>
//#include <cstdlib>
//#include <time.h>
#include <utility>
#include <iomanip>

using namespace std;

#define endl '\n'
#define dendl "\n\n"
#define cti(chr) (((chr) - ('0'))) //char to int
#define itc(inte) (((inte) + ('0')))
#define isd(chr) (((chr) >= ('0') && (chr) <= ('9')))
#define is_upper(chr) (((chr) >= ('A') && (chr) <= ('Z')))
#define isin(x , y , lx , ly , bx , by) (((x) >= (lx)) && ((x) < (bx)) && ((y) >= (ly))  && ((y) < (by)))

typedef long long int ll;
typedef pair<int , int> pii;
typedef pair<ll , ll> pll;
typedef priority_queue<int , vector<int> , less<int> > maxqueue;
typedef priority_queue<int , vector<int> , greater<int> > minqueue;
long long  mod = 1e9 + 7;
long long  mod2 = 1e9 + 9;
double eps = 1e-9;
const int MAX_SIZE = 1e7 + 1;

int n , t , m , q , k , p;

pii rotate90(int &l1 , int &l2 , int a , int b)
{
	swap(l1 , l2);
	l1 *= -1;
	return {a + l1 , b + l2};
}

vector<pii> rotate(int x , int y , int a , int b)
{
	int lx = x - a;
	int ly = y - b;

	vector<pii> pos = {{x , y}};
	pos.push_back(rotate90(lx , ly , a , b));
	pos.push_back(rotate90(lx , ly , a , b));
	pos.push_back(rotate90(lx , ly , a , b));

	return pos;
}

bool check(vector<pii> points)
{
	sort(points.begin() , points.end());

	if( points[0].first != points[1].first ||
		points[2].first != points[3].first ||
		points[0].second != points[2].second ||
		points[1].second != points[3].second )
		return false;

	int lx = points[2].first - points[0].first;
	int ly = points[1].second - points[0].second;

	return (ly == lx && ly);
}

int main()
{
	ios_base::sync_with_stdio(0);

	cin >> n;

	for(int i = 0 ; i < n ; ++i)
	{
		vector<vector<pii> > moles;
		for(int j = 0 ; j < 4 ; ++j)
		{
			int xi , yi , ai , bi; cin >> xi >> yi >> ai >> bi;
			moles.push_back(rotate(xi , yi , ai , bi));
		}

		int ans = 100;
		for(int j = 0 ; j < 4 ; ++j)
			for(int z = 0 ; z < 4 ; ++z)
				for(int k = 0 ; k < 4 ; ++k)
					for(int g = 0 ; g < 4 ; ++g)
						if(check({
							moles[0][j] ,
							moles[1][z] ,
							moles[2][k] ,
							moles[3][g]
						})) ans = min(ans , j + z + k + g);

		if(ans == 100) ans = -1;
		cout << ans << endl;

	}

	// cin >> n;
	//scanf("%d" , &t);
	//printf("%.10lf\n" , bs());
}


#include <bits/stdc++.h>

using namespace std;

struct RTC{~RTC(){cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC <<" seconds\n";}} runtimecount;
#define DBG(X) cerr << #X << " = " << X << '\n';
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define sz(x) ((int)(x).size())
#define all(c) (c).begin(),(c).end()
#define forn(i, n) for (int i = 0; i < (n); i++)
struct Point {
  int x, y;
  Point() {}
  Point(int _x, int _y):x(_x), y(_y) {}
  bool operator < (const Point &other) const {
    if (x == other.x) return y < other.y;
    return x < other.x;
  }
  bool operator == (const Point &other) const {
    return (x == other.x && y == other.y);
  }
  void read() {
    scanf("%d %d", &x, &y);
  }
};
Point center[4], mole[4], p[4];
void rotate(int pos) {
  int x = mole[pos].x - center[pos].x;
  int y = mole[pos].y - center[pos].y;
  mole[pos].x = -y + center[pos].x;
  mole[pos].y =  x + center[pos].y;
}
bool check() {
  forn (i, 4)
    for (int j = i + 1; j < 4; j++)
      if (mole[i] == mole[j])
	return false;
  forn (i, 4)
    p[i] = mole[i];
  sort(p, p + 4);
  swap(p[2], p[3]);
  int dist = -1;
  forn (i, 4) {
    int next = i + 1;
    if (next == 4) next = 0;
    if (p[i].x == p[next].x xor p[i].y == p[next].y) {
      int d = abs(p[i].x - p[next].x) + abs(p[i].y - p[next].y);
      if (dist == -1)
	dist = d;
      else if (d != dist)
	return false;
    } else {
      return false;
    }
  }
  return true;
}
int n;
int main() {
  scanf("%d", &n);
  while (n--) {
    forn (i, 4)
      mole[i].read(), center[i].read();
    int ans = -1;
    for (int a = 0; a < 4; a++) {
      for (int b = 0; b < 4; b++) {
	for (int c = 0; c < 4; c++) {
	  for (int d = 0; d < 4; d++) {
	    if (check()) {
	      if (ans == -1)
		ans = a + b + c + d;
	      else
		ans = min(ans, a + b + c + d);
	    }
	    rotate(3);
	  }
	  rotate(2);
	}
	rotate(1);
      }
      rotate(0);
    }
    printf("%d\n", ans);
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct RTC{~RTC(){cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC <<" seconds\n";}} runtimecount;
#define DBG(X) cerr << #X << " = " << X << '\n';
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define sz(x) ((int)(x).size())
#define all(c) (c).begin(),(c).end()
#define forn(i, n) for (int i = 0; i < (n); i++)
inline int sqr(int x) {return x*x;}
struct Point {
  int x, y;
  Point() {}
  Point(int _x, int _y):x(_x), y(_y) {}
  bool operator < (const Point &other) const {
    if (x == other.x) return y < other.y;
    return x < other.x;
  }
  bool operator == (const Point &other) const {
    return (x == other.x && y == other.y);
  }
  int distsqr(const Point &other) {
    return sqr(x - other.x) + sqr(y - other.y);
  }
  void read() {
    scanf("%d %d", &x, &y);
  }
};

Point center[4], mole[4], p[4];
void rotate(int pos) {
  int x = mole[pos].x - center[pos].x;
  int y = mole[pos].y - center[pos].y;
  mole[pos].x = -y + center[pos].x;
  mole[pos].y =  x + center[pos].y;
}
bool check() {
  forn (i, 4)
    for (int j = i + 1; j < 4; j++)
      if (mole[i] == mole[j])
	return false;
  forn (i, 4)
    p[i] = mole[i];
  sort(p, p + 4);
  swap(p[2], p[3]);
  int dist = -1;
  forn (i, 4) {
    int next = i + 1;
    if (next == 4) next = 0;
    int d = p[i].distsqr(p[next]);;
    if (dist == -1)
      dist = d;
    else if (d != dist)
      return false;
  }
  return (p[0].distsqr(p[2]) == p[1].distsqr(p[3]));
}
int n;
int main() {
  scanf("%d", &n);
  while (n--) {
    forn (i, 4)
      mole[i].read(), center[i].read();
    int ans = -1;
    for (int a = 0; a < 4; a++) {
      for (int b = 0; b < 4; b++) {
	for (int c = 0; c < 4; c++) {
	  for (int d = 0; d < 4; d++) {
	    if (check()) {
	      if (ans == -1)
		ans = a + b + c + d;
	      else
		ans = min(ans, a + b + c + d);
	    }
	    rotate(3);
	  }
	  rotate(2);
	}
	rotate(1);
      }
      rotate(0);
    }
    printf("%d\n", ans);
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct RTC{~RTC(){cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC <<" seconds\n";}} runtimecount;
#define DBG(X) cerr << #X << " = " << X << '\n';
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define sz(x) ((int)(x).size())
#define all(c) (c).begin(),(c).end()
#define forn(i, n) for (int i = 0; i < (n); i++)
struct Point {
  int x, y;
  Point() {}
  Point(int _x, int _y):x(_x), y(_y) {}
  bool operator < (const Point &other) const {
    if (x == other.x) return y < other.y;
    return x < other.x;
  }
  bool operator == (const Point &other) const {
    return (x == other.x && y == other.y);
  }
  void read() {
    scanf("%d %d", &x, &y);
  }
};
inline int sqr(int x) {return x*x;}
Point center[4], mole[4], p[4];
void rotate(int pos) {
  int x = mole[pos].x - center[pos].x;
  int y = mole[pos].y - center[pos].y;
  mole[pos].x = -y + center[pos].x;
  mole[pos].y =  x + center[pos].y;
}
bool check() {
  forn (i, 4)
    for (int j = i + 1; j < 4; j++)
      if (mole[i] == mole[j])
	return false;
  forn (i, 4)
    p[i] = mole[i];
  sort(p, p + 4);
  swap(p[2], p[3]);
  int dist = -1;
  forn (i, 4) {
    int next = i + 1;
    if (next == 4) next = 0;
    int d = sqr(p[i].x - p[next].x) + sqr(p[i].y - p[next].y);
    if (dist == -1)
      dist = d;
    else if (d != dist)
      return false;
  }
  return true;
}
int n;
int main() {
  scanf("%d", &n);
  while (n--) {
    forn (i, 4)
      mole[i].read(), center[i].read();
    int ans = -1;
    for (int a = 0; a < 4; a++) {
      for (int b = 0; b < 4; b++) {
	for (int c = 0; c < 4; c++) {
	  for (int d = 0; d < 4; d++) {
	    if (check()) {
	      if (ans == -1)
		ans = a + b + c + d;
	      else
		ans = min(ans, a + b + c + d);
	    }
	    rotate(3);
	  }
	  rotate(2);
	}
	rotate(1);
      }
      rotate(0);
    }
    printf("%d\n", ans);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
double const eps = 1e-6;
int n;
pair<int,int> v[N][4][4], h[N][4];
int D(pair<int,int> a, pair<int,int> b){
    return pow(a.first-b.first, 2) + pow(a.second-b.second, 2);
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     do{
        int f0 = D(P[0], P[1]), f1 = D(P[1], P[2]), f2 = D(P[2], P[3]), f3 = D(P[3], P[0]);
        if(f0 != f1 || f0 != f2 || f0 != f3)continue;
        return true;
     }while(next_permutation(P.begin(), P.end()));
     return false;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
int n;
pair<int,int> v[N][4][4], h[N][4];

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     map<int,int> x, y;
     ++x[a.first], ++y[a.second];
     ++x[b.first], ++y[b.second];
     ++x[c.first], ++y[c.second];
     ++x[d.first], ++y[d.second];
     if(x.size() != 2 || y.size() != 2)return false;
     for(auto i: x)if(i.second != 2)return false;
     for(auto i: y)if(i.second != 2)return false;
     return true;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
               }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
double const eps = 1e-6;
int n;
pair<int,int> v[N][4][4], h[N][4];
ll D(pair<int,int> a, pair<int,int> b){
    return pow(a.first-b.first, 2ll) + pow(a.second-b.second, 2ll);
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     do{
        ll d0 = D(P[0], P[1]), d1 = D(P[0], P[2]), d2 = D(P[0], P[3]);
        if(d0 == d1 && 2 * d0 == d2 && 2 * D(P[1], P[3]) == D(P[1], P[2]))return true;
        if(d1 == d2 && 2 * d1 == d0 && 2 * D(P[2], P[1]) == D(P[2], P[3]))return true;
        if(d0 == d2 && 2 * d0 == d1 && 2 * D(P[1], P[2]) == D(P[1], P[3]))return true;
     }while(next_permutation(P.begin(), P.end()));
     return false;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
int n;
pair<int,int> v[N][4][4], h[N][4];

double D(int x, int y, int tx, int ty){
       return (double)(sqrt(pow(x-tx, 2ll)+pow(y-ty, 2ll)));
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     bool f = 0;
     do{
        f = 0;
        double ds = D(P[0].first, P[0].second, P[1].first, P[1].second);
        if(!ds)continue;
        for(int i = 1; i<4; ++i){
           double cd = D(P[i].first, P[i].second, P[(i+1)%4].first, P[(i+1)%4].second);
           if(cd != ds){f = 1; break;}
        }
        if(ds && !f){return true;}
     }while(next_permutation(P.begin(), P.end()));
     return false;
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
double const eps = 1e-6;
int n;
pair<int,int> v[N][4][4], h[N][4];
int D(pair<int,int> a, pair<int,int> b){
    return pow(a.first-b.first, 2) + pow(a.second-b.second, 2);
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     do{
        int d0 = D(P[0], P[1]), d1 = D(P[0], P[2]), d2 = D(P[0], P[3]);
        if(d0 == d1 && 2 * d0 == d2 && 2 * D(P[1], P[3]) == D(P[1], P[2]))return true;
        if(d1 == d2 && 2 * d1 == d0 && 2 * D(P[2], P[1]) == D(P[2], P[3]))return true;
        if(d0 == d2 && 2 * d0 == d1 && 2 * D(P[1], P[2]) == D(P[1], P[3]))return true;
     }while(next_permutation(P.begin(), P.end()));
     return false;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
double const eps = 1e-6;
int n;
pair<int,int> v[N][4][4], h[N][4];

double D(int x, int y, int tx, int ty){
       return (double)(sqrt(pow(x-tx, 2ll)+pow(y-ty, 2ll)));
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     bool f = 0;
     do{
        f = 0;
        double ds = D(P[0].first, P[0].second, P[1].first, P[1].second);
        for(int i = 1; i<4; ++i){
           double cd = D(P[i].first, P[i].second, P[(i+1)%4].first, P[(i+1)%4].second);
           if(fabs(ds - cd) >= eps){f = 1; break;}
        }
        if(!f){return true;}
     }while(next_permutation(P.begin(), P.end()));
     return false;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
int n;
pair<int,int> v[N][4][4], h[N][4];

double D(int x, int y, int tx, int ty){
       return (double)(sqrt(pow(x-tx, 2ll)+pow(y-ty, 2ll)));
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     map<int,int> x, y;
     ++x[a.first], ++y[a.second];
     ++x[b.first], ++y[b.second];
     ++x[c.first], ++y[c.second];
     ++x[d.first], ++y[d.second];
     if(x.size() != 2 || y.size() != 2)return false;
     for(auto i: x)if(i.second != 2)return false;
     for(auto i: y)if(i.second != 2)return false;
     return true;
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
   }
    for(int i = 0; i<n; ++i){
      int an = 1e9;
          for(int x0 = 0; x0<4; ++x0){
              for(int x1 = 0; x1<4; ++x1){
                  for(int x2 = 0; x2<4; ++x2){
                      for(int x3 = 0; x3<4; ++x3){
                          int ad = x0 + x1 + x2 + x3;
                          if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3])){
                            an = min(an, ad);
                          }
                      }
               }
           }
      }
      printf("%d\n", an == 1e9? -1: an);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
double const eps = 1e-6;
int n;
pair<int,int> v[N][4][4], h[N][4];

double D(int x, int y, int tx, int ty){
       return (double)(sqrt(pow(x-tx, 2ll)+pow(y-ty, 2ll)));
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     bool f = 0;
     do{
        f = 0;
        int ds = D(P[0].first, P[0].second, P[1].first, P[1].second);
        for(int i = 1; i<4; ++i){
           int cd = D(P[i].first, P[i].second, P[(i+1)%4].first, P[(i+1)%4].second);
           if(fabs(ds - cd) > eps){f = 1; break;}
        }
        if(!f){return true;}
     }while(next_permutation(P.begin(), P.end()));
     return false;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
double const eps = 1e-6;
int n;
pair<int,int> v[N][4][4], h[N][4];

ll D(pair<int,int> a, pair<int,int> b){
    return pow(a.first-b.first, 2ll) + pow(a.second-b.second, 2ll);
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     ll d0 = D(P[0], P[1]), d1 = D(P[0], P[2]), d2 = D(P[0], P[3]);
     if(d0 == d1 && 2 * d0 == d2 && 2 * D(P[1], P[3]) == D(P[1], P[2]))return true;
     if(d1 == d2 && 2 * d1 == d0 && 2 * D(P[2], P[1]) == D(P[2], P[3]))return true;
     if(d0 == d2 && 2 * d0 == d1 && 2 * D(P[1], P[2]) == D(P[1], P[3]))return true;
     return false;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
double const eps = 1e-6;
int n;
pair<int,int> v[N][4][4], h[N][4];

ll D(int x, int y, int tx, int ty){
       return (ll)(sqrt(pow(x-tx, 2ll)+pow(y-ty, 2ll)));
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     bool f = 0;
     do{
        f = 0;
        ll ds = D(P[0].first, P[0].second, P[1].first, P[1].second);
        for(int i = 1; i<4; ++i){
           ll cd = D(P[i].first, P[i].second, P[(i+1)%4].first, P[(i+1)%4].second);
           if(ds != cd){f = 1; break;}
        }
        if(!f){return true;}
     }while(next_permutation(P.begin(), P.end()));
     return false;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 101;
double const eps = 1e-6;
int n;
pair<int,int> v[N][4][4], h[N][4];

double D(int x, int y, int tx, int ty){
       return (double)(sqrt(pow(x-tx, 2ll)+pow(y-ty, 2ll)));
}

void pr(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     cout << a.first <<" "<<a.second <<"\n";
     cout << b.first <<" "<<b.second <<"\n";
     cout << c.first <<" "<<c.second <<"\n";
     cout << d.first <<" "<<d.second <<"\n";
}

bool Sq(pair<int,int> a, pair<int,int> b, pair<int,int> c, pair<int,int> d){
     if(a == b || a == c || a == d || b == c || b == d || c == d)return false;
     vector<pair<int,int> > P;
     P.push_back(a);
     P.push_back(b);
     P.push_back(c);
     P.push_back(d);
     bool f = 0;
     do{
        f = 0;
        double ds = D(P[0].first, P[0].second, P[1].first, P[1].second);
        for(int i = 1; i<4; ++i){
           double cd = D(P[i].first, P[i].second, P[(i+1)%4].first, P[(i+1)%4].second);
           if(fabs(ds - cd) > eps){f = 1; break;}
        }
        if(!f){return true;}
     }while(next_permutation(P.begin(), P.end()));
     return false;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d", &n);
    for(int i = 0, x, y, a, b; i<n; ++i){
      for(int k = 0; k<4; ++k){
        scanf("%d%d%d%d", &x, &y, &a, &b);
        v[i][k][0] = {x, y};
        h[i][k] = {a, b};
        for(int j = 1; j<4; ++j){
           int tx = -(y-b)+a, ty = (x-a)+b;
           x = tx, y = ty;
           v[i][k][j] = {x, y};
        }
     }
     int an = 1e9;
     for(int x0 = 0; x0<4; ++x0){
        for(int x1 = 0; x1<4; ++x1){
           for(int x2 = 0; x2<4; ++x2){
              for(int x3 = 0; x3<4; ++x3){
                  int ad = x0 + x1 + x2 + x3;
                  if(Sq(v[i][0][x0], v[i][1][x1], v[i][2][x2], v[i][3][x3]))an = min(an, ad);
              }
            }
         }
      }
      printf("%d\n", an == 1e9? -1: an);
   }

}


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

#define f first
#define s second
#define P pair<int,int>
#define ll long long
#define db double
#define pb push_back

const int inf=1e9;
const db eps=1e-8;

db len[5];

int flag,ans;

struct node
{
    db x,y,sx,sy;
}a[105],p[105];

double dis(double x1,double y1,double x2,double y2)
{
   return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

bool check()
{
    for(int i=0;i<4;i++)a[i]=p[i];
    sort(a,a+4,[](node a,node b){if(a.x==b.x)return a.y>b.y;return a.x>b.x;});
    swap(a[2],a[3]);//排序完注意点的顺序
    for(int i=0;i<4;i++)
    {
        len[i]=dis(a[i].x,a[i].y,a[(i+1)%4].x,a[(i+1)%4].y);
    }
    db v=(a[0].x-a[1].x)*(a[0].x-a[3].x)+(a[0].y-a[1].y)*(a[0].y-a[3].y);
    if(len[0]>0&&fabs(len[0]-len[1])<eps&&fabs(len[1]-len[2])<eps&&fabs(len[2]-len[3])<eps&&v==0)return 1;
    return 0;
}

void dfs(int cur,int res)
{
    if(cur==4)
    {
        if(check())
        {
            ans=min(ans,res);
            flag=1;
        }
        return;
    }
    node temp=p[cur],t=p[cur];
    for(int i=0;i<4;i++)
    {
        if(i==0)dfs(cur+1,res);
        else
        {
            db x=t.x,y=t.y,sx=t.sx,sy=t.sy;//注意对应参考点的选取，不要用p[cur],p[cur]在过程中值会改变
            t.x=-(y-sy)+sx;
            t.y=(x-sx)+sy;
            p[cur]=t;
            dfs(cur+1,res+i);
            p[cur]=temp;//退出递归后把p[cur]还原一下
        }
    }
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        flag=0;
        ans=inf;
        for(int i=0;i<4;i++)cin>>p[i].x>>p[i].y>>p[i].sx>>p[i].sy;
        dfs(0,0);
        if(flag)cout<<ans<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

#define f first
#define s second
#define P pair<int,int>
#define ll long long
#define db double
#define pb push_back

const int inf=1e9;
const db eps=1e-8;

db len[5];

int flag,ans;

struct node
{
    db x,y,sx,sy;
}a[105],p[105];

//bool cmp(node a,node b)//使正方形的顶点逆时针排序
//{
//    if(a.x==b.x)return a.y>b.y;
//    return a.x>b.x;
//}
//
//bool check()
//{
//    for(int i=0;i<4;i++)a[i]=p[i];
//    sort(a,a+4,cmp);
//    double s1,s2,s3,s4;//四条边长度的平方
//    double v;//相邻两条边向量的点乘
//    s1=(a[0].x-a[1].x)*(a[0].x-a[1].x)+(a[0].y-a[1].y)*(a[0].y-a[1].y);
//    s2=(a[1].x-a[3].x)*(a[1].x-a[3].x)+(a[1].y-a[3].y)*(a[1].y-a[3].y);
//    s3=(a[2].x-a[3].x)*(a[2].x-a[3].x)+(a[2].y-a[3].y)*(a[2].y-a[3].y);
//    s4=(a[0].x-a[2].x)*(a[0].x-a[2].x)+(a[0].y-a[2].y)*(a[0].y-a[2].y);
//    v=(a[0].x-a[1].x)*(a[0].x-a[2].x)+(a[0].y-a[1].y)*(a[0].y-a[2].y);
//    if(s1==s2&&s2==s3&&s3==s4&&v==0&&s1!=0)//四条边相等有一个直角则是正方形,防止四个点转到一个点的情况
//        return 1;
//    return 0;
//}

double dis(double x1,double y1,double x2,double y2)
{
   return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

bool check()
{
    for(int i=0;i<4;i++)a[i]=p[i];
    sort(a,a+4,[](node a,node b){if(a.x==b.x)return a.y>b.y;return a.x>b.x;});
    swap(a[2],a[3]);
    for(int i=0;i<4;i++)
    {
        len[i]=dis(a[i].x,a[i].y,a[(i+1)%4].x,a[(i+1)%4].y);
        //cout<<"len"<<len[i]<<endl;
    }
    db v=(a[0].x-a[1].x)*(a[0].x-a[3].x)+(a[0].y-a[1].y)*(a[0].y-a[3].y);
    if(len[0]>0&&fabs(len[0]-len[1])<eps&&fabs(len[1]-len[2])<eps&&fabs(len[2]-len[3])<eps&&v==0)return 1;
    return 0;
}

void dfs(int cur,int res)
{
    if(cur==4)
    {
        if(check())
        {
            //for(int i=0;i<4;i++)cout<<p[i].x<<" "<<p[i].y<<endl;
            ans=min(ans,res);
            flag=1;
        }
        return;
    }
    node temp=p[cur],t=p[cur];
    for(int i=0;i<4;i++)
    {
        if(i==0)dfs(cur+1,res);
        else
        {
            db x=t.x,y=t.y,sx=t.sx,sy=t.sy;
            t.x=-(y-sy)+sx;
            t.y=(x-sx)+sy;
            p[cur]=t;
            dfs(cur+1,res+i);
        }
        p[cur]=temp;
    }
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        flag=0;
        ans=inf;
        for(int i=0;i<4;i++)cin>>p[i].x>>p[i].y>>p[i].sx>>p[i].sy;
        dfs(0,0);
        if(flag)cout<<ans<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


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

const int inf=1e9;
const int maxn=1e6+5;

struct node
{
    double x,y,sx,sy;
}a[maxn],p[maxn];

int ans=inf;

bool cmp(node a,node b)//使正方形的顶点逆时针排序
{
    if(a.x==b.x)return a.y>b.y;
    return a.x>b.x;
}

bool check()
{
    for(int i=0;i<4;i++)a[i]=p[i];
    sort(a,a+4,cmp);
    double s1,s2,s3,s4;//四条边长度的平方
    double v;//相邻两条边向量的点乘
    s1=(a[0].x-a[1].x)*(a[0].x-a[1].x)+(a[0].y-a[1].y)*(a[0].y-a[1].y);
    s2=(a[1].x-a[3].x)*(a[1].x-a[3].x)+(a[1].y-a[3].y)*(a[1].y-a[3].y);
    s3=(a[2].x-a[3].x)*(a[2].x-a[3].x)+(a[2].y-a[3].y)*(a[2].y-a[3].y);
    s4=(a[0].x-a[2].x)*(a[0].x-a[2].x)+(a[0].y-a[2].y)*(a[0].y-a[2].y);
    v=(a[0].x-a[1].x)*(a[0].x-a[2].x)+(a[0].y-a[1].y)*(a[0].y-a[2].y);
    if(s1==s2&&s2==s3&&s3==s4&&v==0&&s1!=0)//四条边相等有一个直角则是正方形,防止四个点转到一个点的情况
        return 1;
    return 0;
}

void dfs(int cur,int res)//第几个点和旋转次数
{
    if(cur==4)
    {
        if(check())
        ans=min(ans,res);//如果符合则需更新一下旋转次数，取最小值
        return;
    }
    node tmp=p[cur],t=p[cur];//保存当前要旋转的点
    for(int i=0;i<4;i++)
    {
        double x=t.x,y=t.y,sx=t.sx,sy=t.sy;
        if(i>0)//第一次是该点本身，第二次开始旋转
        {
            t.x=-(y-sy)+sx;
            t.y=(x-sx)+sy;
            p[cur]=t;
        }
        dfs(cur+1,res+i);
        p[cur]=tmp;//如果退出递归则恢复原位
    }
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        for(int i=0;i<4;i++)cin>>p[i].x>>p[i].y>>p[i].sx>>p[i].sy;
        ans=inf;
        dfs(0,0);
        if(ans==inf)cout<<"-1"<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}


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

typedef long long             ll;
typedef unsigned long long    ull;
typedef vector<int>           vi;
typedef vector<ll>            vll;
typedef vector<vi>            vvi;
typedef pair<int,int>         pii;
typedef pair<ll,ll>           pll;
typedef complex<double>       point;

#define X                           real()
#define Y                           imag()
#define angle(p)                    atan2(p.Y,p.X)
#define vec(a,b)                    (b-a)
#define rotateO(p,theta)            (p*exp(point(0,theta)))
#define rotateA(p,theta,about)      rotateO(vec(about,p), theta) + about
#define reflectO(p,m)               conj(p/m)*m
#define reflectA(p,p0,pm)           conj(vec(p0,p)/vec(p0,pm))*vec(p0,pm) + p0
#define scf                         scanf
#define prf                         printf
#define F_IO                        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(c)                      c.begin(),c.end()
#define sz(c)                       ((int)((c).size()))
#define pb                          push_back
#define eb                          emplace_back
#define popb                        pop_back
#define mp                          make_pair
#define xf                          first
#define ys                          second
#define lp(i,s,e)                   for(__typeof(e) i=s ; i<e ;  ++i)
#define rlp(i,e,s)                  for(__typeof(e) i=e ; i>s ;  --i)
#define lp_(var , c)                for(const auto &var : c)
#define mems(c,val)                 memset(c,val,sizeof(c))
#define mod                         1000000007
#define OOi                         (~(1<<31))
#define OOl                         (~(1LL<<63))
#define PI                          (3.14159265358979)
#define EPS                         (1e-6)
#define F_open_r                    freopen("D:\\in.txt", "r", stdin)
#define F_open_w                    freopen("D:\\in.txt", "w", stdout)

int dcmp(double a, double b)                          {return((fabs(a-b)<EPS)?0:(a<b)?-1:+1);}
double crossP(const point &v1, const point &v2)       {return (conj(v1)*v2).Y;}
double dotP(const point &v1, const point &v2)         {return (conj(v1)*v2).X;}
double lengthSqr(const point &p)                      {return dotP(p, p);}
double length(const point &p)                         {return hypot(p.X,p.Y);}
double fixAngle(double a)                             {return ((a>1)?1:(a<-1)?-1:a);}
bool same(const point &p1, const point &p2)           {return((dcmp(p1.X, p2.X)==0 && dcmp(p1.Y, p2.Y)==0)?1:0);}
double getAngle_A_abc(double a, double b, double c)   {return acos(fixAngle((b*b+c*c-a*a)/(2*b*c)));}
//_______________________________________________________________________________________________________________________.

struct cmp{
	bool operator()(const double a, const double b){
		return (dcmp(a, b)==-1? 1:0);
	}

};
vector<point> v(4), O(4);

bool ok(){
	map<double, int, cmp> m;

	lp(i1,0,4)
		lp(i2,i1+1,4)
			m[length(v[i2]-v[i1])]++;

	if(sz(m)!=2 || m.begin()->ys!=4 || m.rbegin()->ys!=2) return 0;

	double s = m.begin()->xf, d = m.rbegin()->xf;
	double ss = 2*(s*s);
	double dd = d*d;

	return (dcmp(ss, dd)==0 && dcmp(s, 0)==1);

}

int f(int i){
	if(i==4)
		return (ok()?0:111);

	int ret = f(i+1);

	lp(t,0,3){
		v[i] = rotateA(v[i], PI/2, O[i]);
		ret = min(ret, t+1 + f(i+1));
	}

	v[i] = rotateA(v[i], PI/2, O[i]);

	return ret;
}

int main()
{
	int q;
	scf("%d", &q);

	while(q--){
		lp(i,0,4){
			double x, y, cx, cy;
			scf("%lf %lf %lf %lf", &x, &y, &cx, &cy);

			v[i] = point(x, y);
			O[i] = point(cx, cy);
		}

		int ans = f(0);

		if(ans>=111) ans = -1;

		prf("%d\n", ans);

	}

	return 0;
}


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

typedef long long             ll;
typedef unsigned long long    ull;
typedef vector<int>           vi;
typedef vector<ll>            vll;
typedef vector<vi>            vvi;
typedef pair<int,int>         pii;
typedef pair<ll,ll>           pll;
typedef complex<double>       point;

#define X                           real()
#define Y                           imag()
#define angle(p)                    atan2(p.Y,p.X)
#define vec(a,b)                    (b-a)
#define rotateO(p,theta)            (p*exp(point(0,theta)))
#define rotateA(p,theta,about)      rotateO(vec(about,p), theta) + about
#define reflectO(p,m)               conj(p/m)*m
#define reflectA(p,p0,pm)           conj(vec(p0,p)/vec(p0,pm))*vec(p0,pm) + p0
#define scf                         scanf
#define prf                         printf
#define F_IO                        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(c)                      c.begin(),c.end()
#define sz(c)                       ((int)((c).size()))
#define pb                          push_back
#define eb                          emplace_back
#define popb                        pop_back
#define mp                          make_pair
#define xf                          first
#define ys                          second
#define lp(i,s,e)                   for(__typeof(e) i=s ; i<e ;  ++i)
#define rlp(i,e,s)                  for(__typeof(e) i=e ; i>s ;  --i)
#define lp_(var , c)                for(const auto &var : c)
#define mems(c,val)                 memset(c,val,sizeof(c))
#define mod                         1000000007
#define OOi                         (~(1<<31))
#define OOl                         (~(1LL<<63))
#define PI                          (3.14159265358979)
#define EPS                         (1e-5)
#define F_open_r                    freopen("D:\\in.txt", "r", stdin)
#define F_open_w                    freopen("D:\\in.txt", "w", stdout)

int dcmp(double a, double b)                          {return((fabs(a-b)<EPS)?0:(a<b)?-1:+1);}
double crossP(const point &v1, const point &v2)       {return (conj(v1)*v2).Y;}
double dotP(const point &v1, const point &v2)         {return (conj(v1)*v2).X;}
double lengthSqr(const point &p)                      {return dotP(p, p);}
double length(const point &p)                         {return hypot(p.X,p.Y);}
double fixAngle(double a)                             {return ((a>1)?1:(a<-1)?-1:a);}
bool same(const point &p1, const point &p2)           {return((dcmp(p1.X, p2.X)==0 && dcmp(p1.Y, p2.Y)==0)?1:0);}
double getAngle_A_abc(double a, double b, double c)   {return acos(fixAngle((b*b+c*c-a*a)/(2*b*c)));}
//_______________________________________________________________________________________________________________________.

struct cmp{
	bool operator()(const double a, const double b){
		return (dcmp(a, b)==-1? 1:0);
	}

};
vector<point> v(4), O(4);

bool ok(){
	map<double, int, cmp> m;

	lp(i1,0,4)
		lp(i2,i1+1,4)
			m[length(v[i2]-v[i1])]++;

	if(sz(m)!=2 || m.begin()->ys!=4 || m.rbegin()->ys!=2) return 0;

	double s = m.begin()->xf, d = m.rbegin()->xf;
	double ss = 2*(s*s);
	double dd = d*d;

	return (dcmp(ss, dd)==0 && dcmp(s, 0)==1);

}

int f(int i){
	if(i==4)
		return (ok()?0:111);

	int ret = f(i+1);

	lp(t,0,3){
		v[i] = rotateA(v[i], PI/2, O[i]);
		ret = min(ret, t+1 + f(i+1));
	}

	v[i] = rotateA(v[i], PI/2, O[i]);

	return ret;
}

int main()
{
	int q;
	scf("%d", &q);

	while(q--){
		lp(i,0,4){
			double x, y, cx, cy;
			scf("%lf %lf %lf %lf", &x, &y, &cx, &cy);

			v[i] = point(x, y);
			O[i] = point(cx, cy);
		}

		int ans = f(0);

		if(ans>=111) ans = -1;

		prf("%d\n", ans);

	}

	return 0;
}


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

typedef long long             ll;
typedef unsigned long long    ull;
typedef vector<int>           vi;
typedef vector<ll>            vll;
typedef vector<vi>            vvi;
typedef pair<int,int>         pii;
typedef pair<ll,ll>           pll;
typedef complex<double>       point;

#define X                           real()
#define Y                           imag()
#define angle(p)                    atan2(p.Y,p.X)
#define vec(a,b)                    (b-a)
#define rotateO(p,theta)            (p*exp(point(0,theta)))
#define rotateA(p,theta,about)      rotateO(vec(about,p), theta) + about
#define reflectO(p,m)               conj(p/m)*m
#define reflectA(p,p0,pm)           conj(vec(p0,p)/vec(p0,pm))*vec(p0,pm) + p0
#define scf                         scanf
#define prf                         printf
#define F_IO                        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(c)                      c.begin(),c.end()
#define sz(c)                       ((int)((c).size()))
#define pb                          push_back
#define eb                          emplace_back
#define popb                        pop_back
#define mp                          make_pair
#define xf                          first
#define ys                          second
#define lp(i,s,e)                   for(__typeof(e) i=s ; i<e ;  ++i)
#define rlp(i,e,s)                  for(__typeof(e) i=e ; i>s ;  --i)
#define lp_(var , c)                for(const auto &var : c)
#define mems(c,val)                 memset(c,val,sizeof(c))
#define mod                         1000000007
#define OOi                         (~(1<<31))
#define OOl                         (~(1LL<<63))
#define PI                          (3.14159265358979)
#define EPS                         (1e-4)
#define F_open_r                    freopen("D:\\in.txt", "r", stdin)
#define F_open_w                    freopen("D:\\in.txt", "w", stdout)

int dcmp(double a, double b)                          {return((fabs(a-b)<EPS)?0:(a<b)?-1:+1);}
double crossP(const point &v1, const point &v2)       {return (conj(v1)*v2).Y;}
double dotP(const point &v1, const point &v2)         {return (conj(v1)*v2).X;}
double lengthSqr(const point &p)                      {return dotP(p, p);}
double length(const point &p)                         {return hypot(p.X,p.Y);}
double fixAngle(double a)                             {return ((a>1)?1:(a<-1)?-1:a);}
bool same(const point &p1, const point &p2)           {return((dcmp(p1.X, p2.X)==0 && dcmp(p1.Y, p2.Y)==0)?1:0);}
double getAngle_A_abc(double a, double b, double c)   {return acos(fixAngle((b*b+c*c-a*a)/(2*b*c)));}
//_______________________________________________________________________________________________________________________.

struct cmp{
	bool operator()(const double a, const double b){
		return (dcmp(a, b)==-1? 1:0);
	}

};
vector<point> v(4), O(4);

bool ok(){
	map<double, int, cmp> m;

	lp(i1,0,4)
		lp(i2,i1+1,4)
			m[length(v[i2]-v[i1])]++;

	if(sz(m)!=2 || m.begin()->ys!=4 || m.rbegin()->ys!=2) return 0;

	double s = m.begin()->xf;

	return (dcmp(s, 0)>0);

}

int f(int i){
	if(i==4)
		return (ok()?0:111);

	int ret = f(i+1);

	lp(t,0,3){
		v[i] = rotateA(v[i], PI/2, O[i]);
		ret = min(ret, t+1 + f(i+1));
	}

	v[i] = rotateA(v[i], PI/2, O[i]);

	return ret;
}

int main()
{
	int q;
	scf("%d", &q);

	while(q--){
		lp(i,0,4){
			double x, y, cx, cy;
			scf("%lf %lf %lf %lf", &x, &y, &cx, &cy);

			v[i] = point(x, y);
			O[i] = point(cx, cy);
		}

		int ans = f(0);

		if(ans>=111) ans = -1;

		prf("%d\n", ans);

	}

	return 0;
}


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

typedef long long             ll;
typedef unsigned long long    ull;
typedef vector<int>           vi;
typedef vector<ll>            vll;
typedef vector<vi>            vvi;
typedef pair<int,int>         pii;
typedef pair<ll,ll>           pll;
typedef complex<double>       point;

#define X                           real()
#define Y                           imag()
#define angle(p)                    atan2(p.Y,p.X)
#define vec(a,b)                    (b-a)
#define rotateO(p,theta)            (p*exp(point(0,theta)))
#define rotateA(p,theta,about)      rotateO(vec(about,p), theta) + about
#define reflectO(p,m)               conj(p/m)*m
#define reflectA(p,p0,pm)           conj(vec(p0,p)/vec(p0,pm))*vec(p0,pm) + p0
#define scf                         scanf
#define prf                         printf
#define F_IO                        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(c)                      c.begin(),c.end()
#define sz(c)                       ((int)((c).size()))
#define pb                          push_back
#define eb                          emplace_back
#define popb                        pop_back
#define mp                          make_pair
#define xf                          first
#define ys                          second
#define lp(i,s,e)                   for(__typeof(e) i=s ; i<e ;  ++i)
#define rlp(i,e,s)                  for(__typeof(e) i=e ; i>s ;  --i)
#define lp_(var , c)                for(const auto &var : c)
#define mems(c,val)                 memset(c,val,sizeof(c))
#define mod                         1000000007
#define OOi                         (~(1<<31))
#define OOl                         (~(1LL<<63))
#define PI                          (3.14159265358979)
#define EPS                         (1e-4)
#define F_open_r                    freopen("D:\\in.txt", "r", stdin)
#define F_open_w                    freopen("D:\\in.txt", "w", stdout)

int dcmp(double a, double b)                          {return((fabs(a-b)<EPS)?0:(a<b)?-1:+1);}
double crossP(const point &v1, const point &v2)       {return (conj(v1)*v2).Y;}
double dotP(const point &v1, const point &v2)         {return (conj(v1)*v2).X;}
double lengthSqr(const point &p)                      {return dotP(p, p);}
double length(const point &p)                         {return hypot(p.X,p.Y);}
double fixAngle(double a)                             {return ((a>1)?1:(a<-1)?-1:a);}
bool same(const point &p1, const point &p2)           {return((dcmp(p1.X, p2.X)==0 && dcmp(p1.Y, p2.Y)==0)?1:0);}
double getAngle_A_abc(double a, double b, double c)   {return acos(fixAngle((b*b+c*c-a*a)/(2*b*c)));}
//_______________________________________________________________________________________________________________________.

struct cmp{
	bool operator()(const double a, const double b){
		return (dcmp(a, b)==-1? 1:0);
	}

};
vector<point> v(4), O(4);

bool ok(){
	map<double, int, cmp> m;

	lp(i1,0,4)
		lp(i2,i1+1,4)
			m[length(v[i2]-v[i1])]++;

	if(sz(m)!=2 || m.begin()->ys!=4 || m.rbegin()->ys!=2) return 0;

	double s = m.begin()->xf, d = m.rbegin()->xf;
	double ss = 2*(s*s);
	double dd = d*d;

	return (dcmp(ss, dd)==0 && dcmp(s, 0)==1);

}

int f(int i){
	if(i==4)
		return (ok()?0:111);

	int ret = f(i+1);

	lp(t,0,3){
		v[i] = rotateA(v[i], PI/2, O[i]);
		ret = min(ret, t+1 + f(i+1));
	}

	v[i] = rotateA(v[i], PI/2, O[i]);

	return ret;
}

int main()
{
	int q;
	scf("%d", &q);

	while(q--){
		lp(i,0,4){
			double x, y, cx, cy;
			scf("%lf %lf %lf %lf", &x, &y, &cx, &cy);

			v[i] = point(x, y);
			O[i] = point(cx, cy);
		}

		int ans = f(0);

		if(ans>=111) ans = -1;

		prf("%d\n", ans);

	}

	return 0;
}


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

typedef long long             ll;
typedef unsigned long long    ull;
typedef vector<int>           vi;
typedef vector<ll>            vll;
typedef vector<vi>            vvi;
typedef pair<int,int>         pii;
typedef pair<ll,ll>           pll;
typedef complex<double>       point;

#define X                           real()
#define Y                           imag()
#define angle(p)                    atan2(p.Y,p.X)
#define vec(a,b)                    (b-a)
#define rotateO(p,theta)            (p*exp(point(0,theta)))
#define rotateA(p,theta,about)      rotateO(vec(about,p), theta) + about
#define reflectO(p,m)               conj(p/m)*m
#define reflectA(p,p0,pm)           conj(vec(p0,p)/vec(p0,pm))*vec(p0,pm) + p0
#define scf                         scanf
#define prf                         printf
#define F_IO                        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(c)                      c.begin(),c.end()
#define sz(c)                       ((int)((c).size()))
#define pb                          push_back
#define eb                          emplace_back
#define popb                        pop_back
#define mp                          make_pair
#define xf                          first
#define ys                          second
#define lp(i,s,e)                   for(__typeof(e) i=s ; i<e ;  ++i)
#define rlp(i,e,s)                  for(__typeof(e) i=e ; i>s ;  --i)
#define lp_(var , c)                for(const auto &var : c)
#define mems(c,val)                 memset(c,val,sizeof(c))
#define mod                         1000000007
#define OOi                         (~(1<<31))
#define OOl                         (~(1LL<<63))
#define PI                          (3.14159265358979)
#define EPS                         (1e-4)
#define F_open_r                    freopen("D:\\in.txt", "r", stdin)
#define F_open_w                    freopen("D:\\in.txt", "w", stdout)

int dcmp(double a, double b)                          {return((fabs(a-b)<EPS)?0:(a<b)?-1:+1);}
double crossP(const point &v1, const point &v2)       {return (conj(v1)*v2).Y;}
double dotP(const point &v1, const point &v2)         {return (conj(v1)*v2).X;}
double lengthSqr(const point &p)                      {return dotP(p, p);}
double length(const point &p)                         {return hypot(p.X,p.Y);}
double fixAngle(double a)                             {return ((a>1)?1:(a<-1)?-1:a);}
bool same(const point &p1, const point &p2)           {return((dcmp(p1.X, p2.X)==0 && dcmp(p1.Y, p2.Y)==0)?1:0);}
double getAngle_A_abc(double a, double b, double c)   {return acos(fixAngle((b*b+c*c-a*a)/(2*b*c)));}
//_______________________________________________________________________________________________________________________.
bool is_sqaure(const vector<point> &v){
	auto cmp = [](const double a, const double b)->bool{
		return (dcmp(a, b)==-1? 1:0);
	};

	map<double, int, decltype(cmp)> m(cmp);

	lp(i1,0,4)
		lp(i2,i1+1,4)
			m[lengthSqr(v[i2]-v[i1])]++;

	return sz(m)==2 && m.begin()->ys==4 && m.rbegin()->ys==2 && dcmp(m.begin()->xf, 0)>0;
}

int main(){
	vector<point> v(4), O(4);
	int q;

	scf("%d", &q);

	while(q--){
		lp(i,0,4){
			double x, y, cx, cy;
			scf("%lf %lf %lf %lf", &x, &y, &cx, &cy);

			v[i] = point(x, y);
			O[i] = point(cx, cy);
		}

		int ans = 13;

		lp(r1,0,4)
			lp(r2,0,4)
				lp(r3,0,4)
					lp(r4,0,4){
						vi temp = {r1, r2, r3, r4};

						lp(i,0,4) v[i] = rotateA(v[i], temp[i]*PI/2, O[i]);

						if(is_sqaure(v)) ans = min(ans, accumulate(all(temp), 0));

						lp(i,0,4) v[i] = rotateA(v[i], -temp[i]*PI/2, O[i]);
					}

		if(ans==13) ans = -1;

		prf("%d\n", ans);

	}

	return 0;
}


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

typedef long long             ll;
typedef unsigned long long    ull;
typedef vector<int>           vi;
typedef vector<ll>            vll;
typedef vector<vi>            vvi;
typedef pair<int,int>         pii;
typedef pair<ll,ll>           pll;
typedef complex<double>       point;

#define X                           real()
#define Y                           imag()
#define angle(p)                    atan2(p.Y,p.X)
#define vec(a,b)                    (b-a)
#define rotateO(p,theta)            (p*exp(point(0,theta)))
#define rotateA(p,theta,about)      rotateO(vec(about,p), theta) + about
#define reflectO(p,m)               conj(p/m)*m
#define reflectA(p,p0,pm)           conj(vec(p0,p)/vec(p0,pm))*vec(p0,pm) + p0
#define scf                         scanf
#define prf                         printf
#define F_IO                        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(c)                      c.begin(),c.end()
#define sz(c)                       ((int)((c).size()))
#define pb                          push_back
#define eb                          emplace_back
#define popb                        pop_back
#define mp                          make_pair
#define xf                          first
#define ys                          second
#define lp(i,s,e)                   for(__typeof(e) i=s ; i<e ;  ++i)
#define rlp(i,e,s)                  for(__typeof(e) i=e ; i>s ;  --i)
#define lp_(var , c)                for(const auto &var : c)
#define mems(c,val)                 memset(c,val,sizeof(c))
#define mod                         1000000007
#define OOi                         (~(1<<31))
#define OOl                         (~(1LL<<63))
#define PI                          (3.14159265358979)
#define EPS                         (1e-9)
#define F_open_r                    freopen("D:\\in.txt", "r", stdin)
#define F_open_w                    freopen("D:\\in.txt", "w", stdout)

int dcmp(double a, double b)                          {return((fabs(a-b)<EPS)?0:(a<b)?-1:+1);}
double crossP(const point &v1, const point &v2)       {return (conj(v1)*v2).Y;}
double dotP(const point &v1, const point &v2)         {return (conj(v1)*v2).X;}
double lengthSqr(const point &p)                      {return dotP(p, p);}
double length(const point &p)                         {return hypot(p.X,p.Y);}
double fixAngle(double a)                             {return ((a>1)?1:(a<-1)?-1:a);}
bool same(const point &p1, const point &p2)           {return((dcmp(p1.X, p2.X)==0 && dcmp(p1.Y, p2.Y)==0)?1:0);}
double getAngle_A_abc(double a, double b, double c)   {return acos(fixAngle((b*b+c*c-a*a)/(2*b*c)));}
//_______________________________________________________________________________________________________________________.
vector<point> v(4), O(4);

bool ok(){
	vi per{0,1,2,3};
	do{
		int i1 = per[0], i2 = per[1], i3 = per[2], i4 = per[3];

		point v12 = vec(v[i1], v[i2]);
		point v13 = vec(v[i1], v[i3]);
		point v42 = vec(v[i4], v[i2]);
		point v43 = vec(v[i4], v[i3]);

		if(dcmp(dotP(v12, v13), EPS)>0) continue;
		if(dcmp(dotP(v42, v43), EPS)>0) continue;
		if(dcmp(dotP(v12, v42), EPS)>0) continue;
		if(dcmp(dotP(v13, v43), EPS)>0) continue;

		double l12 = lengthSqr(v12), l13 = lengthSqr(v13), l24 = lengthSqr(v42), l34 =  lengthSqr(v43);
		double l14 = lengthSqr(vec(v[i1], v[i4])), l23 = lengthSqr(vec(v[i2], v[i3]));

		if(dcmp(l12, l13)==0 &&  dcmp(l13, l34)==0 && dcmp(l13, l24)==0 && dcmp(l14, l23)==0 && dcmp(l12, 0)==1 && dcmp(l14, 0)==1)
			return 1;

	}while(next_permutation(all(per)));

	return 0;
}

int f(int i){
	if(i==4)
		return (ok()?0:111);

	int ret = f(i+1);

	lp(t,0,3){
		v[i] = rotateA(v[i], PI/2, O[i]);
		ret = min(ret, t+1 + f(i+1));
	}

	v[i] = rotateA(v[i], PI/2, O[i]);

	return ret;
}

int main()
{

	int q;
	scf("%d", &q);

	while(q--){
		lp(i,0,4){
			double x, y, cx, cy;
			scf("%lf %lf %lf %lf", &x, &y, &cx, &cy);

			v[i] = point(x, y);
			O[i] = point(cx, cy);
		}

		int ans = f(0);

		if(ans>=111) ans = -1;

		prf("%d\n", ans);

	}

	return 0;
}


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

typedef long long             ll;
typedef unsigned long long    ull;
typedef vector<int>           vi;
typedef vector<ll>            vll;
typedef vector<vi>            vvi;
typedef pair<int,int>         pii;
typedef pair<ll,ll>           pll;
typedef complex<double>       point;

#define X                           real()
#define Y                           imag()
#define angle(p)                    atan2(p.Y,p.X)
#define vec(a,b)                    (b-a)
#define rotateO(p,theta)            (p*exp(point(0,theta)))
#define rotateA(p,theta,about)      rotateO(vec(about,p), theta) + about
#define reflectO(p,m)               conj(p/m)*m
#define reflectA(p,p0,pm)           conj(vec(p0,p)/vec(p0,pm))*vec(p0,pm) + p0
#define scf                         scanf
#define prf                         printf
#define F_IO                        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(c)                      c.begin(),c.end()
#define sz(c)                       ((int)((c).size()))
#define pb                          push_back
#define eb                          emplace_back
#define popb                        pop_back
#define mp                          make_pair
#define xf                          first
#define ys                          second
#define lp(i,s,e)                   for(__typeof(e) i=s ; i<e ;  ++i)
#define rlp(i,e,s)                  for(__typeof(e) i=e ; i>s ;  --i)
#define lp_(var , c)                for(const auto &var : c)
#define mems(c,val)                 memset(c,val,sizeof(c))
#define mod                         1000000007
#define OOi                         (~(1<<31))
#define OOl                         (~(1LL<<63))
#define PI                          (3.14159265358979)
#define EPS                         (1e-4)
#define F_open_r                    freopen("D:\\in.txt", "r", stdin)
#define F_open_w                    freopen("D:\\in.txt", "w", stdout)

int dcmp(double a, double b)                          {return((fabs(a-b)<EPS)?0:(a<b)?-1:+1);}
double crossP(const point &v1, const point &v2)       {return (conj(v1)*v2).Y;}
double dotP(const point &v1, const point &v2)         {return (conj(v1)*v2).X;}
double lengthSqr(const point &p)                      {return dotP(p, p);}
double length(const point &p)                         {return hypot(p.X,p.Y);}
double fixAngle(double a)                             {return ((a>1)?1:(a<-1)?-1:a);}
bool same(const point &p1, const point &p2)           {return((dcmp(p1.X, p2.X)==0 && dcmp(p1.Y, p2.Y)==0)?1:0);}
double getAngle_A_abc(double a, double b, double c)   {return acos(fixAngle((b*b+c*c-a*a)/(2*b*c)));}
//_______________________________________________________________________________________________________________________.

struct cmp{
	bool operator()(const double a, const double b){
		return (dcmp(a, b)==-1? 1:0);
	}

};
vector<point> v(4), O(4);

bool ok(){
	map<double, int, cmp> m;

	lp(i1,0,4)
		lp(i2,i1+1,4)
			m[length(v[i2]-v[i1])]++;

	if(sz(m)!=2 || m.begin()->ys!=4 || m.rbegin()->ys!=2) return 0;

	double s = m.begin()->xf, d = m.rbegin()->xf;

	return (dcmp(s, 0)>0 && dcmp(d, 0)>0);

}

int f(int i){
	if(i==4)
		return (ok()?0:111);

	int ret = f(i+1);

	lp(t,0,3){
		v[i] = rotateA(v[i], PI/2, O[i]);
		ret = min(ret, t+1 + f(i+1));
	}

	v[i] = rotateA(v[i], PI/2, O[i]);

	return ret;
}

int main()
{
	int q;
	scf("%d", &q);

	while(q--){
		lp(i,0,4){
			double x, y, cx, cy;
			scf("%lf %lf %lf %lf", &x, &y, &cx, &cy);

			v[i] = point(x, y);
			O[i] = point(cx, cy);
		}

		int ans = f(0);

		if(ans>=111) ans = -1;

		prf("%d\n", ans);

	}

	return 0;
}


#include "bits/stdc++.h"
using namespace std;
std::vector<int> a(4),b(4);
std::vector<pair<int,int> > points(4),sortedpoints(4);
long long int finddis(int i,int j)
{
	return ((sortedpoints[i].first-sortedpoints[j].first)*(sortedpoints[i].first-sortedpoints[j].first))+((sortedpoints[i].second-sortedpoints[j].second)*(sortedpoints[i].second-sortedpoints[j].second));
}
bool check()
{
	set<pair<int,int> >po;
	for(int i=0;i<4;i++)
		po.insert(points[i]);
	if(po.size()<4)
		return 0;
	sortedpoints=points;
	sort(sortedpoints.begin(),sortedpoints.end());
	long long int dis=finddis(0,1);
	// cout<<dis<<endl;
	if(dis==0)	return 0;
	if(dis!=finddis(1,3))	return 0;
	if(dis!=finddis(2,3))	return 0;
	if(dis!=finddis(0,2))	return 0;
	return 1;
}
void per(int i)
{
	int xold=points[i].first-a[i];
	int yold=points[i].second-b[i];
	swap(xold,yold);
	points[i].first=a[i]-xold;
	points[i].second=yold+b[i];
}
void print()
{
	for(int i=0;i<4;i++)
	{
		cout<<points[i].first<<" "<<points[i].second<<"\t";
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int n;
	cin>>n;
	// if(check())
	// {
	// 	cout<<"Square\n";
	// 	return 0;
	// }
	// int tx=1,ty=1;
	// for(int i=0;i<5;i++)
	// {
	// 	cout<<tx<<" "<<ty<<endl;
	// 	swap(tx,ty);
	// 	tx=-tx;
	// }
	while(n--)
	{
		int steps=1<<29;//3*4
		//x y a b
		for(int i=0;i<4;i++)
		{
			cin>>points[i].first>>points[i].second>>a[i]>>b[i];
		}
		for(int f=0;f<4;f++)
		{
			for(int s=0;s<4;s++)
			{
				for(int t=0;t<4;t++)
				{
					for(int m=0;m<4;m++)
					{
						if(check())
						{
							// if(steps>=f+s+t+m)
							// 	cout<<f<<" "<<s<<" "<<t<<" "<<m<<" "<<f+s+t+m<<endl;
							steps=min(steps,f+s+t+m);
							// print();
							// cout<<steps<<" IN\n";
						}
						per(3);
					}
					per(2);
				}
				per(1);
			}
			per(0);
		}
		if(steps==1<<29)
			cout<<-1<<endl;
		else
			cout<<steps<<endl;
	}

}


#include "bits/stdc++.h"
using namespace std;
std::vector<int> a(4),b(4);
std::vector<pair<int,int> > points(4),sortedpoints(4);
long long int finddis(int i,int j)
{
	return ((sortedpoints[i].first-sortedpoints[j].first)*(sortedpoints[i].first-sortedpoints[j].first))+((sortedpoints[i].second-sortedpoints[j].second)*(sortedpoints[i].second-sortedpoints[j].second));
}
bool check()
{
	set<pair<int,int> >po;
	for(int i=0;i<4;i++)
		po.insert(points[i]);
	if(po.size()<4)
		return 0;
	sortedpoints=points;
	sort(sortedpoints.begin(),sortedpoints.end());
	long long int dis=finddis(0,1);
	// cout<<dis<<endl;
	if(dis==0)	return 0;
	if(dis!=finddis(1,3))	return 0;
	if(dis!=finddis(2,3))	return 0;
	if(dis!=finddis(0,2))	return 0;
	if(2*dis!=finddis(1,2))	return 0;
	return 1;
}
void per(int i)
{
	int xold=points[i].first-a[i];
	int yold=points[i].second-b[i];
	swap(xold,yold);
	points[i].first=a[i]-xold;
	points[i].second=yold+b[i];
}
void print()
{
	for(int i=0;i<4;i++)
	{
		cout<<points[i].first<<" "<<points[i].second<<"\t";
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int n;
	cin>>n;
	// if(check())
	// {
	// 	cout<<"Square\n";
	// 	return 0;
	// }
	// int tx=1,ty=1;
	// for(int i=0;i<5;i++)
	// {
	// 	cout<<tx<<" "<<ty<<endl;
	// 	swap(tx,ty);
	// 	tx=-tx;
	// }
	while(n--)
	{
		int steps=1<<29;//3*4
		//x y a b
		for(int i=0;i<4;i++)
		{
			cin>>points[i].first>>points[i].second>>a[i]>>b[i];
		}
		for(int f=0;f<4;f++)
		{
			for(int s=0;s<4;s++)
			{
				for(int t=0;t<4;t++)
				{
					for(int m=0;m<4;m++)
					{
						if(check())
						{
							// if(steps>=f+s+t+m)
							// 	cout<<f<<" "<<s<<" "<<t<<" "<<m<<" "<<f+s+t+m<<endl;
							steps=min(steps,f+s+t+m);
							// print();
							// cout<<steps<<" IN\n";
						}
						per(3);
					}
					per(2);
				}
				per(1);
			}
			per(0);
		}
		if(steps==1<<29)
			cout<<-1<<endl;
		else
			cout<<steps<<endl;
	}

}


#include "bits/stdc++.h"
using namespace std;
std::vector<int> a(4),b(4);
std::vector<pair<long long int,long long int> > points(4),sortedpoints(4);
long long int finddis(int i,int j)
{
	return ((sortedpoints[i].first-sortedpoints[j].first)*(sortedpoints[i].first-sortedpoints[j].first))+((sortedpoints[i].second-sortedpoints[j].second)*(sortedpoints[i].second-sortedpoints[j].second));
}
bool check()
{
	set<pair<int,int> >po;
	for(int i=0;i<4;i++)
		po.insert(points[i]);
	if(po.size()<4)
		return 0;
	sortedpoints=points;
	sort(sortedpoints.begin(),sortedpoints.end());
	long long int dis=finddis(0,1);
	if(dis==0)	return 0;
	if(dis!=finddis(1,3))	return 0;
	if(dis!=finddis(2,3))	return 0;
	if(dis!=finddis(0,2))	return 0;
	if(2*dis!=finddis(1,2))	return 0;
	// cout<<dis<<endl;
	return 1;
}
void per(int i)
{
	int xold=points[i].first-a[i];
	int yold=points[i].second-b[i];
	swap(xold,yold);
	points[i].first=a[i]-xold;
	points[i].second=yold+b[i];
}
void print()
{
	for(int i=0;i<4;i++)
	{
		cout<<points[i].first<<" "<<points[i].second<<endl;
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int n;
	cin>>n;
	// if(check())
	// {
	// 	cout<<"Square\n";
	// 	return 0;
	// }
	// int tx=1,ty=1;
	// for(int i=0;i<5;i++)
	// {
	// 	cout<<tx<<" "<<ty<<endl;
	// 	swap(tx,ty);
	// 	tx=-tx;
	// }
	while(n--)
	{
		int steps=1<<29;//3*4
		//x y a b
		for(int i=0;i<4;i++)
		{
			cin>>points[i].first>>points[i].second>>a[i]>>b[i];
		}
		for(int f=0;f<4;f++)
		{
			for(int s=0;s<4;s++)
			{
				for(int t=0;t<4;t++)
				{
					for(int m=0;m<4;m++)
					{
						if(check())
						{
							// if(steps>=f+s+t+m)
							// {
							// 	cout<<f<<" "<<s<<" "<<t<<" "<<m<<" "<<f+s+t+m<<endl;
							// 	print();
							// 	cout<<endl;
							// }
							steps=min(steps,f+s+t+m);
							// cout<<steps<<" IN\n";
						}
						per(3);
					}
					per(2);
				}
				per(1);
			}
			per(0);
		}
		if(steps==1<<29)
			cout<<-1<<endl;
		else
			cout<<steps<<endl;
	}

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll int x[4],y[4],a[4],b[4];
vector<vector<pair<ll int,ll int> > > v(4,vector<pair<ll int,ll int> >(4));

ll int mysqr(ll int x)
{
	return x*x;
}
ll int dist(pair<ll int,ll int> p1,pair<ll int,ll int> p2)
{
	return mysqr(p1.first-p2.first) +mysqr(p1.second-p2.second);
}
bool issqr(pair<ll int,ll int> p1,pair<ll int,ll int> p2,pair<ll int,ll int> p3,pair<ll int,ll int> p4)
{
	if(p1==p2 | p1==p3  | p1==p4 | p2==p3 | p2==p4 |p3==p4)
	return 0;
	ll int d[3];
	d[0]=dist(p1,p2);
	d[1]=dist(p1,p3);
	d[2]=dist(p1,p4);
	bool flag=0;
	if(d[0]==d[1] && d[2]==2*d[0] && d[0]!=0)
	{
		if(dist(p4,p2)==dist(p4,p3) && dist(p4,p3)==d[0])
		flag=1;
	}
	else if(d[0]==d[2] && d[1]==2*d[0] & d[0]!=0)
	{
		if(dist(p3,p2)==dist(p3,p4) && dist(p4,p3)==d[0])
		flag=1;
	}
	else if(d[1]==d[2] && d[0]==2*d[1] && d[1]!=0)
	{
		if(dist(p2,p4)==dist(p2,p3) && dist(p2,p4)==d[1])
		flag=1;
	}
	return flag;
}
void print(pair<ll int,ll int> p)
{
	cout<<p.first<<" "<<p.second<<" ";
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll int n;
	cin>>n;
	ll int i,j,jj,k,l,dx,dy,m,ans;
	bool flag;
	for(i=0;i<n;i++)
	{
		for(j=0;j<4;j++)
		{
			//~ v[j].celear();
			//~ cin>>x[j]>>y[j]>>a[j]>>b[j];
			//~ dx=(x[j]-a[j]);
			//~ dy=(y[j]-b[j]);
			//~ v[j].push_back(make_pair(a[j]+dx,b[j]+dy));
			//~ v[j].push_back(make_pair(a[j]-dx,b[j]+dy));
			//~ v[j].push_back(make_pair(a[j]-dx,b[j]-dy));
			//~ v[j].push_back(make_pair(a[j]+dx,b[j]-dy));
			//~ x[i][j]=a[i]-y[i][j-1]+b[i];
				//~ y[i][j]=b[i]+x[i][j-1]-a[i];
			//~ v[j].push_back(make_pair(a[j],b[j]));
			//~ v[j].push_back(make_pair(a[j],b[j]+y[j]));
			//~ v[j].push_back(make_pair(a[j]-x[j],b[j]-y[j]));
			//~ v[j].push_back(make_pair(a[j]+x[j],b[j]-y[j]));
			scanf("%lld%lld%lld%lld",&v[j][0].first,&v[j][0].second,&a[j],&b[j]);
			for(int jj=1;jj<=3;++jj)
			{
				v[j][jj].first=a[j]-v[j][jj-1].second+b[j];
				v[j][jj].second=b[j]+v[j][jj-1].first-a[j];
			}
		}
		flag=0;
		ans=INT_MAX;
		for(jj=0;jj<4;jj++)
		{
			for(k=0;k<4;k++)
			{
				for(l=0;l<4;l++)
				{
					for(m=0;m<4;m++)
					{
						if(issqr(v[0][jj],v[1][k],v[2][l],v[3][m]))
						{
							flag=1;
							ans=min(ans,(jj+k+l+m));
							//~ cout<<ans<<"lol "<<endl;
							//~ print(v[0][jj]);
							//~ print(v[1][k]);
							//~ print(v[2][l]);
							//~ print(v[3][m]);
							//~ cout<<endl;
							//~ cout<<(jj+k+l+m)<<endl;
						}
						//~ else
						//~ cout<<"No "<<endl;
					}
				}
			}
		}
		//~ print(v[0][2]);print(v[1][0]);print(v[2][1]);print(v[3][0]);cout<<endl;
		if(flag)
		{
			cout<<ans<<endl;
		}
		else
		cout<<-1<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll int x[4],y[4],a[4],b[4];
vector<vector<pair<ll int,ll int> > > v(4,vector<pair<ll int,ll int> >(4));

ll int mysqr(ll int x)
{
	return x*x;
}
ll int dist(pair<ll int,ll int> p1,pair<ll int,ll int> p2)
{
	return mysqr(p1.first-p2.first) +mysqr(p1.second-p2.second);
}
bool issqr(pair<ll int,ll int> p1,pair<ll int,ll int> p2,pair<ll int,ll int> p3,pair<ll int,ll int> p4)
{
	if(p1==p2 | p1==p3  | p1==p4 | p2==p3 | p2==p4 |p3==p4)
	return 0;
	ll int d[3];
	d[0]=dist(p1,p2);
	d[1]=dist(p1,p3);
	d[2]=dist(p1,p4);
	bool flag=0;
	if(d[0]==d[1] && d[2]==2*d[0] && d[0]!=0)
	{
		if(dist(p4,p2)==dist(p4,p3) && dist(p4,p3)==d[0])
		flag=1;
	}
	else if(d[0]==d[2] && d[1]==2*d[0] & d[0]!=0)
	{
		if(dist(p3,p2)==dist(p3,p4) && dist(p4,p3)==d[0])
		flag=1;
	}
	else if(d[1]==d[2] && d[0]==2*d[1] && d[1]!=0)
	{
		if(dist(p2,p4)==dist(p2,p3) && dist(p2,p4)==d[1])
		flag=1;
	}
	return flag;
}
void print(pair<ll int,ll int> p)
{
	cout<<p.first<<" "<<p.second<<" ";
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll int n;
	cin>>n;
	ll int i,j,jj,k,l,dx,dy,m,ans;
	bool flag;
	for(i=0;i<n;i++)
	{
		for(j=0;j<4;j++)
		{
			//~ v[j].celear();
			//~ cin>>x[j]>>y[j]>>a[j]>>b[j];
			//~ dx=(x[j]-a[j]);
			//~ dy=(y[j]-b[j]);
			//~ v[j].push_back(make_pair(a[j]+dx,b[j]+dy));
			//~ v[j].push_back(make_pair(a[j]-dx,b[j]+dy));
			//~ v[j].push_back(make_pair(a[j]-dx,b[j]-dy));
			//~ v[j].push_back(make_pair(a[j]+dx,b[j]-dy));
			//~ x[i][j]=a[i]-y[i][j-1]+b[i];
				//~ y[i][j]=b[i]+x[i][j-1]-a[i];
			//~ v[j].push_back(make_pair(a[j],b[j]));
			//~ v[j].push_back(make_pair(a[j],b[j]+y[j]));
			//~ v[j].push_back(make_pair(a[j]-x[j],b[j]-y[j]));
			//~ v[j].push_back(make_pair(a[j]+x[j],b[j]-y[j]));
			scanf("%l64d%l64d%l64d%l64d",&v[j][0].first,&v[j][0].second,&a[j],&b[j]);
			for(int jj=1;jj<=3;++jj)
			{
				v[j][jj].first=a[j]-v[j][jj-1].second+b[j];
				v[j][jj].second=b[j]+v[j][jj-1].first-a[j];
			}
		}
		flag=0;
		ans=INT_MAX;
		for(jj=0;jj<4;jj++)
		{
			for(k=0;k<4;k++)
			{
				for(l=0;l<4;l++)
				{
					for(m=0;m<4;m++)
					{
						if(issqr(v[0][jj],v[1][k],v[2][l],v[3][m]))
						{
							flag=1;
							ans=min(ans,(jj+k+l+m));
							//~ cout<<ans<<"lol "<<endl;
							//~ print(v[0][jj]);
							//~ print(v[1][k]);
							//~ print(v[2][l]);
							//~ print(v[3][m]);
							//~ cout<<endl;
							//~ cout<<(jj+k+l+m)<<endl;
						}
						//~ else
						//~ cout<<"No "<<endl;
					}
				}
			}
		}
		//~ print(v[0][2]);print(v[1][0]);print(v[2][1]);print(v[3][0]);cout<<endl;
		if(flag)
		{
			cout<<ans<<endl;
		}
		else
		cout<<-1<<endl;
	}
	return 0;
}


/****************************/
/** Author : thantrongtri3 **/
/****************************/
#include <bits/stdc++.h>

using namespace std;

#define fuv(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

typedef pair<int, int> pii;

const int N = 7e3+10;

long long a[N];
map<long long, long long> m;

long long get_bit(long long x, int pos)
{
    return (x >> pos) & 1;
}

long long set_bit(long long x, int pos){
    return (x | (1LL << pos));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif // ONLINE_JUDGE

    int n;
    cin >> n;
    set <long long> t;
    multiset <long long> k;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        t.insert(a[i]);
        k.insert(a[i]);
    }
    for(int i = 0; i < n; i++){
        long long x;
        cin >> x;
        m[a[i]] += x;
    }

    long long res = 0;
    vector<long long> b(t.begin(), t.end());
    fuv(it, b){
        if (k.count(*it) < 2){
            continue;
        }
        long long cnt = 0;
        long long mx = *it;
        fuv(it1, b){
            int ok = 1;
            for(int i = 0; i < 61; i++){
                if (get_bit(mx, i) == 0 && get_bit(*it1, i) == 1 && k.count(*it1) < 2){
                    ok = 0;
                    break;
                }
                if (get_bit(*it1, i) == 1){
                    mx = set_bit(mx, i);
                }
            }
            cnt += m[*it1]*ok;
        }
        res = max(res, cnt);
    }

    cout << res;
    return 0;
}


/****************************/
/** Author : thantrongtri3 **/
/****************************/
#include <bits/stdc++.h>

using namespace std;

#define fuv(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

typedef pair<int, int> pii;

const int N = 7e3+10;

long long a[N];
map<long long, long long> m;

int get_bit(long long x, int pos)
{
    return (x >> pos) & 1;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif // ONLINE_JUDGE

    int n;
    cin >> n;
    set <long long> t;
    multiset <long long> k;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        t.insert(a[i]);
        k.insert(a[i]);
    }
    for(int i = 0; i < n; i++){
        long long x;
        cin >> x;
        m[a[i]] += x;
    }

    long long res = 0;
    vector<long long> b(t.begin(), t.end());
    fuv(it, b){
        if (k.count(*it) < 2){
            continue;
        }
        long long cnt = 0;
        fuv(it1, b){
            int ok = 1;
            for(int i = 0; i <= 70; i++){
                if (get_bit(*it, i) == 0 && get_bit(*it1, i) == 1){
                    ok = 0;
                    break;
                }
            }
            cnt += m[*it1]*ok;
        }
        res = max(res, cnt);
    }

    cout << res;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<vector<ll>> g;
vector<char> used;
vector<ll> a;
vector<ll> cost;
map<ll, pair<ll, ll>> num;

ll answer = 0;

void dfs(int v)
{
  answer += cost[v];
  used[v] = true;

  for (auto &i : g[v])
  {
    if (!used[i])
    {
      dfs(i);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  used.assign(n, false);
  a.assign(n, 0);
  cost.assign(n, 0);

  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
    ++num[a[i]].first;
    num[a[i]].second = i;
  }

  for (auto &i : cost)
  {
    cin >> i;
  }

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto temp1 = a[i];
      auto temp2 = a[j];

      bool ok = true;

      if (temp1 - (temp1 ^ temp2) == temp2)
      {
        g[i].push_back(j);
      }

    }
  }

  for (auto &i : num)
  {
    if (i.second.first <= 1) continue;
    if (!used[i.second.second]) dfs(i.second.second);
  }

  cout << answer;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<vector<int>> g;
vector<char> used;
vector<ll> a;
vector<ll> cost;
map<int, pair<int, int>> num;

ll answer = 0;

void dfs(int v)
{
  answer += cost[v];
  used[v] = true;

  for (auto &i : g[v])
  {
    if (!used[i])
    {
      dfs(i);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  used.assign(n, false);
  a.assign(n, 0);
  cost.assign(n, 0);

  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
    ++num[a[i]].first;
    num[a[i]].second = i;
  }

  for (auto &i : cost)
  {
    cin >> i;
  }

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto temp1 = a[i];
      auto temp2 = a[j];

      bool ok = true;

      if (temp1 - (temp1 ^ temp2) == temp2)
      {
        g[i].push_back(j);
      }

    }
  }

  for (auto &i : num)
  {
    if (i.second.first <= 1) continue;
    if (!used[i.second.second]) dfs(i.second.second);
  }

  cout << answer;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

int n, m;
vector<vector<int>> g;
vector<pair<ll, ll>> v;
vector<pair<ll, ll>> used;
ll sum = 0;

void dfs(ll vert, ll ans, ll stage)
{
  used[vert].first = stage;
  //sum = max(sum, ans);

  bool added = false;
  for (auto &i : g[vert])
  {
    if (used[i].first == stage)
    {
      ans += v[vert].second;
      added = true;
      sum = max(sum, ans);
    }
  }

  if (!added) ans += v[vert].second;
  for (auto &i : g[vert])
  {
    if (used[i].first != stage)
    {
      dfs(i, ans, stage);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  v.resize(n);

  for (auto &i : v) cin >> i.first;
  for (auto &i : v) cin >> i.second;

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto t1 = v[i].first;
      auto t2 = v[j].first;
      bool ok = true;
      while (t1 || t2)
      {
        if ((t1 % 2) && (t2 % 2 == 0)) ok = false;
        t1 /= 2;
        t2 /= 2;
      }
      if (ok)
      {
        g[i].push_back(j);
      }
    }
  }

  used.assign(n, { -1, 0 });
  for (int i = 0; i < n; ++i)
  {
    dfs(i, 0, i);

  }
  cout << sum;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<vector<ll>> g;
vector<pair<ll, ll>> v;
vector<pair<ll, ll>> used;
ll sum = 0;
bool curCicle = false;

void dfs(ll vert, ll stage)
{
  used[vert].first = stage;

  for (auto &i : g[vert])
  {
    if (i == stage)
    {
      curCicle = true;
    }
  }

  for (auto &i : g[vert])
  {
    if (used[i].first != stage)
    {
      dfs(i, stage);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  v.resize(n);

  for (auto &i : v) cin >> i.first;
  for (auto &i : v) cin >> i.second;

  for (ll i = 0; i < n; ++i)
  {
    for (ll j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto t1 = v[i].first;
      auto t2 = v[j].first;
      bool ok = true;
      while (t1 || t2)
      {
        if ((t1 % 2 == 1) && (t2 % 2 == 0)) ok = false;
        t1 /= 2;
        t2 /= 2;
      }
      if (ok)
      {
        g[j].push_back(i);
      }
    }
  }

  used.assign(n, { -1, 0 });
  for (ll i = 0; i < n; ++i)
  {
    ll ans = 0;
    dfs(i, i);

    if (!curCicle) continue;
    curCicle = false;

    for (ll j = 0; j < n; ++j)
    {
      if (used[j].first == i)
      {
        ans += v[j].second;
      }
    }
    sum = max(ans, sum);
  }
  cout << sum;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<vector<int>> g;
vector<char> used;
vector<ll> a;
vector<ll> cost;
map<ll, pair<int, int>> num;

ll answer = 0;

void dfs(int v)
{
  answer += cost[v];
  used[v] = true;

  for (auto &i : g[v])
  {
    if (!used[i])
    {
      dfs(i);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  used.assign(n, false);
  a.assign(n, 0);
  cost.assign(n, 0);

  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
    ++num[a[i]].first;
    num[a[i]].second = i;
  }

  for (auto &i : cost)
  {
    cin >> i;
  }

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto temp1 = a[i];
      auto temp2 = a[j];

      bool ok = true;

      if (temp1 - (temp1 ^ temp2) == temp2)
      {
        g[i].push_back(j);
      }

    }
  }

  for (auto &i : num)
  {
    if (i.second.first <= 1) continue;
    if (!used[i.second.second]) dfs(i.second.second);
  }

  cout << answer;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<vector<ll>> g;
vector<char> used;
vector<ll> a;
vector<ll> cost;
map<ll, pair<ll, ll>> num;

ll answer = 0;

void dfs(int v)
{
  answer += cost[v];
  used[v] = true;

  for (auto &i : g[v])
  {
    if (!used[i])
    {
      dfs(i);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  used.assign(n, false);
  a.assign(n, 0);
  cost.assign(n, 0);

  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
    ++num[a[i]].first;
    num[a[i]].second = i;
  }

  for (auto &i : cost)
  {
    cin >> i;
  }

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto temp1 = a[i];
      auto temp2 = a[j];

      bool ok = true;

      while (temp1 || temp2)
      {
        if (temp2 % 2 == 1 && temp1 % 2 == 0)
        {
          ok = false;
        }

        temp1 /= 2;
        temp2 /= 2;
      }

      if (ok)
      {
        g[i].push_back(j);
      }
    }
  }

  for (auto &i : num)
  {
    if (i.second.first <= 1) continue;
    if (!used[i.second.second]) dfs(i.second.second);
  }

  cout << answer;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<vector<ll>> g;
vector<pair<ll, ll>> v;
vector<pair<ll, ll>> used;
ll sum = 0;
bool curCicle = false;

void dfs(ll vert, ll stage)
{
  used[vert].first = stage;

  for (auto &i : g[vert])
  {
    if (used[i].first == stage && i == stage)
    {
      curCicle = true;
    }
  }

  for (auto &i : g[vert])
  {
    if (used[i].first != stage)
    {
      dfs(i, stage);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  v.resize(n);

  for (auto &i : v) cin >> i.first;
  for (auto &i : v) cin >> i.second;

  for (ll i = 0; i < n; ++i)
  {
    for (ll j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto t1 = v[i].first;
      auto t2 = v[j].first;
      bool ok = true;
      while (t1 || t2)
      {
        if ((t1 % 2 == 1) && (t2 % 2 == 0)) ok = false;
        t1 /= 2;
        t2 /= 2;
      }
      if (ok)
      {
        g[j].push_back(i);
      }
    }
  }

  used.assign(n, { -1, 0 });
  for (ll i = 0; i < n; ++i)
  {
    ll ans = 0;
    dfs(i, i);

    if (!curCicle) continue;
    curCicle = false;

    for (ll j = 0; j < n; ++j)
    {
      if (used[j].first == i)
      {
        ans += v[j].second;
      }
    }
    sum = max(ans, sum);
  }
  cout << sum;

  return 0;
}