Mark has just purchased a rack of $n$ lightbulbs. The state of the lightbulbs can be described with binary string $s = s_1s_2\dots s_n$, where $s_i={1}$ means that the $i$-th lightbulb is turned on, while $s_i={0}$ means that the $i$-th lightbulb is turned off.

Unfortunately, the lightbulbs are broken, and the only operation he can perform to change the state of the lightbulbs is the following:

Select an index $i$ from $2,3,\dots,n-1$ such that $s_{i-1}\ne s_{i+1}$.

Toggle $s_i$. Namely, if $s_i$ is ${0}$, set $s_i$ to ${1}$ or vice versa.

Mark wants the state of the lightbulbs to be another binary string $t$. Help Mark determine the minimum number of operations to do so.


-----Input-----

The first line of the input contains a single integer $q$ ($1\leq q\leq 10^4$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($3\leq n\leq 2\cdot 10^5$) — the number of lightbulbs.

The second line of each test case contains a binary string $s$ of length $n$ — the initial state of the lightbulbs.

The third line of each test case contains a binary string $t$ of length $n$ — the final state of the lightbulbs.

It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$.


-----Output-----

For each test case, print a line containing the minimum number of operations Mark needs to perform to transform $s$ to $t$. If there is no such sequence of operations, print $-1$.


-----Examples-----

Input
4
4
0100
0010
4
1010
0100
5
01001
00011
6
000101
010011
Output
2
-1
-1
5


-----Note-----

In the first test case, one sequence of operations that achieves the minimum number of operations is the following.

Select $i=3$, changing ${01}{{0}}{0}$ to ${01}{{1}}{0}$.

Select $i=2$, changing ${0}{{1}}{10}$ to ${0}{{0}}{10}$.

In the second test case, there is no sequence of operations because one cannot change the first digit or the last digit of $s$.

In the third test case, even though the first digits of $s$ and $t$ are the same and the last digits of $s$ and $t$ are the same, it can be shown that there is no sequence of operations that satisfies the condition.

In the fourth test case, one sequence that achieves the minimum number of operations is the following:

Select $i=3$, changing ${00}{{0}}{101}$ to ${00}{{1}}{101}$.

Select $i=2$, changing ${0}{{0}}{1101}$ to ${0}{{1}}{1101}$.

Select $i=4$, changing ${011}{{1}}{01}$ to ${011}{{0}}{01}$.

Select $i=5$, changing ${0110}{{0}}{1}$ to ${0110}{{1}}{1}$.

Select $i=3$, changing ${01}{{1}}{011}$ to ${01}{{0}}{011}$.

Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Sorting

Combinatorics

Greedy algorithms

Mathematics
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
def f(s, t, n):
	if s[0] != t[0] or s[n - 1] != t[n - 1]:
		return -1
	(i, j, edits) = (0, 0, 0)
	while True:
		while i < n - 1 and s[i] == s[i + 1]:
			i += 1
		while j < n - 1 and t[j] == t[j + 1]:
			j += 1
		if i < n - 1 and j < n - 1:
			edits += abs(i - j)
		elif i == n - 1 and j == n - 1:
			return edits + abs(i - j)
		else:
			return -1
		i += 1
		j += 1
T = int(input())
while T:
	n = int(input())
	(s, t) = (input(), input())
	print(f(s, t, n))
	T -= 1


for _ in range(int(input())):
	n = int(input())
	numb = input()
	test = input()
	(nu, t) = ([], [])
	for i in range(n - 1):
		if numb[i] != numb[i + 1]:
			nu.append(i)
		if test[i] != test[i + 1]:
			t.append(i)
	if numb[0] != test[0] or numb[n - 1] != test[n - 1] or len(nu) != len(t):
		print('-1')
	else:
		print(sum((abs(nu[i] - t[i]) for i in range(len(nu)))))


for _ in range(int(input())):
	N = int(input())
	S = input()
	T = input()
	(s, t) = ([], [])
	for i in range(N - 1):
		if S[i] != S[i + 1]:
			s.append(i)
		if T[i] != T[i + 1]:
			t.append(i)
	if S[0] != T[0] or S[N - 1] != T[N - 1] or len(s) != len(t):
		print(-1)
		continue
	print(sum((abs(s[i] - t[i]) for i in range(len(s)))))


T = int(input())
while T:
	T -= 1
	n = int(input())
	s = list(input())
	t = list(input())
	ls = []
	lt = []
	if s[0] != t[0] or s[-1] != t[-1]:
		print('-1')
		continue
	for i in range(n - 1):
		if s[i] != s[i + 1]:
			ls.append(i)
		if t[i] != t[i + 1]:
			lt.append(i)
	if len(ls) != len(lt):
		print('-1')
		continue
	ans = 0
	for i in range(len(ls)):
		ans += abs(ls[i] - lt[i])
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	a_ = []
	b_ = []
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			a_.append(i)
		if b[i] != b[i + 1]:
			b_.append(i)
	if len(a_) != len(b_) or a[0] != b[0] or a[-1] != b[-1]:
		print(-1)
		continue
	count = 0
	for i in range(len(a_)):
		count += abs(a_[i] - b_[i])
	print(count)


k = int(input())
while k:
	p = int(input())
	s = input()
	t = input()
	a = []
	b = []
	ac = 0
	bc = 0
	for i in range(1, p):
		if s[i] != s[i - 1]:
			a.append(i)
			ac += 1
		if t[i] != t[i - 1]:
			b.append(i)
			bc += 1
	if s[0] != t[0] or ac != bc:
		print(-1)
	else:
		check = 0
		for i in range(len(a)):
			check += abs(a[i] - b[i])
		print(check)
	k -= 1


num_inp = lambda : int(input())
arr_inp = lambda : list(map(int, input().split()))
sp_inp = lambda : map(int, input().split())
str_inp = lambda : input()
for _ in range(int(input())):
	N = int(input())
	S = input()
	T = input()
	(s, t) = ([], [])
	for i in range(N - 1):
		if S[i] != S[i + 1]:
			s.append(i)
		if T[i] != T[i + 1]:
			t.append(i)
	if S[0] != T[0] or S[N - 1] != T[N - 1] or len(s) != len(t):
		print(-1)
		continue
	print(sum((abs(s[i] - t[i]) for i in range(len(s)))))


def p(s):
	k = 0
	q = 1
	for i in s:
		if i == '1':
			if q:
				k += 1
				q = 0
		else:
			q = 1
	return k
for i in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	s = 0
	if s1[0] != s2[0] or s1[-1] != s2[-1] or p(s1) != p(s2):
		print(-1)
	else:
		l1 = l2 = r1 = r2 = 0
		y = p(s1)
		for i in range(y):
			while s1[l1] != '1':
				l1 += 1
			r1 = l1
			while r1 < n and s1[r1] == '1':
				r1 += 1
			while s2[l2] != '1':
				l2 += 1
			r2 = l2
			while r2 < n and s2[r2] == '1':
				r2 += 1
			s += abs(l1 - l2) + abs(r1 - r2)
			l1 = r1
			l2 = r2
		print(s)


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1[0] != s2[0] or s1[-1] != s2[-1]:
		print(-1)
		continue
	l1 = []
	l2 = []
	l3 = []
	l4 = []
	ans = 0
	for i in range(n - 1):
		if s1[i] == '0' and s1[i + 1] == '1':
			l1.append(i)
		if s1[i] == '1' and s1[i + 1] == '0':
			l2.append(i)
		if s2[i] == '0' and s2[i + 1] == '1':
			l3.append(i)
		if s2[i] == '1' and s2[i + 1] == '0':
			l4.append(i)
	if len(l1) == len(l3) and len(l2) == len(l4):
		for i in range(len(l1)):
			ans += abs(l3[i] - l1[i])
		for i in range(len(l2)):
			ans += abs(l4[i] - l2[i])
	else:
		print(-1)
		continue
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input()))
	t = list(map(int, input()))
	(l1, l2) = ([], [])
	if s[0] != t[0] or s[n - 1] != t[n - 1]:
		print(-1)
	else:
		for j in range(n - 1):
			if s[j] != s[j + 1]:
				l1.append(j)
			if t[j] != t[j + 1]:
				l2.append(j)
		if len(l1) != len(l2):
			print(-1)
		else:
			print(sum((abs(l1[q] - l2[q]) for q in range(len(l1)))))


