#arc125a. [arc125_a]Dial Up

[arc125_a]Dial Up

Problem Statement

Snuke has a sequence of NN integers a=(a1,a2,cdots,aN)a=(a_1,a_2,\\cdots,a_N) consisting of 00s and 11s, and an empty integer sequence bb. The initial state ai=Sia_i=S_i is given to you as input.

Snuke can do the following three operations any number of times in any order.

  • Shift aa to the right. In other words, replace aa with (aN,a1,a2,cdots,aN1)(a_N,a_1,a_2,\\cdots,a_{N-1}).

  • Shift aa to the left. In other words, replace aa with (a2,a3,cdots,aN,a1)(a_2,a_3,\\cdots,a_N,a_1).

  • Append the current value of a1a_1 at the end of bb.

You are also given a sequence of MM integers T=(T1,T2,cdots,TM)T=(T_1,T_2,\\cdots,T_M). Determine whether it is possible to make bb equal to TT. If it is possible, find the minimum number of operations needed to do so.

Constraints

  • 1leqNleq2times1051 \\leq N \\leq 2 \\times 10^5
  • 1leqMleq2times1051 \\leq M \\leq 2 \\times 10^5
  • 0leqSileq10 \\leq S_i \\leq 1
  • 0leqTileq10 \\leq T_i \\leq 1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM S1S_1 S2S_2 cdots\\cdots SNS_N T1T_1 T2T_2 cdots\\cdots TMT_M

Output

If it is impossible to make bb equal to TT, print -1. If it is possible, print the minimum number of operations needed to do so.


Sample Input 1

3 4
0 0 1
0 1 1 0

Sample Output 1

6

The following sequence of six operations will do the job.

  • Append the current value of a1a_1 at the end of bb, making b=(0)b=(0).

  • Shift aa to the right, making a=(1,0,0)a=(1,0,0).

  • Append the current value of a1a_1 at the end of bb, making b=(0,1)b=(0,1).

  • Append the current value of a1a_1 at the end of bb, making b=(0,1,1)b=(0,1,1).

  • Shift aa to the right, making a=(0,1,0)a=(0,1,0).

  • Append the current value of a1a_1 at the end of bb, making b=(0,1,1,0)b=(0,1,1,0).


Sample Input 2

1 1
0
1

Sample Output 2

-1