Under construction

Still being built. Things may change

Interview

Interview

Time limit2sMemory limit1024 MB

Summary
Given two sequences a and b, find the length of the longest subsequence of a that is strictly increasing and also a subsequence of b.
Level

Medium6 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

At the job interview at Sasnart, the candidate solves the longest common increasing subsequence problem for two sequences.

From the sequence aia_i, pick a subsequence aika_{i_k} that satisfies all of the following.

  • It is increasing. That is, aik<aik+1a_{i_k} < a_{i_{k+1}} for every kk.
  • It is a subsequence of bib_i.
  • Its length is not less than the length of any sequence that satisfies the two conditions above.

Input

The first line contains two integers nn and mm (1≤n,m≤5,0001 \le n, m \le 5,000), the lengths of the sequences aia_i and bib_i. The second line contains the nn natural numbers of aia_i, and the third line contains the mm natural numbers of bib_i. Each number does not exceed 10,000.

Output

Print a single integer, the length of the longest sequence that has the properties described above.

Examples1

  1. Example 1

    Input
    6 5
    2 3 1 4 6 5
    1 2 5 4 6
    
    Expected output
    3