Under construction

Still being built. Things may change

Maniac

Memory limit1024 MB

Summary
Given a substitution of the 26 letters and two strings, find how many times it must be applied to the first string to get the second, or -1 if impossible.
Level

Medium5 of 10

Topics
String, Implementation, Math
Solved
No attempts yet

Problem

Recently a maniac appeared in the city, and Sherlock took up the case as usual. Inspector Lestrade found the victims, and Dr. Watson found a Latin letter symbol on each body. Using his method of deduction plus a bit of intuition, Sherlock noticed that all victims' names begin with different letters. There were 26 victims in total, so for each Latin letter (the first letter of a victim's name) he matched it with the symbol found on that victim's body. In this way Sherlock built a correspondence between the symbols of the alphabet. From here on, f(c)f(c) denotes the symbol matched to the letter cc, where cc is one of the Latin letters.

When the detective had nearly lost hope of solving the puzzle, a letter from the maniac arrived. It contained only two lines of equal length and no explanation of what to do with them. After some thought, Sherlock realized that solving the riddle requires computing the following value: the number of times he must replace all characters of the first line (each character cc is replaced with f(c)f(c)) to obtain the second line. Sherlock does not want to clutter his head with extra information, so he asked you for help.

Input

The first line contains 26 characters. The kk-th character of this line is the symbol matched to the kk-th Latin letter.

The second line contains an integer nn (1≤n≤10000001 \le n \le 1000000), the length of the strings sent by the maniac.

Each of the next two lines contains a string of length nn made of lowercase Latin letters.

Latin letters are numbered from 1, starting with a and ending with z.

Output

Print the required number of character replacement operations on a single line.

If no answer exists, print -1.

Note

In the first test, the first string changes as follows. After the first replacement, abacaba becomes bcbdbcb (a is replaced by b, b by c, c by d). After the second replacement, bcbdbcb becomes cdcecdc (b is replaced by c, c by d, d by e). Two full replacements turn the first line into the second, so the answer is 2.

Examples1

  1. Example 1

    Input
    bcdefghijklmnopqrstuvwwwww
    7
    abacaba
    cdcecdc
    
    Expected output
    2