Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.

Initially, each mole i (1 ≤ i ≤ 4n) is placed at some position (x_{i}, y_{i}) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible.

Each mole i has a home placed at the position (a_{i}, b_{i}). Moving this mole one time means rotating his position point (x_{i}, y_{i}) 90 degrees counter-clockwise around it's home point (a_{i}, b_{i}).

A regiment is compact only if the position points of the 4 moles form a square with non-zero area.

Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.


-----Input-----

The first line contains one integer n (1 ≤ n ≤ 100), the number of regiments.

The next 4n lines contain 4 integers x_{i}, y_{i}, a_{i}, b_{i} ( - 10^4 ≤ x_{i}, y_{i}, a_{i}, b_{i} ≤ 10^4).


-----Output-----

Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes).


-----Examples-----
Input
4
1 1 0 0
-1 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-2 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-1 1 0 0
-1 1 0 0
-1 1 0 0
2 2 0 1
-1 0 0 -2
3 0 0 -2
-1 1 -2 0

Output
1
-1
3
3



-----Note-----

In the first regiment we can move once the second or the third mole.

We can't make the second regiment compact.

In the third regiment, from the last 3 moles we can move once one and twice another one.

In the fourth regiment, we can move twice the first mole and once the third mole.

Difficulty: interview

Starter code:

Solutions:
def f(x, y, a, b, n):
    return a + (x - a) * cos[n] - (y - b) * sin[n], b + (x - a) * sin[n] + (y - b) * cos[n]


def check(p):
    d = {}
    for i in range(len(p) - 1):
        for j in range(i + 1, len(p)):
            dist = (p[i][0] - p[j][0]) ** 2 + (p[i][1] - p[j][1]) ** 2
            d[dist] = d.get(dist, 0) + 1
    if len(d) != 2:
        return 0
    a, b = sorted(d)
    return 2 * a == b and d[a] == 4 and d[b] == 2


cos, sin, variants = [1, 0, -1, 0], [0, 1, 0, -1], [[x, y, z, a] for x in range(4) for y in range(4) for z in range(4) for a in range(4)]
for t in range(int(input())):
    moles, ans = [list(map(int, input().split())) for x in range(4)], 13
    for a in variants:
        if check([f(moles[i][0], moles[i][1], moles[i][2], moles[i][3], a[i]) for i in range(4)]):
            ans = min(ans, sum(a))
    print(ans if ans != 13 else -1)


from sys import stdin
from itertools import product

lines = list([_f for _f in stdin.read().split('\n') if _f])

def parseline(line):
	return list(map(int, line.split()))

lines = list(map(parseline, lines))
n = lines[0][0]
assert len(lines) >= 4 * n + 1

class Unit:
	def __init__(self, x, y, a, b):
		self.pos = x, y
		self.home = a, b

def to_unit(l):
	return Unit(l[0], l[1], l[2], l[3])

def squads():
	for i in range(n):
		yield list(map(to_unit, lines[4*i+1:4*i+5]))

def is_90_degree(a, b, c, d):
	return 0 == sum((bi - ai) * (di - ci) for ai,bi,ci,di in zip(a,b,c,d))

