AtCoder Regular Contest 125
A
Dial Up
AtCoder
⌀ 442
原题
←B →
翻译中…

Problem Statement

翻译中…

Snuke has a sequence of $N$ integers $a=(a_1,a_2,\cdots,a_N)$ consisting of $0$s and $1$s, and an empty integer sequence $b$. The initial state $a_i=S_i$ is given to you as input.

翻译中…

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

翻译中…
  • Shift $a$ to the right. In other words, replace $a$ with $(a_N,a_1,a_2,\cdots,a_{N-1})$.
  • Shift $a$ to the left. In other words, replace $a$ with $(a_2,a_3,\cdots,a_N,a_1)$.
  • Append the current value of $a_1$ at the end of $b$.
翻译中…

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

翻译中…

Constraints

翻译中…
  • $1 \leq N \leq 2 \times 10^5$
  • $1 \leq M \leq 2 \times 10^5$
  • $0 \leq S_i \leq 1$
  • $0 \leq T_i \leq 1$
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N M
S_1 S_2 \cdots S_N
T_1 T_2 \cdots T_M

翻译中…

Output

翻译中…

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

翻译中…

样例

Input #1
3 4
0 0 1
0 1 1 0
Output #1
6
The following sequence of six operations will do the job. - Append the current value of $a_1$ at the end of $b$, making $b=(0)$. - Shift $a$ to the right, making $a=(1,0,0)$. - Append the current value of $a_1$ at the end of $b$, making $b=(0,1)$. - Append the current value of $a_1$ at the end of $b$, making $b=(0,1,1)$. - Shift $a$ to the right, making $a=(0,1,0)$. - Append the current value of $a_1$ at the end of $b$, making $b=(0,1,1,0)$.
Input #2
1 1
0
1
Output #2
-1
题目信息
题号
arc125_a
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。