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 pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define pb2 pop_back
#define pf2 pop_front
#define rep(k,i,j) for(int k = (int)i;k<(int)j;k++)
#define repd(k,i,j) for(int k = (int)i;k>=(int)j;k--)
#define ll long long
#define line puts("")


const int INF = 1e9+7;
const ll INFLL = 1e17;
const double EPS = 1e-6;

using namespace std;


string A,B;

bool nol(string a){
	rep(k,0,a.size())if(a[k]!='0')return 0;
	return 1;
}

int n;

const int maxn = 2005;
int arr[maxn];

int L[maxn],R[maxn];


int dist(int a,int b){
	if(a>b)swap(a,b);
	
	return min(b-a, a - b + n);
}
int solve(vector<pii> num,int st){
	if(num.size()==0)return st;
	sort(num.begin(),num.end());
	
	int maxr = -INF;
	
	int ret = INF;
	repd(k,num.size()-1,0){
		int ki = num[k].fi;
		
		int ka = maxr + ki;
		if(ka<0)ka = 0;
		
		int sls = ka - ki;
		if(sls < 0)sls += n;
		
		ret = min(ret,ki + ka + dist(sls , st));
		maxr = max(maxr,num[k].se);
	}
	
	return ret;
}
int main(){
	cin>>A>>B;
	
	if(nol(B)){
		printf("%d\n",nol(A)?0:-1);
		return 0;
	}
	
	n = A.size();
	rep(k,0,n)A[k] -= '0',B[k] -='0';
	
	rep(k,0,n){
		if(k)L[k] = L[k-1] + 1;
		if(B[k])L[k] = 0;
	}
	if(!B[0])L[0] = L[n-1]+1;
	
	repd(k,n-1,0){
		if(k!=n-1)R[k] = R[k+1] + 1;
		if(B[k])R[k] = 0;
	}
	if(!B[n-1])R[n-1] = R[0] + 1;
	
	int ret = INF;
	rep(st,0,n){
		rep(k,0,n)arr[(st+k)%n] = A[(st+k)%n]^B[k];
		
		vector<pii> num;
		rep(k,0,n)if(arr[k])num.pb(mp(L[k],R[k]));
		
		//rep(k,0,n)printf("%d ",arr[k]);line;
		//rep(k,0,num.size())printf("(%d %d) ",num[k].fi,num[k].se);line;
		ret = min(ret,solve(num,st) + (int)num.size());
		
		rep(k,0,num.size())swap(num[k].fi,num[k].se);
		
		ret = min(ret,solve(num,st) + (int)num.size());
	}
	cout<<ret<<endl;
	
}