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: Accepted)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>

namespace IO {
    template <typename _T>
    inline bool read (_T& x) {
        x = 0;
        _T y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-') y = -1;
            c = getchar();
        }
        if (c == EOF) return false;
        while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
        x *= y;
        return true;
    }

    template <typename _T>
    inline _T input () {
        _T x = 0, y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-') y = -1;
            c = getchar();
        }
        if (c == EOF) return 0;
        while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
        x *= y;
        return x;
    }
};
using namespace IO;

using std::pair;

#define reg register
#define MAX_N 6007
#define pii pair<int, int>
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for(int i = l; i <= r; ++i)
#define lep(i, l, r) for(int i = l; i < r; ++i)
#define irep(i, r, l) for(int i = r; i >= l; --i)
#define ilep(i, r, l) for(int i = r; i > l; --i)
typedef long long ll;

int N, M, top;
int a[MAX_N], b[MAX_N], s[MAX_N];
int L[MAX_N], R[MAX_N];
char str[MAX_N];
pii p[MAX_N];

inline void init () {
    rep (i, 1, M) s[i] = s[i - 1] + b[i];
    L[0] = -1e9;
    rep (i, 1, M) L[i] = (b[i] == 1) ? i : L[i - 1];
    R[M + 1] = 1e9;
    irep (i, M, 1) R[i] = (b[i] == 1) ? i : R[i + 1];
}

inline bool has (int l, int r) { return s[r] - s[l - 1] > 0; }

inline int work (int d) {
    int flp = 0;
    top = 0;
    rep (i, N + 1, N * 2) if (a[i] != b[i + d]) {
        flp++;
        int l = i + d, r = i;
        if (l > r) std::swap(l, r);
        if (!has(l, r)) {
            p[++top] = pii(l - L[l], R[r] - r);
        }
    }
    std::sort(p + 1, p + top + 1);
    int mxr = 0, mn = 1e9;
    irep (i, top, 0) {
        mn = std::min(mn, p[i].first + mxr);
        mxr = std::max(mxr, p[i].second);
    }
    // printf("%d %d\n", d, flp);
    return std::abs(d) + flp + mn * 2;
}

inline void solve () {
    // return printf("%d\n", work(-1)), void();
    int res = 1e9;
    rep (i, -(N - 1), N - 1) {
        res = std::min(res, work(i));
    }
    printf("%d\n", res == 1e9 ? -1 : res);
}

int main () {
#ifdef LOCAL
    FIN("in");
#endif
    scanf("%s", str + 1);
    N = strlen(str + 1);
    rep (i, 1, N) a[i] = str[i] - '0';
    scanf("%s", str + 1);
    rep (i, 1, N) b[i] = str[i] - '0';
    M = 3 * N;
    rep (i, N + 1, 2 * N) b[i] = b[i - N], a[i] = a[i - N];
    rep (i, 2 * N + 1, M) b[i] = b[i - N], a[i] = a[i - N];
    init();
    rep (i, 1, N) if (a[i] && !has(1, N)) return puts("-1"), 0;
    solve();
    return 0;
}