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 mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
//#define pdd pair<double,double>
#define pll pair<LL,LL>
#define pb(x) push_back(x)
#define x first
#define y second
#define sqr(x) ((x)*(x))
#define EPS 1e-8
#define mii map<int,int>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define N 500005
//#define M 1000005
#define pi 3.14159265358979323846 
#define cpdd const pdd
#define hash Hash
#define list List
using namespace std;
typedef long long LL;
using namespace std;
int main(){
	char a[2005];
	char b[2005];
	scanf("%s",a);
	scanf("%s",b);
	int noans=0;
	for(int i=0;a[i]!=0;i++){
		if(a[i]=='1'){
			noans=1;
		}	
	}
	for(int i=0;b[i]!=0;i++){
		if(b[i] =='1'){
			noans=0;
		}
	}
	if(noans){
		printf("-1\n");
		return 0;
	}
	int len=strlen(b);
	int opt=1e9;
	vector<int> v;
	v.pb(-1);
	for(int j=0;b[j]!=0;j++){
		if(b[j]=='1'){
			v.pb(j);
		}
	} 
	v[0]=v.back()-len;
	v.pb(v[1]+len);
	for(int i=0;i<len;i++){
		int l=0,r=0;
		pii p[2005];
		int cnt=0;
		for(int j=0;a[j]!=0;j++){
			if(a[j]!=b[j]){
				vector<int>::iterator it=lower_bound(v.begin(),v.end(),j);
				int r=*it-j;
				int l;
				if(*it-j==0)
				l=0;
				else
				{
					it--;
					l=j-*it;
				}
				p[cnt++]=mp(l,r);
			} 
		} 
		sort(p,p+cnt);
		reverse(p,p+cnt);
		int Max=0,ans=1e9;
		for(int j=0;j<cnt;j++){
		//	printf("%d %d\n",p[j].x,p[j].y);
			ans=min(ans,(p[j].x+Max)*2);
			Max=max(p[j].y,Max);
		}
		ans=min(ans,Max*2);
		opt=min(ans+cnt+min(i,len-i),opt);
	//	printf("%d %d?\n",ans,cnt);
		char temp=b[0];
		for(int j=0;j<len-1;j++)
		b[j]=b[j+1];
		b[len-1]=temp;
	//	printf("%s",b);
	}
	printf("%d\n",opt);
}