Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.

Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.

Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.

Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.


-----Input-----

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number s_{i} (1 ≤ s_{i} ≤ m), and one real number x_{i} (0 ≤ x_{i} ≤ 10^9), the species and position of the i-th plant. Each x_{i} will contain no more than 6 digits after the decimal point.

It is guaranteed that all x_{i} are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their x_{i} coordinates (x_{i} < x_{i} + 1, 1 ≤ i < n).


-----Output-----

Output a single integer — the minimum number of plants to be replanted.


-----Examples-----
Input
3 2
2 1
1 2.0
1 3.100

Output
1

Input
3 3
1 5.0
2 5.5
3 6.0

Output
0

Input
6 3
1 14.284235
2 17.921382
1 20.328172
3 20.842331
1 25.790145
1 27.204125

Output
2



-----Note-----

In the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.

In the second test case, the species are already in the correct order, so no replanting is needed.

Difficulty: MEDIUM_HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
(n, m) = map(int, input().split())
b = [list(map(float, input().split())) for i in range(n)]
k = [0] * n
k[0] = 1
for i in range(1, n):
	z = 0
	for y in range(i):
		if b[y][0] <= b[i][0]:
			z = max(k[y], z)
	k[i] = max(0, z) + 1
print(n - max(k))


def CeilIndex(A, l, r, key):
	while r - l > 1:
		m = l + (r - l) // 2
		if A[m] > key:
			r = m
		else:
			l = m
	return r

def LongestIncreasingSubsequenceLength(A, size):
	tailTable = [0 for i in range(size + 1)]
	len = 0
	tailTable[0] = A[0]
	len = 1
	for i in range(1, size):
		if A[i] < tailTable[0]:
			tailTable[0] = A[i]
		elif A[i] >= tailTable[len - 1]:
			tailTable[len] = A[i]
			len += 1
		else:
			tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]
	return len
arr = []
(n, m) = map(int, input().split())
for i in range(n):
	(a, b) = map(float, input().split())
	arr.append(a)
print(n - LongestIncreasingSubsequenceLength(arr, n))


import math
import sys
input = sys.stdin.readline
t = 1
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	l = []
	for __ in range(n):
		(s, x) = list(map(float, input().split()))
		l.append(int(s))
	dp = [0] * (n + 1)
	for i in range(n):
		j = l[i]
		for j1 in range(j, -1, -1):
			dp[j] = max(dp[j], 1 + dp[j1])
	maxi = max(dp[:])
	print(n - maxi)


from math import ceil, sqrt
from collections import defaultdict

def solve():
	(n, m) = list(map(int, input().split()))
	l = []
	for i in range(n):
		(a, b) = list(map(float, input().split()))
		l.append(a)
	dp = [0] * n
	for i in range(n):
		maxi = 0
		for j in range(i):
			if l[i] >= l[j]:
				maxi = max(dp[j], maxi)
		dp[i] = maxi + 1
	print(n - max(dp))
solve()


def f(arr):
	arr = [None] + arr
	dp = [0] * len(arr)
	for i in range(1, len(arr)):
		j = int(arr[i])
		for k in range(int(j), 0, -1):
			dp[j] = max(dp[j], 1 + dp[k])
	return len(arr) - max(dp) - 1

def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] >= arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	return max(len(lst) - max(lis), 0)
(a, b) = map(int, input().strip().split())
lst = []
for i in range(a):
	(x, y) = map(float, input().strip().split())
	lst.append(x)
print(lis(lst))


(n, m) = list(map(int, input().split()))
t = [int(input().split()[0]) for i in range(n)]
p = [0] * (m + 1)
for (i, j) in enumerate(t):
	p[j] = min(p[:j + 1]) - 1
print(n + min(p))


def solve(n, a):
	lis = [0] * n
	for i in range(0, n):
		for j in range(0, i):
			if a[i] >= a[j]:
				lis[i] = max(lis[i], lis[j] + 1)
		if lis[i] == 0:
			lis[i] = 1
	print(n - max(lis))

def main():
	inp = lambda : int(input().split()[0])
	n = inp()
	a = [inp() for _ in range(n)]
	solve(n, a)

def __starting_point():
	main()
__starting_point()


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

def solve(N, M, A):
	bit = [0 for _ in range(N)]

	def add(index, val):
		while index < N:
			bit[index] = max(bit[index], val)
			index |= index + 1

	def query(index):
		s = 0
		while index >= 0:
			s = max(s, bit[index])
			index = (index & index + 1) - 1
		return s
	q = [(v, i) for (i, v) in enumerate(A)]
	q.sort()
	s = 0
	for (v, i) in q:
		t = query(i) + 1
		add(i, t)
		s = max(s, t)
	return N - s
(N, M) = map(int, input().split())
A = []
for i in range(N):
	(x, y) = input().split()
	A.append(int(x))
print(solve(N, M, A))


(n, m) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = input().split()
	l.append(int(a))
dp = [1] * n
for i in range(1, n):
	for j in range(0, i):
		if l[i] >= l[j]:
			dp[i] = max(dp[i], dp[j] + 1)
print(n - max(dp))


(n, m) = [int(x) for x in input().split()]
d = [0 for i in range(m)]
for i in range(n):
	(c, x) = [x for x in input().split()]
	c = int(c)
	d[c - 1] = max(d[:c]) + 1