t_ = int(input())
for _ in range(t_):
	n = int(input())
	s = input().strip()
	t = input().strip()
	if s[0] != t[0]:
		print(-1)
		continue
	s_dist = []
	for i in range(1, n):
		if s[i] != s[i - 1]:
			s_dist.append(i)
	total = 0
	t_seg = 0
	for i in range(1, n):
		if t[i] != t[i - 1]:
			t_seg += 1
			if t_seg > len(s_dist):
				break
			total += abs(i - s_dist[t_seg - 1])
	if t_seg != len(s_dist):
		print(-1)
	else:
		print(total)


q = int(input())
for _ in range(q):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1[0] != s2[0] or s1[-1] != s2[-1]:
		print(-1)
	else:
		tmp1 = []
		tmp = ''
		start = -1
		for (idx, char) in enumerate(s1):
			if char != tmp:
				if tmp == '1':
					tmp1.append([start, idx - 1])
				tmp = char
				start = idx
		if tmp == '1':
			tmp1.append([start, len(s1) - 1])
		tmp2 = []
		tmp = ''
		start = -1
		for (idx, char) in enumerate(s2):
			if char != tmp:
				if tmp == '1':
					tmp2.append([start, idx - 1])
				tmp = char
				start = idx
		if tmp == '1':
			tmp2.append([start, len(s2) - 1])
		if len(tmp1) != len(tmp2):
			print(-1)
		else:
			ans = 0
			for ([x, y], [a, b]) in zip(tmp1, tmp2):
				ans += abs(x - a) + abs(y - b)
			print(ans)


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1[0] == s2[0] and s1[-1] == s2[-1]:
		cur = s1[0]
		num = 0
		l1 = []
		l2 = []
		for i in s1 + '2':
			if i == cur:
				num += 1
			else:
				l1.append(num)
				num = 1
				cur = i
		cur = s1[0]
		num = 0
		for i in s2 + '2':
			if i == cur:
				num += 1
			else:
				l2.append(num)
				num = 1
				cur = i
		if len(l1) != len(l2):
			print(-1)
		else:
			ans = 0
			for i in range(len(l1) - 1):
				ans += abs(l1[i] - l2[i])
				l1[i + 1] = l1[i + 1] + l1[i] - l2[i]
			print(ans)
	else:
		print(-1)


