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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <queue>
#include <cstring>
#include <bitset>
#include <set>
#include <list>
#include <map>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;

#define all(c) (c).begin(),(c).end()
#define forn(i, a, b) for(int i = a; i < b; i++)

#define pb push_back
#define mp make_pair
bitset<2000> ar[2001];
bitset<2000> br[2001];
bitset<2000> brc[2001];
bitset<2000> blc[2001];
int main()
{
//#ifndef ONLINE_JUDGE
//    freopen("input.txt", "rt", stdin);
//    freopen("output.txt", "wt", stdout);
//#endif
    string as,bs;
    cin>>as>>bs;
    int n = as.size();
    bitset<2000> a,b;
    forn(i,0,n) if(as[i] == '1') a.set(i);
    forn(i,0,n) if(bs[i] == '1') b.set(i);
    if(b.count() == 0) {
        if(a.count() == 0) cout<<0;
        else cout<<-1;
        return 0;
    }
    int best = n;
    forn(i,0,n+1) {
        ar[i] = a;
        br[i] = b;
        int bit = a[0];
        a>>=1;
        a[n-1] = bit;
        bit = b[0];
        b>>=1;
        b[n-1] = bit;
    }
    int ans = 1e9;
    b = br[0];
    a = ar[0];
    brc[0] = br[0];
    forn(i,1,n+1) brc[i] = brc[i-1] | br[n-i];
    blc[0] = br[0];
    forn(i,1,n+1) blc[i] = blc[i-1] | br[i];
    forn(mr,0,n+1) {
        bitset<2000> diff = a ^ br[n-mr];
        int cand = diff.count();
        int l = -1;
        int r = n;
        while(r-l>1) {
            int m = (r+l+1)/2;
            bitset<2000> btest = (blc[m] | brc[mr]).flip();
            if((btest & diff).count() == 0) {
                r = m;
                ans = min(ans, cand + mr+2*m);
            }
            else l = m;
        }
    }
    forn(ml,0,n+1) {
        bitset<2000> diff = a ^ br[ml];
        int cand = diff.count();
        int l = -1;
        int r = n;
        while(r-l>1) {
            int m = (r+l+1)/2;
            bitset<2000> btest = (brc[m] | blc[ml]).flip();
            if((btest & diff).count() == 0) {
                r = m;
                ans = min(ans, cand + ml+2*m);
            }
            else l = m;
        }
    }
    cout<<ans;
}