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: Accepted)

#include <bits/stdc++.h>
using namespace std;

string reverse(string s)
{
    reverse(begin(s), end(s));
    return s;
}

bool all_zero(const string& s)
{
    for (const char c : s)
        if (c != '0')
            return false;
    return true;
}

// You have a list of pairs (a_i, b_i).
// Minimize A + B, subject to the constraint that
// for each i, either a_i <= A or b_i <= B
int f(vector<pair<int, int>> costs)
{
    if (costs.empty()) return 0;

    sort(begin(costs), end(costs));
    int x = costs.back().first, y = 0, result = x+y;
    while (!costs.empty())
    {
        y = max(y, costs.back().second);
        costs.pop_back();
        x = costs.empty() ? 0 : costs.back().first;
        result = min(result, x+y);
    }
    return result;
}

int solve(const string& A, const string& B)
{
    if (all_zero(B))
        return all_zero(A) ? 0 : -1;

    const int N = int(A.length());

    vector<int> forward_cost(N), backward_cost(N);
    forward_cost[N-1] = B[N-1] == '1' ? 0 : 1 + int(B.find('1'));
    for (int i = N-2; i >= 0; i--)
        forward_cost[i] = B[i] == '1' ? 0 : forward_cost[i+1] + 1;
    backward_cost[0] = B[0] == '1' ? 0 : 1 + int(reverse(B).find('1'));
    for (int i = 1; i < N; i++)
        backward_cost[i] = B[i] == '1' ? 0 : backward_cost[i-1] + 1;

    int result = numeric_limits<int>::max();
    for (int shift = 0; shift < N; shift++)
    {
        int mismatches = 0;
        for (int i = 0; i < N; i++)
            mismatches += A[i] != B[(i+shift)%N];

        vector<pair<int, int>> costs;
        for (int i = 0; i < N; i++)
            if (A[i] != B[(i+shift)%N] && forward_cost[i] > shift)
                costs.push_back({backward_cost[i], forward_cost[i]-shift});

        result = min(result, shift + 2*f(costs) + mismatches);
    }
    return result;
}

int main()
{
    string A, B;
    cin >> A >> B;
    cout << min(solve(A, B), solve(reverse(A), reverse(B))) << endl;
}