def find(s):
	st = s[0]
	a = []
	c = 0
	for i in range(len(s)):
		if s[i] == st:
			c += 1
		else:
			a.append(c)
			c = 1
			st = s[i]
	if c > 0:
		a.append(c)
	return a
for _ in range(int(input())):
	n = input()
	s = input()
	t = input()
	a = find(s)
	b = find(t)
	ok = True
	if s[0] != t[0] or len(a) != len(b):
		ok = False
	ans = 0
	l1 = r1 = l2 = r2 = 0
	if ok:
		for i in range(len(a)):
			if i % 2 == 0:
				l1 += a[i]
				r1 += a[i]
				l2 += b[i]
				r2 += b[i]
			else:
				r1 += a[i]
				r2 += b[i]
				ans += abs(r2 - r1) + abs(l1 - l2)
				l1 += a[i]
				l2 += b[i]
	if ok:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	if s[0] != t[0] or s[-1] != t[-1]:
		print(-1)
		continue
	s_seg = 0
	s_fl = False
	t_seg = 0
	t_fl = False
	for i in range(n):
		if not s_fl and s[i] == '1':
			s_fl = True
		if s_fl and s[i] == '0':
			s_fl = False
			s_seg += 1
		if not t_fl and t[i] == '1':
			t_fl = True
		if t_fl and t[i] == '0':
			t_fl = False
			t_seg += 1
	s_seg += s_fl
	t_seg += t_fl
	if s_seg != t_seg:
		print(-1)
		continue
	ans = 0
	s_pointer = 0
	t_pointer = 0
	while s_pointer < n and t_pointer < n:
		while t_pointer < n and t[t_pointer] != '1':
			t_pointer += 1
		t_start = t_pointer
		while t_pointer < n and t[t_pointer] != '0':
			t_pointer += 1
		t_end = t_pointer
		while s_pointer < n and s[s_pointer] != '1':
			s_pointer += 1
		s_start = s_pointer
		while s_pointer < n and s[s_pointer] != '0':
			s_pointer += 1
		s_end = s_pointer
		ans += abs(t_start - s_start) + abs(t_end - s_end)
	print(ans)


def miis():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, list(input())))
	t = list(map(int, list(input())))
	l = 0
	cs = []
	ct = []
	for i in range(n):
		if s[i]:
			if l:
				cs[-1][1] += 1
			else:
				cs.append([i, i])
				l = 1
		else:
			l = 0
	l = 0
	for i in range(n):
		if t[i]:
			if l:
				ct[-1][1] += 1
				l = 1
			else:
				ct.append([i, i])
				l = 1
		else:
			l = 0
	if len(ct) == len(cs) and s[0] == t[0] and (s[-1] == t[-1]):
		ans = 0
		for i in range(len(ct)):
			ans += abs(ct[i][0] - cs[i][0]) + abs(ct[i][1] - cs[i][1])
		print(ans)
	else:
		print(-1)


import math, bisect
xx = int(input())

def cal(n, s, t):
	if s[0] != t[0] or s[-1] != t[-1]:
		return -1
	p = []
	q = []
	a = 0
	for i in range(n):
		if s[i] != s[a]:
			p.append([a, i - 1])
			a = i
		if i == n - 1:
			p.append([a, i])
	a = 0
	for i in range(n):
		if t[i] != t[a]:
			q.append([a, i - 1])
			a = i
		if i == n - 1:
			q.append([a, i])
	if p == q:
		return 0
	if len(p) != len(q):
		return -1
	ans = 0
	for i in range(len(p)):
		(a, b) = p[i]
		(c, d) = q[i]
		ans += abs(d - b)
	return ans
for jj in range(xx):
	n = int(input())
	s = input()
	t = input()
	print(cal(n, s, t))


t = eval(input())
for i in range(t):
	n = eval(input())
	s1 = input()
	s2 = input()
	(k1, k2) = (1, 1)
	for i in range(0, len(s1) - 1):
		if s1[i] != s1[i + 1]:
			k1 += 1
	for i in range(0, len(s2) - 1):
		if s2[i] != s2[i + 1]:
			k2 += 1
	if k1 != k2 or s1[0] != s2[0] or s1[-1] != s2[-1]:
		print(-1)
		continue
	(l1, l2) = ([], [])
	for i in range(0, len(s1) - 1):
		if s1[i] != s1[i + 1]:
			l1.append(i)
	for i in range(0, len(s2) - 1):
		if s2[i] != s2[i + 1]:
			l2.append(i)
	sum = 0
	for i in range(len(l1)):
		sum += abs(l1[i] - l2[i])
	print(sum)