def dist_sqr(a, b):
	return (b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2

def is_square(points):
	a, b, c, d = points
	if a == b == c == d:
		return False
	if not is_90_degree(a, b, c, d):
		b, d = d, b
		if not is_90_degree(a, b, c, d):
			a, d = d, a
			if not is_90_degree(a, b, c, d):
				return False
	if not (c[0] + d[0] == b[0] + a[0] and c[1] + d[1] == b[1] + a[1]):
		return False
	if not (dist_sqr(a, b) == dist_sqr(c, d)):
		return False
	return True

def rotate(point, pole, angle):
	result = point
	for i in range(angle):
		result = -result[1]+pole[1]+pole[0], result[0]-pole[0]+pole[1] 
	return result

def rotate_squad(squad, rotation):
	return [rotate(unit.pos, unit.home, angle) for unit, angle in zip(squad, rotation)]

rotations = list(product([0, 1, 2, 3], repeat=4))

INF = 10000000

for squad in squads():
	min_rotations = INF
	for rotation in rotations:
		if is_square(rotate_squad(squad, rotation)):
			min_rotations = min(min_rotations, sum(rotation))
	if min_rotations == INF:
		min_rotations = -1
	print(min_rotations)


def rotate(p) :
    return [-p[1]+p[2]+p[3], -p[2]+p[3]+p[0],p[2],p[3]]
def isS(P) :
    X = [P[0][0]*4,P[1][0]*4,P[2][0]*4,P[3][0]*4]
    Y = [P[0][1]*4,P[1][1]*4,P[2][1]*4,P[3][1]*4]
    p = sum(X)//4
    q = sum(Y)//4
    r = [(X[i]-p)**2 + (Y[i]-q)**2 for i in range(4)]
    if len(set(r)) != 1 :
        return False
    for i in range(4) :
        for j in range(i+1,4) :
            if X[i] == X[j] and Y[i] == Y[j] :
                return False

    for i in range(4) :
        for j in range(i+1,4) :
            k,l = list(set([0,1,2,3])-set([i,j]))
            t1 = (X[i] - X[k])**2 + (Y[i]-Y[k])**2
            t2 = (X[i] - X[l])**2 + (Y[i]-Y[l])**2
            t3 = (X[j] - X[k])**2 + (Y[j]-Y[k])**2
            t4 = (X[j] - X[l])**2 + (Y[j]-Y[l])**2
            if t1 == t2 == t3 == t4 : return True
    return False

N = int(input())
for i in range(N) :
    res = []
    P = []
    for j in range(4) :
        p = list(map(int,input().split(' ')))
        P.append(p)
    
    for k1 in range(4) :
        for k2 in range(4) :
            for k3 in range(4) :
                for k4 in range(4) :
                    if isS(P) : res.append(k1+k2+k3+k4)
                    P[3] = rotate(P[3])
                P[2] = rotate(P[2])
            P[1] = rotate(P[1])
        P[0] = rotate(P[0])
    res = min(res) if res else -1
    print(res)



3

from itertools import product, combinations


def process(case):
    regiment = map(rotate, homes, positions, case)
    return sum(case) if is_square(regiment) else float('inf')


def is_square(regiment):
    dists = list(map(lambda arg: dist_sq(*arg), combinations(regiment, 2)))
    dists.sort()
    return 0 < dists[0] == dists[3] < dists[4] == dists[5]


def dist_sq(p1, p2):
    return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2


def rotate(home, position, step):
    (x, y), (a, b) = position, home
    if step == 0:
        return x, y
    if step == 1:
        return a-(y-b), b+(x-a)
    if step == 2:
        return a-(x-a), b-(y-b)
    if step == 3:
        return a+(y-b), b-(x-a)

n = int(input())
for r in range(n):
    positions = []
    homes = []
    for i in range(4):
        x, y, a, b = map(int, input().split())
        positions.append((x, y))
        homes.append((a, b))
    all_cases = product(range(4), repeat=4)
    result = min(map(process, all_cases))
    print(result if result != float('inf') else -1)

def rotate(i, j):    
    x, y, p, q = a[i] 
    for k in range(1, j+1):
        x, y = p - (y - q), q + (x - p)
    c[i] = x, y

def dist(i, j):
    x, y = c[i]
    p, q = c[j]
    return (x - p) ** 2 + (y - q) ** 2

def check(move):
    for i in range(4):      
        rotate(i, move[i])
    p = [1,2,3] 
    p.sort(key = lambda x: dist(0, x))
    d = [dist(0, x) for x in p]
    if d[0] == d[1] == d[2] / 2 and d[2] > 0:
        return True if dist(p[0], p[1]) == d[2] else False
    else:
        return False

n = int(input())
a = [0] * 4
c = [0] * 4
for i in range(n):
    a[0] = list(map(int, input().split()))
    a[1] = list(map(int, input().split()))
    a[2] = list(map(int, input().split()))
    a[3] = list(map(int, input().split()))
    ans = 17
    for k in range(4):
        for l in range(4):
            for p in range(4):
                for q in range(4):
                    if k + l + p + q < ans and check([k, l, p, q]):
                        ans = k + l + p + q
    print(ans if ans != 17 else -1)


def rotate(p) :
    return [-p[1]+p[2]+p[3], -p[2]+p[3]+p[0],p[2],p[3]]
def isS(P) :
    X = [P[0][0]*4,P[1][0]*4,P[2][0]*4,P[3][0]*4]
    Y = [P[0][1]*4,P[1][1]*4,P[2][1]*4,P[3][1]*4]
    p = sum(X)//4
    q = sum(Y)//4
    r = [(X[i]-p)**2 + (Y[i]-q)**2 for i in range(4)]
    if len(set(r)) != 1 :
        return False
    for i in range(4) :
        for j in range(i+1,4) :
            if X[i] == X[j] and Y[i] == Y[j] :
                return False

    for i in range(4) :
        for j in range(i+1,4) :
            k,l = list(set([0,1,2,3])-set([i,j]))
            t1 = (X[i] - X[k])**2 + (Y[i]-Y[k])**2
            t2 = (X[i] - X[l])**2 + (Y[i]-Y[l])**2
            t3 = (X[j] - X[k])**2 + (Y[j]-Y[k])**2
            t4 = (X[j] - X[l])**2 + (Y[j]-Y[l])**2
            if t1 == t2 == t3 == t4 : return True
    return False

N = int(input())
for i in range(N) :
    res = []
    P = []
    for j in range(4) :
        p = list(map(int,input().split(' ')))
        P.append(p)
    
    for k1 in range(4) :
        for k2 in range(4) :
            for k3 in range(4) :
                for k4 in range(4) :
                    if isS(P) : res.append(k1+k2+k3+k4)
                    P[3] = rotate(P[3])
                P[2] = rotate(P[2])
            P[1] = rotate(P[1])
        P[0] = rotate(P[0])
    res = min(res) if res else -1
    print(res)

import copy
import sys

def solve():
    n = int(input())
    for reg in range(n):
        temp = help()
        print(-1 if temp == 1000 else temp)

def help():
    res = 1000
    read = [list(map(int, input().split())) for _ in range(4)]
    for _1 in range(4):
        for _2 in range(4):
            for _3 in range(4):
                for _4 in range(4):
                    l = copy.deepcopy(read)
                    for i in range(_1): rot(l[0])
                    for i in range(_2): rot(l[1])
                    for i in range(_3): rot(l[2])
                    for i in range(_4): rot(l[3])
                    if square(l):
                        res = min(res, _1 + _2 + _3 + _4)
    return res

def rot(l):
    x = l[0]
    y = l[1]
    homex = l[2]
    homey = l[3]
    yabove = y - homey
    xright = x - homex
    newx = homex - yabove
    newy = homey + xright
    l[0] = newx
    l[1] = newy
    return l

def square(l):
    distances = list()
    for i in range(4):
        for j in range(i + 1, 4):
            distances.append(dist(l[i], l[j]))
    distances.sort()
    if distances[0] < 0.000001: return False #same point
    different = 0
    for i in range(len(distances) - 1):
        if abs(distances[i] - distances[i+1]) > 0.000001:
            different += 1
    return different == 1

def dist(a, b):
    return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1])

