There are $n$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $s$, where the $i$-th candle is lit if and only if $s_i=1$.

Initially, the candle lights are described by a string $a$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit).

You would like to make the candles look the same as string $b$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required.


-----Input-----

The first line contains an integer $t$ ($1\le t\le 10^4$) — the number of test cases. Then $t$ cases follow.

The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$) — the number of candles.

The second line contains a string $a$ of length $n$ consisting of symbols 0 and 1 — the initial pattern of lights.

The third line contains a string $b$ of length $n$ consisting of symbols 0 and 1 — the desired pattern of lights.

It is guaranteed that the sum of $n$ does not exceed $10^5$.


-----Output-----

For each test case, output the minimum number of operations required to transform $a$ to $b$, or $-1$ if it's impossible.


-----Examples-----

Input
5
5
11010
11010
2
01
11
3
000
101
9
100010111
101101100
9
001011011
011010101
Output
0
1
-1
3
4


-----Note-----

In the first test case, the two strings are already equal, so we don't have to perform any operations.

In the second test case, we can perform a single operation selecting the second candle to transform $01$ into $11$.

In the third test case, it's impossible to perform any operations because there are no lit candles to select.

In the fourth test case, we can perform the following operations to transform $a$ into $b$:

Select the $7$-th candle: $100010{1}11\to 011101{ 1}00$.

Select the $2$-nd candle: $0{ 1}1101100\to 1{ 1}0010011$.

Select the $1$-st candle: ${1}10010011\to {1}01101100$.

In the fifth test case, we can perform the following operations to transform $a$ into $b$:

Select the $6$-th candle: $00101{1}011\to 11010{1}100$

Select the $2$-nd candle: $1{1}0101100\to 0{1}1010011$

Select the $8$-th candle: $0110100{1}1\to 1001011{1}0$

Select the $7$-th candle: $100101{1}10\to 011010{1}01$

Difficulty: MEDIUM_HARD
Tags: Complete search

Graph algorithms

Mathematics

Greedy algorithms
Skill types: Complete search

Greedy algorithms

Starter code:

Solutions:
import sys

def menorah(n, a, b):
	_01 = 0
	_00 = 0
	_10 = 0
	_11 = 0
	for i in range(n):
		if a[i] == '0':
			if b[i] == '0':
				_00 += 1
			else:
				_01 += 1
		elif b[i] == '1':
			_11 += 1
		else:
			_10 += 1
	best = float('inf')
	if _01 == _10:
		best = min(best, _01 + _10)
	if _11 > 0:
		__01 = _11 - 1
		__00 = _10
		__10 = _00
		__11 = _01 + 1
		if __01 == __10:
			best = min(best, 1 + __01 + __10)
	if best == float('inf'):
		best = -1
	return best
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	sys.stdout.write(f'{menorah(n, a, b)}\n')


import sys, random
raw_input = sys.stdin.readline

def slove(a, b):
	n = len(a)

	def reverse_pair(a, b):
		ret = 0
		for i in range(n):
			if a[i] != b[i]:
				ret += 1
		return ret

	def countOdd(a, b):

		def lit(s, i):
			ret = list(map(int, list(s)))
			for j in range(n):
				if j != i:
					ret[j] ^= 1
			return ''.join(map(str, ret))
		ret = 1000000
		cases = [0, 0]
		for i in range(n):
			if sum(cases) == 2:
				break
			if a[i] == '1':
				if b[i] == '0':
					cases[0] = 1
					ret = min(ret, 1 + reverse_pair(lit(a, i), b))
				else:
					cases[1] = 1
					ret = min(ret, 1 + reverse_pair(lit(a, i), b))
		return ret
	res = 10000000
	count_a = a.count('1')
	count_b = b.count('1')
	if count_a == count_b:
		res = reverse_pair(a, b)
	if n - count_a + 1 == count_b:
		res = min(res, countOdd(a, b))
	return res if res != 10000000 else -1
for _ in range(int(raw_input())):
	raw_input()
	a = raw_input().strip()
	b = raw_input().strip()
	print(slove(a, b))


t = int(input())
for i in range(t):
	kifkif = 0
	moch_kif = 0
	nbr1_kif = 0
	nbr1_moch = 0
	n = int(input())
	a = input()
	b = input()
	for j in range(len(a)):
		if a[j] == b[j]:
			kifkif += 1
			if a[j] == '1':
				nbr1_kif += 1
		elif a[j] == '1':
			nbr1_moch += 1
	moch_kif = n - kifkif
	if kifkif % 2 == 0 or nbr1_kif != int(kifkif / 2) + 1:
		kifkif = -1
	if moch_kif % 2 != 0 or nbr1_moch != int(moch_kif / 2):
		moch_kif = -1
	if kifkif != -1 and moch_kif != -1:
		print(min(moch_kif, kifkif))
	elif kifkif != -1:
		print(kifkif)
	elif moch_kif != -1:
		print(moch_kif)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	x = y = x1 = y1 = 0
	for i in range(n):
		if a[i] == b[i]:
			x += 1
			if a[i] == '1':
				x1 += 1
		else:
			y += 1
			if a[i] == '1':
				y1 += 1
	ans = float('inf')
	if x % 2 and (x + 1) // 2 == x1:
		ans = x
	if y % 2 == 0 and y // 2 == y1:
		ans = min(ans, y)
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)


def getint():
	return [int(i) for i in input().split()]

def get():
	return int(input())

def getstr():
	return [i for i in input().split()]

def S():
	for test in range(int(input())):
		solve()
import math
import itertools as it
import bisect
import time
import collections as ct

def lower_bound(a, x):
	l = -1
	r = len(a)
	while l + 1 != r:
		mid = l + r >> 1
		if a[mid] < x:
			l = mid
		else:
			r = mid
	return r

def upper_bound(a, x):
	l = -1
	r = len(a)
	while l + 1 != r:
		mid = l + r >> 1
		if a[mid] <= x:
			l = mid
		else:
			r = mid
	return r

def solve():
	n = get()
	s1 = input()
	s2 = input()
	ans = 1000000000.0
	c1 = s1.count('1')
	c2 = s2.count('1')
	if c1 == c2:
		cnt = 0
		for i in range(n):
			if s1[i] != s2[i]:
				cnt += 1
		ans = min(ans, cnt)
	if c1 + c2 == n + 1:
		cnt = 1
		flag = 0
		for i in range(n):
			if s1[i] == '1' and s2[i] == '1':
				flag = i
		for i in range(n):
			if i != flag:
				if s1[i] == '1':
					s1 = s1[:i] + '0' + s1[i + 1:]
				else:
					s1 = s1[:i] + '1' + s1[i + 1:]
		for i in range(n):
			if s1[i] != s2[i]:
				cnt += 1
		ans = min(ans, cnt)
	if ans == 1000000000.0:
		print(-1)
	else:
		print(ans)
S()


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = input()
	uu = 0
	ll = 0
	ul = 0
	lu = 0
	for j in range(n):
		if a[j] == '1' and b[j] == '1':
			ll += 1
		elif a[j] == '1' and b[j] == '0':
			lu += 1
		elif a[j] == '0' and b[j] == '1':
			ul += 1
		elif a[j] == '0' and b[j] == '0':
			uu += 1
	ans1 = n
	ans2 = n
	c = 0
	if ul == lu:
		ans1 = 2 * ul
		c += 1
	if uu == ll - 1:
		ans2 = 2 * uu + 1
		c += 1
	if c == 0:
		print(-1)
	else:
		print(min(ans1, ans2))


def operation(a, b):
	(c1, c2) = (0, 0)
	for (i, j) in zip(a, b):
		if i != j:
			if i == '1':
				c1 += 1
			else:
				c2 += 1
	if c1 != c2:
		return float('inf')
	else:
		return 2 * c1

def change(a, b, f):
	idx = -1
	for i in range(len(a)):
		if a[i] == '1' and b[i] == f:
			idx = i
	if idx == -1:
		return float('inf')
	else:
		l = ''
		for i in range(len(a)):
			if i != idx:
				if a[i] == '1':
					l += '0'
				else:
					l += '1'
			else:
				l += a[i]
		return operation(l, b)
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	t = list(input())
	if s == t:
		print(0)
		continue
	else:
		ans = operation(s, t)
		ans = min(ans, 1 + change(s, t, '1'))
		ans = min(ans, 1 + change(s, t, '0'))
		if ans == float('inf'):
			print(-1)
		else:
			print(ans)


from copy import copy

def stepm(m, u, i):
	if i == 0:
		m1 = [u[1] + 1, u[0]]
		u1 = [m[1], m[0] - 1]
	else:
		m1 = [u[1], u[0] - 1]
		u1 = [m[1] + 1, m[0]]
	return (m1, u1)

def solve(n, s, b):
	m = [0, 0]
	u = [0, 0]
	for (id, i) in enumerate(s):
		if s[id] == b[id]:
			if s[id] == '1':
				m[0] += 1
			else:
				m[1] += 1
		elif s[id] == '1':
			u[0] += 1
		else:
			u[1] += 1
	m1 = copy(m)
	u1 = copy(u)
	ans1 = 0
	for i in range(sum(m)):
		if sum(u) == 0:
			break
		if i % 2 == 0:
			if m[0] == 0:
				ans1 = -1
				break
		elif u[0] == 0:
			ans1 = -1
			break
		(m, u) = stepm(m, u, i % 2)
		ans1 += 1
	if sum(m) == 0:
		ans1 = -1
	ans2 = 0
	for i in range(sum(u1)):
		if sum(u1) == 0:
			break
		if (i + 1) % 2 == 0:
			if m1[0] == 0:
				ans2 = -1
				break
		elif u1[0] == 0:
			ans2 = -1
			break
		(m1, u1) = stepm(m1, u1, (i + 1) % 2)
		ans2 += 1
	if sum(m1) == 0:
		ans2 = -1
	if ans1 != -1 and ans2 != -1:
		return min(ans1, ans2)
	else:
		return max(ans1, ans2)
t = int(input())
for i in range(t):
	n = int(input())
	s = input().rstrip()
	b = input().rstrip()
	ans = solve(n, s, b)
	print(ans)


def sumString(x):
	ans = 0
	for i in x:
		ans += int(i)
	return ans

def solve(a, b):
	if a == b:
		return 0
	suma = sumString(a)
	sumb = sumString(b)
	if suma == 0 or sumb == 0:
		return -1
	eveonOrOdd = []
	evens = 0
	odds = 0
	zeroEvens = 0
	oneEvens = 0
	zeroOdds = 0
	oneOdds = 0
	for i in range(len(a)):
		if a[i] == b[i]:
			eveonOrOdd.append('1')
			evens += 1
			if a[i] == '1':
				oneEvens += 1
			else:
				zeroEvens += 1
		else:
			eveonOrOdd.append('0')
			odds += 1
			if a[i] == '1':
				oneOdds += 1
			else:
				zeroOdds += 1
	ans = []
	if evens % 2 == 1:
		if oneEvens - zeroEvens == 1:
			ans.append(evens)
	if odds % 2 == 0:
		if zeroOdds == oneOdds:
			ans.append(odds)
	if ans:
		return min(ans)
	return -1
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	print(solve(a, b))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	r = a.count('1')
	l = a.count('0')
	rr = 0
	ll = 0
	for i in range(n):
		if a[i] != b[i]:
			if a[i] == '1':
				rr += 1
			else:
				ll += 1
	m = 0
	if ll == rr:
		m = ll * 2
	else:
		m = float('inf')
	rr = r - rr
	ll = l - ll
	if rr and ll + 1 == rr:
		m = min(m, ll * 2 + 1)
	if m == float('inf'):
		m = -1
	print(m)


import sys
from collections import deque

def BFS(candles, target):
	num_00 = num_01 = num_10 = num_11 = 0
	for (a, b) in zip(candles, target):
		if a == '0' and b == '0':
			num_00 += 1
		elif a == '0' and b == '1':
			num_01 += 1
		elif a == '1' and b == '0':
			num_10 += 1
		else:
			num_11 += 1
	state = (num_00, num_01, num_10, num_11)
	seen = {state}
	frontier = deque([(state, 0)])
	while frontier:
		((num_00, num_01, num_10, num_11), num_flips) = frontier.popleft()
		if num_01 == num_10 == 0:
			return num_flips
		if num_10:
			new_state = (num_10 - 1, num_11, 1 + num_00, num_01)
			if new_state not in seen:
				frontier.append((new_state, num_flips + 1))
				seen.add(new_state)
		if num_11:
			new_state = (num_10, num_11 - 1, num_00, 1 + num_01)
			if new_state not in seen:
				frontier.append((new_state, num_flips + 1))
				seen.add(new_state)
	return -1
