Swapping the Stones

Move stones along empty arcs of a circular shore so black and white stones exchange position sets with minimum total carry distance, or report impossibility.

Hard8GreedyString matchingPrefix sumNo attempts yetTime limit2sMemory limit32 MB

Problem

Minhyuk is looking at the 2N2N stones lying along the shore of a lake when a thought strikes him. There are NN white stones and NN black stones, so what if he swapped the positions of the white stones and the black stones?

Minhyuk has nothing better to do, so he decides to really rearrange the colours. He owns no paint or any tool like it, so he has to lift the stones one at a time and carry them.

Before he starts, he walks once around the lake and measures the circumference and the position of every stone, both taken from the point where he started (position 00).

The stones are very heavy, so carrying a stone a distance of xx costs xx force. Another stone in the way is a nuisance, so the stretch of shore he carries a stone across must contain no other stone.

Minhyuk does not want to spend force on a pointless job. Find the smallest force he needs to swap the positions of the black stones and the white stones.

Input

The first line contains the circumference of the lake RR and the number of black stones NN. Each of the next 2N2N lines contains the position PkP_k of one stone and its colour CkC_k. A CkC_k of B means a white stone, and a CkC_k of W means a black stone. The number of black stones equals the number of white stones, and the stones are given in increasing order of PkP_k. (1N2000001 \le N \le 200000, 2NR1092N \le R \le 10^9, 0Pk<R0 \le P_k < R)

Output

If Minhyuk cannot swap the positions of the black stones and the white stones, print -1. Otherwise print the smallest force he needs. The answer can be very large.

Hint

You may assume that a stone has size 00, and the point where Minhyuk puts a stone down does not have to be an integer. After all the stones have been moved, they must sit at the same positions as before with only their colours exchanged.