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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#define pb push_back
#define ll long long
#define inf 0x3f3f3f3f
#define fill(a,b) memset(a,b,sizeof(a))
#define cpy(a,b) memcpy(a,b,sizeof(b))
template<typename T> inline bool MIN(T &a,const T &b) {return a>b? a=b,1:0;}
template<typename T> inline bool MAX(T &a,const T &b) {return a<b? a=b,1:0;}
#define mod 1000000007
inline int plu(int a,int b) {return a+b>=mod? a+b-mod:a+b;}
inline int sub(int a,int b) {return a-b<0? a-b+mod:a-b;}
inline int mul(int a,int b) {return (ll)a*b%mod;}
inline int inc(int &a,int b) {a+=b;if (a>=mod) a-=mod;}
inline int dec(int &a,int b) {a-=b;if (a<0) a+=mod;}
#define mp make_pair
#define SZ(a) ((int)a.size())
#define BK(a) (a[SZ(a)-1])
#define fi first
#define se second
#define N 2010
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;

int n,A[N],B[N],L[N],R[N],C[N];

char s[N];

int MO(int a) {
    return (a%n+n)%n;
}

int main()
{

    scanf("%s",s);
    n=strlen(s);
    for (int i=0;i<n;++i) A[i]=s[i]-'0';
    scanf("%s",s);
    for (int i=0;i<n;++i) B[i]=s[i]-'0';


    for (int i=0;i<n;++i) if (A[i]) {
        L[i]=-1;
        for (int p=0;p<n;++p) if (B[MO(i+p)]) {
            L[i]=p;
            break;
        }
        for (int p=0;p<n;++p) if (B[MO(i-p)]) {
            R[i]=p;
            break;
        }
    }

    for (int i=0;i<n;++i) if (L[i]==-1) {
        printf("%d\n",-1);
        return 0;
    }

    int ans=1e5;
    for (int i=0;i<n-1;++i) {
        for (int j=0;j<n;++j) C[j]=B[MO(j+i)];
        int cnt=0,mx=0;
        for (int j=0;j<n;++j) {
            if (A[j]!=C[j]) {
                ++cnt;
                if (A[j]&&i<L[j]) {
                    MAX(mx,R[j]);
                }
            }

        }

        MIN(ans,cnt+mx+i);
    }

    for (int i=0;i<n-1;++i) {
        for (int j=0;j<n;++j) C[j]=B[MO(j-i)];
        int cnt=0,mx=0;
        for (int j=0;j<n;++j) {
            if (A[j]!=C[j]) {
                ++cnt;
                if (A[j]&&i<R[j]) {
                    MAX(mx,L[j]);
                }
            }

        }

        MIN(ans,cnt+mx+i);
    }

    printf("%d\n",ans);

    return 0;
}