input = sys.stdin.readline
num_test_cases = int(input())
for test_case_ittr in range(num_test_cases):
	n = int(input())
	candles = input().strip()
	target = input().strip()
	ans = BFS(candles, target)
	print(ans)


t = int(input())
while t:
	n = int(input())
	a = input()
	b = input()
	(f00, f01, f10, f11) = (0, 0, 0, 0)
	res = 0
	for i in range(n):
		if a[i] == '0' and b[i] == '0':
			f00 += 1
		elif a[i] == '0' and b[i] == '1':
			f01 += 1
		elif a[i] == '1' and b[i] == '0':
			f10 += 1
		else:
			f11 += 1
	res = 100000000.0 + 1
	if f01 == f10:
		res = min(res, 2 * f01)
	if f00 == f11 - 1:
		res = min(res, 1 + 2 * f00)
	if res > 100000000.0:
		res = -1
	print(res)
	t -= 1


import math
cases = int(input())
for i in range(cases):
	lenght = int(input())
	from_str = input()
	to_str = input()
	matrix = [[0, 0], [0, 0]]
	for j in range(lenght):
		matrix[int(from_str[j])][int(to_str[j])] += 1
	ans = math.inf
	if matrix[1][0] == matrix[0][1]:
		ans = 2 * matrix[1][0]
	if matrix[1][1] == matrix[0][0] + 1:
		ans = min(ans, 2 * matrix[0][0] + 1)
	if ans == math.inf:
		print(-1)
	else:
		print(ans)


import bisect
mod = 10 ** 9 + 7
eps = 10 ** (-9)

def __gcd(a, b):
	return a if b == 0 else __gcd(b, a % b)

def __lcm(a, b):
	return a * b / __gcd(a, b)

def __fact(n):
	return 1 if n == 1 else n * __fact(n - 1)

def __mex(a):
	mex = 0
	a.sort()
	for x in a:
		if x <= mex:
			mex += 1
		else:
			break
	return mex

def __dist(x1, y1, x2, y2):
	return (x1 - x2) ** 2 + (y1 - y2) ** 2

def __getprimes(n):
	isprime = [True for i in range(n + 1)]
	primes = []
	for i in range(2, n + 1):
		if isprime[i]:
			for j in range(i * i, n + 1, i):
				isprime[j] = False
	for i in range(2, n + 1):
		if isprime[i]:
			primes.append(i)
	return primes

def __modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x

def __isprime(n):
	if n < 2:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def __cntprimediv(n):
	ret = 0
	x = n
	i = 2
	while i * i <= x:
		while n % i == 0:
			n //= i
			ret += 1
		i += 1
	if n > 1:
		ret += 1
	return ret

def __primefactors(n):
	ret = []
	x = n
	i = 2
	while i * i <= x:
		while n % i == 0:
			ret.append(i)
			n //= i
		i += 1
	if n > 1:
		ret.append(n)
	return ret

def __sumdigit(n):
	ret = 0
	while n > 0:
		ret += n % 10
		n //= 10
	return ret

def __zfunc(s):
	n = len(s)
	z = [0 for i in range(n)]
	l = 0
	r = 0
	for i in range(1, n):
		if r >= i:
			z[i] = min(z[i - l], r - i + 1)
		while z[i] + i < n and s[z[i]] == s[z[i] + i]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
	return z

def __to(n, x):
	ret = ''
	while n > 0:
		q = n % x
		if q < 10:
			ret += str(q)
		else:
			ret += chr(q - 10 + ord('a'))
		n //= x
	return ret[::-1]

def solve(t):
	n = int(input())
	ans = 0
	s = input()
	t = input()
	eq1 = 0
	eq0 = 0
	neq1 = 0
	neq0 = 0
	for i in range(n):
		if s[i] == t[i]:
			if s[i] == '1':
				eq1 += 1
			else:
				eq0 += 1
		elif s[i] == '1':
			neq1 += 1
		else:
			neq0 += 1
	ans = 10 ** 10
	if eq1 == eq0 + 1:
		ans = min(ans, eq1 + eq0)
	if neq1 == neq0:
		ans = min(ans, neq1 + neq0)
	if ans == 10 ** 10:
		ans = -1
	print(ans)
t = 1
t = int(input())
for i in range(t):
	solve(i + 1)


import sys
input = sys.stdin.readline
t = int(input())
inf = pow(10, 6) + 1
for _ in range(t):
	n = int(input())
	a = list(input().rstrip())
	b = list(input().rstrip())
	c = [0] * 4
	for i in range(n):
		if a[i] == b[i] == '0':
			c[0] += 1
		elif a[i] == b[i] == '1':
			c[3] += 1
		elif a[i] == '0' and b[i] == '1':
			c[1] += 1
		else:
			c[2] += 1
	ans = inf
	if c[3] - 1 == c[0]:
		ans = min(ans, c[0] + c[3])
	if c[2] == c[1]:
		ans = min(ans, c[1] + c[2])
	ans = (ans + 1) % (inf + 1) - 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	ans = float('+inf')
	b = list(map(int, input()))
	(cr, cd) = (0, 0)
	for i in range(n):
		if a[i] and (not b[i]):
			cr += 1
		elif b[i] and (not a[i]):
			cd += 1
	if cr == cd:
		ans = cr + cd
	(cr, cd) = (0, 0)
	for i in range(n):
		if not a[i] and (not b[i]):
			cr += 1
		elif b[i] and a[i]:
			cd += 1
	if cd == cr + 1:
		ans = min(ans, cd + cr)
	print(ans if ans <= n else -1)


from math import *
from bisect import *
import os
import sys
from io import BytesIO, IOBase
_str = str
str = lambda x=b'': x if type(x) is bytes else _str(x).encode()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def getSetBitsFromOneToN(N):
	global sussy
	two = 2
	n = N
	while n != 0:
		ans = 0
		ans += int(N / two) * (two >> 1)
		if N & two - 1 > (two >> 1) - 1:
			ans += (N & two - 1) - (two >> 1) + 1
		sussy.append(ans)
		two <<= 1
		n >>= 1

def dontgetSetBitsFromOneToN(N):
	global sussy
	two = 2
	n = N
	cur = 0
	while n != 0:
		ans = 0
		ans += int(N / two) * (two >> 1)
		if N & two - 1 > (two >> 1) - 1:
			ans += (N & two - 1) - (two >> 1) + 1
		sussy[cur] -= ans
		cur += 1
		two <<= 1
		n >>= 1
t = int(input())
for qqq in range(t):
	n = int(input())
	a = input()
	b = input()
	acount = 0
	bcount = 0
	for i in range(n):
		if a[i] == '1':
			acount += 1
		if b[i] == '1':
			bcount += 1
	ans = 0
	sim = 0
	if acount == n - acount + 1 and acount == bcount:
		for i in range(n):
			if a[i] == b[i]:
				sim += 1
		ans = n - sim
		sim = 0
		for i in range(n):
			if a[i] != b[i]:
				sim += 1
		sim += 1
		ans = min(ans, n - sim + 1)
		print(ans)
	elif acount == bcount:
		for i in range(n):
			if a[i] == b[i]:
				sim += 1
		print(n - sim)
	elif n - acount + 1 == bcount:
		for i in range(n):
			if a[i] != b[i]:
				sim += 1
		sim += 1
		print(n - sim + 1)
	else:
		print(-1)


from __future__ import print_function
from math import *
from collections import deque
import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	a = input()
	b = input()
	l = []
	s0 = 0
	s1 = 0
	a0 = 0
	a1 = 0
	for i in range(n):
		if a[i] == b[i]:
			if a[i] == '1':
				s1 += 1
			else:
				s0 += 1
		elif a[i] == '1':
			a1 += 1
		else:
			a0 += 1
	if (s1 + s0) % 2 == 1:
		if s1 - s0 == 1:
			l.append(s1 + s0)
	if (a1 + a0) % 2 == 0:
		if a1 == a0:
			l.append(a1 + a0)
	if len(l) == 0:
		print(-1)
	else:
		print(min(l))


from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	s1 = input()
	s2 = input()
	(a, b, c, d) = (0, 0, 0, 0)
	for i in range(n):
		if s1[i] == '1':
			if s2[i] == '0':
				a += 1
			else:
				c += 1
		elif s2[i] == '1':
			b += 1
		else:
			d += 1
	start = (a, b, c, d)
	if a == 0 and b == 0:
		print(0)
	else:
		solved = False
		visited = set([start])
		bfs = deque([start])
		dist = {start: 0}
		while bfs:
			now = bfs.popleft()
			if now[0] > 0:
				new = (now[3] + 1, now[2], now[1], now[0] - 1)
				if not new in visited:
					dist[new] = dist[now] + 1
					bfs.append(new)
					visited.add(new)
					if new[0] == 0 and new[1] == 0:
						print(dist[new])
						solved = True
						break
			if now[2] > 0:
				new = (now[3], now[2] - 1, now[1] + 1, now[0])
				if not new in visited:
					dist[new] = dist[now] + 1
					bfs.append(new)
					visited.add(new)
					if new[0] == 0 and new[1] == 0:
						print(dist[new])
						solved = True
						break
		if not solved:
			print(-1)


from sys import stdin, stdout
from collections import defaultdict
input = stdin.readline
from random import randint
import math
import sys
for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	L = [(int(b[i]), int(a[i])) for i in range(n)]
	(zz, oz, zo, oo) = (0, 0, 0, 0)
	for i in range(n):
		if L[i][0] == 0 and L[i][1] == 0:
			zz += 1
		if L[i][0] == 0 and L[i][1] == 1:
			oz += 1
		if L[i][0] == 1 and L[i][1] == 0:
			zo += 1
		if L[i][0] == 1 and L[i][1] == 1:
			oo += 1
	(same, dif) = ((zz, oo), (oz, zo))
	(s2, d2) = (same, dif)
	(oper, oper1) = (10000000000, 10000000000)
	for i in range(10 * n):
		if dif[0] == 0 and dif[1] == 0:
			oper = i
			break
		else:
			(zz, oo, oz, zo) = (same[0], same[1], dif[0], dif[1])
			if i % 2 == 0:
				(same, dif) = ((oz, zo + 1), (zz, oo - 1))
			else:
				(same, dif) = ((oz - 1, zo), (zz + 1, oo))
	(same, dif) = (s2, d2)
	(zz, oo, oz, zo) = (same[0], same[1], dif[0], dif[1])
	for i in range(10 * n):
		if dif[0] == 0 and dif[1] == 0:
			oper1 = i
			break
		else:
			(zz, oo, oz, zo) = (same[0], same[1], dif[0], dif[1])
			if i % 2 == 1:
				(same, dif) = ((oz, zo + 1), (zz, oo - 1))
			else:
				(same, dif) = ((oz - 1, zo), (zz + 1, oo))
	if min(oper, oper1) == 10000000000:
		print(-1)
	else:
		print(min(oper, oper1))


for i in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	(x, y, z) = (0, 0, 0)
	for i in range(n):
		if a[i] != b[i]:
			x += 1
		if a[i] == '1':
			y += 1
		if b[i] == '1':
			z += 1
	if y == z:
		if n - y + 1 == z:
			print(min(x, n - x))
		else:
			print(x)
	elif n - y + 1 == z:
		print(n - x)
	else:
		print(-1)


def f(a, b):
	(zo, oz) = (0, 0)
	for (x, y) in zip(a, b):
		if a == '0' and b == '1':
			zo += 1
		elif a == '1' and b == '0':
			oz += 1
	return (oz, zo)
