Under construction

Still being built. Things may change

Double Deck

Time limit2sMemory limit1024 MB

Summary
Find the maximum number of matched pairs between two decks of N*K cards, where each mismatch discards the top card of one deck.
Level

Hard8 of 10

Topics
Dynamic programming, Bit manipulation, String
Solved
No attempts yet

Problem

You are playing a new card game. Each of the two decks has N⋅KN \cdot K cards, labeled with integers from 11 to NN. Each label appears exactly KK times in each deck.

The rules are simple. You shuffle both decks and place them face up in front of you, so you always see the top card of each deck. If the top cards match, you take both and get one point. If they do not match, you must discard one of the two cards. Your goal is to score as many points as possible.

You have just finished a round. Given the layout of both decks, find the maximum score you could have gotten.

Input

The first line contains two integers NN and KK (1≤N≤1041 \leq N \leq 10^4, 1≤K≤151 \leq K \leq 15). The second and third lines each contain N⋅KN \cdot K integers xix_i (1≤xi≤N1 \leq x_i \leq N), which describe the layout of the decks. x1x_1 is the top card, x2x_2 is the next card, and so on. No integer appears more than KK times in either line.

Output

Print one integer, the maximum possible score.

Examples2

  1. Example 1

    Input
    3 2
    3 1 2 3 1 2
    2 1 3 1 3 2
    
    Expected output
    4
    
  2. Example 2

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