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)

//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\
 
#include <bits/stdc++.h>
 
#define F first
#define S second
#define _sz(x) (int)x.size()
#define pb push_back
 
using namespace std ;
using ll = long long ;
using ld = long double ;
using pii = pair <int , int> ;
 
const int N = 2000 + 10 ;
int n , l[N] , r[N] ;
string a , b ;
 
inline int sum (int a , int b) {
	int res = a + b ;
	if (res >= n) res -= n ;
	return res ;
}
 
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ;
 
	cin >> a >> b ;
	n = _sz(a) ;
 
	for (int i = 0 ; i < n ; i ++) l[i] = r[i] = n + 1 ;
 
	for (int i = 0 ; i < n ; i ++) {
		if (b[i] == '0') continue ;
 
		for (int j = 0 ; j < n ; j ++) {
			l[j] = min(l[j] , sum(j , n - i)) ;
			r[j] = min(r[j] , sum(i , n - j)) ;
		}
	}	
 
	for (int i = 0 ; i < n ; i ++)
		if (a[i] == '1' && l[i] > n) return cout << "-1\n" , 0 ;
 
	int ans = 10 * n ;
 
	for (int i = 0 ; i <= 2 * n ; i ++) {
		// move right
	
		int mx = 0 , res = i ;
		for (int j = 0 ; j < n ; j ++) {
			int k = sum(j , i) ;
 
			if (a[j] != b[k]) {
				if (r[j] > i) mx = max(mx , l[j]) ;
				res ++ ;
			}
		}
 
		res += 2 * mx ;
		ans = min(ans , res) ;
 
		// move left
 
		mx = 0 , res = i ;
		for (int j = 0 ; j < n ; j ++) {
			int k = sum(j , n - i) ;
 
			if (a[j] != b[k]) {
				if (l[j] > i) mx = max(mx , r[j]) ;
				res ++ ;
			}
		}
 
		res += 2 * mx ;
		ans = min(ans , res) ;
	}
 
	cout << ans << '\n' ;
}