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

#BS0000343. Tăng dãy theo phép chia dư (Increasing by Modulo)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tăng dãy theo phép chia dư (Increasing by Modulo)

Increasing by Modulo

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn integers in [0,m−1][0,m-1], an operation selects any set of distinct indices and increments their values modulo m simultaneously. Find the minimum number of operations needed to make the array nondecreasing.

Input

First line: n,mn,m. Second line: nn integers aia_i.

Output

Print the minimum number of operations.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤81\le n,m\le8; 0≤ai<m0\le a_i<m.
  • Subtask 2 (30 points): 1≤n≤3000001\le n\le300000; 1≤m≤301\le m\le30; 0≤ai<m0\le a_i<m.
  • Subtask 3 (50 points): 1≤n,m≤3000001\le n,m\le300000; 0≤ai<m0\le a_i<m.

Examples

Input 1

5 7
0 6 1 3 2

Output 1

1

Explanation

Increment positions 2 and 5 to obtain [0,0,1,3,3]. The initial array is not nondecreasing.

Input 2

4 3
0 0 1 2

Output 2

0

Explanation

The array is already nondecreasing.