#D8691. Librarian's Work

    ID: 7226 Type: Default 5000ms 536MiB

Librarian's Work

Librarian's Work

Japanese Animal Girl Library (JAG Library) is famous for a long bookshelf. It contains NN books numbered from 11 to NN from left to right. The weight of the ii-th book is wiw_i.

One day, naughty Fox Jiro shuffled the order of the books on the shelf! The order has become a permutation b1,...,bNb_1, ..., b_N from left to right. Fox Hanako, a librarian of JAG Library, must restore the original order. She can rearrange a permutation of books p1,...,pNp_1, ..., p_N by performing either operation A or operation B described below, with arbitrary two integers ll and rr such that 1l<rN1 \leq l < r \leq N holds.

Operation A:

  • A-1. Remove plp_l from the shelf.
  • A-2. Shift books between pl+1p_{l+1} and prp_r to leftleft.
  • A-3. Insert plp_l into the rightright of prp_r.

Operation B:

  • B-1. Remove prp_r from the shelf.
  • B-2. Shift books between plp_l and pr1p_{r-1} to rightright.
  • B-3. Insert prp_r into the leftleft of plp_l.

This picture illustrates the orders of the books before and after applying operation A and B for p=(3,1,4,5,2,6),l=2,r=5p = (3,1,4,5,2,6), l = 2, r = 5.

Since the books are heavy, operation A needs $\sum_{i=l+1}^r w_{p_i} + C \times (r-l) \times w_{p_l}$ units of labor and operation B needs $\sum_{i=l}^{r-1} w_{p_i} + C \times (r-l) \times w_{p_r}$ units of labor, where CC is a given constant positive integer.

Hanako must restore the initial order from bi,...,bNb_i, ..., b_N by performing these operations repeatedly. Find the minimum sum of labor to achieve it.

Input

The input consists of a single test case formatted as follows.

NN CC b1b_1 wb1w_{b_1} : bNb_N wbNw_{b_N}

The first line conists of two integers NN and CC (1N105,1C1001 \leq N \leq 10^5, 1 \leq C \leq 100). The (i+1i+1)-th line consists of two integers bib_i and wbiw_{b_i} (1biN,1wbi1051 \leq b_i \leq N, 1 \leq w_{b_i} \leq 10^5). The sequence (b1,...,bNb_1, ..., b_N) is a permutation of (1,...,N1, ..., N).

Output

Print the minimum sum of labor in one line.

Examples

Input

3 2 2 3 3 4 1 2

Output

15

Input

3 2 1 2 2 3 3 3

Output

0

Input

10 5 8 3 10 6 5 8 2 7 7 6 1 9 9 3 6 2 4 5 3 5

Output

824

inputFormat

Input

The input consists of a single test case formatted as follows.

NN CC b1b_1 wb1w_{b_1} : bNb_N wbNw_{b_N}

The first line conists of two integers NN and CC (1N105,1C1001 \leq N \leq 10^5, 1 \leq C \leq 100). The (i+1i+1)-th line consists of two integers bib_i and wbiw_{b_i} (1biN,1wbi1051 \leq b_i \leq N, 1 \leq w_{b_i} \leq 10^5). The sequence (b1,...,bNb_1, ..., b_N) is a permutation of (1,...,N1, ..., N).

outputFormat

Output

Print the minimum sum of labor in one line.

Examples

Input

3 2 2 3 3 4 1 2

Output

15

Input

3 2 1 2 2 3 3 3

Output

0

Input

10 5 8 3 10 6 5 8 2 7 7 6 1 9 9 3 6 2 4 5 3 5

Output

824

样例

3 2
2 3
3 4
1 2
15