if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
# print(rot(rot(rot(rot([-11, -22, 2, 3])))))
solve()

import copy
import sys

def solve():
    n = int(input())
    for reg in range(n):
        temp = help()
        print(-1 if temp == 1000 else temp)

def help():
    res = 1000
    l = [list(map(int, input().split())) for _ in range(4)]
    for _1 in range(4):
        for _2 in range(4):
            for _3 in range(4):
                for _4 in range(4):
                    for i in range(_1): rot(l[0])
                    for i in range(_2): rot(l[1])
                    for i in range(_3): rot(l[2])
                    for i in range(_4): rot(l[3])
                    if square(l):
                        res = min(res, _1 + _2 + _3 + _4)
                    for i in range(4-_1): rot(l[0])
                    for i in range(4-_2): rot(l[1])
                    for i in range(4-_3): rot(l[2])
                    for i in range(4-_4): rot(l[3])
    return res

def rot(l):
    x = l[0]
    y = l[1]
    homex = l[2]
    homey = l[3]
    yabove = y - homey
    xright = x - homex
    newx = homex - yabove
    newy = homey + xright
    l[0] = newx
    l[1] = newy
    return l

def square(l):
    distances = list()
    for i in range(4):
        for j in range(i + 1, 4):
            distances.append(dist(l[i], l[j]))
    distances.sort()
    if distances[0] < 0.000001: return False #same point
    different = 0
    for i in range(len(distances) - 1):
        if abs(distances[i] - distances[i+1]) > 0.000001:
            different += 1
    return different == 1

