Under construction

Still being built. Things may change

JOI Logo Design

Time limit1sMemory limit512 MB

Summary
Given a circular string of length 4^K over {J,O,I}, find the rotation that minimizes mismatches with a level-K JOI sequence built by recursive quarter block replacement.
Level

Hard8 of 10

Topics
Dynamic programming, Divide and conquer, String, Brute force
Solved
No attempts yet

Problem

Chairman K has decided to design a logo for merchandise supporting the Japanese Olympiad in Informatics contestants. One day Chairman K hit on the idea of arranging the characters 'J', 'O', and 'I' in a circle as the logo. The idea carries a wish for people to enjoy JOI.

For an integer k≥0k \ge 0, a level kk JOI sequence is defined as follows.

  • A level 0 JOI sequence is a string of length 1 consisting of one of the characters 'J', 'O', and 'I'.
  • A level k+1k+1 JOI sequence is a string of length 4k+14^{k+1} whose first 4k4^k characters are all 'J', whose next 4k4^k characters are all 'O', whose next 4k4^k characters are all 'I', and whose last 4k4^k characters form a level kk JOI sequence.

Chairman K now has a sheet of paper with 4K4^K characters written in a circle. Each character on the paper is one of 'J', 'O', and 'I'. Chairman K will rewrite some of the characters so that, when the characters on the paper are read clockwise around the circle starting from some character, they form a level KK JOI sequence. He wants to keep the number of rewritten characters as small as possible.

Given a string of length 4K4^K written in a circle on the paper, write a program that finds the minimum number of characters that must be rewritten so that reading the string clockwise around the circle from some starting character yields a level KK JOI sequence.

Input

Read the following data from standard input.

  • The first line contains an integer KK. This means that 4K4^K characters are written in a circle on the paper.
  • The second line contains a string of length 4K4^K consisting of 'J', 'O', and 'I'. This means that reading the characters on the paper clockwise around the circle starting from some character yields this string.

Output

Print the minimum number of characters Chairman K rewrites on a single line to standard output.

Constraints

  • 1≤K≤101 \le K \le 10.

Examples2

  1. Example 1

    Input
    1
    IJOI
    
    Expected output
    0
    
  2. Example 2

    Input
    2
    JJOIJJOJOIOJOOOI
    
    Expected output
    7