Under construction

Still being built. Things may change

Just Shuffle the Input

Interview

Time limit3sMemory limit512 MB

Summary
Given strings s and t and a cyclic permutation p, find the fewest shuffles of t by p that make it a substring of s, or -1 if impossible.
Level

Medium6 of 10

Topics
String matching, String, Hash map
Solved
No attempts yet

Statement

A permutation pp of size nn is a sequence of nn pairwise distinct numbers from 11 to nn. We write p(i)p(i) for the ii-th element. pk(i)p^k(i) denotes pp applied kk times, that is, p(p(…p(i)…))p(p(\ldots p(i)\ldots)). A permutation is cyclic if the smallest positive integer kk with pk(1)=1p^k(1)=1 equals nn.

You are given a string ss of length nn, a string tt of length mm, and a cyclic permutation pp of size mm. You want tt to become a substring of ss. To do this, you may apply the shuffle operation zero or more times. The shuffle operation replaces tt with t′t' such that the ii-th letter of tt equals the p(i)p(i)-th letter of t′t' for every ii from 11 to mm.

A string aa is a substring of ss if there exists an ll with 1≤l≤∣s∣−∣a∣+11 \le l \le |s|-|a|+1 such that sl+i−1=ais_{l+i-1}=a_i for every ii from 11 to ∣a∣|a|.

Determine whether you can obtain a substring of ss from tt. If you can, find the minimum number of shuffle operations needed.

Input

The first line contains two integers nn and mm (1≤m≤n≤2000001 \le m \le n \le 200000). The second line contains mm integers p(1),…,p(m)p(1), \ldots, p(m), the permutation you can apply. The next two lines contain the string ss of length nn and the string tt of length mm. Both strings consist of lowercase English letters. The given permutation is guaranteed to be cyclic.

Output

If you cannot obtain a substring of ss from tt, print −1-1. Otherwise, print the minimum number of shuffle operations needed.

Examples2

  1. Example 1

    Input
    3 2
    2 1
    aba
    ba
    
    Expected output
    0
    
  2. Example 2

    Input
    7 4
    3 4 2 1
    dcabadc
    abcd
    
    Expected output
    1