PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000028. Chèn ít nhất để tạo dãy con (Minimum Operations to Make a Subsequence)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chèn ít nhất để tạo dãy con (Minimum Operations to Make a Subsequence)

Minimum Operations to Make a Subsequence

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given arrays target and arr. All values in target are distinct; arr may contain duplicates. In one operation, you may insert any integer anywhere in arr. Determine the minimum number of insertions required so that target becomes a subsequence of arr. A subsequence preserves order but need not consist of consecutive elements.

Input

The first line contains n,mn,m, the lengths of target and arr. The second line contains nn target values; the third contains mm array values.

Output

Print the minimum number of insertions.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤201\le n,m\le 20; 1≤targeti,arrj≤1091\le target_i,arr_j\le10^9; all elements of target are distinct.
  • Subtask 2 (30 points): 1≤n,m≤7001\le n,m\le 700; 1≤targeti,arrj≤1091\le target_i,arr_j\le10^9; all elements of target are distinct.
  • Subtask 3 (50 points): 1≤n,m≤1000001\le n,m\le 100000; 1≤targeti,arrj≤1091\le target_i,arr_j\le10^9; all elements of target are distinct.

Examples

Input

3 5
5 1 3
9 4 2 3 4

Output

2

Explanation

We can keep the existing 3 and insert 5 and 1 before it, requiring two insertions.