for _ in range(int(input())):
	n = int(input())
	s = input()
	desire = input()
	ones_d = desire.count('1')
	zeros_d = n - ones_d
	(zo, oz) = (0, 0)
	(oz_index, oo_index) = (None, None)
	num_ones = 0
	i = 0
	for (x, y) in zip(s, desire):
		if x == '0' and y == '1':
			zo += 1
		elif x == '1':
			num_ones += 1
			if y == '0':
				oz += 1
				oz_index = i
			else:
				oo_index = i
		i += 1
	num_zeros = n - num_ones
	if not num_ones:
		if ones_d:
			print(-1)
		else:
			print(0)
		continue
	if ones_d not in (num_ones, num_zeros + 1):
		print(-1)
		continue
	diff1 = oz + zo
	diff2 = n - diff1 - 1 if oo_index is not None else n - diff1 + 1
	if diff2 < diff1:
		if num_zeros + 1 == ones_d:
			if oo_index is not None:
				(oz, zo) = (num_zeros - zo, num_ones - oz - 1)
			else:
				(oz, zo) = (num_zeros - zo + 1, num_ones - oz)
			print(1 + 2 * oz)
		else:
			print(2 * oz)
	elif num_ones == ones_d:
		print(2 * oz)
	else:
		if oo_index is not None:
			(oz, zo) = (num_zeros - zo, num_ones - oz - 1)
		else:
			(oz, zo) = (num_zeros - zo + 1, num_ones - oz)
		print(1 + 2 * oz)


from collections import deque

def make(s, i):
	if s[i] == 0:
		return None
	res = s.copy()
	for x in range(len(s)):
		if x != i:
			res[x] = 1 - res[x]
	return res
t = int(input())
for _ in range(t):
	k = int(input())
	s1 = list(map(int, list(input())))
	s2 = list(map(int, list(input())))
	res1 = 10 ** 9
	if sum(s1) == sum(s2):
		res = 0
		for i in range(k):
			if s1[i] != s2[i]:
				res += 1
		res1 = min(res1, res)
	if sum(s1) + sum(s2) == k + 1:
		res = 0
		besti = -1
		for i in range(k):
			if s1[i] == s2[i] == 1:
				besti = i
				break
			if s1[i] == 1:
				besti = i
		if besti == -1:
			res1 = 0
		else:
			s1 = make(s1, besti)
			for j in range(k):
				if s1[j] != s2[j]:
					res += 1
			res1 = min(res1, res + 1)
	print(res1 if res1 != 10 ** 9 else -1)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main(t):
	n = int(input())
	s1 = input().decode('utf-8')
	s2 = input().decode('utf-8')
	same1 = 0
	same0 = 0
	diff1 = 0
	diff0 = 0
	for i in range(n):
		if s1[i] == s2[i]:
			if s1[i] == '1':
				same1 += 1
			else:
				same0 += 1
		elif s1[i] == '1':
			diff1 += 1
		else:
			diff0 += 1
	ans = float('inf')
	if (diff0 + diff1) % 2 == 0:
		if diff0 == diff1:
			ans = min(ans, diff0 + diff1)
	if (same0 + same1) % 2 == 1:
		if same1 - same0 == 1:
			ans = min(ans, same0 + same1)
	if ans == float('inf'):
		ans = -1
	print(ans)
T = int(input())
t = 1
while t <= T:
	main(t)
	t += 1


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
from collections import deque, Counter, OrderedDict, defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return int(input())

def inps():
	return input().strip()

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def flip_bits(s, idx):
	t = []
	n = len(s)
	d = {'0': '1', '1': '0'}
	for i in range(n):
		if i != idx:
			t.append(d[s[i]])
		else:
			t.append(s[i])
	return t

def recur(temp, cnt):
	if temp == s2:
		count[0] = min(count[0], cnt)
		return cnt
	for i in range(len(temp)):
		if temp[i] == '1':
			v = flip_bits(temp, i)
			k = ''.join(v)
			if vis[k] > cnt + 1:
				vis[k] = cnt + 1
				recur(v, cnt + 1)
for _ in range(inp()):
	n = inp()
	s1 = insr()
	s2 = insr()
	(o1, z1) = (s1.count('1'), s1.count('0'))
	(o2, z2) = (s2.count('1'), s2.count('0'))
	if o1 == o2 or o1 == z2 + 1:
		(c1, c2) = (0, 0)
		count = [sys.maxsize]
		vis = defaultdict(lambda : sys.maxsize)
		for i in range(n):
			if s1[i] != s2[i]:
				c1 += 1
			else:
				c2 += 1
		if c2 == 0:
			c2 = n
		if n % 2:
			if o1 == o2:
				if o1 == z2 + 1:
					print(min(c1, c2))
				elif c2 % 2 == 0:
					print(c2)
				else:
					print(c1)
			elif c2 % 2 == 0:
				print(c1)
			else:
				print(c2)
		elif o1 == o2:
			print(c1)
		else:
			print(c2)
	else:
		print(-1)


import sys
input = sys.stdin.readline

def read_strs():
	s = input().strip()
	return list(map(int, list(s)))

def solve(a, b):
	(x, y, diff) = (0, 0, 0)
	n = len(a)
	result = n + 1
	for i in range(n):
		x += a[i]
		y += b[i]
		diff += 1 if a[i] != b[i] else 0
	if x == y:
		result = min(diff, result)
	if x == n - y + 1:
		result = min(n - diff, result)
	return result if result < n + 1 else -1
n = int(input())
for _ in range(n):
	m = int(input())
	a = read_strs()
	b = read_strs()
	print(solve(a, b))


from collections import defaultdict
import sys
import math
input = sys.stdin.readline
flush = sys.stdout.flush
get_int = lambda : int(input().rstrip())
get_arr = lambda : [int(w) for w in input().split()]
from collections import deque
t = get_int()

def solve(s1, s2):
	ct = 0
	for i in range(n):
		if s1[i] != s2[i]:
			ct += 1
	return ct
for _ in range(t):
	n = get_int()
	s1 = input().rstrip()
	s2 = input().rstrip()
	k = sum((1 for ch in s1 if ch == '1'))
	k2 = sum((1 for ch in s2 if ch == '1'))
	mn = 10 ** 9
	if k == k2:
		mn = min(mn, solve(s1, s2))
	if k + k2 == n + 1:
		eq = -1
		for i in range(n):
			if s1[i] == '1':
				eq = i
			if s1[i] == s2[i]:
				eq = i
				break
		ns1 = ''
		for i in range(n):
			if i == eq:
				ns1 += s1[i]
			else:
				ns1 += '1' if s1[i] == '0' else '0'
		mn = min(mn, solve(ns1, s2) + 1)
	if mn == 10 ** 9:
		print(-1)
	else:
		print(mn)


from sys import stdin, stdout
rd = lambda : list(map(int, stdin.readline().split()))
rds = lambda : stdin.readline().rstrip()
ii = lambda : int(stdin.readline())
INF = 1 << 62
mod = 10 ** 9 + 7
for _ in range(ii()):
	n = ii()
	a = rds()
	b = rds()
	if a == b:
		print(0)
		continue
	d = 0
	e1 = 0
	e0 = 0
	ac = 0
	bc = 0
	for (x, y) in zip(a, b):
		if x == '1':
			ac += 1
		if y == '1':
			bc += 1
		if x == y == '1':
			e1 += 1
		elif x == y == '0':
			e0 += 1
		elif x != y:
			d += 1
	if e1 == 1 and d == len(a) - 1:
		print(1)
		continue
	res1 = INF
	if ac == bc:
		res1 = d
	res2 = INF
	if e1 - e0 == 1:
		res2 = 1 + 2 * e0
	res = min(res1, res2)
	if res == INF:
		print(-1)
	else:
		print(res)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	x = [0] * 4
	for i in range(n):
		if a[i] == '1' and b[i] == '1':
			x[0] += 1
		elif a[i] == '0' and b[i] == '0':
			x[1] += 1
		elif a[i] == '1' and b[i] == '0':
			x[2] += 1
		elif a[i] == '0' and b[i] == '1':
			x[3] += 1
	if x[2] == x[3] == 0:
		print(0)
	elif x[0] == x[2] == 0:
		print(-1)
	else:
		if x[2] == x[3]:
			ans = x[2] + x[3]
		else:
			ans = n + 1
		if x[2] > 0 and x[1] + 1 == x[0]:
			temp_ans = x[0] * 2 + 1
			if temp_ans < ans:
				ans = temp_ans
		if x[0] > 0 and x[1] + 1 == x[0]:
			temp_ans = x[1] * 2 + 1
			if temp_ans < ans:
				ans = temp_ans
		if ans > n:
			ans = -1
		print(ans)


import sys
t = int(sys.stdin.readline())
ans = []
for _ in range(t):
	n = int(sys.stdin.readline())
	s1 = sys.stdin.readline().strip()
	s2 = sys.stdin.readline().strip()
	if s1 == s2:
		ans.append('0')
		continue
	elif len(s1) != len(s2) or s1.count('1') == 0:
		ans.append('-1')
	else:
		cc = [0] * 4
		for i in range(len(s1)):
			cc[int(s1[i]) * 2 + int(s2[i])] += 1
		ret = n * 2
		if cc[1] == cc[2]:
			ret = cc[1] + cc[2]
		if cc[3] == cc[0] + 1:
			ret = min(ret, cc[0] + cc[3])
		if ret < n * 2:
			ans.append(str(ret))
		else:
			ans.append('-1')
print('\n'.join(ans))


import math
import os
import sys
from decimal import *
from io import BytesIO, IOBase
import threading
from itertools import *
from collections import *
from array import *
import random
M = 10 ** 9 + 7
import heapq
import bisect
from functools import lru_cache
from queue import PriorityQueue
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def inpu():
	return int(inp())

def regularbracket(t):
	p = 0
	for i in t:
		if i == '(':
			p += 1
		else:
			p -= 1
		if p < 0:
			return False
	else:
		if p > 0:
			return False
		else:
			return True

def binarySearchcount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def binarySearch(arr, n, key):
	left = 0
	right = n - 1
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] == key:
			return mid
		if arr[mid] <= key:
			left = mid + 1
		else:
			right = mid - 1
	return -1

def ternarysearch(arr, n, key):
	(l, r) = (0, n - 1)
	while l <= r:
		mid = (-l + r) // 3 + l
		mid2 = mid + (-l + r) // 3
		if arr[mid] == key:
			return mid
		if arr[mid2] == key:
			return mid2
		if arr[mid] > key:
			r = mid - 1
		elif arr[mid2] < key:
			l = mid2 + 1
		else:
			l = mid + 1
			r = mid2 - 1
	return -1

def reverse1(string):
	pp = ''
	for i in string[::-1]:
		pp += i
	if pp == string:
		return True
	return False

def reverse2(list1):
	l = []
	for i in list1[::-1]:
		l.append(i)
	if l == list1:
		return True
	return False

def sumofdigits(n):
	n = str(n)
	s1 = 0
	for i in n:
		s1 += int(i)
	return s1

def perfect_square(n):
	s = math.sqrt(n)
	if s == int(s):
		return True
	return False

