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>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 4000 + 5 ; 
int n, a[N], b[N], nxt[N], pre[N], sum[N], Ans ; 
char A[N], B[N] ;
void solve() {
	int m = 2 * n ; nxt[m + 1] = m + 1 ; 
	drep( i, 1, m ) nxt[i] = ( b[i] ) ? i : nxt[i + 1] ;
	rep( i, 1, m ) sum[i] = sum[i - 1] + b[i] ; 
	rep( de, 0, n ) { 
		int num = 0, ans = de, u ; 
		rep( i, 1, n ) { 
			if( a[i] == b[i - de + n] ) continue ; 
			++ ans, u = sum[i + n] - sum[i + n - de - 1] ; 
			if( !u ) num = max( num, nxt[i] - i ) ; 
		}
		Ans = min( Ans, 2 * num + ans ) ;
	}
}
signed main()
{
	scanf("%s", A + 1 ), scanf("%s", B + 1 ), n = strlen(A + 1) ;
	int fl = 0, fA = 0 ;
	rep( i, 1, n ) a[i] = A[i] - '0', b[i] = B[i] - '0', b[i + n] = b[i], fl += b[i], fA += a[i] ; 
	if( !fl ) {
		if( !fA ) cout << "0" << endl ; 
		else cout << "-1" << endl ; exit(0) ; 
	}
	Ans = 4 * n, solve() ; 
	rep( i, 1, n ) if( (n - i) > i ) swap( a[i], a[n - i] ), swap( b[i], b[n - i] ) ;
	rep( i, 1, n ) b[i + n] = b[i] ; 
	solve() ;
	printf("%lld\n", Ans ) ;
	return 0 ;
}