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: Runtime Error)

#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"---------------"#x"---------------"<<endl
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
						For(_x,L,R)cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> vi;
typedef long double LD;
LL read(){
	LL x=0,f=0;
	char ch=getchar();
	while (!isdigit(ch))
		f|=ch=='-',ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int N=2e3+5,INF=1e9;
int n;
int a[N],b[N];
void IN(){
	char s[N];
	cin>>s+1;
	n=strlen(s+1);
	For(i,1,n)
		a[i]=s[i]-'0';
	cin>>s+1;
	For(i,1,n)
		b[i]=s[i]-'0';
}
bool check(){
	For(i,1,n)
		if (b[i])
			return 0;
	For(i,1,n)
		if (a[i])
			return 1;
	return 2;
}
int L[N],R[N];
int id[N],c;
bool cmp(int a,int b){
	return L[a]>L[b];
}
int main(){
	IN();
	int checkres=check();
	if (checkres==1)
		return puts("-1"),0;
	else if (checkres==2)
		return puts("0"),0;
	assert(0);
	For(i,1,n){
		L[i]=R[i]=0;
		int p=i;
		while (!b[p])
			L[i]++,p=p==1?n:p-1;
		p=i;
		while (!b[p])
			R[i]++,p=p==n?1:p+1;
	}
	int ans=INF;
	For(r,0,n){
		int dif=0;
		c=0;
		For(i,1,n)
			if (a[i]!=b[(i+r-1)%n+1]){
				dif++;
				if (L[i]>0&&R[i]>r)
					id[++c]=i;
			}
		sort(id+1,id+c+1,cmp);
		int tmp=INF,mx=0;
		For(i,1,c){
			tmp=min(tmp,mx*2+L[id[i]]*2);
			mx=max(mx,R[id[i]]-r);
		}
		tmp=min(tmp,mx*2);
		ans=min(ans,tmp+dif+r);
		ans=min(ans,n+r+dif);
	}
	For(l,0,n){
		int dif=0;
		c=0;
		For(i,1,n)
			if (a[i]!=b[(i-l+n-1)%n+1]){
				dif++;
				if (L[i]>l&&R[i]>0)
					id[++c]=i;
			}
		sort(id+1,id+c+1,cmp);
		int tmp=INF,mx=0;
		For(i,1,c){
			tmp=min(tmp,mx*2+(L[id[i]]-l)*2);
			mx=max(mx,R[id[i]]);
		}
		tmp=min(tmp,mx*2);
		ans=min(ans,tmp+dif+l);
		ans=min(ans,n+l+dif);
	}
	cout<<ans<<endl;
	return 0;
}