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<iostream>
#include<algorithm>
#include<cstring>
#define f first
#define s second
using namespace std;
int n, i, j, u, sol, sum, oka, okb, x;
char a[2005], b[2005];
int st2[2005], dr2[2005];
pair<int, int> st[2005], dr[2005];
int poz(int p){
    if(p <= 0){
        return p + n;
    }
    if(p > n){
        return p - n;
    }
    return p;
}
int main(){
    cin>> a + 1;
    cin>> b + 1;
    n = strlen(a + 1);
    for(i = 1; i <= n; i++){
        a[i] -= '0';
        b[i] -= '0';
        if(a[i] == 1){
            oka = 1;
        }
        if(b[i] == 1){
            okb = 1;
        }
    }
    if(okb == 0){
        if(oka == 0){
            cout<< 0;
        }
        else{
            cout<< -1;
        }
        return 0;
    }
    u = 1;
    for(i = 1; i <= n; i++){
        while(b[u] == 0){
            u++;
            if(u == n + 1){
                u = 1;
            }
        }
        dr[i] = make_pair( (u - i + n) % n, i);
        dr2[i] = dr[i].f;
        if(u == i){
            u++;
        }
    }
    u = n;
    for(i = n; i >= 1; i--){
        while(b[u] == 0 || u > i + n){
            u--;
            if(u == 0){
                u = n;
            }
        }
        st[i] = make_pair( (i - u + n) % n, i);
        st2[i] = st[i].f;
        if(u == i){
            u--;
        }
    }
    sort(st + 1, st + n + 1);
    sort(dr + 1, dr + n + 1);
    sol = 1000000;
    for(i = 0; i < n; i++){
        u = n;
        x = 0;
        for(j = 1; j <= n; j++){
            if(b[ poz(j - i) ] != a[j]){
                x++;
            }
        }
        for(j = 0; j < n; j++){
            while(u >= 1 && (dr2[ st[u].s ] <= j || a[ st[u].s ] == 0 || b[ poz(st[u].s - i) ] == 1) ){
                u--;
            }
            if(st[u].f <= i){
                sum = i + 2 * j + x;
            }
            else{
                sum = i + 2 * (st[u].f - i) + 2 * j + x;
            }
            sol = min(sol, sum);
        }
    }
    for(i = 0; i < n; i++){
        u = n;
        x = 0;
        for(j = 1; j <= n; j++){
            if(b[ poz(j + i) ] != a[j]){
                x++;
            }
        }
        for(j = 0; j < n; j++){
            while(u >= 1 && (st2[ dr[u].s ] <= j || a[ dr[u].s ] == 0 || b[ poz(dr[u].s + i) ] == 1) ){
                u--;
            }
            if(dr[u].f <= i){
                sum = i + 2 * j + x;
            }
            else{
                sum = i + 2 * (dr[u].f - i) + 2 * j + x;
            }
            sol = min(sol, sum);
        }
    }
    cout<< sol;
}