Problem:
Score : 1000 points

Problem Statement

You have two strings A = A_1 A_2 ... A_n and B = B_1 B_2 ... B_n of the same length consisting of 0 and 1.

You can transform A using the following operations in any order and as many times as you want:

Shift A by one character to the left (i.e., if A = A_1 A_2 ... A_n, replace A with A_2 A_3 ... A_n A_1).

Shift A by one character to the right (i.e., if A = A_1 A_2 ... A_n, replace A with A_n A_1 A_2 ... A_{n-1}).

Choose any i such that B_i = 1. Flip A_i (i.e., set A_i = 1 - A_i).

You goal is to make strings A and B equal.

Print the smallest number of operations required to achieve this, or -1 if the goal is unreachable.

Constraints

1 \leq |A| = |B| \leq 2,000

A and B consist of 0 and 1.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print the smallest number of operations required to make strings A and B equal, or -1 if the goal is unreachable.

Sample Input 1

1010
1100

Sample Output 1

3

Here is one fastest way to achieve the goal:

Flip A_1: A = 0010

Shift A to the left: A = 0100

Flip A_1 again: A = 1100

Sample Input 2

1
0

Sample Output 2

-1

There is no way to flip the only bit in A.

Sample Input 3

11010
10001

Sample Output 3

4

Here is one fastest way to achieve the goal:

Shift A to the right: A = 01101

Flip A_5: A = 01100

Shift A to the left: A = 11000

Shift A to the left again: A = 10001

Sample Input 4

0100100
1111111

Sample Output 4

5

Flip A_1, A_3, A_4, A_6 and A_7 in any order.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

const int MAXN = 2000 + 100;
const int INF = 0x1f1f1f1f;

int n, ans;
char a[MAXN], b[MAXN];
int l[MAXN], r[MAXN];

bool check()
{
	for(int i = 0; i < n; i++)
		if(b[i] == '1')
			return true;
	return false;
}

void init()
{
	int last = -1;
	for(int i = 0; i < n; i++)
		last = (b[i] == '1' ? i : last);
	for(int i = 0; i < n; i++)
		last = (b[i] == '1' ? i : last), l[i] = (i - last + n) % n;
	last = -1;
	for(int i = n - 1; i >= 0; i--)
		last = (b[i] == '1' ? i : last);
	for(int i = 0; i < n; i++)
		last = (b[i] == '1' ? i : last), r[i] = (last - i + n) % n;
}

typedef pair<int, int> pii;

void work()
{
	init();
	for(int d = 0; d <= n - 1; d++)
	{
		static pii vec[MAXN];
		static int lef[MAXN], rig[MAXN];
		int tot = 0;
		for(int i = 0; i < n; i++)
			if(a[i] != b[(i + d) % n] && !(r[i] <= d))
				vec[++tot] = make_pair(l[i], r[(i + d) % n]);
		sort(vec + 1, vec + tot + 1);
		lef[0] = 0;
		for(int i = 1; i <= tot; i++)
			lef[i] = max(lef[i - 1], vec[i].first);
		rig[tot + 1] = 0;
		for(int i = tot; i >= 1; i--)
			rig[i] = max(rig[i + 1], vec[i].second);
		int tmp = INF;
		for(int i = 0; i <= tot; i++)
			tmp = min(tmp, 2 * lef[i] + d + 2 * rig[i + 1]);
		for(int i = 0; i < n; i++)
			tmp += (a[i] != b[(i + d) % n]);
		ans = min(ans, tmp);
		// cerr << d << ' ' << tmp << ' ' << tot << endl;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	// freopen("1.in", "r", stdin);
	// freopen("1.out", "w", stdout);

	cin >> a;
	cin >> b;
	n = strlen(a);

	if(!check())
	{
		cout << -1 << endl;
		return 0;
	}

	ans = INF;
	work();
	reverse(a, a + n);
	reverse(b, b + n);
	work();

	cout << ans << endl;



	return 0;
}