Under construction

Still being built. Things may change

Reading List

Time limit1sMemory limit128 MB

Summary
Total lifted books over a sequence of assignments, where each assigned book moves to the top of the tower.
Level

Medium5 of 10

Topics
Array, Sorting, Prefix sum, Simulation
Solved
No attempts yet

Problem

Mirko is a model student, yet he has always struggled with his required reading. This year he decided to put an end to it. He got hold of all NN books that the teacher can assign as required reading, numbered from 11 to NN. He stacked them all into a single tower from the smallest number to the largest, so book 11 is at the top of the tower.

Each time the teacher assigns book LiL_i, Mirko has to pull it out of the tower to read it. He does this by lifting all the books above book LiL_i, pulling out book LiL_i, and putting the lifted books back down on the tower. After Mirko finishes reading book LiL_i, he puts it back on top of the tower. Book LiL_i also counts as a lifted book.


Mirko reads the book numbered 33

For example, look at the tower in the figure. If the first assigned book is book 33, Mirko lifts three books, numbered 11, 22 and 33. He then puts books 11 and 22 back down. After he reads book 33, he places it on top of the tower.

You are given the sequence LL of books the teacher will assign. Compute how many books Mirko lifts in total this school year to read all the assigned books.

Input

The first line contains two positive integers NN and QQ (1≤N,Q≤1000001 \le N, Q \le 100000), the number of books and the number of assignments.

The second line contains QQ positive integers L1,L2,…,LQL_1, L_2, \ldots, L_Q (1≤Li≤N1 \le L_i \le N), the assigned books in order.

Some books may never be assigned. A book may also be assigned more than once, in which case Mirko must read it again.

Output

Print a single positive integer on the first line: the total number of books Mirko lifts.

Hint

Consider 44 books assigned in the order 11, 33, 22. Listing the tower from top to bottom, it changes as follows.

  • (1,2,3,4)→(1,2,3,4)(1, 2, 3, 4) \to (1, 2, 3, 4): only book 11 is lifted.
  • (1,2,3,4)→(3,1,2,4)(1, 2, 3, 4) \to (3, 1, 2, 4): books 11, 22 and 33 are lifted.
  • (3,1,2,4)→(2,3,1,4)(3, 1, 2, 4) \to (2, 3, 1, 4): books 11, 22 and 33 are lifted.

In total Mirko lifts 1+3+3=71 + 3 + 3 = 7 books.

Examples3

  1. Example 1

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

    Input
    2 8
    2 1 2 1 2 2 2 1
    
    Expected output
    14
    
  3. Example 3

    Input
    10 3
    10 10 10
    
    Expected output
    12