Under construction

Still being built. Things may change

Bajka

Time limit1sMemory limit512 MB

Summary
Find the least time to write word t by moving a finger over word s, where adjacent moves write a letter and same-letter jumps write nothing.
Level

Medium6 of 10

Topics
Dynamic programming, String
Solved
No attempts yet

Problem

Little Fabijan got bored with picture books, so he decided to read his first fairytale. Unfortunately, Fabijan often encounters a word that scares him. To overcome his fear, he will play a game he invented.

The scary word is an array of nn lowercase letters. To start the game, Fabijan puts his finger on some position of the array and writes the letter at that position on a piece of paper. He then performs one of the following moves any number of times:

  • He moves the finger to a position one place to the left or to the right of the current position, if that position exists. He then writes the letter at the new position on the paper, after the last written letter.
  • He moves the finger to any position with the same letter as the current one. He writes nothing on the paper in this case.

Moving the finger from position xx to position yy takes ∣x−y∣|x - y| seconds.

Fabijan overcomes his fear if his favourite word is written on the paper at the end of the game. He wants to finish the fairytale as soon as possible, so find the minimum number of seconds needed to write his favourite word.

Input

The first line contains two integers nn and mm (1≤n,m≤3001 \le n, m \le 300).

The second line contains nn lowercase letters, the scary word.

The third line contains mm lowercase letters, Fabijan's favourite word.

Output

Print the shortest possible time in seconds needed to write the favourite word on the paper, or −1-1 if that is not possible.

Hint

Clarification of the third sample: Fabijan first puts his finger on position 7 and writes down the letter 'b'. He then moves the finger two times to the left, and each time writes down the letter 'o'. Next, he moves the finger to position 6 using the second type of move. Finally, he moves the finger two times to the left and writes down the letters 'o' and 'k'. Each move takes one second, so the total time is 5 seconds.

Examples3

  1. Example 1

    Input
    2 2
    wa
    ac
    
    Expected output
    -1
    
  2. Example 2

    Input
    7 7
    monolog
    nogolom
    
    Expected output
    10
    
  3. Example 3

    Input
    14 5
    niskoobrazovan
    boook
    
    Expected output
    5