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>
const int MAX = 2e3 + 10;
typedef long long i64;
using namespace std;
const double EPS = 1e-6;
const int MOD = 1e9 + 7;
#define mul(a, b) ((i64)(a) * b % MOD)


int minm(int low, int high, int target0, int target1){
	int a1 = -low + high - low + abs(target0 - high);
	int a2 = high + high - low + abs(target0 - low);
	int a3 = -low + high - low + abs(target1 - high);
	int a4 = high + high - low + abs(target1 - low);
	return min(min(a1, a2), min(a3, a4));
}
int main() {

    #ifdef LOCAL_DEBUG
    freopen ("data.in", "r", stdin );
    //freopen ("data.out", "w", stdout );
    #endif
    ios_base::sync_with_stdio(0); cin.tie(0);
    #define endl '\n'
    string a, b;
    cin >> a >>b ;
    bool poss = false;
    int n =(int) a.size();
    int beg = -1;
    for(int i = 0 ;i < n; i++)
    	if(b[i]!= '0'){
    		beg = i;
    		poss = true; break;

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

    vector<int> le(n);
    vector<int>re(n);
    for(int i = beg; i <beg + n; i++){
    	if(b[i % n] == '0'){
    		int j = i;
    		while(b[ j % n] == '0')j++;
    		for(int t = i; t < j; t++){
    			if(a[t % n] == '1'){
    				le[t % n] = t - i + 1;
    				re[t % n] = j - t;
    			}
    		}
    		i = j;
    	}
    }
    int ans = 2 * n;
    for(int i = 0; i < n; i++){
    	string c = a.substr(i) + a.substr(0, i);
    	vector<pair<int, int>> ss;
    	int cur = 0;
    	for(int j = 0; j < n; j++)
    		if(c[j] != b[j]){
    			cur += 1;
    			if(c[j] == '1'){
    				int o = (j - i + n) % n;
    				if(le[o] != 0)
    					ss.push_back({-le[o], re[o]});
    			}
    		}
    	if(ss.size() == 0){
    		ans = min(ans, cur + min(i, n - i));
    		continue;
    	}
    	vector<int> mx(ss.size() + 1);
    	sort(ss.begin(), ss.end());
    	ss.push_back({0, 0});
    	mx[0] = ss[0].second;
    	int mn = minm(ss[0].first, 0, i, -(n - i));
    	for(int j = 1; j < (int)ss.size(); j++){
    		mx[j] = max(mx[j - 1], ss[j].second);
    		mn = min(mn, minm(ss[j].first, mx[j - 1], i, -(n - i)));
    	}
    	cur += mn;
    	ans = min(ans, cur);

    }
    cout << ans << endl;



}