#abc261g. [abc261_g]Replace
[abc261_g]Replace
Problem Statement
You are given two strings and consisting of lowercase English letters.
Takahashi starts with the string . He can perform kinds of operations any number of times in any order.
The -th operation is the following:
Pay a cost of . Then, if the current string contains the character , choose one of its occurrences and replace it with the string . Otherwise, do nothing.
Find the minimum total cost needed to make the string equal . If it is impossible to do so, print .
Constraints
- is
a
,b
,, orz
. - , , and are strings consisting of lowercase English letters.
- , regarding as a string of length .
- All pairs are distinct.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum total cost needed to make the string equal . If it is impossible to do so, print .
Sample Input 1
ab
cbca
3
a b
b ca
a efg
Sample Output 1
4
Starting with ab
, Takahashi can make cbca
in four operations as follows:
- Replace the -st character
a
inab
withb
(Operation of the -st kind). The string is nowbb
. - Replace the -nd character
b
inbb
withca
(Operation of the -nd kind). The string is nowbca
. - Replace the -st character
b
inbca
withca
(Operation of the -nd kind). The string is nowcaca
. - Replace the -nd character
a
incaca
withb
(Operation of the -st kind). The string is nowcbca
.
Each operation incurs a cost of , for a total of , which is the minimum possible.
Sample Input 2
a
aaaaa
2
a aa
a aaa
Sample Output 2
2
Two operations a
aaa
aaaaa
incur a cost of , which is the minimum possible.
Sample Input 3
a
z
1
a abc
Sample Output 3
-1
No sequence of operations makes z
from a
.