atcoder#AGC019D. [AGC019D] Shift and Flip

[AGC019D] Shift and Flip

Score : 10001000 points

Problem Statement

You have two strings A=A1A2...AnA = A_1 A_2 ... A_n and B=B1B2...BnB = B_1 B_2 ... B_n of the same length consisting of 00 and 11.

You can transform AA using the following operations in any order and as many times as you want:

  • Shift AA by one character to the left (i.e., if A=A1A2...AnA = A_1 A_2 ... A_n, replace AA with A2A3...AnA1A_2 A_3 ... A_n A_1).
  • Shift AA by one character to the right (i.e., if A=A1A2...AnA = A_1 A_2 ... A_n, replace AA with AnA1A2...An1A_n A_1 A_2 ... A_{n-1}).
  • Choose any ii such that Bi=1B_i = 1. Flip AiA_i (i.e., set Ai=1AiA_i = 1 - A_i).

You goal is to make strings AA and BB equal.

Print the smallest number of operations required to achieve this, or 1-1 if the goal is unreachable.

Constraints

  • 1A=B2,0001 \leq |A| = |B| \leq 2,000
  • AA and BB consist of 00 and 11.

Input

Input is given from Standard Input in the following format:

AA

BB

Output

Print the smallest number of operations required to make strings AA and BB equal, or 1-1 if the goal is unreachable.

1010
1100
3

Here is one fastest way to achieve the goal:

  • Flip A1A_1: A=0010A = 0010
  • Shift AA to the left: A=0100A = 0100
  • Flip A1A_1 again: A=1100A = 1100
1
0
-1

There is no way to flip the only bit in AA.

11010
10001
4

Here is one fastest way to achieve the goal:

  • Shift AA to the right: A=01101A = 01101
  • Flip A5A_5: A=01100A = 01100
  • Shift AA to the left: A=11000A = 11000
  • Shift AA to the left again: A=10001A = 10001
0100100
1111111
5

Flip A1A_1, A3A_3, A4A_4, A6A_6 and A7A_7 in any order.