#arc125a. [arc125_a]Dial Up
[arc125_a]Dial Up
Problem Statement
Snuke has a sequence of integers consisting of s and s, and an empty integer sequence . The initial state is given to you as input.
Snuke can do the following three operations any number of times in any order.
-
Shift to the right. In other words, replace with .
-
Shift to the left. In other words, replace with .
-
Append the current value of at the end of .
You are also given a sequence of integers . Determine whether it is possible to make equal to . If it is possible, find the minimum number of operations needed to do so.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
If it is impossible to make equal to , 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 at the end of , making .
-
Shift to the right, making .
-
Append the current value of at the end of , making .
-
Append the current value of at the end of , making .
-
Shift to the right, making .
-
Append the current value of at the end of , making .
Sample Input 2
1 1
0
1
Sample Output 2
-1