def roman_number(x):
	if x > 15999:
		return
	value = [5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
	symbol = ['F', 'MF', 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
	roman = ''
	i = 0
	while x > 0:
		div = x // value[i]
		x = x % value[i]
		while div:
			roman += symbol[i]
			div -= 1
		i += 1
	return roman

def soretd(s):
	for i in range(1, len(s)):
		if s[i - 1] > s[i]:
			return False
	return True

def countRhombi(h, w):
	ct = 0
	for i in range(2, h + 1, 2):
		for j in range(2, w + 1, 2):
			ct += (h - i + 1) * (w - j + 1)
	return ct

def countrhombi2(h, w):
	return h * h // 4 * (w * w // 4)

def binpow(a, b):
	if b == 0:
		return 1
	else:
		res = binpow(a, b // 2)
	if b % 2 != 0:
		return res * res * a
	else:
		return res * res

def coprime_to_n(n):
	result = n
	i = 2
	while i * i <= n:
		if n % i == 0:
			while n % i == 0:
				n //= i
			result -= result // i
		i += 1
	if n > 1:
		result -= result // n
	return result

def luckynumwithequalnumberoffourandseven(x, n, a):
	if x >= n and str(x).count('4') == str(x).count('7'):
		a.append(x)
	elif x < 1000000000000.0:
		luckynumwithequalnumberoffourandseven(x * 10 + 4, n, a)
		luckynumwithequalnumberoffourandseven(x * 10 + 7, n, a)
	return a

def luckynum(x, l, r, a):
	if x >= l and x <= r:
		a.append(x)
	if x > r:
		a.append(x)
		return a
	if x < 10000000000.0:
		luckynum(x * 10 + 4, l, r, a)
		luckynum(x * 10 + 7, l, r, a)
	return a

def luckynuber(x, n, a):
	p = set(str(x))
	if len(p) <= 2:
		a.append(x)
	if x < n:
		luckynuber(x + 1, n, a)
	return a

def interact(type, x):
	if type == 'r':
		inp = input()
		return inp.strip()
	else:
		print(x, flush=True)

def findTrailingZeros(n):
	count = 0
	while n >= 5:
		n //= 5
		count += n
	return count

def mergeSort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		L = arr[:mid]
		R = arr[mid:]
		mergeSort(L)
		mergeSort(R)
		i = j = k = 0
		while i < len(L) and j < len(R):
			if L[i] < R[j]:
				arr[k] = L[i]
				i += 1
			else:
				arr[k] = R[j]
				j += 1
			k += 1
		while i < len(L):
			arr[k] = L[i]
			i += 1
			k += 1
		while j < len(R):
			arr[k] = R[j]
			j += 1
			k += 1

def solven(p, l, a, b, n, res):
	if p > n or l > 10:
		return
	if p > 0:
		res.add(p)
	solven(p * 10 + a, l + 1, a, b, n)
	solven(p * 10 + b, l + 1, a, b, n)

def subsetsUtil(A, subset, index, d):
	print(*subset)
	s = sum(subset)
	d.append(s)
	for i in range(index, len(A)):
		subset.append(A[i])
		subsetsUtil(A, subset, i + 1, d)
		subset.pop(-1)
	return d

def subsetSums(arr, l, r, d, sum=0):
	if l > r:
		d.append(sum)
		return
	subsetSums(arr, l + 1, r, d, sum + arr[l])
	subsetSums(arr, l + 1, r, d, sum)
	return d

def print_factors(x):
	factors = []
	for i in range(1, x + 1):
		if x % i == 0:
			factors.append(i)
	return factors

def calc(X, d, ans, D):
	if len(X) == 0:
		return
	i = X.index(max(X))
	ans[D[max(X)]] = d
	Y = X[:i]
	Z = X[i + 1:]
	calc(Y, d + 1, ans, D)
	calc(Z, d + 1, ans, D)

def generate(st, s):
	if len(s) == 0:
		return
	if s not in st:
		st.add(s)
		for i in range(len(s)):
			t = list(s).copy()
			t.remove(s[i])
			t = ''.join(t)
			generate(st, t)
	return

def largestincreasingsubsequence(A):
	l = [1] * len(A)
	for i in range(1, len(l)):
		for k in range(i):
			if A[k] <= A[i]:
				l[i] = max(l[i], l[k] + 1)
	return max(l)

def findOR(nums, N):
	prefix_sum = 0
	result = 0
	for i in range(N):
		result |= nums[i]
		prefix_sum += nums[i]
		result |= prefix_sum
	return result

def OR(a, n):
	ans = a[0]
	for i in range(1, n):
		ans |= a[i]
	return ans

def toString(List):
	return ''.join(List)

def permute(a, l, r, p):
	if l == r:
		p.append(toString(a))
	else:
		for i in range(l, r + 1):
			(a[l], a[i]) = (a[i], a[l])
			permute(a, l + 1, r, p)
			(a[l], a[i]) = (a[i], a[l])

def squareRoot(number, precision):
	start = 0
	(end, ans) = (number, 1)
	while start <= end:
		mid = int((start + end) / 2)
		if mid * mid == number:
			ans = mid
			break
		if mid * mid < number:
			start = mid + 1
		else:
			end = mid - 1
	increment = 0.1
	for i in range(0, precision):
		while ans * ans <= number:
			ans += increment
		ans = ans - increment
		increment = increment / 10
	return ans

def countRectangles(l, w):
	squareSide = math.gcd(l, w)
	return int(l * w / (squareSide * squareSide))

def count_same_digit(L, R):
	tmp = 0
	ans = 0
	n = int(math.log10(R) + 1)
	for i in range(0, n):
		tmp = tmp * 10 + 1
		for j in range(1, 10):
			if L <= tmp * j and tmp * j <= R:
				ans += 1
	return ans

def findCrossOver(arr, low, high, x):
	if arr[high] <= x:
		return high
	if arr[low] > x:
		return low
	mid = (low + high) // 2
	if arr[mid] <= x and arr[mid + 1] > x:
		return mid
	if arr[mid] < x:
		return findCrossOver(arr, mid + 1, high, x)
	return findCrossOver(arr, low, mid - 1, x)

def Kclosest(arr, x, k, n, ans):
	l = findCrossOver(arr, 0, n - 1, x)
	r = l + 1
	count = 0
	if arr[l] == x:
		l -= 1
	while l >= 0 and r < n and (count < k):
		if x - arr[l] < arr[r] - x:
			ans.append(arr[l])
			l -= 1
		else:
			ans.append(arr[r])
			r += 1
		count += 1
	while count < k and l >= 0:
		ans.append(arr[l])
		l -= 1
		count += 1
	while count < k and r < n:
		ans.append(arr[r])
		r += 1
		count += 1
	return ans

def dfs(root, nodeVal, nodeConnection, visited):
	leftVal = nodeVal[root][0]
	rightVal = nodeVal[root][1]
	solution = []
	if nodeConnection[root]:
		visited.add(root)
		for i in nodeConnection[root]:
			if i not in visited:
				solution.append(dfs(i, nodeVal, nodeConnection, visited))
		leftMax = 0
		rightMax = 0
		for i in solution:
			(l, r) = i
			leftMax += max(abs(leftVal - l[0]) + l[1], abs(leftVal - r[0]) + r[1])
			rightMax += max(abs(rightVal - l[0]) + l[1], abs(rightVal - r[0]) + r[1])
		return ((leftVal, leftMax), (rightVal, rightMax))
	else:
		return ((leftVal, 0), (rightVal, 0))

def luckynumber(x, n, a):
	if x > 0:
		a.append(x)
	if x > 10 ** 9:
		return a
	elif x < 1000000000000.0:
		luckynumber(x * 10 + 4, n, a)
		luckynumber(x * 10 + 7, n, a)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def query1(l, r):
	if l >= r:
		return -1
	print('?', l + 1, r + 1)
	sys.stdout.flush()
	return int(input()) - 1

def answer(p):
	print('!', p + 1)
	sys.stdout.flush()
	exit()

def buildPrefix(MAX, prefix):
	prime = [1] * (MAX + 1)
	p = 2
	while p * p <= MAX:
		if prime[p] == 1:
			i = p * 2
			while i <= MAX:
				prime[i] = 0
				i += p
		p += 1
	for p in range(2, MAX + 1):
		prefix[p] = prefix[p - 1]
		if prime[p] == 1:
			prefix[p] += 1

def query(L, R, prefix):
	return prefix[R] - prefix[L - 1]

def maxSubArraySum(a, size):
	max_so_far = a[0]
	curr_max = a[0]
	for i in range(1, size):
		curr_max = max(a[i], curr_max + a[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far

def solvepp(n, k):
	if n == 1 and k == 1:
		return 0
	mid = 2 ** (n - 1) // 2
	if k <= mid:
		return solvepp(n - 1, k)
	else:
		return solvepp(n - 1, k - mid) == 0

def solvr(s, p):
	if len(s) == 0:
		print(p, end=' ')
		return
	op1 = p
	op2 = p + s[0]
	s = s[1:]
	solvr(s, op1)
	solvr(s, op2)
	return

def paranthesis(n, m, ans, l):
	if n == 0 and m == 0:
		print(ans)
		return
	if n != 0:
		op1 = ans + '('
		paranthesis(n - 1, m, op1, l)
	if m > n:
		op2 = ans + ')'
		paranthesis(n, m - 1, op2, l)

def rse(arr, n):
	stack = []
	ans = []
	for i in range(n - 1, -1, -1):
		if len(stack) == 0:
			ans.append(n)
		else:
			while len(stack) != 0:
				if stack[-1][0] >= arr[i]:
					stack.pop()
				else:
					break
			if len(stack) == 0:
				ans.append(n)
			else:
				ans.append(stack[-1][1])
		stack.append([arr[i], i])
	ans.reverse()
	return ans

def lse(arr, n):
	stack = []
	ans = []
	for i in range(n):
		if len(stack) == 0:
			ans.append(-1)
		else:
			while len(stack) != 0:
				if stack[-1][0] >= arr[i]:
					stack.pop()
				else:
					break
			if len(stack) == 0:
				ans.append(-1)
			else:
				ans.append(stack[-1][1])
		stack.append([arr[i], i])
	return ans

def mah(arr):
	max1 = 0
	p = rse(arr, len(arr))
	q = lse(arr, len(arr))
	for i in range(len(arr)):
		a = (p[i] - q[i] - 1) * arr[i]
		max1 = max(a, max1)
	return max1

def lcs(s, r):
	rr = len(r)
	ss = len(s)
	l = [[0] * (rr + 1) for i in range(ss + 1)]
	for i in range(1, ss + 1):
		for j in range(1, rr + 1):
			if s[i - 1] == r[j - 1]:
				l[i][j] = l[i - 1][j - 1] + 1
			else:
				l[i][j] = max(l[i - 1][j], l[i][j - 1])
	return l[ss][rr]

def subsetsum(arr, sum, len):
	dp = [[False] * (sum + 1) for i in range(len + 1)]
	for i in range(len + 1):
		dp[i][0] = True
	for i in range(1, len + 1):
		for j in range(1, sum + 1):
			if arr[i - 1] > j:
				dp[i][j] = dp[i - 1][j]
			else:
				dp[i][j] = dp[i - 1][j] or dp[i - 1][j - arr[i - 1]]
	return dp[len][sum]

def matrixmincost(cost, n, m):
	dp = [[0 for x in range(m)] for x in range(n)]
	for i in range(n):
		for j in range(m):
			if i == 0 and j == 0:
				dp[i][j] = cost[i][j]
			elif i == 0 and j != 0:
				dp[i][j] = dp[i][j - 1] + cost[i][j]
			elif j == 0 and i != 0:
				dp[i][j] = dp[i - 1][j] + cost[i][j]
			else:
				dp[i][j] = cost[i][j] + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])
	return dp[n - 1][m - 1]

def coinchange(n, arr, len1):
	dp = [0] * (n + 1)
	dp[0] = 1
	for i in range(len1):
		for j in range(arr[i], n + 1):
			dp[j] += dp[j - arr[i]]
	return dp[n]

class Graph(object):

	def __init__(self, vertices):
		self.graph = defaultdict(list)
		self.V = vertices

	def addEdge(self, u, v):
		self.graph[u].append(v)
		self.graph[v].append(u)

	def connectedComponents(self):
		unvisited = set(range(self.V))
		queue = deque()
		count = 0
		while len(unvisited) > 0:
			count += 1
			v = next(iter(unvisited))
			unvisited.remove(v)
			queue.append(v)
			while len(queue) > 0:
				v = queue.popleft()
				for w in self.graph[v]:
					if w in unvisited:
						unvisited.remove(w)
						queue.append(w)
		return count

def maxSumIS(arr, n):
	msis = arr.copy()
	for i in range(1, n):
		for j in range(i):
			if arr[i] > arr[j] and msis[i] < msis[j] + arr[i]:
				msis[i] = msis[j] + arr[i]
	c = max(msis)
	p = 5
	return c

def binarysearch2(arr, target):
	lo = 0
	hi = len(arr) - 1
	while lo <= hi:
		mid = (lo + hi) // 2
		if arr[mid] == target:
			if mid != len(arr) - 1:
				if arr[mid + 1] != target:
					return mid
				else:
					lo += 1
			else:
				return mid
			continue
		if arr[mid] > target:
			hi = mid - 1
		else:
			lo = mid + 1

def nge(arr, n):
	stack = []
	ans = []
	for i in range(n - 1, -1, -1):
		if len(stack) == 0:
			ans.append(-1)
		else:
			while len(stack) > 0:
				if stack[-1][0] < arr[i]:
					stack.pop()
				else:
					break
			if len(stack) == 0:
				ans.append(-1)
			else:
				ans.append(stack[-1][1])
		stack.append([arr[i], i])
	ans.reverse()
	return ans

def nse(arr, n):
	stack = []
	ans = []
	for i in range(n - 1, -1, -1):
		if len(stack) == 0:
			ans.append(-1)
		else:
			while len(stack) > 0:
				if stack[-1][0] > arr[i]:
					stack.pop()
				else:
					break
			if len(stack) == 0:
				ans.append(-1)
			else:
				ans.append(stack[-1][1])
		stack.append([arr[i], i])
	ans.reverse()
	return ans

def alperm(nums, path, result):
	if not nums:
		result.add(tuple(path))
		return
	for i in range(0, len(nums)):
		alperm(nums[:i] + nums[i + 1:], path + [nums[i]], result)
	return result

def minsum(arr, n, m, res, l):
	if n == 1 and m == 1:
		res += arr[0][0]
		l.append(res)
	else:
		if n != 1:
			p = res + arr[n - 1][m - 1]
			minsum(arr, n - 1, m, p, l)
		if m != 1:
			p = res + arr[n - 1][m - 1]
			minsum(arr, n, m - 1, p, l)
	return min(l)

def catalan(n):
	if n == 0 or n == 1:
		return 1
	catalan = [0] * (n + 1)
	catalan[0] = 1
	catalan[1] = 1
	for i in range(2, n + 1):
		for j in range(i):
			catalan[i] += catalan[j] * catalan[i - j - 1]
	return catalan[n]

def find_shortest_path(graph, start, end, path):
	path = path + [start]
	if start == end:
		return path
	if start not in graph:
		return None
	shortest = None
	for node in graph[start]:
		if node not in path:
			newpath = find_shortest_path(graph, node, end, path)
			if newpath:
				if not shortest or len(newpath) < len(shortest):
					shortest = newpath
	return shortest

def test(n, d):
	a = random.randint(1, n - 2)
	p = pow(a, d, n)
	if p == 1 or p == n - 1:
		return True
	while d != n - 1:
		d *= 2
		p = pow(p, 2, n)
		if p == n - 1:
			return True
	return False

def isprime(n):
	if n == 1:
		return False
	if n == 2:
		return True
	if n == 3:
		return True
	d = n - 1
	while d % 2 == 0:
		d //= 2
	for i in range(5):
		if test(n, d) == False:
			return False
	return True

def muin(a, m):
	(x, y, mo) = (1, 0, m)
	if m == 1:
		return 0
	while m > 0:
		(x, y) = (y, x - a // m * y)
		(a, m) = (m, a % m)
	if x < 0:
		return mo + x
	else:
		return x

def good(l, c):
	ch = chr(c)
	if len(l) == 1:
		return 1 if ch != l[0] else 0
	k = len(l) // 2
	lm = k - l[:k].count(ch) + good(l[k:], c + 1)
	rm = k - l[k:].count(ch) + good(l[:k], c + 1)
	return min(lm, rm)

def facto(fact):
	(fact[0], fact[1]) = (1, 1)
	for i in range(2, 10 ** 5 + 1):
		fact[i] = fact[i - 1] * i % M

def comb(n, r, fact):
	return fact[n] * pow(fact[n - r] * fact[r], M - 2, M) % M

def numberToBase(n, b):
	if n == 0:
		return 0
	digits = []
	while n:
		digits.append(n % b)
		n //= b
	return sum(digits)

def sieveoferonthesis(prime, maxn):
	(prime[0], prime[1]) = (False, False)
	for i in range(2, int(math.sqrt(maxn)) + 1):
		if prime[i] == True:
			for j in range(i * i, maxn + 1, i):
				prime[j] = False

def sieve(spf):
	MAXN = 10000001
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, math.ceil(math.sqrt(MAXN))):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def getFactorization(x, spf):
	ret = list()
	while x != 1:
		ret.append(spf[x])
		x = x // spf[x]
	return ret

def main():
	tt = 1
	tt = inpu()
	for _ in range(tt):
		n = inpu()
		a = inp()
		b = inp()
		if a == b:
			print(0)
			continue
		if '1' not in a:
			print(-1)
			continue
		ans10 = 0
		ans00 = 0
		ans01 = 0
		ans11 = 0
		for i in range(n):
			if a[i] == '1' and b[i] == '0':
				ans10 += 1
			if a[i] == '0' and b[i] == '1':
				ans01 += 1
			if a[i] == '0' and b[i] == '0':
				ans00 += 1
			if a[i] == '1' and b[i] == '1':
				ans11 += 1
		res1 = float('inf')
		res2 = float('inf')
		if ans11 == ans00 + 1:
			res1 = ans11 + ans00
		if ans01 == ans10:
			res2 = ans10 + ans01
		if res1 == float('inf') and res2 == float('inf'):
			print(-1)
		else:
			print(min(res1, res2))
main()


import sys
input = sys.stdin.readline
for i in ' ' * int(input()):
	n = int(input())
	s1 = input().strip()
	s2 = input().strip()
	k1 = s1.count('1')
	k2 = s2.count('1')
	if k1 != k2 and k1 + k2 != n + 1:
		print(-1)
		continue
	res = 10 ** 9
	if k1 == k2:
		ct = 0
		for i in range(n):
			if s1[i] != s2[i]:
				ct += 1
		res = min(res, ct)
	if k1 + k2 == n + 1:
		ct = 0
		for i in range(n):
			if s1[i] == s2[i]:
				ct += 1
		res = min(res, ct)
	print(res)


t = int(input())
for i in range(t):
	opp = True
	n = int(input())
	a = [int(j) for j in list(input())]
	b = [int(j) for j in list(input())]
	ans = 0
	if sum(a) == sum(b) and sum(a) == n - sum(b) + 1:
		for j in range(n):
			if b[j] == 1 and a[j] == 0:
				ans += 1
		if ans < sum(a) / 2:
			print(ans * 2)
		else:
			print((sum(a) - ans - 1) * 2 + 1)
	elif sum(a) == sum(b):
		for j in range(n):
			if b[j] == 1 and a[j] == 0:
				ans += 2
		print(ans)
	elif sum(a) == n - sum(b) + 1:
		for j in range(n):
			if b[j] == 1 and a[j] == 1:
				ans += 1
				a = [1 if k == 0 else 0 for k in a]
				a[j] = 1
				for k in range(n):
					if b[k] == 1 and a[k] == 0:
						ans += 2
				print(ans)
				break
	else:
		print(-1)


import sys, math, cmath, time, collections

def solve():
	n = inp()
	a = insr()
	b = insr()
	n1 = 0
	n2 = 0
	m1 = 0
	m2 = 0
	same = 0
	diff = 0
	ones = 0
	zero = 0
	onessame = 0
	zerosame = 0
	for i in range(n):
		if a[i] == '1':
			n1 = n1 + 1
		else:
			m1 = m1 + 1
		if b[i] == '1':
			n2 = n2 + 1
		else:
			m2 = m2 + 1
		if a[i] == b[i]:
			same = same + 1
			if a[i] == '1':
				onessame = onessame + 1
			else:
				zerosame = zerosame + 1
		else:
			diff = diff + 1
			if a[i] == '1':
				ones = ones + 1
			else:
				zero = zero + 1
	result = 0
	if not (n1 == n2 and m1 == m2 or (n2 == m1 + 1 and m2 == n1 - 1)):
		result = -1
	if result != -1:
		if same % 2 == 0:
			if ones == zero:
				result = diff
			else:
				result = -1
		elif ones == zero and onessame == zerosame + 1:
			result = min(diff, same)
		elif ones == zero and onessame != zerosame + 1:
			result = diff
		elif ones != zero and onessame == zerosame + 1:
			result = same
		else:
			result = -1
	print(result)

def main():
	global tt
	t = inp()
	for tt in range(1, t + 1):
		solve()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def inls():
	return list(map(str, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def convert(set):
	return list(set)

def unique(list):
	list_set = set(list)
	unique_list = convert(list_set)
	return unique_list

def seive(n):
	a = [1]
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p = p + 1
	for p in range(2, n + 1):
		if prime[p]:
			a.append(p)
	return a

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def power_two(x):
	return x and (not x & x - 1)

def ceil(a, b):
	return -(-a // b)
main()


import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192
sys.setrecursionlimit(10 ** 5)

def bootstrap(f, stack=[]):

	def fun(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return fun

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
from collections import deque
import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	c11 = 0
	c10 = 0
	c01 = 0
	c00 = 0
	for j in range(n):
		if a[j] == '0' and b[j] == '0':
			c00 += 1
		if a[j] == '1' and b[j] == '0':
			c10 += 1
		if a[j] == '1' and b[j] == '1':
			c11 += 1
		if a[j] == '0' and b[j] == '1':
			c01 += 1
	d00 = c00
	d11 = c11
	d10 = c10
	d01 = c01
	m1 = 10 ** 6
	ans = -1
	for i in range(m1):
		if c01 == 0 and c10 == 0:
			break
		if i % 2 == 0:
			if c11 == 0:
				break
			else:
				(c01, c11) = (c11 - 1, c01 + 1)
				(c10, c00) = (c00, c11)
		elif c10 == 0:
			break
		else:
			(c10, c00) = (c00 + 1, c10 - 1)
			(c01, c11) = (c11, c01)
	if c10 == 0 and c01 == 0:
		ans = i
	m1 = 10 ** 6
	for i in range(m1):
		if d10 == 0 and d01 == 0:
			break
		if i % 2 == 1:
			if d11 == 0:
				break
			else:
				(d01, d11) = (d11 - 1, d01 + 1)
				(d10, d00) = (d00, d10)
		elif d10 == 0:
			break
		else:
			(d10, d00) = (d00 + 1, d10 - 1)
			(d01, d11) = (d11, d01)
	if d01 == 0 and d10 == 0:
		if ans == -1:
			ans = i
		else:
			ans = min(ans, i)
	print(ans)


def find(n, a, b):
	dif = 0
	sum_a = 0
	sum_b = 0
	for i in range(n):
		if a[i] == '1':
			sum_a += 1
		if b[i] == '1':
			sum_b += 1
		if a[i] != b[i]:
			dif += 1
	if sum_a != sum_b and sum_a + sum_b != n + 1:
		return -1
	if sum_a == sum_b:
		if sum_a + sum_b == n + 1:
			return min(dif, n - dif)
		return dif
	return n - dif
cases = int(input())
for _ in range(cases):
	n = int(input())
	a = input()
	b = input()
	print(find(n, a, b))


def checkeven(k, res):
	if k == res // 2:
		return True
	else:
		return False

def checkodd(g, res):
	if g == (n - res) // 2 + 1:
		return True
	else:
		return False

def find(a, b, n):
	if int(a) == 0 or int(b) == 0:
		if int(a) != 0 or int(b) != 0:
			return -1
	if int(a) == int(b):
		return 0
	res = 0
	g = k = 0
	for i in range(n):
		if a[i] == '1':
			if a[i] != b[i]:
				res += 1
				k += 1
			else:
				g += 1
		elif a[i] != b[i]:
			res += 1
	ans1 = ans2 = -1
	extra = 0
	if res % 2 == 0 and checkeven(k, res):
		ans1 = res
	if (n - res) % 2 != 0 and checkodd(g, res):
		ans2 = n - res
	if ans2 == ans1 and ans2 == -1:
		return -1
	if ans1 == -1:
		return ans2
	elif ans2 == -1:
		return ans1
	else:
		return min(ans2, ans1)
r = int(input())
res = []
for i in range(r):
	n = int(input())
	a = input()
	b = input()
	res.append(find(a, b, n))
for i in res:
	print(i)


maxx = 100000000
T = int(input())
for t in range(T):
	n = int(input())
	a = input()
	b = input()
	a1 = a.count('1')
	b1 = b.count('1')
	diff = 0
	for i in range(n):
		diff += a[i] != b[i]
	(op1, op2) = (maxx, maxx)
	if a1 == b1:
		op1 = diff
	if a1 == n - b1 + 1:
		op2 = n - diff
	if op1 == op2 == maxx:
		print(-1)
	else:
		print(min(op1, op2))


for t in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	x = a.count('1')
	y = b.count('1')
	dif = 0
	for i in range(n):
		dif += a[i] != b[i]
	o1 = 100000000
	o2 = 100000000
	if x == y:
		o1 = dif
	if x == n - y + 1:
		o2 = n - dif
	if o1 == o2 == 100000000:
		print(-1)
	else:
		print(min(o1, o2))


import sys
I = lambda : [*map(int, sys.stdin.readline().split())]
prin = sys.stdout.write
(t,) = I()
for _ in range(t):
	(n,) = I()
	s1 = input()
	s2 = input()
	o1 = s1.count('1')
	o2 = s2.count('1')
	if o1 != o2 and o1 + o2 != n + 1:
		print(-1)
	elif o1 == o2 and o1 + o2 != n + 1:
		diff = 0
		for i in range(n):
			if s1[i] != s2[i]:
				diff += 1
		print(diff)
	elif o1 + o2 == n + 1 and o1 != o2:
		diff = 0
		for i in range(n):
			if s1[i] == s2[i]:
				diff += 1
		print(diff)
	else:
		diff = 0
		for i in range(n):
			if s1[i] != s2[i]:
				diff += 1
		print(min(diff, n - diff))


def solve():
	n = int(input())
	a = input()
	b = input()
	(x, y, z) = (0, 0, 0)
	for i in range(n):
		if a[i] == '1':
			x += 1
		if b[i] == '1':
			y += 1
		if a[i] != b[i]:
			z += 1
	if x == y:
		if n - x + 1 == y:
			return min(z, n - z)
		return z
	elif n - x + 1 == y:
		return n - z
	return -1
for _ in range(int(input())):
	print(solve())


import math
cases = int(input())
for i in range(cases):
	input()
	from_str = input()
	to_str = input()
	mat = [[0, 0], [0, 0]]
	for j in range(len(from_str)):
		from_bit = int(from_str[j])
		to_bit = int(to_str[j])
		mat[from_bit][to_bit] += 1
	ans = math.inf
	if mat[1][0] == mat[0][1]:
		ans = 2 * mat[1][0]
	if mat[1][1] == mat[0][0] + 1:
		ans = min(ans, mat[1][1] + mat[0][0])
	print(ans if ans != math.inf else -1)


MOD = 10 ** 9 + 7
MAX = 1000
fact = [1]
for zz in range(1, MAX + 1):
	fact.append(fact[-1] * zz % MOD)

def nCr(n, r):
	num = fact[n]
	den = fact[r] * fact[n - r] % MOD
	return num * pow(den, MOD - 2, MOD) % MOD
from collections import deque

def dfs(s):
	st = deque()
	st.append((s, 0))
	vi = dict()
	vi[s] = 0
	n = len(s)
	while st:
		(t, nMoves) = st.popleft()
		for i in range(n):
			if t[i] == '0':
				continue
			arr = list(t)
			for j in range(n):
				if j != i:
					if arr[j] == '1':
						arr[j] = '0'
					else:
						arr[j] = '1'
			t2 = ''.join(arr)
			if t2 not in vi.keys():
				vi[t2] = nMoves + 1
				st.append((t2, nMoves + 1))
	oneCnts = s.count('1')
	cnts2 = nCr(n, oneCnts) + nCr(n, n + 1 - oneCnts)
	for mask in range(1 << n):
		arr = []
		for i in range(n):
			if mask & 1 << i > 0:
				arr.append('1')
			else:
				arr.append('0')
		s2 = ''.join(arr)
		if s2 in vi.keys():
			ans2 = vi[s2]
		else:
			ans2 = -1
		ans1 = solve(s, s2)
		assert ans1 == ans2, 's:{} s2:{} ans1:{} ans2:{}'.format(s, s2, ans1, ans2)

def solve(a, b):
	n = len(a)
	aOnes = a.count('1')
	bOnes = b.count('1')
	if not (aOnes == bOnes or n + 1 - aOnes == bOnes):
		return -1
	ans = n
	if aOnes == bOnes:
		diffCnts = 0
		for i in range(n):
			if a[i] != b[i]:
				diffCnts += 1
		ans = min(ans, diffCnts)
	if n + 1 - aOnes == bOnes:
		a2 = []
		for x in a:
			if x == '1':
				a2.append('0')
			else:
				a2.append('1')
		a2 = ''.join(a2)
		diffCnts = 0
		for i in range(n):
			if a2[i] != b[i]:
				diffCnts += 1
		for i in range(n):
			if a2[i] == '0':
				if b[i] == '1':
					ans2 = diffCnts - 1
				else:
					ans2 = diffCnts + 1
				ans = min(ans, 1 + ans2)
	return ans

def main():
	t = int(input())
	allans = []
	for _ in range(t):
		n = int(input())
		a = input()
		b = input()
		if a == b:
			allans.append(0)
			continue
		if a.count('0') == n:
			allans.append(-1)
			continue
		aOnes = a.count('1')
		bOnes = b.count('1')
		if not (aOnes == bOnes or n + 1 - aOnes == bOnes):
			allans.append(-1)
			continue
		ans = solve(a, b)
		allans.append(ans)
	multiLineArrayPrint(allans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(a, b, c):
	print('? {} {} {}'.format(a, b, c))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ansArr):
	print('! {}'.format(' '.join([str(x) for x in ansArr])))
	sys.stdout.flush()
inf = float('inf')
from math import gcd, floor, ceil
import math
for _abc in range(1):
	main()


import sys

def readnum():
	input = sys.stdin.readline
	a = list(map(int, input().split()))
	return a

def readstr():
	input = sys.stdin.readline
	a = str(input().split()[0])
	return a

def printf(val):
	sys.stdout.write(str(val) + '\n')

def power(a, b, mod):
	if b == 1:
		return a
	if b == 0:
		return 1
	ans = power(a, b // 2, mod)
	ans *= ans
	if b % 2:
		ans *= a
	ans = ans % mod
	return ans

def factorial(n, start=1, skip=None):
	res = start
	for i in range(1, n + 1):
		if i == skip:
			continue
		res *= i
	return res

def make_deg(l):
	deg = {}
	for i in l:
		if not i in deg:
			deg[i] = 0
		deg[i] += 1
	return deg

def get_num_of_bits(n):
	res = []
	for i in range(1, 31):
		res.append(2 ** (i - 1) * (n // 2 ** i) + max(0, n % 2 ** i - 2 ** (i - 1)))
	return res

def minus(l, r):
	res = []
	for i in range(len(l)):
		res.append(r[i] - l[i])
	return res

def revert(s, idx):
	res = ''
	for i in range(len(s)):
		if i == idx:
			res += '1'
			continue
		if s[i] == '0':
			res += '1'
		elif s[i] == '1':
			res += '0'
	return res
for _ in range(readnum()[0]):
	n = readnum()[0]
	a = readstr()
	b = readstr()
	ones_a = sum([i == '1' for i in a])
	ones_b = sum([i == '1' for i in b])
	res = []
	if ones_a == ones_b:
		res.append(sum((a[i] != b[i] for i in range(n))))
	if ones_a == 1 + n - ones_b:
		reverted = None
		for i in range(n):
			if a[i] == b[i] and a[i] == '1':
				reverted = i
				break
		if reverted is None:
			for i in range(n):
				if a[i] == '1':
					reverted = i
					break
		a = revert(a, reverted)
		res.append(1 + sum((a[i] != b[i] for i in range(n))))
	if len(res) == 0:
		print(-1)
	else:
		print(min(res))


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	a = input().strip()
	b = input().strip()
	diff = cnt1 = cnt2 = 0
	for i in range(n):
		cnt1 += int(a[i])
		cnt2 += int(b[i])
		diff += 1 if a[i] != b[i] else 0
	ans = float('inf')
	if cnt1 == cnt2:
		ans = min(ans, diff)
	if cnt1 + cnt2 == n + 1:
		ans = min(ans, n - diff)
	return ans if ans < float('inf') else -1
for _ in range(int(input())):
	print(solve())


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	b = list(input())
	if a != b and (a.count('1') == 0 or (a.count('1') != 0 and b.count('1') == 0)):
		print(-1)
	elif a == b:
		print(0)
	else:
		count = 0
		count11 = 0
		count21 = 0
		ans1 = 10 ** 9
		ans2 = 10 ** 9
		for i in range(len(a)):
			if a[i] != b[i]:
				count += 1
				if a[i] == '1':
					count11 += 1
			elif a[i] == '1':
				count21 += 1
		eq = len(a) - count
		if eq % 2 == 1:
			if count21 - (eq - count21) == 1:
				ans1 = eq
		if count % 2 == 0:
			if count - count11 == count11:
				ans2 = count
		ans = min(ans1, ans2)
		if ans != 10 ** 9:
			print(ans)
		else:
			print(-1)


inf = 1000000000.0
t = int(input())
for u in range(t):
	n = int(input())
	a = input()
	b = input()
	ok = False
	for i in range(n):
		if a[i] == '1':
			ok = True
	if ok == False and a != b:
		print(-1)
		continue
	ans = inf
	for i in range(2):
		cnt = [0, 0, 0, 0]
		for j in range(n):
			if a[j] == '0':
				if b[j] == '0':
					cnt[0] += 1
				else:
					cnt[1] += 1
			elif b[j] == '0':
				cnt[2] += 1
			else:
				cnt[3] += 1
		if i == 1:
			if cnt[3] - cnt[0] == 1 or cnt[3] - cnt[0] == 0:
				if (cnt[3] + cnt[0]) % 2 == i:
					ans = min(ans, cnt[3] + cnt[0])
		elif cnt[2] - cnt[1] == 1 or cnt[2] - cnt[1] == 0:
			if (cnt[2] + cnt[1]) % 2 == i:
				ans = min(ans, cnt[2] + cnt[1])
	if ans == inf:
		print(-1)
	else:
		print(ans)


import sys, os, io
input = sys.stdin.readline
import math
import bisect
t = int(input())
for a0 in range(t):
	n = int(input())
	l1 = [+(i == '1') for i in input().strip()]
	l2 = [+(i == '1') for i in input().strip()]
	c = 0
	f = 0
	if l1 == l2:
		print(0)
		continue
	if l1.count(1) == l2.count(1):
		f = 1
		for i in range(n):
			if l1[i] != l2[i]:
				c += 1
	l3 = []
	l4 = []
	ind1 = -1
	ind2 = -1
	for i in range(n):
		if l1[i] == 1 and l2[i] == 0:
			ind1 = i
			break
	for i in range(n):
		if l1[i] == 1 and l2[i] == 1:
			ind2 = i
			break
	if ind1 != -1:
		for i in range(n):
			if i == ind1:
				l3.append(l1[i])
			elif l1[i] == 1:
				l3.append(0)
			else:
				l3.append(1)
	if ind2 != -1:
		for i in range(n):
			if i == ind2:
				l4.append(l1[i])
			elif l1[i] == 1:
				l4.append(0)
			else:
				l4.append(1)
	c1 = 0
	c2 = 0
	if len(l3) != 0 and l2.count(1) == l3.count(1):
		c1 = 1
		f = 1
		for i in range(n):
			if l2[i] != l3[i]:
				c1 += 1
	if len(l4) != 0 and l2.count(1) == l4.count(1):
		f = 1
		c2 = 1
		for i in range(n):
			if l2[i] != l4[i]:
				c2 += 1
	if f == 0:
		print(-1)
	else:
		z = [c, c1, c2]
		res = float('inf')
		for i in z:
			if i != 0 and i < res:
				res = i
		print(res)


def f(a, b):
	if a == b:
		return 0
	one = []
	z = []
	bigo = set()
	for (id, i) in enumerate(a):
		if i == '1':
			one.append(id)
		else:
			z.append(id)
		if b[id] == '1':
			bigo.add(id)
	cst11 = 0
	cst12 = 0
	if one == []:
		return -1
	bado = []
	goodo = []
	badz = []
	goodz = []
	for i in one:
		if i not in bigo:
			bado.append(i)
		else:
			goodo.append(i)
	for i in z:
		if i not in bigo:
			badz.append(i)
		else:
			goodz.append(i)
	cst1 = float('inf')
	if len(goodo) - len(badz) == 1:
		cst1 = len(goodo) + len(badz)
	if cst1 % 2 == 0 and cst1 != 0:
		cst1 = float('inf')
	cst2 = float('inf')
	if len(bado) - len(goodz) == 1 or len(bado) - len(goodz) == 0:
		cst2 = len(bado) + len(goodz)
	if cst2 % 2 != 0 and cst2 != 0:
		cst2 = float('inf')
	if min(cst2, cst1) == float('inf'):
		return -1
	return min(cst2, cst1)
for _ in range(int(input())):
	n = input()
	a = input()
	b = input()
	print(f(a, b))


problem_num = int(input())
for i in range(problem_num):
	n = int(input())
	arr1 = input()
	arr2 = input()
	if arr2.count('1') not in [arr1.count('1'), n - arr1.count('1') + 1]:
		print(-1)
		continue
	same = 0
	diff = 0
	for j in range(len(arr1)):
		if arr1[j] == arr2[j]:
			same += 1
		else:
			diff += 1
	if arr1.count('1') == n - arr1.count('1') + 1:
		print(min([same, diff]))
	elif arr2.count('1') == arr1.count('1'):
		print(diff)
	else:
		print(same)


def func():
	n = int(input())
	a = input()
	b = input()
	x = 0
	y = 0
	x1 = 0
	y1 = 0
	for i in range(n):
		if a[i] == b[i]:
			x += 1
			if a[i] == '1':
				x1 += 1
		else:
			y += 1
			if a[i] == '1':
				y1 += 1
	flg = True
	ans = 10 ** 9
	if x % 2 != 0 and (x + 1) // 2 == x1:
		ans = x
		flg = False
	if y % 2 == 0 and y // 2 == y1:
		ans = min(ans, y)
		flg = False
	if flg:
		print(-1)
	else:
		print(ans)
t = int(input())
for i in range(t):
	func()


t = int(input())
for test in range(t):
	n = int(input())
	a = input()
	b = input()
	zC = a.count('0')
	oC = n - zC
	desiredOC = b.count('1')
	if oC == 0:
		if desiredOC == 0:
			print(0)
			continue
		else:
			print(-1)
			continue
	flag = False
	if oC == desiredOC:
		diff = 0
		for idx in range(n):
			if a[idx] == '1' and b[idx] == '0':
				diff += 1
		m = 2 * diff
		flag = True
	if zC + 1 == desiredOC:
		diff = 0
		for idx in range(n):
			if a[idx] == '0' and b[idx] == '0':
				diff += 1
		if not flag:
			flag = True
			m = diff * 2 + 1
		else:
			m = min(diff * 2 + 1, m)
	if flag:
		print(m)
	else:
		print(-1)


import sys, os, io
input = sys.stdin.readline
PI = 3.141592653589793
INF = float('inf')
MOD = 1000000007

def bin32(num):
	return '{0:032b}'.format(num)

def add(x, y):
	return (x + y) % MOD

def sub(x, y):
	return (x - y + MOD) % MOD

def mul(x, y):
	return x * y % MOD

def gcd(x, y):
	if y == 0:
		return x
	return gcd(y, x % y)

def lcm(x, y):
	return x * y // gcd(x, y)

def power(x, y):
	res = 1
	x %= MOD
	while y != 0:
		if y & 1:
			res = mul(res, x)
		y >>= 1
		x = mul(x, x)
	return res

def mod_inv(n):
	return power(n, MOD - 2)

def prob(p, q):
	return mul(p, power(q, MOD - 2))

def ii():
	return int(input())

def li():
	return [int(i) for i in input().split()]

def ls():
	return [i for i in input().split()]
for t in range(ii()):
	t += 1
	n = ii()
	a = input().strip()
	b = input().strip()
	a = list(a)
	b = list(b)
	sa = a.count('1')
	sb = b.count('1')
	if sa != sb:
		if sa == 0 or n - sa + 1 != sb:
			print(-1)
			continue
	zz = 0
	zo = 0
	oo = 0
	oz = 0
	for i in range(n):
		if a[i] == '0' and b[i] == '0':
			zz += 1
		if a[i] == '0' and b[i] == '1':
			zo += 1
		if a[i] == '1' and b[i] == '1':
			oo += 1
		if a[i] == '1' and b[i] == '0':
			oz += 1
	out = INF
	if (zz + oo) % 2 == 1 and (zz + oo + 1) // 2 == oo:
		out = min(out, zz + oo)
	if (zo + oz + 1) % 2 and (zo + oz) // 2 == oz:
		out = min(out, zo + oz)
	if out == INF:
		out = -1
	print(out)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def solve(n, a, b):
	num_zero_same = sum((i == j == '0' for (i, j) in zip(a, b)))
	num_one_same = sum((i == j == '1' for (i, j) in zip(a, b)))
	num_one_zero = sum((i == '1' and j == '0' for (i, j) in zip(a, b)))
	num_zero_one = sum((i == '0' and j == '1' for (i, j) in zip(a, b)))
	total_flips = num_one_zero + num_zero_one
	total_same = n - total_flips
	total_lit = num_one_zero + num_one_same
	if total_same == n:
		return 0
	if total_lit == 0:
		return -1
	ans = []
	if total_flips % 2 == 0 and num_one_zero == num_zero_one:
		ans.append(total_flips)
	if total_same % 2 == 1 and num_one_same == num_zero_same + 1:
		ans.append(total_same)
	if len(ans) == 0:
		return -1
	else:
		return min(ans)
for _ in range(int(input())):
	n = int(input())
	(a, b) = (input(), input())
	val = solve(n, a, b)
	print(val)


for t in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	_1_1 = _0_0 = _1_0 = _0_1 = 0
	if a == b:
		print(0)
	elif int(b) == 0 or int(a) == 0:
		print(-1)
	else:
		for (i, j) in zip(a, b):
			if i == j:
				if i == '1':
					_1_1 += 1
				else:
					_0_0 += 1
			elif i == '1':
				_1_0 += 1
			else:
				_0_1 += 1
		s = _1_1 + _0_0
		s1 = _1_0 + _0_1
		if _1_1 - _0_0 == 1 and _1_0 == _0_1:
			print(min(s, s1))
		elif _1_1 - _0_0 == 1:
			print(s)
		elif _1_0 == _0_1:
			print(s1)
		else:
			print('-1')


import bisect
import collections
import math
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()

def li():
	return [int(i) for i in input().split()]

def lli(rows):
	return [li() for _ in range(rows)]

def si():
	return input()

def ii():
	return int(input())

def ins():
	return input().split()

def drank(d, processing, da, rank):
	tmp = 10 ** 9
	if len(d[da]) == 1:
		return 1
	for di in d[da]:
		if processing[di - 1] == 0:
			processing[di - 1] = 1
			tmp = min(tmp, drank(d, processing, di, rank))
			processing[di - 1] = 0
	rank[da - 1] = tmp + 1
	return tmp + 1

def binary_search(n, a):
	l = len(a)
	low = 0
	high = l - 1
	while high >= low:
		mid = (high + low) // 2
		if a[mid] == n:
			return mid
		elif a[mid] > n:
			high = mid - 1
		else:
			low = mid + 1
	return -1
is_prime = [True] * (10 ** 6 + 1)

def primeseive(n):
	is_prime[0] = is_prime[1] = False
	i = 2
	while i * i <= n:
		if is_prime[i] == True:
			for j in range(i * i, n + 1, i):
				is_prime[j] = False
		i += 1

def solve():
	n = II()
	a = SI()
	b = SI()
	similar = 0
	onesimilar = 0
	onedisimilar = 0
	for i in range(n):
		if a[i] == b[i]:
			similar += 1
			if a[i] == '1':
				onesimilar += 1
		elif a[i] == '1':
			onedisimilar += 1
	disimilar = n - similar
	zerosimilar = similar - onesimilar
	zerodisimilar = disimilar - onedisimilar
	x = n * 100
	y = n * 100
	if onesimilar - zerosimilar == 1:
		x = similar
	if onedisimilar == zerodisimilar:
		y = disimilar
	if x == y == n * 100:
		return -1
	else:
		return min(x, y)

def main():
	for _ in range(II()):
		sys.stdout.write(str(solve()) + '\n')
main()


import sys
import bisect
from bisect import bisect_left as lb
from bisect import bisect_right as rb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
sys.setrecursionlimit(3 * 10 ** 5)
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs2(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0
a = []
(n, k) = (0, 0)
b = []
vis = []
tot = 0
time = []
time1 = []
adj = []
mx = -1
eps = 1e-07
x = 0
pref = []
lvl = []
gp = []
ans = []
f = 0
w = []

def hnbhai(tc):
	n = sb()
	s = list(sa())
	t = list(sa())
	if s == t:
		print(0)
		return
	if s.count('0') == n:
		print(-1)
		return
	zo = 0
	oz = 0
	for i in range(n):
		if s[i] == t[i]:
			continue
		if s[i] == '1':
			oz += 1
		else:
			zo += 1
	lits = n - s.count('0')
	zero = s.count('0') - zo
	one = lits - oz
	tot = 10 ** 18
	if oz == zo:
		tot = min(tot, oz + zo)
	if one == zero + 1:
		tot = min(tot, one + zero)
	if tot > 10 ** 8:
		print(-1)
		return
	print(tot)
for _ in range(sb()):
	hnbhai(_ + 1)


def operation(a, b):
	(c01, c10) = (0, 0)
	for i in range(len(a)):
		if a[i] == '1' and b[i] == '0':
			c10 += 1
		elif a[i] == '0' and b[i] == '1':
			c01 += 1
	if c10 != c01:
		return float('inf')
	return c10 + c01

def change(a, b, f):
	idx = -1
	for i in range(len(a)):
		if a[i] == '1' and b[i] == f:
			idx = i
			break
	if idx == -1:
		return float('inf')
	for i in range(len(a)):
		if i == idx:
			continue
		if a[i] == '1':
			a[i] = '0'
		else:
			a[i] = '1'
	return operation(a, b)
for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	(a1, b1) = (list(a), list(b))
	ans = operation(a1, b1)
	ans = min(ans, 1 + change(a1, b1, '1'), 1 + change(a1, b1, '0'))
	if a == b:
		print(0)
	elif ans == float('inf'):
		print(-1)
	else:
		print(ans)


import sys
import os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
	sys.stderr = open('error.txt', 'w')
depth = 200001
mod = 1000000007
lim = mod * mod
sys.setrecursionlimit(depth)
linp = lambda : list(minp())
minp = lambda : map(int, input().split())
from math import inf, ceil, sqrt, log2, gcd
from collections import defaultdict, deque
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]

def expo(a, b):
	if not b:
		return 1
	v = expo(a, b // 2)
	v = v * v % mod
	if b % 2:
		v = v * a % mod
	return v

def modinv(a, b):
	d = expo(b, mod - 2)
	v = a * d % mod
	return v

def checkPrime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	for i in range(3, int(sqrt(n)) + 1, 2):
		if n % i == 0:
			return False
	return True

def highestPowerof2(x):
	x |= x >> 1
	x |= x >> 2
	x |= x >> 4
	x |= x >> 8
	x |= x >> 16
	return x ^ x >> 1

def isVowel(char):
	return char.lower() in 'aeiou'
for _ in range(int(input())):
	n = int(input())
	(a, b) = (input(), input())
	x = a.count('1')
	y = n - x + 1
	z = b.count('1')
	if z != x and z != y:
		print(-1)
		continue
	if x == 0 and z > 0:
		print(-1)
		continue
	if x == z:
		c = 0
		for i in range(n):
			if a[i] != b[i]:
				c += 1
	else:
		c = inf
	if y == z:
		(e, idx) = (list(), 0)
		for i in range(n):
			if a[i] == '1' and b[i] == '1':
				idx = i
				break
		for i in range(n):
			if i == idx:
				e.append(a[i])
			elif a[i] == '1':
				e.append('0')
			else:
				e.append('1')
		d = 1
		for i in range(n):
			if e[i] != b[i]:
				d += 1
	else:
		d = inf
	ans = min(c, d)
	if ans == inf:
		print(-1)
	else:
		print(ans)


for m in range(int(input())):
	dev = int(input())
	mad = input()
	dad = input()
	xer = mad.count('1')
	yer = dad.count('1')
	key = 0
	for j in range(dev):
		key += mad[j] != dad[j]
	l1 = 100000000
	l2 = 100000000
	if xer == yer:
		l1 = key
	if xer == dev - yer + 1:
		l2 = dev - key
	if l1 == l2 == 100000000:
		print(-1)
	else:
		print(min(l1, l2))


import sys
from bisect import bisect_left as lb
from bisect import bisect_right as rb
from collections import deque
from queue import PriorityQueue as pq
from math import gcd
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
lii = lambda : list(map(int, list(ip())))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
mod = 10 ** 9 + 7
mod1 = 998244353
for _ in range(ii()):
	n = ii()
	a = list(ip())
	b = list(ip())
	if a == b:
		print(0)
		continue
	x = a.count('0')
	if x == n:
		print(-1)
		continue
	t = 0
	t1 = 0
	for i in range(n):
		if a[i] == '1' and b[i] == '0':
			t += 1
		if a[i] == '0' and b[i] == '1':
			t1 += 1
	y = n - x
	(t, t1) = (t1, t)
	t2 = x - t
	t3 = y - t1
	ans = 10 ** 13
	if t3 == t2 + 1:
		ans = min(ans, t3 + t2)
	if t1 == t:
		ans = min(ans, t + t1)
	if ans == 10 ** 13:
		print(-1)
	else:
		print(ans)


from sys import stdin
input = stdin.readline

def solve(a):
	(ans, bal, flip) = (0, 0, 0)
	for i in range(n):
		if a[i] == '1' and b[i] == '0':
			bal += 1
			ans += 1
		if a[i] == '0' and b[i] == '1':
			bal -= 1
			ans += 1
	if bal == 0:
		return ans
	return float('inf')

def answer():
	ans = solve(a)
	ind = -1
	for i in range(n):
		if a[i] == '1' and b[i] == '1':
			ind = i
			break
	if ind == -1:
		if ans == float('inf'):
			return -1
		return ans
	na = ''
	for i in range(n):
		if ind == i:
			na += a[i]
		elif a[i] == '1':
			na += '0'
		else:
			na += '1'
	ans = min(ans, solve(na) + 1)
	if ans == float('inf'):
		return -1
	return ans
for T in range(int(input())):
	n = int(input())
	a = input().strip()
	b = input().strip()
	print(answer())


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	b = list(map(int, input()))
	a1 = a.count(1)
	a0 = a.count(0)
	b1 = b.count(1)
	b0 = b.count(0)
	ans = 0
	if a1 != b1 or a0 != b0:
		if a1 == 0:
			print(-1)
			continue
		j = a.index(1)
		for i in range(n):
			if a[i] and b[i]:
				j = i
		for i in range(n):
			if i != j:
				a[i] = 1 - a[i]
		ans += 1
	a1 = a.count(1)
	a0 = a.count(0)
	if a1 != b1 or a0 != b0:
		print(-1)
		continue
	for i in range(n):
		ans += a[i] != b[i]
	ans2 = float('inf')
	if 1 in a:
		j = a.index(1)
		ans2 = 1
		for i in range(n):
			if a[i] and b[i]:
				j = i
		for i in range(n):
			if i != j:
				a[i] = 1 - a[i]
		for i in range(n):
			ans2 += a[i] != b[i]
		a1 = a.count(1)
		a0 = a.count(0)
		if a1 != b1 or a0 != b0:
			ans2 = float('inf')
	print(min(ans, ans2))


import math
t = int(input())
for test in range(t):
	n = int(input())
	a = input()
	b = input()
	if a == b:
		print(0)
	else:
		onesA = 0
		onesB = 0
		same = 0
		different = 0
		for i in range(n):
			if a[i] == '1':
				onesA += 1
			if b[i] == '1':
				onesB += 1
			if a[i] == '1' and b[i] == '1':
				same += 1
			if a[i] == '1' and b[i] == '0' or (a[i] == '0' and b[i] == '1'):
				different += 1
		best = float('inf')
		if onesA == onesB:
			best = min(best, 2 * (onesA - same))
		if onesB == n - onesA + 1:
			if same > 0:
				best = min(best, 2 * (same - 1) + 1)
			else:
				best = min(best, 1)
		if onesA == n - onesB + 1:
			if same > 0:
				best = min(best, 2 * (same - 1) + 1)
			else:
				best = min(best, 1)
		if best == float('inf'):
			print(-1)
		else:
			print(best)


import sys
import math
from collections import deque, Counter
from sys import stdin
int1 = lambda x: int(x) - 1
stinput = lambda : stdin.readline()[:-1]
ii = lambda : int(stinput())
mi = lambda : map(int, stdin.readline().split())
li = lambda : list(mi())
mi1 = lambda : map(int1, stdin.readline().split())
li1 = lambda : list(mi1())
mis = lambda : map(str, stdin.readline().split())
lis = lambda : list(mis())
from collections import defaultdict
import heapq
mod = 10 ** 9 + 7
Mod = 998244353
INF = 10 ** 18
ans = 0
t = ii()
for _ in range(t):
	n = ii()
	a = stinput()
	b = stinput()
	ans = 10 ** 9
	(cnt00, cnt01, cnt10, cnt11) = (0, 0, 0, 0)
	for i in range(n):
		if a[i] == '1' and b[i] == '1':
			cnt11 += 1
		elif a[i] == b[i]:
			cnt00 += 1
		elif a[i] == '0' and b[i] == '1':
			cnt01 += 1
		else:
			cnt10 += 1
	if (cnt00 + cnt11) % 2 == 1 and cnt11 - cnt00 == 1:
		ans = min(ans, cnt00 + cnt11)
	if (cnt10 + cnt01) % 2 == 0 and cnt01 == cnt10:
		ans = min(ans, cnt01 + cnt10)
	if ans != 10 ** 9:
		print(ans)
	else:
		print(-1)


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, Counter
from typing import AsyncIterator
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math

def I():
	return int(input())

def lint():
	return list(map(int, input().split()))

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
import heapq

def ask(a, b, c):
	print(f'? {a} {b} {c}')
	sys.stdout.flush()
	res = int(input())
	sys.stdout.flush()
	return res
t = I()
for _ in range(t):
	n = I()
	a = list(map(int, input().strip()))
	b = list(map(int, input().strip()))
	if a == b:
		print(0)
		continue
	(o, e, o2, e2) = (0, 0, 0, 0)
	for i in range(n):
		if a[i] != b[i]:
			if a[i] == 1:
				o += 1
			else:
				e += 1
		elif a[i] == 1:
			o2 += 1
		else:
			e2 += 1
	ans = -1
	if o == e:
		ans = o + e
	if o2 - e2 == 1:
		if ans == -1:
			ans = o2 + e2
		else:
			ans = min(o2 + e2, ans)
	print(ans)


for _ in range(int(input())):
	(ans1, ans2) = (100000, 100000)
	n = int(input())
	str1 = input()
	str2 = input()
	(x, y, v01) = (str1.count('1'), str2.count('1'), 0)
	for i in range(n):
		if str1[i] != str2[i]:
			v01 += 1
	if x == y:
		ans1 = v01
	if y == n - x + 1:
		ans2 = n - v01
	if ans1 == ans2 == 100000:
		print(-1)
	else:
		print(min(ans1, ans2))


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	counts = [0, 0, 0, 0]
	ones = [0, 0]
	for i in range(n):
		counts[int(a[i]) * 2 + int(b[i])] += 1
		ones[0] += a[i] == '1'
		ones[1] += b[i] == '1'
	ops = 1000000000.0
	if ones[0] == ones[1]:
		ops = min(ops, counts[1] + counts[2])
	if n - ones[0] + 1 == ones[1]:
		if counts[3]:
			counts = [counts[2], counts[3] - 1, counts[0], counts[1] + 1]
			ops = min(ops, counts[1] + counts[2] + 1)
		else:
			counts = [counts[2] - 1, counts[3], counts[0] + 1, counts[1]]
			ops = min(ops, counts[1] + counts[2] + 1)
	if ops > 100000000.0:
		print(-1)
	else:
		print(ops)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = input()[:-1]
	s2 = input()[:-1]
	pos1 = [0, 0]
	pos2 = [0, 0]
	comp = int(s2, 2)
	for i in range(len(s)):
		if s[i] != s2[i]:
			pos1[int(s[i])] += 1
		else:
			pos2[int(s[i])] += 1
	ans = []
	if pos1[0] == pos1[1]:
		ans.append(sum(pos1))
	if pos2[1] == pos2[0] + 1:
		ans.append(sum(pos2))
	if ans:
		print(min(ans))
	else:
		print(-1)


import io, os
import sys
from sys import stdin
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, namedtuple
from heapq import heappush, heappop
from math import gcd, ceil, floor, factorial, sqrt
from itertools import combinations, permutations
input = sys.stdin.readline

def work_1(n, a, b):
	if a == b:
		return 0
	if a.count('1') == 0:
		return -1
	cnt = 0
	zero = one = 0
	t1 = t2 = 0
	for i in range(n):
		if a[i] != b[i]:
			cnt += 1
			if a[i] == '0':
				t1 += 1
			else:
				t2 += 1
		elif a[i] == '0':
			zero += 1
		else:
			one += 1
	INF = float('inf')
	ans = INF
	if cnt % 2 == 0 and t1 == t2 and (t1 > 0):
		ans = min(ans, cnt)
	if zero >= 0 and one > 0 and ((one + zero) % 2 == 1) and (zero + 1 == one):
		ans = min(ans, one + zero)
	if cnt % 2 == 1:
		if one == 1 and zero == 0:
			ans = min(ans, 1)
	if ans == INF:
		ans = -1
	return ans

def work(n, a, b):
	if a == b:
		return 0
	t01 = t10 = 0
	t0 = t1 = 0
	for i in range(n):
		if a[i] == b[i]:
			if a[i] == '0':
				t0 += 1
			else:
				t1 += 1
		elif a[i] == '0':
			t01 += 1
		else:
			t10 += 1
	INF = float('inf')
	ans = INF
	if (t01 + t10) % 2 == 0 and t01 == t10 and (t01 > 0):
		ans = min(ans, t01 + t10)
	if (t0 + t1) % 2 == 1 and t0 + 1 == t1:
		ans = min(ans, t0 + t1)
	if ans == INF:
		ans = -1
	return ans

def main():
	test = int(input())
	for idt in range(test):
		n = int(input())
		a = input().strip('\r\n')
		b = input().strip('\r\n')
		res = work_1(n, a, b)
		print(res)
	return
main()


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	n1 = 0
	for i in a:
		if i == '1':
			n1 += 1
	n2 = 0
	for i in b:
		if i == '1':
			n2 += 1
	noc = 0
	for i in range(n):
		if a[i] != b[i]:
			noc += 1
	ans = 999999
	if noc == 0:
		ans = 0
	elif n1 == 0:
		ans = -1
	else:
		if n1 == n2:
			ans = noc
		if n1 == n - n2 + 1:
			ans = min(ans, n - noc)
	if ans == 999999:
		ans = -1
	print(ans)


from sys import stdin, stdout, setrecursionlimit
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
from bisect import bisect, bisect_left
from time import time
from itertools import permutations as per
from heapq import heapify, heappush, heappop, heappushpop
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\r\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
nCr = lambda x, y: f[x] * inv(f[y] * f[x - y] % N) % N
inv = lambda x: pow(x, N - 2, N)
sumx = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I()
	a = [int(i) for i in S()]
	b = [int(i) for i in S()]
	m = [0, 0]
	o = [0, 0]
	for i in range(n):
		if a[i] == b[i]:
			m[0] += 1
			m[1] += a[i]
		else:
			o[0] += 1
			o[1] += a[i]
	ans = inf
	if m[0] % 2 and m[0] // 2 + 1 == m[1]:
		ans = m[0]
	if o[0] % 2 == 0 and o[0] // 2 == o[1]:
		ans = min(ans, o[0])
	print(ans if ans != inf else -1)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	(x, x1, y, y1) = (0, 0, 0, 0)
	for i in range(n):
		if a[i] == b[i]:
			x += 1
			if a[i] == '1':
				x1 += 1
		else:
			y += 1
			if a[i] == '1':
				y1 += 1
	flag = 1
	ans = 10 ** 9
	if x % 2 != 0 and (x + 1) // 2 == x1:
		ans = x
		flag = 0
	if y % 2 == 0 and y // 2 == y1:
		ans = min(ans, y)
		flag = 0
	if flag:
		print(-1)
	else:
		print(ans)


def solve():
	n = int(input())
	a = list(input())
	b = list(input())
	bcount = b.count('1')
	acount = a.count('1')
	aicount = n - acount + 1
	if acount != bcount and aicount != bcount:
		print(-1)
		return
	diffcount = 0
	for i in range(len(a)):
		if a[i] != b[i]:
			diffcount += 1
	if acount != bcount:
		print(n - diffcount)
		return
	if aicount != bcount:
		print(diffcount)
		return
	print(min(diffcount, n - diffcount))
for _ in range(int(input())):
	solve()


import math, sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
mod1 = 1000000007
mod2 = 998244353
for _ in range(I()):
	n = I()
	a = S()
	b = S()
	p = a.count('1')
	q = b.count('1')
	c = ans = 0
	for i in range(n):
		c += a[i] != b[i]
	if p == q:
		if n - p + 1 == q:
			ans = min(c, n - c)
		else:
			ans = c
	elif n - p + 1 == q:
		ans = n - c
	else:
		ans = -1
	print(ans)