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<algorithm> 
#include<bitset> 
#include<memory.h> 
#include<cstdio> 
#include<cmath>
#include<cstring> 
#define maxn 400100
#define ll long long
#define pa pair<ll,ll> 
#define ld long double 
#define For(i,x,y)  for(ll i=x;i<=y;++i) 
#define FOr(i,x,y)  for(ll i=x;i>=y;--i) 
#define i1 (i+j-1)%n+1
#define i2 (j-i+n-1)%n+1
using namespace std; 
inline ll read(){   ll x=0,f=1;char ch=getchar();   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}  while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}  return x*f; } 
inline void write(ll x){    if (x<0) putchar('-'),x=-x; if (x>=10) write(x/10);   putchar(x%10+'0');  } 
inline void writeln(ll x){ write(x);   puts("");   }
ll answ=1e9,sum[maxn],Answ[maxn],n,las;	bool vis[maxn],mark[maxn];
char s1[maxn],s2[maxn];
void work1(){
	For(i,1,n){
		if (s2[i]=='1')	las=i;
		Answ[i]=i-las;
	}
	For(i,1,n){
		if (s2[i]=='1')	las=i;
		Answ[i]=i-las;
	}
	For(i,1,n)	if (Answ[i]<0)	Answ[i]+=n;
}
void work2(){
	For(i,n,1){
		if (s2[i]=='1')	las=i;
		Answ[i]=las-i;
	}
	For(i,n,1){
		if (s2[i]=='1')	las=i;
		Answ[i]=las-i;
	}
	For(i,1,n)	if (Answ[i]<0)	Answ[i]+=n;
}
int main(){
	scanf("%s",s1+1);
	scanf("%s",s2+1);
	n=strlen(s1+1);
	bool flag=0,fl2=0;
	For(i,1,n)	flag|=s1[i]=='1',fl2|=s2[i]=='1';
	if (flag&&!fl2)	return puts("-1"),0;
	if (!fl2)	return puts("0"),0;
	work1();
	For(i,0,n-1){
		ll ned=0,ans=0;
		For(j,1,n)	if (s2[j]=='1')	mark[i1]=1;
		For(j,1,n)	if (s1[i1]!=s2[j]){
			++ned;
			if (!mark[i1])	ans=max(ans,Answ[j]);
		}
		answ=min(answ,ned+i+ans);
	}
	memset(mark,0,sizeof mark);
	For(i,0,n-1){
		ll ned=0,ans=0;
		For(j,1,n)	if (s2[j]=='1')	mark[i2]=1;
		For(j,1,n)	if (s1[i2]!=s2[j]){
			++ned;
			if (!mark[i2])	ans=max(ans,Answ[j]);
		}
		answ=min(answ,ned+i+ans);
	}
	work2();
	memset(mark,0,sizeof mark);
	For(i,0,n-1){
		ll ned=0,ans=0;
		For(j,1,n)	if (s2[j]=='1')	mark[i2]=1;
		For(j,1,n)	
		if (s1[i2]!=s2[j]){
			++ned;
			if (!mark[i2])	ans=max(ans,Answ[j]);
		}
		answ=min(answ,ned+i+ans);
	}
	memset(mark,0,sizeof mark);
	For(i,0,n-1){
		ll ned=0,ans=0;
		For(j,1,n)	if (s2[j]=='1')	mark[i1]=1;
		For(j,1,n)	
		if (s1[i1]!=s2[j]){
			++ned;
			if (!mark[i1])	ans=max(ans,Answ[j]);
		}
		answ=min(answ,ned+i+ans);
	}
	writeln(answ);
}
//0000000
//0000000