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 all(v) (v).begin(), (v).end()

using li = long long;

typedef bitset<2001> bs;

int n;
int tt = 0;

bs read() {
    bool rd = true;
    
    bs x = {};
    string s;
    if (rd)
        cin >> s;
    else {
        tt++;
        s = string(2000, '1');
        
        if (tt == 2) {
            s = string(2000, '0');
            s[200] = '1';
        }
            
        //for (int i = 0; i < s.length(); i++)
        //    s[i] = '0' + rand() % 2;
    }
    n = s.length();
    for (int i = 0; i < n; i++)
        x[i] = (s[i] == '1') ? 1 : 0;
    return x;
}

void shift_left(bs& x) {
    int first = x[0];
    x >>= 1;
    x[n - 1] = first;
}

void shift_right(bs& x) {
    int last = x[n - 1];
    x[n - 1] = 0;
    x <<= 1;
    x[0] = last;
}

void print(bs& x) {
    for (int i = 0; i < n; i++)
        printf("%d", x[i] ? 1 : 0);
    printf("\n");
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    bs a_orig = read(), b_orig = read();
    
    //printf("%d\n", n);
    
    // 1 0 are problems
    
    const int INF = 1e9;
    int answer = INF;
    
    if (b_orig.count() == 0) {
        cout << ((a_orig.count() != 0) ? "-1" : "0") << endl;
        return 0;
    }
    
    bs b_neg = ~b_orig;
    
    for (int t = 0; t < 2; t++) {
        bs a = a_orig;
        bs mask_ok = b_orig;
        
        for (int l_shift = 0; l_shift <= n; l_shift++) {
            bs cur_a = a;
            
            //print(cur_a);
            //print(b);
            
            bs cur_ok = mask_ok;
            
            for (int r_shift = 0; r_shift <= n * 2; r_shift++) {
                bs need = cur_a & b_neg;
                
                if (l_shift + r_shift >= answer)
                    break;
                
#if 0
                if (l_shift == 1 && r_shift == 0) {
                    printf("cur_a: "); print(cur_a);
                    printf("b    : "); print(b_orig);
                    printf("need : "); print(need);
                    printf("have : "); print(cur_ok);
                }
#endif
                
                if ((cur_ok & need) == need) {
                    int can = (int)(l_shift + r_shift + (cur_a ^ b_orig).count());
                    //if (can < answer)
                    //    printf("can %d with shifts %d %d\n", can, l_shift, r_shift);
                    answer = min(answer, can);
                }
                
                (t == 0 ? shift_right : shift_left)(cur_a);
                (t == 0 ? shift_right : shift_left)(cur_ok);
                cur_ok |= b_orig;
            }
            
            (t == 0 ? shift_left : shift_right)(a);
            (t == 0 ? shift_left : shift_right)(mask_ok);
            mask_ok |= b_orig;
        }
    }
    
    cout << answer << "\n";
    
    return 0;
}