Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with $2$ rows and $n$ columns, every element of which is either $0$ or $1$. In one move you can swap two values in neighboring cells.

More formally, let's number rows $1$ to $2$ from top to bottom, and columns $1$ to $n$ from left to right. Also, let's denote a cell in row $x$ and column $y$ as $(x, y)$. We consider cells $(x_1, y_1)$ and $(x_2, y_2)$ neighboring if $|x_1 - x_2| + |y_1 - y_2| = 1$.

Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.


-----Input-----

The first line contains an integer $n$ ($1 \leq n \leq 200000$) — the number of columns in the puzzle.

Following two lines describe the current arrangement on the puzzle. Each line contains $n$ integers, every one of which is either $0$ or $1$.

The last two lines describe Alice's desired arrangement in the same format.


-----Output-----

If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print $-1$.


-----Examples-----

Input
5
0 1 0 1 0
1 1 0 0 1
1 0 1 0 1
0 0 1 1 0
Output
5
Input
3
1 0 0
0 0 0
0 0 0
0 0 0
Output
-1


-----Note-----

In the first example the following sequence of swaps will suffice:

$(2, 1), (1, 1)$,

$(1, 2), (1, 3)$,

$(2, 2), (2, 3)$,

$(1, 4), (1, 5)$,

$(2, 5), (2, 4)$.

It can be shown that $5$ is the minimal possible answer in this case.

In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $-1$.

Difficulty: VERY_HARD
Tags: Greedy algorithms

Dynamic programming

Constructive algorithms
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
n = int(input())
m_1 = [input().strip()[::2], input().strip()[::2]]
m_2 = [input().strip()[::2], input().strip()[::2]]
top = 0
bot = 0
tot = 0
for i in range(n):
	top += (m_1[0][i] == '1') - (m_2[0][i] == '1')
	bot += (m_1[1][i] == '1') - (m_2[1][i] == '1')
	if top > 0 and bot < 0:
		tot += 1
		top -= 1
		bot += 1
	elif top < 0 and bot > 0:
		tot += 1
		top += 1
		bot -= 1
	tot += abs(top) + abs(bot)
if top + bot != 0:
	print(-1)
else:
	print(tot)


num = int(input())
k1 = [input().strip()[::2], input().strip()[::2]]
k2 = [input().strip()[::2], input().strip()[::2]]
top = 0
bot = 0
tot = 0
for j in range(num):
	top += (k1[0][j] == '1') - (k2[0][j] == '1')
	bot += (k1[1][j] == '1') - (k2[1][j] == '1')
	if top > 0 and bot < 0:
		tot += 1
		top -= 1
		bot += 1
	elif top < 0 and bot > 0:
		tot += 1
		top += 1
		bot -= 1
	tot += abs(top) + abs(bot)
if top + bot != 0:
	print(-1)
else:
	print(tot)


num = int(input())
m1 = [input().strip()[::2], input().strip()[::2]]
m2 = [input().strip()[::2], input().strip()[::2]]
top = 0
bot = 0
tot = 0
for j in range(num):
	top += (m1[0][j] == '1') - (m2[0][j] == '1')
	bot += (m1[1][j] == '1') - (m2[1][j] == '1')
	if top > 0 and bot < 0:
		tot += 1
		top -= 1
		bot += 1
	elif top < 0 and bot > 0:
		tot += 1
		top += 1
		bot -= 1
	tot += abs(top) + abs(bot)
if top + bot != 0:
	print(-1)
else:
	print(tot)


n = int(input())
s = (input()[::2], input()[::2])
t = (input()[::2], input()[::2])
d = [0, 0]
total = 0
for y in range(n):
	for x in (0, 1):
		d[x] += (s[x][y] == '1') - (t[x][y] == '1')
	if d[0] > 0 and d[1] < 0:
		total += 1
		d[0] -= 1
		d[1] += 1
	elif d[0] < 0 and d[1] > 0:
		total += 1
		d[0] += 1
		d[1] -= 1
	total += abs(d[0]) + abs(d[1])
print(total if d == [0, 0] else -1)


import time

def main():
	n = int(input())
	curr = [[int(x) for x in input().split(' ')] for _ in range(2)]
	want = [[int(x) for x in input().split(' ')] for _ in range(2)]
	out = 0
	s1 = 0
	s2 = 0
	for x in range(n):
		out += abs(s1) + abs(s2)
		s1 += curr[0][x]
		s2 += curr[1][x]
		s1 -= want[0][x]
		s2 -= want[1][x]
		if abs(s1 + s2) < abs(s1) + abs(s2):
			if abs(s1) <= abs(s2):
				out += abs(s1)
				s2 += s1
				s1 = 0
			else:
				out += abs(s2)
				s1 += s2
				s2 = 0
	if s1 != 0 or s2 != 0:
		print(-1)
	else:
		print(out)
main()


n = int(input())
a = [list(map(int, input().split())), list(map(int, input().split()))]
b = [list(map(int, input().split())), list(map(int, input().split()))]
s1 = 0
s2 = 0
stp = 0
for i in range(n):
	s1 += a[0][i] - b[0][i]
	s2 += a[1][i] - b[1][i]
	if s1 * s2 < 0:
		k = min(abs(s1), abs(s2))
		if s1 < 0:
			s1 += k
			s2 -= k
		else:
			s2 += k
			s1 -= k
		stp += k
	stp += abs(s1) + abs(s2)
if s1 + s2 != 0:
	print(-1)
else:
	print(stp)


n = int(input())
a = [list(map(int, input().split(' '))), list(map(int, input().split(' ')))]
b = [list(map(int, input().split(' '))), list(map(int, input().split(' ')))]
d1 = 0
d2 = 0
ans = 0
for i in range(n):
	d1 += a[0][i] - b[0][i]
	d2 += a[1][i] - b[1][i]
	if d1 * d2 < 0:
		k = min(abs(d1), abs(d2))
		if d1 < 0:
			d1 += k
			d2 -= k
		else:
			d2 += k
			d1 -= k
		ans += k
	ans += abs(d1) + abs(d2)
if d1 + d2 != 0:
	print(-1)
else:
	print(ans)


n = int(input())
s = (input()[::2], input()[::2])
t = (input()[::2], input()[::2])
d = [0, 0]
total = 0
for i in range(n):
	for j in (0, 1):
		d[j] += (s[j][i] == '1') - (t[j][i] == '1')
	if d[0] > 0 and d[1] < 0:
		total += 1
		d[0] -= 1
		d[1] += 1
	elif d[0] < 0 and d[1] > 0:
		total += 1
		d[0] += 1
		d[1] -= 1
	total += abs(d[0]) + abs(d[1])
print(total if d == [0, 0] else -1)