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 (Clang 3.8.0) (judge verdict: Accepted)

#include <algorithm>
#include <iostream>
#include <vector>
#include <bitset>
#include <set>
using namespace std;

typedef long long ll;
typedef pair <ll, ll> pii;

const ll MAX_N = 4e3 + 6, inf = 1e9 + 7;
string A, B;
bitset <MAX_N> a, b, c, d, Lf[MAX_N], Rt[MAX_N];
vector <pii> seg;
ll n, ans = inf;
pii Arr[MAX_N];
ll mx[MAX_N];

bool cmp (pii a, pii b) {
	return a.second < b.second;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> A >> B;
    n = A.size();

    for (ll i = 0; i < n; i++) {
        a[i] = (A[n - i - 1] == '1');
        b[i] = (B[n - i - 1] == '1');
        d[i] = true;
    }
    ll tmp = b.count() + a.count();
    if (b.count() == 0) {
    	if (a.count() == 0)
    		cout << "0\n";
    	else
    		cout << "-1\n";

    	return 0;
    }

    for (ll i = 0; i < n; i++) {
    	ll ind = -1, cnt = 0;
    	for (ll j = i; ~j; j--, cnt++)
    		if (B[j] == '1') {
    			ind = j;
    			break;
    		}

    	if (ind == -1)
    		for (ll j = n - 1; ~j; j--, cnt++)
    			if (B[j] == '1') {
    				ind = j;
    				break;
    			}

    	Arr[i].first = cnt;
    	cnt = 0;
    	ind = -1;
    	for (ll j = i; j < n; j++, cnt++)
    		if (B[j] == '1') {
    			ind = j;
    			break;
    		}

    	if (ind == -1)
    		for (ll j = 0; j < n - 1; j++, cnt++)
    			if (B[j] == '1') {
    				ind = j;
    				break;
    			}

    	Arr[i].second = cnt;
  		if (A[i] == '1')
  			seg.push_back({Arr[i].second, Arr[i].first});
    }

    sort(seg.begin(), seg.end());
    for (ll i = seg.size() - 1; ~i; i--)
    	mx[i] = max(mx[i + 1], seg[i].second);

    for (ll len = 0; len < n; len++) {
    	ll res = inf;
    	for (ll i = len; i < n; i++) {
    		ll ind = upper_bound(seg.begin(), seg.end(), pii(i, inf)) - seg.begin();
    		res = min(res, i + (i - len) + (mx[ind] << 1));
    	}

        c = a;
        c = (c >> len) | ((c << (n - len)) & d);

        res += tmp - ((b & c).count() << 1);
        ans = min(ans, res);
    }

    seg.clear();
    for (int i = 0; i < n; i++)
    	if (A[i] == '1')
    		seg.push_back(Arr[i]);

	sort(seg.begin(), seg.end());
    for (ll i = seg.size() - 1; ~i; i--)
    	mx[i] = max(mx[i + 1], seg[i].second);

    for (ll len = 0; len < n; len++) {
    	ll res = inf;
    	for (ll i = len; i < n; i++) {
    		ll ind = upper_bound(seg.begin(), seg.end(), pii(i, inf)) - seg.begin();
    		res = min(res, i + (i - len) + (mx[ind] << 1));
    	}

        c = a;
        c = ((c << len) & d) | ((c << len) >> n);

        res += tmp - ((b & c).count() << 1);
        ans = min(ans, res);
    }

	cout << ans << "\n";
    return 0;
}