Under construction

Still being built. Things may change

Restaurant Guide

Time limit2sMemory limit1024 MB

Summary
Split restaurants ranked by two critics into the most star levels, each holding at least K restaurants, with higher stars ranked above in both rankings.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Sorting
Solved
No attempts yet

Problem

Yoon is building a guide app, UNIST Guide, that recommends restaurants near UNIST. Yoon picked NN restaurants and numbered them from 11 to NN.

Yoon found rankings from two food critics, Donggyu and Wone, who each ranked all NN restaurants. Yoon wants to assign ratings based on these two rankings. A rating is an integer star count from 11 to SS, where SS is a positive integer. A higher star count means a tastier restaurant.

A rating must not contradict either critic's ranking. That is, if restaurant X has a higher rating than restaurant Y, then X must rank above Y in both critics' rankings. The converse does not need to hold.

Every rating level must also be given to at least KK restaurants.

Yoon believes the guide becomes more credible when its information is more detailed, so Yoon wants to split the ratings as finely as possible. In other words, Yoon wants the highest star count SS to be as large as possible. When the ratings satisfy all the conditions, what is the maximum number of rating levels?

Input

The first line contains two integers NN and KK, separated by a space, where NN is the number of restaurants and KK is the minimum number of restaurants for each rating level. (1≤K≤N≤200 0001\le K\le N\le 200\ 000)

The second line contains the NN restaurant numbers in the order of Donggyu's ranking, from highest to lowest, separated by spaces.

The third line contains the NN restaurant numbers in the order of Wone's ranking, from highest to lowest, separated by spaces.

Each restaurant number is an integer from 11 to NN, and no number repeats within a line.

Output

Print the maximum number of rating levels that Yoon can achieve when the ratings are split as finely as possible.

Examples2

  1. Example 1

    Input
    8 3
    1 2 3 4 5 6 7 8
    2 1 4 3 6 5 8 7
    
    Expected output
    2
    
  2. Example 2

    Input
    4 1
    4 2 3 1
    2 1 4 3
    
    Expected output
    1