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.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
  static Scanner sc = new Scanner(System.in);
  static int N;

  public static void main(String[] args) {
    System.out.println(solve());
  }

  static int solve() {
    char[] A = sc.next().toCharArray();
    char[] B = sc.next().toCharArray();
    N = A.length;
    int lastOnePos = N;
    for (int i = N - 1; i >= 0; i--) {
      if (B[i] == '1') {
        lastOnePos = i;
        break;
      }
    }
    if (lastOnePos == N) {
      for (char c : A) {
        if (c == '1') {
          return -1;
        }
      }
      return 0;
    }
    int ans = solve(A, B, lastOnePos);
    for (int i = 0; i < N / 2; i++) {
      char tmp = A[i];
      A[i] = A[N - 1 - i];
      A[N - 1 - i] = tmp;
      tmp = B[i];
      B[i] = B[N - 1 - i];
      B[N - 1 - i] = tmp;
    }
    for (int i = N - 1; i >= 0; i--) {
      if (B[i] == '1') {
        lastOnePos = i;
        break;
      }
    }
    return Math.min(ans, solve(A, B, lastOnePos));
  }

  static int solve(char[] A, char[] B, int lastOnePos) {
    int ans = N;
    for (int i = 0; i < N; i++) {
      if (A[i] != B[i]) ++ans;
    }
    int[] prevPos = new int[N];
    int[] nextPos = new int[N];
    int p = lastOnePos - N;
    for (int i = 0; i < N; i++) {
      if (B[i] == '1') p = i;
      prevPos[i] = p;
    }
    int n = 0;
    while (B[n] == '0') {
      ++n;
    }
    n += N;
    for (int i = N - 1; i >= 0; i--) {
      if (B[i] == '1') n = i;
      nextPos[i] = n;
    }
    ArrayList<Integer> rest = new ArrayList<>();
    for (int i = 0; i < N; i++) {
      rest.clear();
      int diff = 0;
      for (int j = 0; j < N; j++) {
        if (A[j] != B[(j - i + N) % N]) {
          ++diff;
          if (j - prevPos[j] > i) {
            rest.add(((j - prevPos[j] - i) << 16) | (nextPos[j] - j));
          }
        }
      }
      int move;
      if (!rest.isEmpty()) {
        Collections.sort(rest);
        int[] revMax = new int[rest.size() + 1];
        for (int j = rest.size() - 1; j >= 0; j--) {
          revMax[j] = Math.max(revMax[j + 1], rest.get(j) & 0xFFFF);
        }
        move = revMax[0];
        for (int j = 0; j < rest.size(); j++) {
          move = Math.min(move, (rest.get(j) >> 16) + revMax[j + 1]);
        }
      } else {
        move = 0;
      }
//      System.out.println(rest);
//      System.out.println(i + " " + diff + " " + move);
      ans = Math.min(ans, diff + move * 2 + i);
    }
    return ans;
  }

}