def dist(a, b):
    return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1])

if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
# print(rot(rot(rot(rot([-11, -22, 2, 3])))))
solve()

import copy
import sys

def solve():
    n = int(input())
    for reg in range(n):
        temp = help()
        print(-1 if temp == 1000 else temp)

def help():
    res = 1000
    l = [list(map(int, input().split())) for _ in range(4)]
    for _1 in range(4):
        for _2 in range(4):
            for _3 in range(4):
                for _4 in range(4):
                    for i in range(_1): rot(l[0])
                    for i in range(_2): rot(l[1])
                    for i in range(_3): rot(l[2])
                    for i in range(_4): rot(l[3])
                    if square(l):
                        res = min(res, _1 + _2 + _3 + _4)
                    for i in range(4-_1): rot(l[0])
                    for i in range(4-_2): rot(l[1])
                    for i in range(4-_3): rot(l[2])
                    for i in range(4-_4): rot(l[3])
    return res

def rot(l):
    x = l[0]
    y = l[1]
    homex = l[2]
    homey = l[3]
    yabove = y - homey
    xright = x - homex
    newx = homex - yabove
    newy = homey + xright
    l[0] = newx
    l[1] = newy
    return l

def square(l):
    distances = list()
    for i in range(4):
        for j in range(i + 1, 4):
            distances.append(dist(l[i], l[j]))
    distances.sort()
    if distances[0] < 0.000001: return False #same point
    different = 0
    for i in range(len(distances) - 1):
        if abs(distances[i] - distances[i+1]) > 0.000001:
            different += 1
    return different == 1

def dist(a, b):
    return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1])

if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
# print(rot(rot(rot(rot([-11, -22, 2, 3])))))
solve()

def f(x, y, a, b, n):
    return a + (x - a) * cos[n] - (y - b) * sin[n], b + (x - a) * sin[n] + (y - b) * cos[n]


def check(p):
    d = {}
    for i in range(len(p) - 1):
        for j in range(i + 1, len(p)):
            dist = (p[i][0] - p[j][0]) ** 2 + (p[i][1] - p[j][1]) ** 2
            d[dist] = d.get(dist, 0) + 1
    if len(d) != 2:
        return 0
    a, b = sorted(d)
    return 2 * a == b and d[a] == 4 and d[b] == 2


cos, sin, variants = [1, 0, -1, 0], [0, 1, 0, -1], [[x, y, z, a] for x in range(4) for y in range(4) for z in range(4) for a in range(4)]
for t in range(int(input())):
    moles, ans = [list(map(int, input().split())) for x in range(4)], 13
    for a in variants:
        if check([f(moles[i][0], moles[i][1], moles[i][2], moles[i][3], a[i]) for i in range(4)]):
            ans = min(ans, sum(a))
    print(ans if ans != 13 else -1)

def siblings(initial, root):
    th = (initial[0] - root[0], initial[1] - root[1])
    return [(root[0] + th[0], root[1] + th[1]),
            (root[0] -th[1], root[1] + th[0]),
            (root[0] - th[0], root[1] - th[1]),
            (root[0] + th[1], root[1] - th[0])]
