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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

public class Main {

	int solve(char[] a, char[] b) {
		if (Arrays.equals(a, b)) {
			return 0;
		}

		boolean bZero = true;
		for (char c : b) {
			bZero &= c == '0';
		}

		if (bZero) {
			return -1;
		}

		int n = a.length;

		int[] diff = new int[n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {

				if (b[(j + i) % n] != a[j]) {
					diff[i]++;
				}
			}
		}

		boolean[][] bad = new boolean[n][n];

		for (int i = 0; i < n; i++) {
			if (a[i] == '1') {

				if (b[i] == '1') {
					continue;
				}

				int pos = 0;
				while (b[(i + pos) % n] == '0') {
					pos++;
				}

				int neg = 0;
				while (b[(i + n - neg) % n] == '0') {
					neg++;
				}

				// x >= pos or y >= neg
				bad[pos - 1][neg - 1] = true;
			}
		}

		for (int i = n - 1; i >= 0; i--) {
			for (int j = n - 1; j >= 0; j--) {
				if (bad[i][j]) {
					if (i > 0) {
						bad[i - 1][j] = true;
					}
					if (j > 0) {
						bad[i][j - 1] = true;
					}
				}
			}
		}

		int ret = Integer.MAX_VALUE;

		for (int pos = 0; pos < n; pos++) {

			boolean isFirst = true;

			for (int neg = 0; pos + neg < n; neg++) {

				if (bad[pos][neg]) {
					continue;
				}

				if (isFirst) {

					for (int end = -neg; end <= pos; end++) {

						int path = getPath(0, -neg, pos, end);
						ret = Math.min(ret, path + diff[(end + n) % n]);

					}
					isFirst = false;

				} else {
					int end = -neg;
					{

						int path = getPath(0, -neg, pos, end);
						ret = Math.min(ret, path + diff[(end + n) % n]);

					}
				}

			}

		}
		
		return ret;

	}
	
	void submit() {
		char[] a = nextToken().toCharArray();
		char[] b = nextToken().toCharArray();
		
		out.println(solve(a, b));
	}

	int getPath(int from, int c1, int c2, int to) {
		return Math.min(getOrderedPath(from, c1, c2, to),
				getOrderedPath(from, c2, c1, to));
	}

	int getOrderedPath(int a, int b, int c, int d) {
		return Math.abs(b - a) + Math.abs(c - b) + Math.abs(d - c);
	}

	void preCalc() {

	}

	void stress() {

	}

	void test() {

	}

	Main() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		preCalc();
		submit();
		// stress();
		// test();
		out.close();
	}

	static final Random rng = new Random();

	static int rand(int l, int r) {
		return l + rng.nextInt(r - l + 1);
	}

	public static void main(String[] args) throws IOException {
		new Main();
	}

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	long nextLong() {
		return Long.parseLong(nextToken());
	}

	double nextDouble() {
		return Double.parseDouble(nextToken());
	}
}