QOJ.ac

QOJ

시간 제한: 3 s 메모리 제한: 1024 MB 총점: 100 해킹 가능 ✓

#18671. Double

통계

Minggu has developed the next-generation chat app ChatChatA!

After deploying ChatChatA, a critical bug report came to Minggu! During the process of typing a message, when the last character of the message is a specific character and a specific character is entered, the message becomes "doubled"!

To explain precisely, "doubling" means the following.

  • Let the current message be $M$, the last character of $M$ be $s$, and the character to be input be $c$.
  • Let $D$ be the set of $(s, c)$ pairs that cause the message to "double".
    • If $(s, c) \in D$, then when $c$ is input, $M$ becomes $M + M + c$ instead of $M + c$!
    • If $(s, c) \notin D$, then when $c$ is input, $M$ becomes $M + c$.
  • If $M$ is the empty string, $M$ does not "double".

Guming, a user of ChatChatA, became curious about the minimum number of inputs needed to type the string $T$.

The current message $M$ starts as an empty string, and each input is one of the following two actions.

  • Add a character $c$ to $M$. The message may double according to the condition above.
  • Delete the last character of $M$. However, this action can only be chosen when $M$ is not empty.

Help Guming find the minimum number of inputs required to make the current message $M$ become $T$!

Input

The first line contains $N$, the size of $D$. ($1 \le N \le 676 = 26^2$)

The second line contains a string $S$ of length $N$ consisting of lowercase English letters, and the third line contains a string $C$ of length $N$ consisting of lowercase English letters.

Let the $i$-th character of $S$ be $S_i$, and the $i$-th character of $C$ be $C_i$. Then $D = \{(S_i, C_i) : 1 \le i \le N\}$.

$|D| = N$. That is, no identical $(S_i, C_i)$ pair is given.

The fourth line contains a string $T$ consisting of lowercase English letters. ($1 \le |T| \le 500\,000$)

Output

Output the minimum number of inputs required to make the current message become $T$ on the first line.

If it is impossible to make the current message become $T$, output $-1$.

Examples

Input 1

1
t
a
chatchata

Output 1

5

Input 2

2
ct
ha
chatchata

Output 2

-1

Input 3

2
af
bd
aafaaafaafaaafd

Output 3

8

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.