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 (GCC 5.4.1) (judge verdict: Accepted)

#if 0
cat <<EOF >mistaken-paste
#endif
// thanks for @rsk0315_h4x

#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#define _USE_MATH_DEFINES

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>

#define BIG 2000000007
#define VERYBIG 2000000000000007LL

#define MOD 1000000007
#define FOD  998244353
typedef uint64_t ull;
typedef  int64_t sll;

#define N_MAX 1000000

typedef struct {
	sll a;
	sll b;
	sll c;
} hwllc;

ull n;

ull umin (ull x, ull y) {
	return (x < y) ? x : y;
}

ull umax (ull x, ull y) {
	return (x > y) ? x : y;
}

sll smin (sll x, sll y) {
	return (x < y) ? x : y;
}

sll smax (sll x, sll y) {
	return (x > y) ? x : y;
}

sll sdiff (sll a, sll b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

int32_t ptriplecomp (const void *left, const void *right) {
	hwllc l = *(hwllc*)left;
	hwllc r = *(hwllc*)right;

	if (l.a < r.a) {
		return -1;
	}
	if (l.a > r.a) {
		return +1;
	}
	if (l.b < r.b) {
		return -1;
	}
	if (l.b > r.b) {
		return +1;
	}
	if (l.c < r.c) {
		return -1;
	}
	if (l.c > r.c) {
		return +1;
	}
	return 0;
}

char s[N_MAX + 1];
char t[N_MAX + 1];

hwllc roll[N_MAX];
bool must[N_MAX];

sll f (sll l, sll r, sll f) {
	return l + r + smin(r + sdiff(l, f) % n, l + sdiff(n - r, f) % n);
}

ull solve () {
	sll i, j, ki, li;
	// ull result = 0;
	sll result = 0;
	sll sum = 0;

	n = strlen(s);

	sll v = -1;
	for (i = 0; i < n; i++) {
		if (t[i] == '1') {
			v = i;
			break;
		}
	}
	if (v < 0) {
		for (i = 0; i < n; i++) {
			if (s[i] == '1') break;
		}
		if (i == n) {
			puts("0");
		} else {
			puts("-1");
		}
		return 0;
	}

	for (i = 0; i < n; i++) {
		ull curr = (v + i) % n;
		if (t[curr] == '1') {
			sum = 0;
		} else {
			sum++;
		}
		roll[curr].a = sum;
		roll[curr].c = curr;
	}
	for (i = 0; i < n; i++) {
		ull curr = (n + v - i) % n;
		if (t[curr] == '1') {
			sum = 0;
		} else {
			sum++;
		}
		roll[curr].b = sum;
	}
	qsort(roll, n, sizeof(hwllc), ptriplecomp);

	result = VERYBIG;
	for (i = 0; i < n; i++) {
		ull flips = 0;
		for (j = 0; j < n; j++) {
			if (must[j] = (t[(n + j - i) % n] != s[j])) flips++;
		}

		ull shifts = VERYBIG;
		sll l = n, r = 0;

		j = n - 1;
		while (l >= 0) {
			shifts = umin(shifts, f(l, r, i));

			while (j >= 0) {
				if (!must[roll[j].c]) {
					j--;
					continue;
				}
				if (roll[j].a < l) break;
				r = smax(r, roll[j].b);
				j--;
			}

			l--;
		}

		result = umin(result, flips + shifts);
	}

	printf("%lld\n", result);
	return 0;
}

int32_t main (void) {
	scanf("%s", s);
	scanf("%s", t);

	solve();

	return 0;
}