Under construction

Still being built. Things may change

Fotografen

Interview

Time limit2sMemory limit1024 MB

Summary
Given N photos with rotations, find the fewest operations that rotate k adjacent photos clockwise at once to make all upright, or -1 if impossible.
Level

Medium5 of 10

Topics
Greedy, Prefix sum, Array
Solved
No attempts yet

Problem

A photographer took many nice photos with a digital camera and connects it to a computer to transfer them. The camera was held at different angles for each shot, so some photos are rotated. The four possible rotations are called up, right, down, and left, where up means the side that points up in the picture. A photo is correct when it is rotated up. The computer shows the photos in a list and has a function that rotates one photo 90 degrees clockwise. The rotation order is shown below.

How a photo is rotated

The photographer finds rotating photos one by one boring, so turns it into a game. The photographer picks a positive integer kk. The only way to rotate photos is to select exactly kk adjacent photos from the list and rotate all of them at once. Formally, there are NN photos a1,a2,…,aNa_1, a_2, \dots, a_N. The photographer picks an index ii (1≤i≤N−k+11 \leq i \leq N-k+1), and photos ai,ai+1,…,ai+k−1a_i, a_{i+1}, \dots, a_{i+k-1} are rotated 90 degrees clockwise. This is one operation.

The goal is to make every photo upright with as few operations as possible. Write a program that computes the minimum number of operations needed.

Input

The first line contains two integers NN and kk (1≤k≤N≤100 0001 \leq k \leq N \leq 100\,000), the total number of photos and the number of photos that must be rotated together.

The second line contains NN characters that give the starting rotation of the photos: U for up, H for right, N for down, and V for left.

Output

Print the minimum number of operations needed. If it is impossible to make all photos upright, print −1-1.

Hint

One possible optimal solution: rotate positions 3-4 three times to get UVVU, then rotate positions 2-3 once to get UUUU. This uses four operations in total.

Examples3

  1. Example 1

    Input
    4 2
    UVUH
    
    Expected output
    4
    
  2. Example 2

    Input
    8 5
    HUNVVNVH
    
    Expected output
    9
    
  3. Example 3

    Input
    5 2
    UUUUV
    
    Expected output
    -1