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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

bool isSquare(i64 x1,i64 y1,i64 x2,i64 y2,i64 x3,i64 y3,i64 x4,i64 y4)
{
   dd[1].xx=x1; dd[1].yy=y1;
   dd[2].xx=x2; dd[2].yy=y2;
   dd[3].xx=x3; dd[3].yy=y3;
   dd[4].xx=x4; dd[4].yy=y4;
   //sort(dd+1,dd+5);
   i64 d1,d2,d3,d4,i,j,k,l,d5,d6;

    do
    {
        i=tt[0];j=tt[1];k=tt[2];l=tt[3];
      d1=((dd[i].xx-dd[j].xx)*(dd[i].xx-dd[j].xx))+((dd[i].yy-dd[j].yy)*(dd[i].yy-dd[j].yy));
      d2=((dd[i].xx-dd[k].xx)*(dd[i].xx-dd[k].xx))+((dd[i].yy-dd[k].yy)*(dd[i].yy-dd[k].yy));
      d3=((dd[k].xx-dd[l].xx)*(dd[k].xx-dd[l].xx))+((dd[k].yy-dd[l].yy)*(dd[k].yy-dd[l].yy));
      d4=((dd[j].xx-dd[l].xx)*(dd[j].xx-dd[l].xx))+((dd[j].yy-dd[l].yy)*(dd[j].yy-dd[l].yy));
      d5=((dd[i].xx-dd[l].xx)*(dd[i].xx-dd[l].xx))+((dd[i].yy-dd[l].yy)*(dd[i].yy-dd[l].yy));
      d6=((dd[j].xx-dd[k].xx)*(dd[j].xx-dd[k].xx))+((dd[j].yy-dd[k].yy)*(dd[j].yy-dd[k].yy));
      if(d1==d2 && d2==d3 && d3==d4 && d1>0 && d5>0 && d6>0 && d5==d6){ return true;}
    }while(next_permutation(tt,tt+4));

   return false;
}

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

    while(n--)
    {
        res=2000000000;
tt[0]=1;tt[1]=2;tt[2]=3;tt[3]=4;
        repc(i,1,4)
        {
          sc("%I64d %I64d %I64d %I64d",&x[i],&y[i],&a[i],&b[i]);
        }
        repc(i,1,4)
        {
            g[1][i]=X(1,i); g[2][i]=Y(1,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g1[1][i]=X(2,i); g1[2][i]=Y(2,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g2[1][i]=X(3,i); g2[2][i]=Y(3,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g3[1][i]=X(4,i); g3[2][i]=Y(4,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }

        repc(i,1,4)
        {
            x1=g[1][i]; y1=g[2][i];
            repc(j,1,4)
            {
                x2=g1[1][j];y2=g1[2][j];
                repc(k,1,4)
                {
                    x3=g2[1][k];y3=g2[2][k];

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

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

       // pfn;

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

bool isSquare(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
   dd[1].xx=x1; dd[1].yy=y1;
   dd[2].xx=x2; dd[2].yy=y2;
   dd[3].xx=x3; dd[3].yy=y3;
   dd[4].xx=x4; dd[4].yy=y4;
   //sort(dd+1,dd+5);
   int d1,d2,d3,d4,i,j,k,l,d5,d6;

    do
    {
        i=tt[0];j=tt[1];k=tt[2];l=tt[3];
      d1=((dd[i].xx-dd[j].xx)*(dd[i].xx-dd[j].xx))+((dd[i].yy-dd[j].yy)*(dd[i].yy-dd[j].yy));
      d2=((dd[i].xx-dd[k].xx)*(dd[i].xx-dd[k].xx))+((dd[i].yy-dd[k].yy)*(dd[i].yy-dd[k].yy));
      d3=((dd[k].xx-dd[l].xx)*(dd[k].xx-dd[l].xx))+((dd[k].yy-dd[l].yy)*(dd[k].yy-dd[l].yy));
      d4=((dd[j].xx-dd[l].xx)*(dd[j].xx-dd[l].xx))+((dd[j].yy-dd[l].yy)*(dd[j].yy-dd[l].yy));
      d5=((dd[i].xx-dd[l].xx)*(dd[i].xx-dd[l].xx))+((dd[i].yy-dd[l].yy)*(dd[i].yy-dd[l].yy));
      d6=((dd[j].xx-dd[k].xx)*(dd[j].xx-dd[k].xx))+((dd[j].yy-dd[k].yy)*(dd[j].yy-dd[k].yy));
      if(d1==d2 && d2==d3 && d3==d4 && d1>0 && d5>0 && d6>0 && d5==d6){ return true;}
    }while(next_permutation(tt,tt+4));

   return false;
}

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

    while(n--)
    {
        res=2000000000;
tt[0]=1;tt[1]=2;tt[2]=3;tt[3]=4;
        repc(i,1,4)
        {
          sc("%d %d %d %d",&x[i],&y[i],&a[i],&b[i]);
        }
        repc(i,1,4)
        {
            g[1][i]=X(1,i); g[2][i]=Y(1,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g1[1][i]=X(2,i); g1[2][i]=Y(2,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g2[1][i]=X(3,i); g2[2][i]=Y(3,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g3[1][i]=X(4,i); g3[2][i]=Y(4,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }

        repc(i,1,4)
        {
            x1=g[1][i]; y1=g[2][i];
            repc(j,1,4)
            {
                x2=g1[1][j];y2=g1[2][j];
                repc(k,1,4)
                {
                    x3=g2[1][k];y3=g2[2][k];

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

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

       // pfn;

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

bool isSquare(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
   dd[1].xx=x1; dd[1].yy=y1;
   dd[2].xx=x2; dd[2].yy=y2;
   dd[3].xx=x3; dd[3].yy=y3;
   dd[4].xx=x4; dd[4].yy=y4;
   //sort(dd+1,dd+5);
   int d1,d2,d3,d4,i,j,k,l,d5,d6;

    do
    {
        i=tt[0];j=tt[1];k=tt[2];l=tt[3];
      d1=((dd[i].xx-dd[j].xx)*(dd[i].xx-dd[j].xx))+((dd[i].yy-dd[j].yy)*(dd[i].yy-dd[j].yy));
      d2=((dd[i].xx-dd[k].xx)*(dd[i].xx-dd[k].xx))+((dd[i].yy-dd[k].yy)*(dd[i].yy-dd[k].yy));
      d3=((dd[k].xx-dd[l].xx)*(dd[k].xx-dd[l].xx))+((dd[k].yy-dd[l].yy)*(dd[k].yy-dd[l].yy));
      d4=((dd[j].xx-dd[l].xx)*(dd[j].xx-dd[l].xx))+((dd[j].yy-dd[l].yy)*(dd[j].yy-dd[l].yy));
      d5=((dd[i].xx-dd[l].xx)*(dd[i].xx-dd[l].xx))+((dd[i].yy-dd[l].yy)*(dd[i].yy-dd[l].yy));
      d6=((dd[j].xx-dd[k].xx)*(dd[j].xx-dd[k].xx))+((dd[j].yy-dd[k].yy)*(dd[j].yy-dd[k].yy));
      if(d1==d2 && d2==d3 && d3==d4 && d1>0 && d5>0 && d6>0 && d5==d6){ return true;}
    }while(next_permutation(tt,tt+4));

   return false;
}

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

    while(n--)
    {
        res=2000000000;
tt[0]=1;tt[1]=2;tt[2]=3;tt[3]=4;
        repc(i,1,4)
        {
          sc("%d %d %d %d",&x[i],&y[i],&a[i],&b[i]);
        }
        repc(i,1,4)
        {
            g[1][i]=X(1,i); g[2][i]=Y(1,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g1[1][i]=X(2,i); g1[2][i]=Y(2,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g2[1][i]=X(3,i); g2[2][i]=Y(3,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g3[1][i]=X(4,i); g3[2][i]=Y(4,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }

        repc(i,1,4)
        {
            x1=g[1][i]; y1=g[2][i];
            repc(j,1,4)
            {
                x2=g1[1][j];y2=g1[2][j];
                repc(k,1,4)
                {
                    x3=g2[1][k];y3=g2[2][k];

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

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

       // pfn;

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

bool isSquare(i64 x1,i64 y1,i64 x2,i64 y2,i64 x3,i64 y3,i64 x4,i64 y4)
{
   dd[1].xx=x1; dd[1].yy=y1;
   dd[2].xx=x2; dd[2].yy=y2;
   dd[3].xx=x3; dd[3].yy=y3;
   dd[4].xx=x4; dd[4].yy=y4;
   //sort(dd+1,dd+5);
   i64 d1,d2,d3,d4,i,j,k,l,d5,d6;

    do
    {
        i=tt[0];j=tt[1];k=tt[2];l=tt[3];
      d1=((dd[i].xx-dd[j].xx)*(dd[i].xx-dd[j].xx))+((dd[i].yy-dd[j].yy)*(dd[i].yy-dd[j].yy));
      d2=((dd[i].xx-dd[k].xx)*(dd[i].xx-dd[k].xx))+((dd[i].yy-dd[k].yy)*(dd[i].yy-dd[k].yy));
      d3=((dd[k].xx-dd[l].xx)*(dd[k].xx-dd[l].xx))+((dd[k].yy-dd[l].yy)*(dd[k].yy-dd[l].yy));
      d4=((dd[j].xx-dd[l].xx)*(dd[j].xx-dd[l].xx))+((dd[j].yy-dd[l].yy)*(dd[j].yy-dd[l].yy));
      d5=((dd[i].xx-dd[l].xx)*(dd[i].xx-dd[l].xx))+((dd[i].yy-dd[l].yy)*(dd[i].yy-dd[l].yy));
      d6=((dd[j].xx-dd[k].xx)*(dd[j].xx-dd[k].xx))+((dd[j].yy-dd[k].yy)*(dd[j].yy-dd[k].yy));
      if(d1==d2 && d2==d3 && d3==d4 && d1>0 && d5>0 && d6>0 && d5==d6){ return true;}
    }while(next_permutation(tt,tt+4));

   return false;
}

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

    while(n--)
    {
        res=2000000000;
tt[0]=1;tt[1]=2;tt[2]=3;tt[3]=4;
        repc(i,1,4)
        {
          sc("%I64d %I64d %I64d %I64d",&x[i],&y[i],&a[i],&b[i]);
        }
        repc(i,1,4)
        {
            g[1][i]=X(1,i); g[2][i]=Y(1,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g1[1][i]=X(2,i); g1[2][i]=Y(2,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g2[1][i]=X(3,i); g2[2][i]=Y(3,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }
       // pfn;
        repc(i,1,4)
        {
            g3[1][i]=X(4,i); g3[2][i]=Y(4,i);
           // pf("%d %d    %d\n",x1,y1,i);
        }

        repc(i,1,4)
        {
            x1=g[1][i]; y1=g[2][i];
            repc(j,1,4)
            {
                x2=g1[1][j];y2=g1[2][j];
                repc(k,1,4)
                {
                    x3=g2[1][k];y3=g2[2][k];

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

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

       // pfn;

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

       // pfn;

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


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#include <memory.h>
#include <set>
#include <list>
#include <string>
#include <math.h>
#include <queue>
#define ll long long
#define forn(i,n) for(int i=0;i<n;++i)
#define rep(i,n) for(int i=1;i<=n;++i)
#define repc(i,l,r) for(int i=l;i<=r;++i)
#define mp(a,b) make_pair(a,b)
using namespace std;
int x[4], y[4];
int a[4], b[4];
int S;
void rotate(int a, int b, int &x, int &y){
	int dx = x - a;
	int dy = y - b;
	dy *= -1;
	swap(dx, dy);
	x = a + dx;
	y = b + dy;
}
void rotate(double &dx, double &dy){
	dy *= -1;
	swap(dx, dy);
}
int same(double a, double b){
	return abs(a - b) < 1e-5;
}
int chk(){
	double dx[4];
	double dy[4];
	double a, b;
	a = 0, b = 0;
	forn(i, 4){
		a += x[i];
		b += y[i];
	}
	a /= 4;
	b /= 4;
	forn(i, 4){
		dx[i] = x[i] - a;
		dy[i] = y[i] - b;
	}
	if (same(dx[0],0) && same(dy[0],0))
		return 0;
	int out[4] = {};
	out[0] = 1;
	forn(i, 3){
		rotate(dx[0], dy[0]);
		rep(j, 3)
			if (!out[j]){
				if (same(dx[0], dx[j]), same(dy[0], dy[j]))
				{
					out[j] = 1;
					break;
				}
			}
	}
	forn(i, 4)
		if (!out[i])
			return 0;
	return 1;
}
void dfs(int k, int s){
	if (chk()){
		S = min(S, s);
	}
	if (s >= S || k >= 4)
		return;
	dfs(k + 1, s);
	rep(i, 3){
		rotate(a[k], b[k], x[k], y[k]);
		dfs(k + 1, s + i);
	}
	rotate(a[k], b[k], x[k], y[k]);
}
int main(){
	//freopen("abc.txt", "r", stdin);
	int n;
	cin >> n;
	forn(t, n){
		forn(i, 4)
			cin >> x[i] >> y[i] >> a[i] >> b[i];
		S = 18;
		dfs(0, 0);
		if (S == 18)
			cout << -1 << endl;
		else
			cout << S << endl;
	}	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#include <memory.h>
#include <set>
#include <list>
#include <string>
#include <math.h>
#include <queue>
#define ll long long
#define forn(i,n) for(int i=0;i<n;++i)
#define rep(i,n) for(int i=1;i<=n;++i)
#define repc(i,l,r) for(int i=l;i<=r;++i)
#define mp(a,b) make_pair(a,b)
using namespace std;
int x[4], y[4];
int a[4], b[4];
int S;
void rotate(int a, int b, int &x, int &y){
	int dx = x - a;
	int dy = y - b;
	dy *= -1;
	swap(dx, dy);
	x = a + dx;
	y = b + dy;
}
void rotate(double &dx, double &dy){
	dx *= -1;
	swap(dx, dy);
}
int same(double a, double b){
	return abs(a - b) < 1e-5;
}
int chk(){
	double dx[4];
	double dy[4];
	double a, b;
	a = 0, b = 0;
	forn(i, 4){
		a += x[i];
		b += y[i];
	}
	a /= 4;
	b /= 4;
	forn(i, 4){
		dx[i] = x[i] - a;
		dy[i] = y[i] - b;
	}
	if (dx[0] <1e-5 && dy[0] <1e-5)
		return 0;
	int out[4] = {};
	out[0] = 1;
	forn(i, 3){
		rotate(dx[0], dy[0]);
		rep(j, 3)
			if (!out[j]){
				if (same(dx[0], dx[j]), same(dy[0], dy[j]))
				{
					out[j] = 1;
					break;
				}
			}
	}
	forn(i, 4)
		if (!out[i])
			return 0;
	return 1;
}
void dfs(int k, int s){
	if (chk()){
		S = min(S, s);
	}
	if (s >= S || k >= 4)
		return;
	dfs(k + 1, s);
	rep(i, 3){
		rotate(a[k], b[k], x[k], y[k]);
		dfs(k + 1, s + i);
	}
	rotate(a[k], b[k], x[k], y[k]);
}
int main(){
	//freopen("abc.txt", "r", stdin);
	int n;
	cin >> n;
	forn(t, n){
		forn(i, 4)
			cin >> x[i] >> y[i] >> a[i] >> b[i];
		S = 18;
		dfs(0, 0);
		if (S == 18)
			cout << -1 << endl;
		else
			cout << S << endl;
	}	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#include <memory.h>
#include <set>
#include <list>
#include <string>
#include <math.h>
#include <queue>
#define ll long long
#define forn(i,n) for(int i=0;i<n;++i)
#define rep(i,n) for(int i=1;i<=n;++i)
#define repc(i,l,r) for(int i=l;i<=r;++i)
#define mp(a,b) make_pair(a,b)
using namespace std;
int x[4], y[4];
int a[4], b[4];
int S;
void rotate(int a, int b, int &x, int &y){
	int dx = x - a;
	int dy = y - b;
	dy *= -1;
	swap(dx, dy);
	x = a + dx;
	y = b + dy;
}
void rotate(double &dx, double &dy){
	dy *= -1;
	swap(dx, dy);
}
int same(double a, double b){
	return abs(a - b) < 1e-5;
}
int chk(){
	double dx[4];
	double dy[4];
	double a, b;
	a = 0, b = 0;
	forn(i, 4){
		a += x[i];
		b += y[i];
	}
	a /= 4;
	b /= 4;
	forn(i, 4){
		dx[i] = x[i] - a;
		dy[i] = y[i] - b;
	}
	if (same(dx[0],0) && same(dy[0],0))
		return 0;
	int out[4] = {};
	out[0] = 1;
	forn(i, 3){
		rotate(dx[0], dy[0]);
		rep(j, 3)
			if (!out[j]){
				if (same(dx[0], dx[j])&& same(dy[0], dy[j]))
				{
					out[j] = 1;
					break;
				}
			}
	}
	forn(i, 4)
		if (!out[i])
			return 0;
	cout << endl;
	return 1;
}
void dfs(int k, int s){
	if (chk()){
		S = min(S, s);
	}
	if (s >= S || k >= 4)
		return;
	dfs(k + 1, s);
	rep(i, 3){
		rotate(a[k], b[k], x[k], y[k]);
		dfs(k + 1, s + i);
	}
	rotate(a[k], b[k], x[k], y[k]);
}
int main(){
//	freopen("abc.txt", "r", stdin);
	int n;
	cin >> n;
	forn(t, n){
		forn(i, 4)
			cin >> x[i] >> y[i] >> a[i] >> b[i];
		S = 18;
		dfs(0, 0);
		if (S == 18)
			cout << -1 << endl;
		else
			cout << S << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int a[MAX],suml[MAX],sumr[MAX];
struct point{
    int x,y,a,b;
};
point p[4];
point rot(point p)
{
    point ret = p;
    ret.x = -p.y;
    ret.y = p.x;
    return ret;
}
double dist(point p1, point p2)
{
    return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
}
bool check()
{
    if(dist(p[0],p[1])==dist(p[2],p[3])&&dist(p[0],p[2])==dist(p[1],p[3])&&dist(p[0],p[3])==dist(p[1],p[2]))
        return true;
    return false;
}
int main()
{
//    ios_base::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<4;++j)
            scanf("%d%d%d%d",&p[j].x,&p[j].y,&p[j].a,&p[j].b);
        bool c=true, finish=false;
        int ans=0;
        for(int j=0;j<4;++j){
            if(!c) break;
            for(int k=0;k<4;++k){
                if(!c) break;
                for(int l=0;l<4;++l){
                    if(!c) break;
                    for(int m=0;m<4;++m){
                        if(check()){
                            ans=j+k+l+m;
                            c=false;
                            finish=true;
                            break;
                        }
                        p[3]=rot(p[3]);
                    }
                    p[2]=rot(p[2]);
                }
                p[1]=rot(p[1]);
            }
            p[0]=rot(p[0]);
        }
        if(finish)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int a[MAX],suml[MAX],sumr[MAX];
ll d[20];
struct point{
    int x,y,a,b;
};
point p[4];
point rot(point p)
{
    point ret = p;
    ret.x = -p.y + p.b + p.a;
    ret.y = p.x - p.a + p.b;
    return ret;
}
ll dist(point p1, point p2)
{
    return (ll)(p1.x-p2.x)*(p1.x-p2.x)+(ll)(p1.y-p2.y)*(p1.y-p2.y);
}
bool check(){
    int t=0;
    for(int i=0;i<4;i++)
        for(int j=i+1;j<4;j++)
            d[t++]=dist(p[i],p[j]);
    sort(d,d+t);
    if(!d[0])return false;
    for(int i=1;i<4;i++)
        if(d[0]!=d[i])
            return false;
    if(d[4]!=d[5])
        return false;
    return true;
}

int main()
{
//    ios_base::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<4;++j)
            scanf("%d%d%d%d",&p[j].x,&p[j].y,&p[j].a,&p[j].b);
        bool finish=false;
        int ans=50;
        for(int j=0;j<4;++j){
            for(int k=0;k<4;++k){
                for(int l=0;l<4;++l){
                    for(int m=0;m<4;++m){
                        if(check()){
                            ans=min(ans,j+k+l+m);
                            finish=true;
                        }
                        p[3]=rot(p[3]);
                    }
                    p[2]=rot(p[2]);
                }
                p[1]=rot(p[1]);
            }
            p[0]=rot(p[0]);
        }
        if(finish)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int a[MAX],suml[MAX],sumr[MAX];
struct point{
    int x,y,a,b;
    bool operator !=(point p){
        if(p.x!=x || p.y!=y)
            return true;
        return false;
    }
};
point p[4];
point rot(point p)
{
    point ret = p;
    ret.x = -p.y + p.b + p.a;
    ret.y = p.x - p.a + p.b;
    return ret;
}
double dist(point p1, point p2)
{
    return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
}
bool check()
{
    set<double> s;
    s.insert(dist(p[0],p[1]));
    s.insert(dist(p[0],p[2]));
    s.insert(dist(p[0],p[3]));
    if(s.size()>2)
        return false;
    if(p[0]!=p[1]&&p[0]!=p[2]&&p[0]!=p[3]&&dist(p[0],p[1])==dist(p[2],p[3])&&dist(p[0],p[2])==dist(p[1],p[3])&&dist(p[0],p[3])==dist(p[1],p[2]))
        return true;
    return false;
}
int main()
{
//    ios_base::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<4;++j)
            scanf("%d%d%d%d",&p[j].x,&p[j].y,&p[j].a,&p[j].b);
        bool c=true, finish=false;
        int ans=0;
        for(int j=0;j<4;++j){
            if(!c) break;
            for(int k=0;k<4;++k){
                if(!c) break;
                for(int l=0;l<4;++l){
                    if(!c) break;
                    for(int m=0;m<4;++m){
                        if(check()){
                            ans=j+k+l+m;
                            c=false;
                            finish=true;
                            break;
                        }
                        p[3]=rot(p[3]);
                    }
                    p[2]=rot(p[2]);
                }
                p[1]=rot(p[1]);
            }
            p[0]=rot(p[0]);
        }
        if(finish)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int a[MAX],suml[MAX],sumr[MAX];
int d[20];
struct point{
    int x,y,a,b;
};
point p[4];
point rot(point p)
{
    point ret = p;
    ret.x = -p.y + p.b + p.a;
    ret.y = p.x - p.a + p.b;
    return ret;
}
double dist(point p1, point p2)
{
    return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
}
bool check(){
    int t=0;
    for(int i=0;i<4;i++)
        for(int j=i+1;j<4;j++)
            d[t++]=dist(p[i],p[j]);
    sort(d,d+t);
    if(!d[0])return false;
    for(int i=1;i<4;i++)
        if(d[0]!=d[i])
            return false;
    if(d[4]!=d[5])
        return false;
    return true;
}

int main()
{
//    ios_base::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<4;++j)
            scanf("%d%d%d%d",&p[j].x,&p[j].y,&p[j].a,&p[j].b);
        bool finish=false;
        int ans=50;
        for(int j=0;j<4;++j){
            for(int k=0;k<4;++k){
                for(int l=0;l<4;++l){
                    for(int m=0;m<4;++m){
                        if(check()){
                            ans=min(ans,j+k+l+m);
                            finish=true;
                        }
                        p[3]=rot(p[3]);
                    }
                    p[2]=rot(p[2]);
                }
                p[1]=rot(p[1]);
            }
            p[0]=rot(p[0]);
        }
        if(finish)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int a[MAX],suml[MAX],sumr[MAX];
struct point{
    int x,y,a,b;
};
point p[4];
point rot(point p)
{
    point ret = p;
    ret.x = -p.y + p.b + p.a;
    ret.y = p.x - p.a + p.b;
    return ret;
}
double dist(point p1, point p2)
{
    return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
}
bool check()
{
    if(dist(p[0],p[1])==dist(p[2],p[3])&&dist(p[0],p[2])==dist(p[1],p[3])&&dist(p[0],p[3])==dist(p[1],p[2]))
        return true;
    return false;
}
int main()
{
//    ios_base::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<4;++j)
            scanf("%d%d%d%d",&p[j].x,&p[j].y,&p[j].a,&p[j].b);
        bool c=true, finish=false;
        int ans=0;
        for(int j=0;j<4;++j){
            if(!c) break;
            for(int k=0;k<4;++k){
                if(!c) break;
                for(int l=0;l<4;++l){
                    if(!c) break;
                    for(int m=0;m<4;++m){
                        if(check()){
                            ans=j+k+l+m;
                            c=false;
                            finish=true;
                            break;
                        }
                        p[3]=rot(p[3]);
                    }
                    p[2]=rot(p[2]);
                }
                p[1]=rot(p[1]);
            }
            p[0]=rot(p[0]);
        }
        if(finish)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int a[MAX],suml[MAX],sumr[MAX];
struct point{
    int x,y,a,b;
    bool diff(point p){
        if(p.x!=x || p.y!=y)
            return true;
        return false;
    }
};
point p[4];
point rot(point p)
{
    point ret = p;
    ret.x = -p.y + p.b + p.a;
    ret.y = p.x - p.a + p.b;
    return ret;
}
double dist(point p1, point p2)
{
    return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
}
bool check()
{
    set<double> s;
    s.insert(dist(p[0],p[1]));
    s.insert(dist(p[0],p[2]));
    s.insert(dist(p[0],p[3]));
    if(s.size()>2)
        return false;
    if(p[0].diff(p[1])&&p[0].diff(p[2])&&p[0].diff(p[3])&&dist(p[0],p[1])==dist(p[2],p[3])&&dist(p[0],p[2])==dist(p[1],p[3])&&dist(p[0],p[3])==dist(p[1],p[2]))
        return true;
    return false;
}
int main()
{
//    ios_base::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<4;++j)
            scanf("%d%d%d%d",&p[j].x,&p[j].y,&p[j].a,&p[j].b);
        bool c=true, finish=false;
        int ans=0;
        for(int j=0;j<4;++j){
            if(!c) break;
            for(int k=0;k<4;++k){
                if(!c) break;
                for(int l=0;l<4;++l){
                    if(!c) break;
                    for(int m=0;m<4;++m){
                        if(check()){
                            ans=j+k+l+m;
                            c=false;
                            finish=true;
                            break;
                        }
                        p[3]=rot(p[3]);
                    }
                    p[2]=rot(p[2]);
                }
                p[1]=rot(p[1]);
            }
            p[0]=rot(p[0]);
        }
        if(finish)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int a[MAX],suml[MAX],sumr[MAX];
int d[20];
struct point{
    int x,y,a,b;
};
point p[4];
point rot(point p)
{
    point ret = p;
    ret.x = -p.y + p.b + p.a;
    ret.y = p.x - p.a + p.b;
    return ret;
}
double dist(point p1, point p2)
{
    return (ll)(p1.x-p2.x)*(p1.x-p2.x)+(ll)(p1.y-p2.y)*(p1.y-p2.y);
}
bool check(){
    int t=0;
    for(int i=0;i<4;i++)
        for(int j=i+1;j<4;j++)
            d[t++]=dist(p[i],p[j]);
    sort(d,d+t);
    if(!d[0])return false;
    for(int i=1;i<4;i++)
        if(d[0]!=d[i])
            return false;
    if(d[4]!=d[5])
        return false;
    return true;
}

int main()
{
//    ios_base::sync_with_stdio(false);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<4;++j)
            scanf("%d%d%d%d",&p[j].x,&p[j].y,&p[j].a,&p[j].b);
        bool finish=false;
        int ans=50;
        for(int j=0;j<4;++j){
            for(int k=0;k<4;++k){
                for(int l=0;l<4;++l){
                    for(int m=0;m<4;++m){
                        if(check()){
                            ans=min(ans,j+k+l+m);
                            finish=true;
                        }
                        p[3]=rot(p[3]);
                    }
                    p[2]=rot(p[2]);
                }
                p[1]=rot(p[1]);
            }
            p[0]=rot(p[0]);
        }
        if(finish)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
std::vector < std::vector < long long > > perm( 16*16, std::vector < long long >( 4, 0));
void preCalc(){
	long long i = 0;
	for(long long a = 0; a < 4; a++){
		for(long long b = 0; b < 4; b++){
			for(long long c = 0; c < 4; c++){
				for(long long d = 0; d < 4; d++){
					perm[ i++] = std::vector < long long >{ a, b, c, d};
				}
			}
		}
	}
}

void callF( long long a, long long b, long long x, long long y, std::vector < std::vector < long long > >& points){
	x = x - a;
	y = y - b;
	if(x == 0 && y == 0){
		for(long long index = 0; index < 3; index++){
			points[index][0] = a;
			points[index][1] = b;
		}
		return;
	}

	points[0][0] = x;
	points[0][1] = y;
	points[1][0] = -y;
	points[1][1] = x;
	points[2][0] = -x;
	points[2][1] = -y;
	points[3][0] = y;
	points[3][1] = -x;

	for(long long index = 0; index < 4; index++){
		points[index][0] += a;
		points[index][1] += b;
	}
}

long long dist( long long x1, long long y1, long long x2, long long y2){
	return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
}

bool auxf( std::vector < long long > dists){
	for(long long x = 0; x < 6; x++){
		for(long long y = x + 1; y < 6; y++){
			long long i = 0;
			std::vector < long long > temp( 4, 0);
			for(long long index = 0; index < 6; index++){
				if(index != x && index != y){
					temp[i++] = dists[index];
				}
			}

			if(dists[x] == dists[y] && temp[0] == temp[1] && temp[0] == temp[2] && temp[0] == temp[3] && temp[0] > 0 && dists[x] > 0){
				// for( auto p : dists){
				// 	std::cout << p << " ";
				// }
				// std::cout << "\n";
				return true;
			}
		}
	}
	return false;
}

bool isSQ(std::vector < long long >& p1, std::vector < long long >& p2, std::vector < long long >& p3, std::vector < long long >& p4){
	long long s1 = dist( p1[0], p1[1], p2[0], p2[1]), s2 = dist( p2[0], p2[1], p3[0], p3[1]), s3 = dist( p3[0], p3[1], p4[0], p4[1]), s4 = dist( p4[0], p4[1], p1[0], p1[1]);
	long long d1 = dist( p1[0], p1[1], p3[0], p3[1]), d2 = dist( p4[0], p4[1], p2[0], p2[1]);
	std::vector < long long > dd{ s1, s2, s3, s4, d1, d2};
	if(auxf( dd)){
		// std::cout << p1[0] << " " << p1[1] << " " << p2[0] << " " << p2[1] << " " << p3[0] << " " << p3[1] << " " << p4[0] << " " << p4[1] << "\n";
		return true;
	}
	return false;
}

long long finalAnsF( std::vector < std::vector < long long > >& p1, std::vector < std::vector < long long > >& p2, std::vector < std::vector < long long > >& p3, std::vector < std::vector < long long > >& p4){
	long long ans = 50;
	for(long long i = 0; i < 16*16; i++){
		if(isSQ( p1[perm[i][0]], p2[perm[i][1]], p3[perm[i][2]], p4[perm[i][3]])){
			if( ans > (perm[i][0] + perm[i][1] + perm[i][2] + perm[i][3])){
				ans = (perm[i][0] + perm[i][1] + perm[i][2] + perm[i][3]);
			}
		}
	}
	return ans;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL);
	preCalc();
	long long n;
	std::cin >> n;
	std::vector < long long > ans( n, 20);
	std::vector < std::vector < long long > > p1( 4, std::vector < long long >( 2, 0)), p2( 4, std::vector < long long >( 2, 0)), p3( 4, std::vector < long long >( 2, 0)), p4( 4, std::vector < long long >( 2, 0)) ;
	for(long long i = 0; i < n; i++){
		long long a, b, x, y;
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p1);
		// for( auto z : p1){
		// 	std::cout << z[0] << " " << z[1] << "   ";
		// }
		// std::cout << "\n";
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p2);
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p3);
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p4);

		ans[i] = finalAnsF( p1, p2, p3, p4);
		// std::cout << "\n";
		if(ans[i] > 30){
			ans[i] = -1;
		}
	}

	for( auto x : ans){
		std::cout << x << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
std::vector < std::vector < int > > perm( 16*16, std::vector < int >( 4, 0));
void preCalc(){
	int i = 0;
	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++){
					perm[ i++] = std::vector < int >{ a, b, c, d};
				}
			}
		}
	}
}

void callF( int a, int b, int x, int y, std::vector < std::vector < int > >& points){
	x = x - a;
	y = y - b;
	if(x == 0 && y == 0){
		for(int index = 0; index < 3; index++){
			points[index][0] = a;
			points[index][1] = b;
		}
		return;
	}

	points[0][0] = x;
	points[0][1] = y;
	points[1][0] = -y;
	points[1][1] = x;
	points[2][0] = -x;
	points[2][1] = -y;
	points[3][0] = y;
	points[3][1] = -x;

	for(int index = 0; index < 4; index++){
		points[index][0] += a;
		points[index][1] += b;
	}
}

int dist( int x1, int y1, int x2, int y2){
	return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
}

bool auxf( std::vector < int > dists){
	for(int x = 0; x < 6; x++){
		for(int y = x + 1; y < 6; y++){
			int i = 0;
			std::vector < int > temp( 4, 0);
			for(int index = 0; index < 6; index++){
				if(index != x && index != y){
					temp[i++] = dists[index];
				}
			}

			if(dists[x] == dists[y] && temp[0] == temp[1] && temp[0] == temp[2] && temp[0] == temp[3] && temp[0] > 0 && dists[x] > 0){
				// for( auto p : dists){
				// 	std::cout << p << " ";
				// }
				// std::cout << "\n";
				return true;
			}
		}
	}
	return false;
}

bool isSQ(std::vector < int >& p1, std::vector < int >& p2, std::vector < int >& p3, std::vector < int >& p4){
	int s1 = dist( p1[0], p1[1], p2[0], p2[1]), s2 = dist( p2[0], p2[1], p3[0], p3[1]), s3 = dist( p3[0], p3[1], p4[0], p4[1]), s4 = dist( p4[0], p4[1], p1[0], p1[1]);
	int d1 = dist( p1[0], p1[1], p3[0], p3[1]), d2 = dist( p4[0], p4[1], p2[0], p2[1]);
	std::vector < int > dd{ s1, s2, s3, s4, d1, d2};
	if(auxf( dd)){
		// std::cout << p1[0] << " " << p1[1] << " " << p2[0] << " " << p2[1] << " " << p3[0] << " " << p3[1] << " " << p4[0] << " " << p4[1] << "\n";
		return true;
	}
	return false;
}

int finalAnsF( std::vector < std::vector < int > >& p1, std::vector < std::vector < int > >& p2, std::vector < std::vector < int > >& p3, std::vector < std::vector < int > >& p4){
	int ans = 50;
	for(int i = 0; i < 16*16; i++){
		if(isSQ( p1[perm[i][0]], p2[perm[i][1]], p3[perm[i][2]], p4[perm[i][3]])){
			if( ans > (perm[i][0] + perm[i][1] + perm[i][2] + perm[i][3])){
				ans = (perm[i][0] + perm[i][1] + perm[i][2] + perm[i][3]);
			}
		}
	}
	return ans;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL);
	preCalc();
	int n;
	std::cin >> n;
	std::vector < int > ans( n, 20);
	std::vector < std::vector < int > > p1( 4, std::vector < int >( 2, 0)), p2( 4, std::vector < int >( 2, 0)), p3( 4, std::vector < int >( 2, 0)), p4( 4, std::vector < int >( 2, 0)) ;
	for(int i = 0; i < n; i++){
		int a, b, x, y;
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p1);
		// for( auto z : p1){
		// 	std::cout << z[0] << " " << z[1] << "   ";
		// }
		// std::cout << "\n";
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p2);
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p3);
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p4);

		ans[i] = finalAnsF( p1, p2, p3, p4);
		// std::cout << "\n";
		if(ans[i] > 30){
			ans[i] = -1;
		}
	}

	for( auto x : ans){
		std::cout << x << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
std::vector < std::vector < int > > perm( 16*16, std::vector < int >( 4, 0));
void preCalc(){
	int i = 0;
	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++){
					perm[ i++] = std::vector < int >{ a, b, c, d};
				}
			}
		}
	}
}

void callF( int a, int b, int x, int y, std::vector < std::vector < int > >& points){
	x = x - a;
	y = y - b;
	if(x == 0 && y == 0){
		for(int index = 0; index < 3; index++){
			points[index][0] = a;
			points[index][1] = b;
		}
		return;
	}

	points[0][0] = x;
	points[0][1] = y;
	points[1][0] = -y;
	points[1][1] = x;
	points[2][0] = -x;
	points[2][1] = -y;
	points[3][0] = y;
	points[3][1] = -x;

	for(int index = 0; index < 4; index++){
		points[index][0] += a;
		points[index][1] += b;
	}
}

long dist( long x1, long y1, long x2, long y2){
	return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
}

bool auxf( std::vector < long > dists){
	for(int x = 0; x < 6; x++){
		for(int y = x + 1; y < 6; y++){
			int i = 0;
			std::vector < long > temp( 4, 0);
			for(int index = 0; index < 6; index++){
				if(index != x && index != y){
					temp[i++] = dists[index];
				}
			}

			if(dists[x] == dists[y] && temp[0] == temp[1] && temp[0] == temp[2] && temp[0] == temp[3] &&temp[0] > 0 && dists[x] > 0){
				// for( auto p : dists){
				// 	std::cout << p << " ";
				// }
				// std::cout << "\n";
				return true;
			}
		}
	}
	return false;
}

bool isSQ(std::vector < int >& p1, std::vector < int >& p2, std::vector < int >& p3, std::vector < int >& p4){
	long s1 = dist( p1[0], p1[1], p2[0], p2[1]), s2 = dist( p2[0], p2[1], p3[0], p3[1]), s3 = dist( p3[0], p3[1], p4[0], p4[1]), s4 = dist( p4[0], p4[1], p1[0], p1[1]);
	long d1 = dist( p1[0], p1[1], p3[0], p3[1]), d2 = dist( p4[0], p4[1], p2[0], p2[1]);
	std::vector < long > dd{ s1, s2, s3, s4, d1, d2};
	if(auxf( dd)){
		// std::cout << p1[0] << " " << p1[1] << " " << p2[0] << " " << p2[1] << " " << p3[0] << " " << p3[1] << " " << p4[0] << " " << p4[1] << "\n";
		return true;
	}
	return false;
}

int finalAnsF( std::vector < std::vector < int > >& p1, std::vector < std::vector < int > >& p2, std::vector < std::vector < int > >& p3, std::vector < std::vector < int > >& p4){
	int ans = 50;
	for(int i = 0; i < 16*16; i++){
		if(isSQ( p1[perm[i][0]], p2[perm[i][1]], p3[perm[i][2]], p4[perm[i][3]])){
			if( ans > (perm[i][0] + perm[i][1] + perm[i][2] + perm[i][3])){
				ans = (perm[i][0] + perm[i][1] + perm[i][2] + perm[i][3]);
			}
		}
	}
	return ans;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL);
	preCalc();
	int n;
	std::cin >> n;
	std::vector < int > ans( n, 20);
	std::vector < std::vector < int > > p1( 4, std::vector < int >( 2, 0)), p2( 4, std::vector < int >( 2, 0)), p3( 4, std::vector < int >( 2, 0)), p4( 4, std::vector < int >( 2, 0)) ;
	for(int i = 0; i < n; i++){
		int a, b, x, y;
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p1);
		// for( auto z : p1){
		// 	std::cout << z[0] << " " << z[1] << "   ";
		// }
		// std::cout << "\n";
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p2);
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p3);
		std::cin >> x >> y >> a >> b;
		callF( a, b, x, y, p4);

		ans[i] = finalAnsF( p1, p2, p3, p4);
		// std::cout << "\n";
		if(ans[i] > 30){
			ans[i] = -1;
		}
	}

	for( auto x : ans){
		std::cout << x << "\n";
	}
	return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="captain marmot" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/captain marmot" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/captain marmot" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<debugger />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include <bits/stdc++.h>
#define eps 1e-6
using namespace std;

struct pt
{
    double x,y;
};

bool operator<(const pt& a,const pt& b)
{
    return (fabs(a.x-b.x)>eps || fabs(a.y-b.y)>eps);
}

pt home[4];

pt rot(pt center,pt sr)
{
    pt nw;
    nw.x = sr.x - center.x;
    nw.y = sr.y - center.y;

    double aux = nw.x;
    nw.x = -nw.y;
    nw.y = aux;

    nw.x += center.x;
    nw.y += center.y;

    return nw;
}

double dist(pt a,pt b)
{
    return sqrt(pow(a.x-b.x,2.0)+pow(a.y-b.y,2.0));
}

bool check(pt i,pt j,pt k,pt l)
{
    pt md,mdd;
    pt pts[4];
    pts[0] = i;
    pts[1] = j;
    pts[2] = k;
    pts[3] = l;
    int it=1;
    while(it<4)
    {
        if(pts[it].x<pts[0].x || (fabs(pts[it].x-pts[0].x)<eps && pts[it].y<pts[0].y))swap(pts[it],pts[0]);
        ++it;
    }
    it=2;
    while(it<4)
    {
        if(pts[it].y<pts[1].y)swap(pts[it],pts[1]);
        ++it;
    }
    if(pts[2].x<pts[3].x)swap(pts[3],pts[2]);

    md.x=(pts[0].x+pts[2].x)/2.0;
    md.y=(pts[0].y+pts[2].y)/2.0;
    mdd.x=(pts[1].x+pts[3].x)/2.0;
    mdd.y=(pts[1].y+pts[3].y)/2.0;
//    cout<<mdd.x<<" "<<mdd.y<<endl;
//    cout<<md.x<<" "<<md.y<<endl;
    if(dist(md,mdd)>eps)return false;
//    cout<<"*****************"<<endl;
    for(it=0;it<3;++it)
    {
        if(dist(rot(md,pts[it]),pts[it+1])>eps)return false;
    }

    return true;
}

int main()
{
//    freopen("in","r",stdin);
    int n;
    ios_base::sync_with_stdio(false);
    cin>>n;

    pt moles[4][4];
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<4;++j)
        {
            cin>>moles[j][0].x>>moles[j][0].y>>home[j].x>>home[j].y;
        }
        for(int j=0;j<4;++j)
        {
//            cout<<moles[j][0].x<<" ; "<<moles[j][0].y;
            for(int it=0;it<3;++it)
            {
                moles[j][it+1]=rot(home[j],moles[j][it]);
//                cout<<" => "<<moles[j][it+1].x<<" ; "<<moles[j][it+1].y;
            }
//            cout<<endl;
        }

        int ans=100;

        for(int i0=0;i0<4;++i0)
        {
            for(int i1=0;i1<4;++i1)
            {
                for(int i2=0;i2<4;++i2)
                {
                    for(int i3=0;i3<4;++i3)
                    {
                       int nb = i0 + i1 + i2 + i3;
                       if(nb>ans)break;
                       set<pt> se;
                       se.insert(moles[0][i0]);
                       se.insert(moles[3][i3]);
                       se.insert(moles[2][i2]);
                       se.insert(moles[1][i1]);
                       if(se.size()==4 && check(moles[0][i0],moles[1][i1],moles[2][i2],moles[3][i3]))
                       {
                            ans = nb;
//                            cout<<i0<<" "<<i1<<" "<<i2<<" "<<i3<<endl;
                       }
                    }
                }
            }
        }

        if(ans==100)ans = -1;
        cout<<ans<<"\n";

    }
    return 0;
}


#include <bits/stdc++.h>
#define eps 1e-8
using namespace std;

struct pt
{
    double x,y;
};

bool operator<(const pt& a,const pt& b)
{
    return (fabs(a.x-b.x)>eps || fabs(a.y-b.y)>eps);
}

pt home[4];

pt rot(int j,pt sr)
{
    pt nw;
    nw.x = sr.x - home[j].x;
    nw.y = sr.y - home[j].y;

    double aux = nw.x;
    nw.x = -nw.y;
    nw.y = aux;

    nw.x += home[j].x;
    nw.y += home[j].y;

    return nw;
}

bool equals(pt a,pt b)
{
    return (fabs(a.x-b.x)<eps && fabs(b.y-a.y)<eps);
}

double dist(pt a,pt b)
{
    return sqrt(pow(a.x-b.x,2.0)+pow(a.y-b.y,2.0));
}

bool check(pt i,pt j,pt k,pt l)
{
    pt md,mdd;
    pt pts[4];
    pts[0] = i;
    pts[1] = j;
    pts[2] = k;
    pts[3] = l;
    int it=1;
    while(it<4)
    {
        if(pts[it].x<pts[0].x)swap(pts[it],pts[0]);
        ++it;
    }
    it=2;
    while(it<4)
    {
        if(pts[it].y<pts[1].y)swap(pts[it],pts[1]);
        ++it;
    }
    if(pts[2].x<pts[3].x)swap(pts[3],pts[2]);

    md.x=(pts[0].x+pts[2].x)/2.0;
    md.y=(pts[0].y+pts[2].y)/2.0;
    mdd.x=(pts[1].x+pts[3].x)/2.0;
    mdd.y=(pts[1].y+pts[3].y)/2.0;

    if(!equals(md,mdd))return false;

    if(fabs(dist(pts[0],pts[1])-dist(pts[1],pts[2]))>eps)return false;
    if(fabs(dist(pts[0],pts[2])-dist(pts[1],pts[3]))>eps)return false;

    return true;
}

int main()
{
//    freopen("in","r",stdin);
    int n;
    ios_base::sync_with_stdio(false);
    cin>>n;

    pt moles[4][4];
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<4;++j)
        {
            cin>>moles[j][0].x>>moles[j][0].y>>home[j].x>>home[j].y;
        }
        for(int j=0;j<4;++j)
        {
//            cout<<moles[j][0].x<<" ; "<<moles[j][0].y;
            for(int it=0;it<3;++it)
            {
                moles[j][it+1]=rot(j,moles[j][it]);
//                cout<<" => "<<moles[j][it+1].x<<" ; "<<moles[j][it+1].y;
            }
//            cout<<endl;
        }

        int ans=100;

        for(int i0=0;i0<4;++i0)
        {
            for(int i1=0;i1<4;++i1)
            {
                for(int i2=0;i2<4;++i2)
                {
                    for(int i3=0;i3<4;++i3)
                    {
                       int nb = i0 + i1 + i2 + i3;
                       if(nb>ans)break;
                       set<pt> se;
                       se.insert(moles[0][i0]);
                       se.insert(moles[3][i3]);
                       se.insert(moles[2][i2]);
                       se.insert(moles[1][i1]);
                       if(se.size()==4 && check(moles[0][i0],moles[1][i1],moles[2][i2],moles[3][i3]))
                       {
                            ans = nb;
//                            cout<<i0<<" "<<i1<<" "<<i2<<" "<<i3<<endl;
                       }
                    }
                }
            }
        }

        if(ans==100)ans = -1;
        cout<<ans<<"\n";

    }
    return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 1e5 + 10;
class Point
{
private:
	int x, y;
public:
	Point(int _x = 0, int _y = 0)
	{
		x = _x, y = _y;
	}
	void init()
	{
		cin >> x >> y;
	}
	Point operator - (Point &p)
	{
		return Point(x - p.x, y - p.y);
	}
	Point operator + (Point &p)
	{
		return Point(x + p.x, y + p.y);
	}
	bool operator == (Point p)
	{
		return x == p.x && y == p.y;
	}
	bool operator != (Point &p)
	{
		return !(*this == p);
	}
	bool operator < (Point &p)
	{
		if (x != p.x) return x < p.x;
		else return y < p.y;
	}
	int operator *(Point p)
	{
		return x * p.x + y * p.y;
	}
	int length()
	{
		return x*x + y*y;
	}
	int dist(Point &p1)
	{
		return (*this - p1).length();
	}
	void next(Point &p)
	{
		Point ret = *this - p;
		*this = Point(-ret.y, ret.x) + p;
	}
};

bool isrect(Point p1, Point p2, Point p3, Point p4)
{

	vector<Point> vp = { p1,p2,p3,p4 };
	sort(vp.begin(), vp.end());
	if (vp[0] == vp[1] || vp[1] == vp[2] || vp[2] == vp[3]) return false;
	int flag = 0;
	do
	{
		if ((vp[0] - vp[1])*(vp[0] - vp[2]) == 0)
		{
			flag = 1;
			break;
		}
	} while (next_permutation(vp.begin(), vp.end()));
	if (!flag) return false;
	p1 = vp[0], p2 = vp[1], p3 = vp[2], p4 = vp[3];
	if (p1.dist(p2) != p1.dist(p3)) return false;
	return (p2 - p1 == p4 - p3);
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int ncase;
	cin >> ncase;
	while (ncase--)
	{
		Point p1, p2, p3, p4;
		Point o1, o2, o3, o4;
		p1.init(), o1.init();
		p2.init(), o2.init();
		p3.init(), o3.init();
		p4.init(), o4.init();
		int ans = INF;
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				for (int k = 0; k < 4; k++)
				{
					for (int t = 0;t < 4; t++)
					{
						if (isrect(p1, p2, p3, p4))
						{
							ans = min(ans, i + j + k + t);
						}
						p4.next(o4);
					}
					p3.next(o3);
				}
				p2.next(o2);
			}
			p1.next(o1);
		}
		if (ans == INF) ans = -1;
		cout << ans << endl;
	}
	return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 1e5 + 10;
class Point
{
private:
	int x, y;
public:
	Point(int _x = 0, int _y = 0)
	{
		x = _x, y = _y;
	}
	void init()
	{
		cin >> x >> y;
	}
	Point operator - (Point &p)
	{
		return Point(x - p.x, y - p.y);
	}
	Point operator + (Point &p)
	{
		return Point(x + p.x, y + p.y);
	}
	bool operator == (Point &p)
	{
		return x == p.x && y == p.y;
	}
	bool operator != (Point &p)
	{
		return !(*this == p);
	}
	bool operator < (Point &p)
	{
		if (x != p.x) return x < p.x;
		else return y < p.y;
	}
	int operator *(Point &p)
	{
		return x * p.x + y * p.y;
	}
	int length()
	{
		return x*x + y*y;
	}
	int dist(Point &p1)
	{
		return (*this - p1).length();
	}
	void next(Point &p)
	{
		Point ret = *this - p;
		*this = Point(-ret.y, ret.x) + p;
	}
};

bool isrect(Point p1, Point p2, Point p3, Point p4)
{

	vector<Point> vp = { p1,p2,p3,p4 };
	sort(vp.begin(), vp.end());
	if (vp[0] == vp[1] || vp[1] == vp[2] || vp[2] == vp[3]) return false;
	int flag = 0;
	do
	{
		if ((vp[0] - vp[1])*(vp[0]-vp[2]) == 0)
		{
			flag = 1;
			break;
		}
	} while (next_permutation(vp.begin(), vp.end()));
	if (!flag) return false;
	p1 = vp[0], p2 = vp[1], p3 = vp[2], p4 = vp[3];
	return (p2 - p1 == p4 - p3);
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int ncase;
	cin >> ncase;
	while (ncase--)
	{
		Point p1, p2, p3, p4;
		Point o1, o2, o3, o4;
		p1.init(), o1.init();
		p2.init(), o2.init();
		p3.init(), o3.init();
		p4.init(), o4.init();
		int ans = INF;
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				for (int k = 0; k < 4; k++)
				{
					for (int t = 0;t < 4; t++)
					{
						if (isrect(p1, p2, p3, p4))
						{
							ans = min(ans, i + j + k + t);
						}
						p4.next(o4);
					}
					p3.next(o3);
				}
				p2.next(o2);
			}
			p1.next(o1);
		}
		if (ans == INF) ans = -1;
		cout << ans << endl;
	}
	return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 1e5 + 10;
class Point
{
private:
	int x, y;
public:
	Point(int _x = 0, int _y = 0)
	{
		x = _x, y = _y;
	}
	void init()
	{
		cin >> x >> y;
	}
	Point operator - (Point &p)
	{
		return Point(x - p.x, y - p.y);
	}
	Point operator + (Point &p)
	{
		return Point(x + p.x, y + p.y);
	}
	bool operator == (Point p)
	{
		return x == p.x && y == p.y;
	}
	bool operator != (Point &p)
	{
		return !(*this == p);
	}
	bool operator < (Point &p)
	{
		if (x != p.x) return x < p.x;
		else return y < p.y;
	}
	int operator *(Point p)
	{
		return x * p.x + y * p.y;
	}
	int length()
	{
		return x*x + y*y;
	}
	int dist(Point &p1)
	{
		return (*this - p1).length();
	}
	void next(Point &p)
	{
		Point ret = *this - p;
		*this = Point(-ret.y, ret.x) + p;
	}
};

bool isrect(Point p1, Point p2, Point p3, Point p4)
{

	vector<Point> vp = { p1,p2,p3,p4 };
	sort(vp.begin(), vp.end());
	if (vp[0] == vp[1] || vp[1] == vp[2] || vp[2] == vp[3]) return false;
	int flag = 0;
	do
	{
		if ((vp[0] - vp[1])*(vp[0] - vp[2]) == 0)
		{
			flag = 1;
			break;
		}
	} while (next_permutation(vp.begin(), vp.end()));
	if (!flag) return false;
	p1 = vp[0], p2 = vp[1], p3 = vp[2], p4 = vp[3];
	return (p2 - p1 == p4 - p3);
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int ncase;
	cin >> ncase;
	while (ncase--)
	{
		Point p1, p2, p3, p4;
		Point o1, o2, o3, o4;
		p1.init(), o1.init();
		p2.init(), o2.init();
		p3.init(), o3.init();
		p4.init(), o4.init();
		int ans = INF;
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				for (int k = 0; k < 4; k++)
				{
					for (int t = 0;t < 4; t++)
					{
						if (isrect(p1, p2, p3, p4))
						{
							ans = min(ans, i + j + k + t);
						}
						p4.next(o4);
					}
					p3.next(o3);
				}
				p2.next(o2);
			}
			p1.next(o1);
		}
		if (ans == INF) ans = -1;
		cout << ans << endl;
	}
	return 0;
}


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

struct point {
  int x, y;

  void read() {
    int a, b;
    cin >> a >> b;
    x = a; y = b;
  }

  long long dist(point b) {
    long long dx = x - b.x;
    long long dy = y - b.y;
    return dx * dx + dy * dy;
  }

  void rot(point b) {
    x -= b.x; y -= b.y;
    y = -y; swap(x, y);
    x += b.x; y += b.y;
  }

  bool operator == (point &b) {
    if(x == b.x && y == b.y) return 1;
    return 0;
  }
};

int i, t, rs, j, k, it;
point w[4], e[4];
vector<point> poz[4];

bool check() {
  int perm[] = {0, 1, 2, 3};
  int x, y, z, q;

  do {
    x = perm[0]; y = perm[1];
    z = perm[2]; q = perm[3];

    if(poz[x][i].dist(poz[y][j]) != poz[y][j].dist(poz[z][k])) continue;
    if(poz[x][i].dist(poz[y][j]) != poz[z][k].dist(poz[q][it])) continue;
    if(poz[x][i].dist(poz[y][j]) != poz[x][i].dist(poz[q][it])) continue;
    if(poz[x][i].dist(poz[z][k]) != poz[q][it].dist(poz[y][j])) continue;
    if(poz[x][i] == poz[y][j] || poz[x][i] == poz[z][k] || poz[x][i] == poz[q][it]) continue;
    if(poz[y][j] == poz[z][k] || poz[y][j] == poz[q][it] || poz[q][it] == poz[z][k]) continue;

    return 1;
  } while(next_permutation(perm, perm + 4));

  return 0;
}

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

  for(cin >> t; t; --t) {
    for(i = 0; i < 4; ++i) {
      w[i].read(); e[i].read();
      poz[i].clear(); poz[i].push_back(w[i]);

      for(j = 0; j < 9; ++j) w[i].rot(e[i]), poz[i].push_back(w[i]);
    }

    rs = 1e9;

    for(i = 0; i < 10; ++i)
      for(j = 0; j < 10; ++j)
        for(k = 0; k < 10; ++k)
          for(it = 0; it < 10; ++it)
            if(rs > i + j + k + it && check()) rs = min(rs, i + j + k + it);

    if(rs > 1e7) rs = -1;

    cout << rs << '\n';
  }

  return 0;
}


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

struct point {
  int x, y;

  void read() {
    int a, b;
    cin >> a >> b;
    x = a; y = b;
  }

  long long dist(point b) {
    long long dx = x - b.x;
    long long dy = y - b.y;
    return dx * dx + dy * dy;
  }

  void rot(point b) {
    x -= b.x; y -= b.y;
    y = -y; swap(x, y);
    x += b.x; y += b.y;
  }

  bool operator == (point &b) {
    if(x == b.x && y == b.y) return 1;
    return 0;
  }
};

int i, t, rs, j, k, it;
point w[4], e[4];
vector<point> poz[4];

bool check() {
  int perm[] = {0, 1, 2, 3};
  int x, y, z, q;

  do {
    x = perm[0]; y = perm[1];
    z = perm[2]; q = perm[3];

    if(poz[x][i].dist(poz[y][j]) != poz[y][j].dist(poz[z][k])) continue;
    if(poz[x][i].dist(poz[y][j]) != poz[z][k].dist(poz[q][it])) continue;
    if(poz[x][i].dist(poz[y][j]) != poz[x][i].dist(poz[q][it])) continue;
    if(poz[x][i] == poz[y][j] || poz[x][i] == poz[z][k] || poz[x][i] == poz[q][it]) continue;
    if(poz[y][j] == poz[z][k] || poz[y][j] == poz[q][it] || poz[q][it] == poz[z][k]) continue;

    return 1;
  } while(next_permutation(perm, perm + 4));

  return 0;
}

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

  for(cin >> t; t; --t) {
    for(i = 0; i < 4; ++i) {
      w[i].read(); e[i].read();
      poz[i].clear(); poz[i].push_back(w[i]);

      for(j = 0; j < 9; ++j) w[i].rot(e[i]), poz[i].push_back(w[i]);
    }

    rs = 1e9;

    for(i = 0; i < 10; ++i)
      for(j = 0; j < 10; ++j)
        for(k = 0; k < 10; ++k)
          for(it = 0; it < 10; ++it)
            if(rs > i + j + k + it && check()) rs = min(rs, i + j + k + it);

    if(rs > 1e7) rs = -1;

    cout << rs << '\n';
  }

  return 0;
}


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

struct point {
  int x, y;

  void read() {
    int a, b;
    cin >> a >> b;
    x = a; y = b;
  }

  long long dist(point b) {
    long long dx = x - b.x;
    long long dy = y - b.y;
    return dx * dx + dy * dy;
  }

  void rot(point b) {
    x -= b.x; y -= b.y;
    y = -y; swap(x, y);
    x += b.x; y += b.y;
  }

  bool operator == (point &b) {
    if(x == b.x && y == b.y) return 1;
    return 0;
  }
};

int i, t, rs, j, k, it;
point w[4], e[4];
vector<point> poz[4];

bool check() {
  int perm[] = {0, 1, 2, 3};
  int x, y, z, q;

  do {
    x = perm[0]; y = perm[1];
    z = perm[2]; q = perm[3];

    if(poz[x][i].dist(poz[y][j]) != poz[y][j].dist(poz[z][k])) continue;
    if(poz[x][i].dist(poz[y][j]) != poz[z][k].dist(poz[q][it])) continue;
    if(poz[x][i].dist(poz[y][j]) != poz[x][i].dist(poz[q][it])) continue;
    if(poz[x][i] == poz[y][j] || poz[x][i] == poz[z][k] || poz[x][i] == poz[q][it]) continue;
    if(poz[y][j] == poz[z][k] || poz[y][j] == poz[q][it] || poz[q][it] == poz[z][k]) continue;

    return 1;
  } while(next_permutation(perm, perm + 4));

  return 0;
}

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

  for(cin >> t; t; --t) {
    for(i = 0; i < 4; ++i) {
      w[i].read(); e[i].read();
      poz[i].clear(); poz[i].push_back(w[i]);

      for(j = 0; j < 3; ++j) w[i].rot(e[i]), poz[i].push_back(w[i]);
    }

    rs = 1e9;

    for(i = 0; i < 4; ++i)
      for(j = 0; j < 4; ++j)
        for(k = 0; k < 4; ++k)
          for(it = 0; it < 4; ++it)
            if(rs > i + j + k + it && check()) rs = min(rs, i + j + k + it);

    if(rs > 1e7) rs = -1;

    cout << rs << '\n';
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <deque>
#include <cstdio>
#include <cstring>
#include <iomanip>

#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;

const int MOD = 1e9 + 7;

struct point{
	int x, y;

	bool operator == (point other) const{
		return (x == other.x && y == other.y);
	}
};

point p[5];
point r[5];

point rotate90( point a, point b){

	point aux = a;
	aux.x -= b.x;
	aux.y -= b.y;

	return (point){ -aux.y + b.x, aux.x + b.y};
}

point rotate( point a, point b, int angle ){

	while( angle ){
		a = rotate90(a, b);
		angle -= 90;
	}
	return a;
}

bool square(point a, point b, point c, point d){

	if( a == b || a == c || a == d || b == c || b == d || c == d )
		return false;

	int min_y = min( a.y, min(b.y, min(c.y, d.y)));

	queue<point> bottom;
	queue<point> top;
	bottom.push(a); bottom.push(b); bottom.push(c); bottom.push(d);

	int prev_x = (1 << 30);
	int last_x = (1 << 30);

	while( !bottom.empty() ){
		point cur = bottom.front();
		bottom.pop();

		if(  cur.y == min_y ) {
			if(prev_x > 100000 ) prev_x = cur.x;

			last_x = cur.x;

			continue;
		}

		top.push( cur );

	}

	if( top.size() !=  2 ) return false;

	point p = top.front();
	top.pop();

	point q = top.front();

	if( p.y != q.y) return false;

	if(p.x == last_x && q.x == prev_x) return true;

	if(p.x == prev_x && q.x == last_x ) return true;

	return false;

}
int main(){

	point a = (point) { 1, 1 };
	point b = (point) { -1,-1 };
	point c = (point) { -1, 1 };
	point d = (point) { 1, -1 };

	square(a, b, c, d);

	int n; cin >> n;

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

		for(int j = 0; j < 4; j++){
			cin >> p[j].x >> p[j].y >> r[j].x >> r[j].y;
		}

		int ans = 100;
		for(int a = 0; a < 4; a++){

			point x = rotate( p[0], r[0], 90 * a);

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

				point y = rotate(p[1], r[1], 90*b);

				for(int c = 0; c < 4; c++){
					point z = rotate(p[2], r[2], 90*c);

					for(int d = 0; d < 4; d++){
						point q = rotate(p[3], r[3], 90*d);

						if( square (x, y, z, q) ){
							ans = min(ans, a+b+c+d);
						}
					}
				}
			}
		}

		if( ans > 50 )
			cout << -1 << endl;
		else
			cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <deque>
#include <cstdio>
#include <cstring>
#include <iomanip>

#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;

const int MOD = 1e9 + 7;

struct point{
	int x, y;

	bool operator == (point other) const{
		return (x == other.x && y == other.y);
	}
};

point p[5];
point r[5];

point rotate90( point a, point b){

	point aux = a;
	aux.x -= b.x;
	aux.y -= b.y;

	return (point){ -aux.y + b.x, aux.x + b.y};
}

point rotate( point a, point b, int angle ){

	while( angle ){
		a = rotate90(a, b);
		angle -= 90;
	}
	return a;
}

int dotProduct( point a, point b){
    return a.x * b.x + a.y * b.y;
}

point get_vector( point a, point b){
    return (point){ a.x - b.x , a.y - b.y };
}

bool all_90_angles(point a, point b, point c, point d){

    if( dotProduct( get_vector(a, b), get_vector(b, c) ) ) return false;
    if( dotProduct( get_vector(b, c), get_vector(c, d) ) ) return false;
    if( dotProduct( get_vector(c, d), get_vector(d, a) ) ) return false;

	return true;
}

pair<int, int> dist(point a, point b){

	int deltax = abs(a.x - b.x);
	int deltay = abs(a.y - b.y);

	return make_pair( min(deltax, deltay), max(deltax, deltay) );
}

bool square(point a, point b, point c, point d){

	if( a == b || a == c || a == d || b == c || b == d || c == d )
		return false;

	if( !all_90_angles(a, b, c, d) ) return false;

	pair<int, int> cur = dist(a , b);

	if( cur != dist(b, c) ) return false;
	if( cur != dist(c, d) ) return false;
	if( cur != dist(d, a) ) return false;
	return true;

}

int main(){

	int n; cin >> n;

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

		for(int j = 0; j < 4; j++){
			cin >> p[j].x >> p[j].y >> r[j].x >> r[j].y;
		}

		int ans = 100;
		for(int a = 0; a < 4; a++){

			point x = rotate( p[0], r[0], 90 * a);

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

				point y = rotate(p[1], r[1], 90*b);

				for(int c = 0; c < 4; c++){
					point z = rotate(p[2], r[2], 90*c);

					for(int d = 0; d < 4; d++){
						point q = rotate(p[3], r[3], 90*d);

						bool ok = square (x, y, z, q);
						ok = ok || square(x, y, q, z);
						ok = ok || square(x, z, q, y);
						ok = ok || square(x, z, y, q);
						ok = ok || square(x, q, y, z);
						ok = ok || square(x, q, z, y);

						ok = square (y, x, z, q);
						ok = ok || square(y, x, q, z);
						ok = ok || square(y, z, q, x);
						ok = ok || square(y, z, x, q);
						ok = ok || square(y, q, x, z);
						ok = ok || square(y, q, z, x);

						ok = square (z, x, y, q);
						ok = ok || square(z, x, q, y);
						ok = ok || square(z, y, q, x);
						ok = ok || square(z, y, x, q);
						ok = ok || square(z, q, x, y);
						ok = ok || square(z, q, y, x);

						ok = square (q, x, z, y);
						ok = ok || square(q, x, y, z);
						ok = ok || square(q, z, y, x);
						ok = ok || square(q, z, x, y);
						ok = ok || square(q, y, x, z);
						ok = ok || square(q, y, z, x);

						if( ok ){
							ans = min(ans, a+b+c+d);
						}
					}
				}
			}
		}

		if( ans > 50 )
			cout << -1 << endl;
		else
			cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <deque>
#include <cstdio>
#include <cstring>
#include <iomanip>

#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;

const int MOD = 1e9 + 7;

struct point{
	int x, y;

	bool operator == (point other) const{
		return (x == other.x && y == other.y);
	}
};

point p[5];
point r[5];

point rotate90( point a, point b){

	point aux = a;
	aux.x -= b.x;
	aux.y -= b.y;

	return (point){ -aux.y + b.x, aux.x + b.y};
}

point rotate( point a, point b, int angle ){

	while( angle ){
		a = rotate90(a, b);
		angle -= 90;
	}
	return a;
}

int dotProduct( point a, point b){
    return a.x * b.x + a.y * b.y;
}

point get_vector( point a, point b){
    return (point){ a.x - b.x , a.y - b.y };
}

bool all_90_angles(point a, point b, point c, point d){

    if( dotProduct( get_vector(a, b), get_vector(b, c) ) ) return false;
    if( dotProduct( get_vector(b, c), get_vector(c, d) ) ) return false;
    if( dotProduct( get_vector(c, d), get_vector(d, a) ) ) return false;

	return true;
}

pair<int, int> dist(point a, point b){

	int deltax = abs(a.x - b.x);
	int deltay = abs(a.y - b.y);

	return make_pair( min(deltax, deltay), max(deltax, deltay) );
}

bool square(point a, point b, point c, point d){

	if( a == b || a == c || a == d || b == c || b == d || c == d )
		return false;

	if( !all_90_angles(a, b, c, d) ) return false;

	pair<int, int> cur = dist(a , b);

	if( cur != dist(b, c) ) return false;
	if( cur != dist(c, d) ) return false;
	if( cur != dist(d, a) ) return false;
	return true;

}

int main(){

	point a = (point) { -2, 0 };
	point b = (point) { -1, 0 };
	point c = (point) { -2, 1 };
	point d = (point) { 1,  1 };

	square(a, b, c, d);

	int n; cin >> n;

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

		for(int j = 0; j < 4; j++){
			cin >> p[j].x >> p[j].y >> r[j].x >> r[j].y;
		}

		int ans = 100;
		for(int a = 0; a < 4; a++){

			point x = rotate( p[0], r[0], 90 * a);

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

				point y = rotate(p[1], r[1], 90*b);

				for(int c = 0; c < 4; c++){
					point z = rotate(p[2], r[2], 90*c);

					for(int d = 0; d < 4; d++){
						point q = rotate(p[3], r[3], 90*d);

						bool ok = square (x, y, z, q);
						ok = ok || square(x, y, q, z);
						ok = ok || square(x, z, q, y);
						ok = ok || square(x, z, y, q);
						ok = ok || square(x, q, y, z);
						ok = ok || square(x, q, z, y);

						ok = square (y, x, z, q);
						ok = ok || square(y, x, q, z);
						ok = ok || square(y, z, q, x);
						ok = ok || square(y, z, x, q);
						ok = ok || square(y, q, x, z);
						ok = ok || square(y, q, z, x);

						ok = square (z, x, y, q);
						ok = ok || square(z, x, q, y);
						ok = ok || square(z, y, q, x);
						ok = ok || square(z, y, x, q);
						ok = ok || square(z, q, x, y);
						ok = ok || square(z, q, y, x);

						ok = square (q, x, z, y);
						ok = ok || square(q, x, y, z);
						ok = ok || square(q, z, y, x);
						ok = ok || square(q, z, x, y);
						ok = ok || square(q, y, x, z);
						ok = ok || square(q, y, z, x);

						if( square (x, y, z, q) ){
							ans = min(ans, a+b+c+d);
						}
					}
				}
			}
		}

		if( ans > 50 )
			cout << -1 << endl;
		else
			cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <deque>
#include <cstdio>
#include <cstring>
#include <iomanip>

#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;

const int MOD = 1e9 + 7;

struct point{
	int x, y;

	bool operator == (point other) const{
		return (x == other.x && y == other.y);
	}
};

point p[5];
point r[5];

point rotate90( point a, point b){

	point aux = a;
	aux.x -= b.x;
	aux.y -= b.y;

	return (point){ -aux.y + b.x, aux.x + b.y};
}

point rotate( point a, point b, int angle ){

	while( angle ){
		a = rotate90(a, b);
		angle -= 90;
	}
	return a;
}

int dotProduct( point a, point b){
    return a.x * b.x + a.y * b.y;
}

point get_vector( point a, point b){
    return (point){ a.x - b.x , a.y - b.y };
}

bool all_90_angles(point a, point b, point c, point d){

    if( dotProduct( get_vector(a, b), get_vector(b, c) ) ) return false;
    if( dotProduct( get_vector(b, c), get_vector(c, d) ) ) return false;
    if( dotProduct( get_vector(c, d), get_vector(d, a) ) ) return false;

	return true;
}

pair<int, int> dist(point a, point b){

	int deltax = abs(a.x - b.x);
	int deltay = abs(a.y - b.y);

	return make_pair( min(deltax, deltay), max(deltax, deltay) );
}

bool square(point a, point b, point c, point d){

	if( a == b || a == c || a == d || b == c || b == d || c == d )
		return false;

	if( !all_90_angles(a, b, c, d) ) return false;

	pair<int, int> cur = dist(a , b);

	if( cur != dist(b, c) ) return false;
	if( cur != dist(c, d) ) return false;
	if( cur != dist(d, a) ) return false;
	return true;

}

int main(){

	point a = (point) { 1, 1 };
	point b = (point) { -1,-1 };
	point c = (point) { -1, 1 };
	point d = (point) { 1, -1 };

	square(a, b, c, d);

	int n; cin >> n;

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

		for(int j = 0; j < 4; j++){
			cin >> p[j].x >> p[j].y >> r[j].x >> r[j].y;
		}

		int ans = 100;
		for(int a = 0; a < 4; a++){

			point x = rotate( p[0], r[0], 90 * a);

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

				point y = rotate(p[1], r[1], 90*b);

				for(int c = 0; c < 4; c++){
					point z = rotate(p[2], r[2], 90*c);

					for(int d = 0; d < 4; d++){
						point q = rotate(p[3], r[3], 90*d);

						if( square (x, y, z, q) ){
							ans = min(ans, a+b+c+d);
						}
					}
				}
			}
		}

		if( ans > 50 )
			cout << -1 << endl;
		else
			cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
typedef long long ll ;
using namespace std;
const double pi = acos(-1.0) ;
int n ;
int X[4][4] , Y[4][4] ;

int rotate_x (int x , int y) { return -y ;}
int rotate_y (int x , int y) { return x ;}

void draw (int id , int x , int y , int ax , int ay) {
    X[id][0] = x , Y[id][0] = y ;
    int _x = x-ax , _y = y-ay ;
    for (int i = 1 ; i < 4 ; i ++) {
        X[id][i] = ax + rotate_x (_x , _y) ;
        Y[id][i] = ay + rotate_y (_x , _y) ;
        _x = X[id][i]-ax , _y = Y[id][i]-ay ;
    }
}

bool judge (int *XX , int *YY) {
    vector<ll> vc ;
    for (int i = 0 ; i < 4 ; i ++) {
        for (int j = i+1 ; j < 4 ; j ++) {
            vc.push_back ( 1ll*(XX[i]-XX[j])*(XX[i]-XX[j])+1ll*(YY[i]-YY[j])*(YY[i]-YY[j]) ) ;
        }
    }
    sort (vc.begin(),vc.end()) ;
    //for (int i = 0 ; i < 6 ; i ++) printf ("%I64d " , vc[i]) ; puts ("") ;
    if (vc[0] > 0 && (vc[0]==vc[1] && vc[1]==vc[2] && vc[2]==vc[3])&&(vc[4]==vc[5])&&(vc[2]*2==vc[4])){
       return true ;
    }
    return false ;
}

void work () {
    int minn = 100 ;
    for (int i = 0 ; i < 4 ; i ++) {
        for (int j = 0 ; j < 4 ; j ++) {
            for (int f = 0 ; f < 4 ; f ++) {
                for (int t = 0 ; t < 4 ; t ++) {
                    //printf ("i=%d,j=%d,f=%d,t=%d\n",i,j,f,t) ;
                    int XX[4] = {X[0][i],X[1][j],X[2][f],X[3][t]} ;
                    int YY[4] = {Y[0][i],Y[1][j],Y[2][f],Y[3][t]} ;
                    //if (i==0&&j==0&&f==1&&t==2) {
                    //    for (int h = 0 ; h < 4 ; h ++) printf ("%d %d\n",XX[h],YY[h]) ;
                    //}
                    if (judge(XX , YY)) minn = min (i+j+f+t,minn) ;
                }
            }
        }
    }
    if (minn == 100) puts ("-1") ;
    else printf ("%d\n" , minn) ;
}

int main () {
    int n ;
    scanf ("%d" , &n) ;
    for (int i = 0 ; i < n ; i ++) {
        for (int j = 0 ; j < 4 ; j ++) {
            int x , y , ax , ay ;
            scanf ("%d%d%d%d" , &x , &y , &ax , &ay) ;
            draw (j , x , y , ax , ay) ;
        }
        work () ;
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7 ;
int a , b ;
typedef long long ll ;

bool judge (int mid) {
    //cout << "mid = " << mid << "mid/b = " << mid/b << " " << "mid(MOD)b = " << mid%b << endl ;
    ll tmp = (mid/b) / (mid%b) ;
    if (tmp >= 1 && tmp <= a) return true ;
    return false;
}

ll add (ll a , ll b) {
    return ((a+b)%MOD+MOD)%MOD ;
}

ll qkadd (ll a , ll b) {
    ll ret = 0 ;
    while (b) {
        if (b&1) ret = add(ret , a) ;
        b >>= 1 ;
        a = add(a , a) ;
    }
    return ret ;
}

int main () {
    scanf ("%d%d" , &a , &b) ;
    if (b == 1) return puts ("0") , 0 ;
    ll l = b+1 , r = 1e18 ;
    ll ret = r ;
    while (l < r) {
        //cout << l << " " << r << endl ;
        ll mid = l+r>>1 ;
        if (mid % b == 0) {
            if(judge(mid+1)) {
                ret = mid+1 ;
                l = mid+2 ;
            }
            else if (judge(mid-1)) {
                ret = mid-1 ;
                r = mid-2 ;
            }
            else r = mid-2 ;
        }
        else if (judge(mid)) {
            l = mid+1 ;
            ret = mid ;
        }
        else  r = mid-1 ;
    }
    //cout << l << " " << r << endl ;
    //if (judge(l)) ret = max(ret , l) ;
    //if (judge(r)) ret = max(ret , r) ;
    ll tmp = ret/b ;
    ll ans = qkadd(1+tmp,tmp)/2 ;
    ans = qkadd(ans , b) ;
    printf ("%I64d\n" , add (qkadd(b+ret,ret-b+1)/2 , -ans)) ;
    return 0 ;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
const double pi = acos(-1.0) ;
int n ;
int X[4][4] , Y[4][4] ;

int rotate_x (int x , int y) { return -y ;}
int rotate_y (int x , int y) { return x ;}

void draw (int id , int x , int y , int ax , int ay) {
    X[id][0] = x , Y[id][0] = y ;
    int _x = x-ax , _y = y-ay ;
    for (int i = 1 ; i < 4 ; i ++) {
        X[id][i] = ax + rotate_x (_x , _y) ;
        Y[id][i] = ay + rotate_y (_x , _y) ;
        _x = X[id][i]-ax , _y = Y[id][i]-ay ;
    }
}

bool judge (int *XX , int *YY) {
    vector<int> vc ;
    for (int i = 0 ; i < 4 ; i ++) {
        for (int j = i+1 ; j < 4 ; j ++) {
            vc.push_back ( (XX[i]-XX[j])*(XX[i]-XX[j])+(YY[i]-YY[j])*(YY[i]-YY[j]) ) ;
        }
    }
    sort (vc.begin(),vc.end()) ;
    if (vc[0] > 0 && (vc[0]==vc[1] && vc[1]==vc[2] && vc[2]==vc[3])&&(vc[4]==vc[5])&&(vc[2]*2==vc[4])){
       return true ;
    }
    return false ;
}

void work () {
    int minn = 100 ;
    for (int i = 0 ; i < 4 ; i ++) {
        for (int j = 0 ; j < 4 ; j ++) {
            for (int f = 0 ; f < 4 ; f ++) {
                for (int t = 0 ; t < 4 ; t ++) {
                    //printf ("i=%d,j=%d,f=%d,t=%d\n",i,j,f,t) ;
                    int XX[4] = {X[0][i],X[1][j],X[2][f],X[3][t]} ;
                    int YY[4] = {Y[0][i],Y[1][j],Y[2][f],Y[3][t]} ;
                    //if (i==0&&j==0&&f==1&&t==2) {
                    //    for (int h = 0 ; h < 4 ; h ++) printf ("%d %d\n",XX[h],YY[h]) ;
                    //}
                    if (judge(XX , YY)) minn = min (i+j+f+t,minn) ;
                }
            }
        }
    }
    if (minn == 100) puts ("-1") ;
    else printf ("%d\n" , minn) ;
}

int main () {
    int n ;
    scanf ("%d" , &n) ;
    for (int i = 0 ; i < n ; i ++) {
        for (int j = 0 ; j < 4 ; j ++) {
            int x , y , ax , ay ;
            scanf ("%d%d%d%d" , &x , &y , &ax , &ay) ;
            draw (j , x , y , ax , ay) ;
        }
        work () ;
    }
    return 0 ;
}


// Author : Bony Roopchandani

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

typedef pair<double, double> Point;
Point home[4], s[4], org[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);
	s[pos]=org[pos];
	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]=org[0]={x, y}; home[0]={hx, hy};
		cin>>x>>y>>hx>>hy;
		s[1]=org[1]={x, y}; home[1]={hx, hy};
		cin>>x>>y>>hx>>hy;
		s[2]=org[2]={x, y}; home[2]={hx, hy};
		cin>>x>>y>>hx>>hy;
		s[3]=org[3]={x, y}; home[3]={hx, hy};
		ok=false;
		ans=0;
		res=12345678;
		solve(0);
		cout<<(ok?res:-1)<<"\n";
	}
	return (0);
}