print(n - max(d))


import os
from sys import stdin, stdout
from collections import deque
from bisect import bisect_left, bisect_right
INF = int(1000000000.0)

def solve(tc):
	(n, m) = map(int, stdin.readline().split())
	species = []
	for i in range(n):
		(si, xi) = stdin.readline().split()
		species.append(int(si))
	dp = [[INF for j in range(m + 1)] for i in range(n + 1)]
	dp[0][0] = 0
	for i in range(1, n + 1):
		pk = species[i - 1]
		if dp[i - 1][pk] < INF:
			dp[i][pk] = dp[i - 1][pk]
		for j in range(pk):
			if dp[i - 1][j] < INF:
				dp[i][j] = dp[i - 1][j] + 1
				dp[i][pk] = min(dp[i][pk], dp[i - 1][j])
		for j in range(pk + 1, m + 1):
			if dp[i - 1][j] < INF:
				dp[i][j] = dp[i - 1][j] + 1
	ans = INF
	for j in range(m + 1):
		ans = min(ans, dp[n][j])
	print(ans)
tcs = 1
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


(n, m) = list(map(int, input().split()))
t = [int(input().split()[0]) for i in range(n)]
p = [0] * (m + 1)
for i in t:
	p[i] = max(p[1:i + 1]) + 1
print(n - max(p))


(n, m) = [int(x) for x in input().split()]
plant = [int(input().split()[0]) for x in range(n)]
dp = [1 for x in range(n)]
for i in range(len(plant)):
	for j in range(0, i):
		if plant[j] > plant[i]:
			continue
		dp[i] = max(dp[i], dp[j] + 1)
print(n - max(dp))


n = int(input().split()[0])
a = []
for _ in range(n):
	a.append(int(input().split()[0]))
dp = [0] * n
for i in range(n):
	dp[i] = 1
	for j in range(i):
		if a[j] <= a[i] and dp[j] >= dp[i]:
			dp[i] = dp[j] + 1
print(n - max(dp))


def lis(l, arr):
	ls = [1] * l
	for i in range(1, l):
		for j in range(0, i):
			if arr[i] >= arr[j] and ls[i] < ls[j] + 1:
				ls[i] = ls[j] + 1
	return l - max(ls)
(n, m) = map(int, input().split())
lst = []
for i in range(n):
	(a, b) = map(float, input().split())
	lst.append(a)
print(lis(n, lst))


from bisect import bisect_right
n = int(input().split()[0])
t = [int(input().split()[0]) for i in range(n)]
(p, k) = ([], 0)
for i in t:
	x = bisect_right(p, i)
	if x < k:
		p[x] = i
	else:
		p.append(i)
		k += 1
print(n - k)


(numbers, types) = list(map(int, input().split()))
position = [0] * numbers
binary_search_array = [1] * numbers
for i in range(numbers):
	position[i] = int(input().split()[0])
max_length = int(0)
for i in range(numbers):
	low = int(0)
	high = int(max_length)
	while low < high:
		mid = int((low + high) / 2)
		if binary_search_array[mid] <= position[i]:
			low = mid + 1
		else:
			high = mid
	binary_search_array[low] = position[i]
	if low == max_length:
		max_length += 1
print(numbers - max_length)


(n, m) = map(int, input().split())
type = [0]
for x in range(n):
	type.append(int(input().split()[0]))
dp = [[0] * (m + 1) for x in range(2)]
for i in range(1, n + 1):
	dp[i & 1][0] = 1000000000.0
	for j in range(1, m + 1):
		if type[i] == j:
			dp[i & 1][j] = dp[1 - (i & 1)][j]
		elif type[i] > j:
			dp[i & 1][j] = 1 + dp[1 - (i & 1)][j]
		elif j >= 1:
			dp[i & 1][j] = min(dp[i & 1][j - 1], 1 + dp[1 - (i & 1)][j])
		else:
			dp[i & 1][j] = dp[1 - (i & 1)][j]
print(dp[n & 1][m])


import sys
input = sys.stdin.buffer.readline

def solution():
	(n, m) = map(int, input().split())
	l = []
	dp = [1] * (n + 1)
	for i in range(n):
		(x, y) = map(float, input().split())
		l.append([x, y])
	for i in range(1, n):
		for j in range(i):
			if l[i][0] >= l[j][0]:
				dp[i] = max(dp[i], dp[j] + 1)
	print(n - max(dp))
t = 1
for _ in range(t):
	solution()


from sys import stdin
(n, m) = map(int, input().split())
s = [list(map(float, stdin.readline().strip().split()))[::-1] for i in range(n)]
s.sort()
s1 = [int(i[1]) for i in s]
dp = [1 for i in range(n)]
ans = 1
for i in range(n - 2, -1, -1):
	for j in range(i + 1, n):
		if s1[j] >= s1[i]:
			dp[i] = max(dp[i], 1 + dp[j])
	ans = max(dp[i], ans)
print(n - ans)


(n, m) = list(map(int, input().split()))
arr = []
for i in range(n):
	l = list(map(float, input().split()))
	arr.append(int(l[0]))
dp = [1 for i in range(n)]
for i in range(1, n):
	for j in range(i):
		if arr[j] <= arr[i]:
			dp[i] = max(dp[i], dp[j] + 1)
print(n - max(dp))