atcoder#ARC119B. [ARC119B] Electric Board

[ARC119B] Electric Board

Score: 500500 points

Problem Statement

An electric bulletin board is showing a string SS of length NN consisting of 0 and 1.

You can do the following operation any number of times, where SiS_i denotes the ii-th character (1iN)(1 \leq i \leq N) of the string shown in the board.

Operation: choose a pair of integers (l,r)(l, r) (1l<rN)(1 \leq l < r \leq N) satisfying one of the conditions below, and swap SlS_l and SrS_r.

  • Sl=S_l= 0 and Sl+1==Sr=S_{l+1}=\cdots=S_r= 1.
  • Sl==Sr1=S_{l}=\cdots=S_{r-1}= 1 and Sr=S_r= 0.

Determine whether it is possible to make the string shown in the board match TT, and find the minimum number of operations needed if it is possible.

Constraints

  • 2N5000002 \leq N \leq 500000
  • SS is a string of length NN consisting of 0 and 1.
  • TT is a string of length NN consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

NN

SS

TT

Output

If it is impossible to make the board show the string TT, print -1.

If it is possible, find the minimum number of operations needed.

7
1110110
1010111
2

Here is one possible way to make the board show the string 1010111 in two operations:

  • Do the operation with (l,r)=(2,4)(l, r) = (2, 4), changing the string in the board from 1110110 to 1011110.
  • Do the operation with (l,r)=(4,7)(l, r) = (4, 7), changing the string in the board from 1011110 to 1010111.
20
11111000000000011111
11111000000000011111
0

The board already shows the string TT before doing any operation, so the answer is 00.

6
111100
111000
-1

If there is no sequence of operations that makes the board show the string TT, print -1.

119
10101111011101001011111000111111101011110011010111111111111111010111111111111110111111110111110111101111111111110111011
11111111111111111111111111011111101011111011110111110010100101001110111011110111111111110010011111101111111101110111011
22