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 Rust (1.15.1) (judge verdict: Runtime Error)

use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let a: Vec<char> = it.next().unwrap().chars().collect();
    let b: Vec<char> = it.next().unwrap().chars().collect();
    if b.iter().all(|c| *c == '0') {
        if a.iter().all(|c| *c == '0') {
            println!("0");
        } else {
            println!("-1");
        }
        return;
    }
    let n = a.len();
    let mut x: Vec<char> = a.iter().cloned().cycle().take(2 * n).collect();
    let mut y: Vec<char> = b.iter().cloned().cycle().take(3 * n).collect();
    let mut ans = 3 * n;
    for _ in 0..2 {
        let mut left = vec![0; n];
        let mut right = vec![0; n];
        for i in 0..n {
            let mut j = i + n;
            while y[j] != '1' {
                j += 1;
            }
            right[i] = (j + n - i) % n;
            let mut j = i + n;
            while y[j] != '1' {
                j -= 1;
            }
            left[i] = (i + n - j) % n;
//            println!("{}: {} {}", i, left[i], right[i]);
        }
        for k in 0..n {
            let mut sum = k;
            let mut cnt = 0;
            for i in 0..k {
                cnt += y[i].to_digit(10).unwrap();
            }
            let mut p = vec![];
            for i in 0..n {
                cnt += y[i + k].to_digit(10).unwrap();
                if x[i] != y[i + k] {
                    sum += 1;
                    if cnt == 0 {
                        p.push((left[i], right[i + k]));
                    }
                }
                cnt -= y[i].to_digit(10).unwrap();
            }
            /*
            println!("shift {}, rec {}", k, sum);
            for &(l, r) in &p {
                println!("{} - {}", l, r);
            }
            */
            if p.len() == 0 {
                ans = std::cmp::min(ans, sum);
                continue;
            }
            p.sort_by_key(|p| (n - p.0, p.1));
            let mut r = 0;
            for &(x, y) in &p {
                ans = std::cmp::min(ans, sum + 2 * x + 2 * r);
                r = std::cmp::max(r, y);
            }
            p.sort_by_key(|p| (n - p.1, p.0));
            let mut l = 0;
            for &(x, y) in &p {
                ans = std::cmp::min(ans, sum + 2 * y + 2 * l);
                l = std::cmp::max(l, x);
            }
        }
        x.reverse();
        y.reverse();
    }
    println!("{}", ans);
}

fn main() {
    run();
}