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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string A = Console.ReadLine();
        string B = Console.ReadLine();
        {
            bool b = false;
            for(int i=0;i<B.Length;i++){
                b |= B[i] == '1';
            }
            if(!b){
                if(A == B){
                    sb.Append("0\n");
                }
                else{
                    sb.Append("-1\n");
                }
                return;
            }
        }
        int N = B.Length;
        int[] Left = new int[N];
        int[] Right = new int[N];
        for(int i=0;i<N;i++){
            Left[i] = B[i] == '1' ? 0 : N;
            Right[i] = B[i] == '1' ? 0 : N;
        }
        for(int i=0;i<N;i++){
            Left[i] = Math.Min(Left[i],i == 0 ? Left[N-1]+1 : Left[i-1]+1); 
        }
        for(int i=0;i<N;i++){
            Left[i] = Math.Min(Left[i],i == 0 ? Left[N-1]+1 : Left[i-1]+1); 
        }
        for(int i=N-1;i>=0;i--){
            Right[i] = Math.Min(Right[i],i == N-1 ? Right[0]+1 : Right[i+1]+1); 
        }
        for(int i=N-1;i>=0;i--){
            Right[i] = Math.Min(Right[i],i == N-1 ? Right[0]+1 : Right[i+1]+1); 
        }
        int count = 4*N;
        for(int i=0;i<N;i++){
            int c = 0;
            int[] DL;
            int[] DR;
            {
                int[] L = new int[N];
                int[] R = new int[N];
                for(int j=0;j<N;j++){
                    if((i + j < N ? A[i+j] : A[i+j-N]) != B[j]){
                        L[j] = (i + j < N ? Left[i+j] : Left[i+j-N]);
                        R[j] = (i + j < N ? Right[i+j] : Right[i+j-N]);
                        c++;
                    }
                }
                Array.Sort(L,R);
                List<int> bL = new List<int>();
                List<int> bR = new List<int>();
                bL.Add(L[N-1]);
                bR.Add(R[N-1]);
                for(int j=N-2;j>=0;j--){
                    if(R[j] > bR[bR.Count-1]){
                        bL.Add(L[j]);
                        bR.Add(R[j]);
                    }
                }
                DL = bL.ToArray();
                DR = bR.ToArray();
            }
            int d = 4*N;
            int n = DL.Length;
            {
                int d1 = DL[0];
                int d2 = Math.Max(DL[0] - i,i - DL[0]);
                d = Math.Min(d,d1 + d2);
            }
            {
                int d1 = DL[0];
                int d2 = DL[0] + N - i;
                d = Math.Min(d,d1 + d2);
            }
            {
                int d1 = DR[n-1];
                int d2 = Math.Max(DR[n-1] - (N - i),(N - i) - DR[n-1]);
                d = Math.Min(d,d1 + d2);
            }
            {
                int d1 = DR[n-1];
                int d2 = DR[n-1] + i;
                d = Math.Min(d,d1 + d2);
            }
            for(int j=0;j<n-1;j++){
                {
                    int d1 = DR[j];
                    int d2 = DR[j] + DL[j+1];
                    int d3 = Math.Max(DL[j+1]-i,i-DL[j+1]);
                    d = Math.Min(d,d1 + d2 + d3);
                }
                {
                    int d1 = DL[j+1];
                    int d2 = DL[j+1] + DR[j];
                    int d3 = Math.Max(DR[j]-(N-i),(N-i)-DR[j]);
                    d = Math.Min(d,d1 + d2 + d3);
                }
            }
            count = Math.Min(count,d + c);
        }
        sb.Append(count+"\n");
    }
}