def dist(x, y):
    return (x[0]-y[0])**2 + (x[1] - y[1])**2

def isSquare(p1,p2,p3,p4):
    d2 = dist(p1, p2)
    d3 = dist(p1, p3)
    d4 = dist(p1, p4)

    u = {p1, p2, p3, p4}
    if (len(u) != 4):
        return 0

    if (d2==d3 and 2*d2 == d4):
        d = dist(p2, p4)
        return (d == dist(p3,p4) and d == d2)

    if (d3==d4 and 2*d3 == d2):
        d = dist(p2, p3)
        return (d == dist(p2,p4) and d == d3)

    if (d2==d4 and 2*d2 == d3):
        d = dist(p2, p3)
        return (d == dist(p3,p4) and d == d2)

    return False

def distOri(x, y, root):
    th1 = (y[0] - root[0], y[1] - root[1])
    th2 = (x[0] - root[0], x[1] - root[1])
    if th1[0]==th2[0] and th1[1] == th2[1]:
        return 0
    if (th2[0] ==  -th1[1] and th2[1] == th1[0]):
        return 1
    if (th2[0] == - th1[0] and th2[1] == -th1[1]):
        return 2
    if (th2[0] == th1[1] and th2[1] == -th1[0]):
        return 3
    
n = int(input())

for i in range(n):
    initial = []
    root = []

    _max = 1000
    for j in range(4):
        x,y,a,b = [int(k) for k in input().split()]
        initial.append((x,y))
        root.append((a,b))

        
    for x1 in siblings(initial[0], root[0]):
        for x2 in siblings(initial[1], root[1]):
            for x3 in siblings(initial[2], root[2]):
                for x4 in siblings(initial[3], root[3]):
                    if isSquare(x1, x2, x3, x4):
                       #print(x1, x2, x3, x4)
                        _max = min(_max,
                                    distOri(x1, initial[0], root[0]) + distOri(x2, initial[1], root[1]) + \
                                    distOri(x3, initial[2], root[2]) + distOri(x4, initial[3], root[3])) 
    if (_max == 1000):
        print(-1)
    else:
        print(_max)
                            
                            
                        
            


def siblings(initial, root):
    th = (initial[0] - root[0], initial[1] - root[1])
    return [(root[0] + th[0], root[1] + th[1]),
            (root[0] -th[1], root[1] + th[0]),
            (root[0] - th[0], root[1] - th[1]),
            (root[0] + th[1], root[1] - th[0])]
def dist(x, y):
    return (x[0]-y[0])**2 + (x[1] - y[1])**2

def isSquare(p1,p2,p3,p4):
    d2 = dist(p1, p2)
    d3 = dist(p1, p3)
    d4 = dist(p1, p4)

    u = {p1, p2, p3, p4}
    if (len(u) != 4):
        return 0

    if (d2==d3 and 2*d2 == d4):
        d = dist(p2, p4)
        return (d == dist(p3,p4) and d == d2)

    if (d3==d4 and 2*d3 == d2):
        d = dist(p2, p3)
        return (d == dist(p2,p4) and d == d3)

    if (d2==d4 and 2*d2 == d3):
        d = dist(p2, p3)
        return (d == dist(p3,p4) and d == d2)

    return False

def distOri(x, y, root):
    th1 = (y[0] - root[0], y[1] - root[1])
    th2 = (x[0] - root[0], x[1] - root[1])
    if th1[0]==th2[0] and th1[1] == th2[1]:
        return 0
    if (th2[0] ==  -th1[1] and th2[1] == th1[0]):
        return 1
    if (th2[0] == - th1[0] and th2[1] == -th1[1]):
        return 2
    if (th2[0] == th1[1] and th2[1] == -th1[0]):
        return 3
    
n = int(input())

