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>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1e9+7;
const int SIZE = 1e6+10;
char A[SIZE],B[SIZE];
int rr[SIZE],ll[SIZE];
const int DD=4000;
int main(){
    RS(A+DD);
    RS(B+DD);
    if(strcmp(A+DD,B+DD)==0)return 0*puts("0");
    int n=LEN(A+DD);
    REP(i,n)A[DD-n+i]=A[DD+n+i]=A[DD+n+n+i]=A[DD+i];
    REP(i,n)B[DD-n+i]=B[DD+n+i]=B[DD+n+n+i]=B[DD+i];
    bool A_have_one=0,B_have_one=0;
    REP(i,n){
        if(B[DD+i]=='1')B_have_one=1;
        if(A[DD+i]=='1')A_have_one=1;
    }
    ll[DD-n-1]=-1e5;
    REPP(i,DD-n,DD+n+n+n){
        if(B[i]=='1')ll[i]=i;
        else ll[i]=ll[i-1];
    }
    rr[DD+n+n+n]=1e5;
    for(int i=DD+n+n+n-1;i>=DD-n;i--){
        if(B[i]=='1')rr[i]=i;
        else rr[i]=rr[i+1];
    }
    if(!B_have_one&&A_have_one)return 0*puts("-1");
    int an=1e9;
    REPP(offset,-n,2*n){
        VPII pp;
        int dif=0;
        multiset<int>H;
        REP(i,n){
            if(B[DD+offset+i]!=A[DD+i]){
                dif++;
                int mi=min(DD+offset+i,DD+i);
                int ma=max(DD+offset+i,DD+i);
                if(rr[mi]>ma){
                    pp.PB(MP(mi-ll[mi],rr[ma]-ma));
                    H.insert(pp.back().S);
                }
            }
        }
        if(!SZ(H)){
            an=min(an,abs(offset)+dif);
            continue;
        }
        sort(ALL(pp));
        an=min(an,abs(offset)+dif+*H.rbegin()*2);
        an=min(an,abs(offset)+dif+pp.back().F*2);
        REP(i,SZ(pp)-1){
            H.erase(H.find(pp[i].S));
            an=min(an,abs(offset)+dif+pp[i].F+*H.rbegin()+min(pp[i].F,*H.rbegin()));
        }
    }
    printf("%d\n",an);
    return 0;
}