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 <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vl;

const int N = 2003, inf = 1e9;
int n, p[N], val[N];
string a, b;
char cur[N];

int right_check(int pos, int k) {
	int res = 0;
	if (pos + k < n) {
		res = p[pos + k];
		if (pos > 0) {
			res -= p[pos - 1];
		}
	}	else {
		res = p[n - 1];
		if (pos > 0) {
			res -= p[pos - 1];
		}
		int r = (pos + k) % n;
		res += p[r];
	}
	return res > 0;
}

int left_check(int pos, int k) {
	int res = 0;
	if (pos - k >= 0) {
		res = p[pos];
		if (pos - k > 0) {
			res -= p[pos - k - 1];
		}
	}	else {
		res = p[pos];
		int l = (pos - k + n) % n;
		res += p[n - 1];
		if (l > 0) {
			res -= p[l - 1];
		}
	}
	return res > 0;
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	#ifdef LOCAL
		freopen("input.txt", "r", stdin);
	#endif
	
	cin >> a >> b;
	if (a == b) {
		cout << 0;
		return 0;
	}
	n = a.size();
	int ok = 0;
	for (int i = 0; i < n; i++) {
		if (b[i] == '1') {
			ok |= 1;
		}
	}
	if (!ok) {
		cout << -1;
		return 0;
	}
	for (int i = 0; i < n; i++) {
		if (i > 0) {
			p[i] = p[i - 1];
		}
		p[i] += (b[i] == '1');
	}
	int ans = inf;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cur[j] = a[(j + i) % n];
		}
		int m = 0;
		memset(& val, 0, sizeof(val));
		for (int j = 0; j < n; j++) {
			if (cur[j] == b[j]) {
				continue;
			}
			int l = -1, r = n - 1;
			while (l < r - 1) {
				int mid = l + r >> 1;
				if (right_check(j, mid)) {
					r = mid;
				}	else {
					l = mid;
				}
			}
			int L = r;
			l = -1, r = n - 1;
			while (l < r - 1) {
				int mid = l + r >> 1;
				if (left_check(j, mid)) {
					r = mid;
				}	else {
					l = mid;
				}
			}
			int R = r;
			val[L] = max(val[L], R);
			m++;
		}
		int mx = 0;
		for (int j = n - 1; j >= 0; j--) {
			int L = j, R = mx;
			ans = min(ans, L + R + abs(i - (L - R)) + m);
			mx = max(mx, val[j]);
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cur[j] = a[(j - i + n) % n];
		}
		int m = 0;
		memset(& val, 0, sizeof(val));
		for (int j = 0; j < n; j++) {
			if (cur[j] == b[j]) {
				continue;
			}
			int l = -1, r = n - 1;
			while (l < r - 1) {
				int mid = l + r >> 1;
				if (right_check(j, mid)) {
					r = mid;
				}	else {
					l = mid;
				}
			}
			int L = r;
			l = -1, r = n - 1;
			while (l < r - 1) {
				int mid = l + r >> 1;
				if (left_check(j, mid)) {
					r = mid;
				}	else {
					l = mid;
				}
			}
			int R = r;
			val[R] = max(val[R], L);
			m++;
		}
		int mx = 0;
		for (int j = n - 1; j >= 0; j--) {
			int R = j, L = mx;
			ans = min(ans, L + R + abs(i - (R - L)) + m);
			mx = max(mx, val[j]);
		}
	}
	cout << ans;
}