for i in range(n):
    initial = []
    root = []

    _min = 1000
    for j in range(4):
        x,y,a,b = [int(k) for k in input().split()]
        initial.append((x,y))
        root.append((a,b))

        
    for x1 in siblings(initial[0], root[0]):
        for x2 in siblings(initial[1], root[1]):
            for x3 in siblings(initial[2], root[2]):
                for x4 in siblings(initial[3], root[3]):
                    if isSquare(x1, x2, x3, x4):
                       #print(x1, x2, x3, x4)
                        _min = min(_min,
                                    distOri(x1, initial[0], root[0]) + distOri(x2, initial[1], root[1]) + \
                                    distOri(x3, initial[2], root[2]) + distOri(x4, initial[3], root[3])) 
    if (_min == 1000):
        print(-1)
    else:
        print(_min)
                            
                            
                        
            



def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)

def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = range(4)
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
h = [(i, j) for i in f for j in f if i < j]
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in f]
    s = 13
    for a in f:
        for b in f:
            for c in f:
                for d in f:
                    t = sorted(g(p[u], p[v]) for u, v in h)
                    if 0 != t[4] == t[5] == 2 * t[0] == 2 * t[3]: s = min(s, a + b + c + d)
                    r(p[3])
                r(p[2])
            r(p[1])
        r(p[0])
    print(s if s < 13 else -1)

def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)


def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)


def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)


def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)


def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)


def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)


def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)


def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2]
f = [(0, 0), (1, 3), (2, 15), (3, 63)]
h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2
for i in range(int(input())):
    p = [list(map(int, input().split())) for j in range(4)]
    s = 13
    for q in range(256):
        t = sum((q >> k) & 3 for k in (0, 2, 4, 6))
        d = sorted(g(p[u], p[v]) for u, v in h)
        if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t)
        for i, k in f:
            if q & k == k: r(p[i])
    print(s if s < 13 else -1)


def f(x, y, a, b, n):
    return a + (x - a) * cos[n] - (y - b) * sin[n], b + (x - a) * sin[n] + (y - b) * cos[n]


def check(p):
    d = {}
    for i in range(len(p) - 1):
        for j in range(i + 1, len(p)):
            dist = (p[i][0] - p[j][0]) ** 2 + (p[i][1] - p[j][1]) ** 2
            d[dist] = d.get(dist, 0) + 1
    if len(d) != 2:
        return 0
    a, b = sorted(d)
    return 2 * a == b and d[a] == 4 and d[b] == 2


cos, sin, variants = [1, 0, -1, 0], [0, 1, 0, -1], [[x, y, z, a] for x in range(4) for y in range(4) for z in range(4) for a in range(4)]
for t in range(int(input())):
    moles, ans = [list(map(int, input().split())) for x in range(4)], 13
    for a in variants:
        if check([f(moles[i][0], moles[i][1], moles[i][2], moles[i][3], a[i]) for i in range(4)]):
            ans = min(ans, sum(a))
    print(ans if ans != 13 else -1)

def f(x, y, a, b, n):

    return a + (x - a) * cos[n] - (y - b) * sin[n], b + (x - a) * sin[n] + (y - b) * cos[n]





def check(p):

    d = {}

    for i in range(len(p) - 1):

        for j in range(i + 1, len(p)):

            dist = (p[i][0] - p[j][0]) ** 2 + (p[i][1] - p[j][1]) ** 2

            d[dist] = d.get(dist, 0) + 1

    if len(d) != 2:

        return 0

    a, b = sorted(d)

    return 2 * a == b and d[a] == 4 and d[b] == 2





cos, sin, variants = [1, 0, -1, 0], [0, 1, 0, -1], [[x, y, z, a] for x in range(4) for y in range(4) for z in range(4) for a in range(4)]

for t in range(int(input())):

    moles, ans = [list(map(int, input().split())) for x in range(4)], 13

    for a in variants:

        if check([f(moles[i][0], moles[i][1], moles[i][2], moles[i][3], a[i]) for i in range(4)]):

            ans = min(ans, sum(a))

    print(ans if ans != 13 else -1)



# Made By Mostafa_Khaled