// {{{ Templates
#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

// }}}

template <typename T>
T scan()
{
    T v;
    cin >> v;
    return v;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    const string A = scan<string>();
    const int N = A.size();
    const string B = scan<string>();
    int anum = 0;
    int bnum = 0;
    for (int i = 0; i < N; i++) {
        if (A[i] == '1') {
            anum++;
        }
        if (B[i] == '1') {
            bnum++;
        }
    }
    if (bnum == 0) {
        if (anum > 0) {
            cout << -1 << endl;
            return 0;
        } else {
            cout << 0 << endl;
            return 0;
        }
    }

    vector<string> shiftB(N, B);  // iだけshiftしたB
    for (int i = 1; i < N; i++) {
        for (int j = 0; j < N; j++) {
            shiftB[i][j] = shiftB[i - 1][(j > 0) ? j - 1 : N - 1];
        }
    }
    vector<vector<int>> shiftBpos(N);  // iだけshiftしたBの1-bitの場所
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (shiftB[i][j] == '1') {
                shiftBpos[i].push_back(j);
            }
        }
    }
    const int popnum = bnum;

    vector<vector<vector<int>>> neighbor(2, vector<vector<int>>(N, vector<int>(N, 0)));  // neighbor[dir(0:right)][i][j] = shiftB[i]がdir方向に動く時何番目の1-bitが初めてA[j]を通り過ぎるか
    fill(neighbor[0].begin(), neighbor[0].end(), vector<int>(N, popnum - 1));
    // 右shift
    for (int i = 0; i < N; i++) {
        int shiftBRind = popnum - 1;
        for (int j = N - 1; j >= 0; j--) {
            if (j < shiftBpos[i][shiftBRind]) {
                shiftBRind--;
                if (shiftBRind == -1) {
                    break;
                }
            }
            neighbor[0][i][j] = shiftBRind;
        }
    }
    //左shift
    for (int i = 0; i < N; i++) {
        int shiftBLind = 0;
        for (int j = 0; j < N; j++) {
            if (j > shiftBpos[i][shiftBLind]) {
                shiftBLind++;
                if (shiftBLind == popnum) {
                    break;
                }
            }
            neighbor[1][i][j] = shiftBLind;
        }
    }

    vector<vector<vector<int>>> dist(2, vector<vector<int>>(N, vector<int>(N, 0)));  // dist[dir(0:right)][i][j] = shiftB[i]がdir方向にどれだけ動けば初めて1-bitがA[j]を通り過ぎるか
    for (int dir = 0; dir < 2; dir++) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                dist[dir][i][j] = (dir == 0) ? (j - shiftBpos[i][neighbor[dir][i][j]] + N) % N : (shiftBpos[i][neighbor[dir][i][j]] - j + N) % N;
            }
        }
    }

    int mini = INF<int>;
    for (int dir = 0; dir < 2; dir++) {             //shift方向
        for (int shift = 0; shift <= N; shift++) {  //shift幅
            int cost = shift;
            const int sft = (dir == 0) ? shift % N : (N - shift) % N;
            vector<pii> edge;
            for (int j = 0; j < N; j++) {  // A[j]
                if (A[j] != shiftB[sft][j]) {
                    cost++;
                    if (dist[dir][0][j] > shift) {  // フリップ漏れ
                        const int before_dist = dist[1 - dir][0][j];
                        const int after_dist = dist[dir][sft][j];
                        edge.push_back(make_pair(before_dist, after_dist));
                    }
                }
            }
            if (not edge.empty()) {
                sort(edge.begin(), edge.end());
                const int num = edge.size();
                priority_queue<int> after_max;
                int edge_mini = edge[num - 1].first;
                for (int j = 0; j < num; j++) {
                    after_max.push(edge[num - 1 - j].second);
                    const int sum = ((j == num - 1) ? 0 : edge[num - j - 2].first) + after_max.top();
                    edge_mini = min(edge_mini, sum);
                }
                cost += edge_mini * 2;
            }
            mini = min(mini, cost);
        }
    }
    cout << mini << endl;

    return 0;
}


Measured memory_bytes: 98944