// Arup Guha
// 2/24/2017
// Solution to 2017 January USACO Gold problem: Hoof, Paper Scissors!

import java.util.*;
import java.io.*;

public class hps_gold {

	public static void main(String[] args) throws Exception {

		// Read # of games.
		BufferedReader stdin = new BufferedReader(new FileReader("hps.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int n = Integer.parseInt(tok.nextToken());
		int numSwitch = Integer.parseInt(tok.nextToken());
		int[] moves = new int[n];
		for (int i=0; i<n; i++)
			moves[i] = convert(stdin.readLine());

		// Get frequency array of each move.
		int[][] cumfreq = new int[n][3];
		int[][][] dp = new int[numSwitch+1][n][3];
		for (int i=0; i<n; i++)
			cumfreq[i][moves[i]]++;

		// The result is at least this...
		int res = Math.min(n, numSwitch+1);

		// Calculate a cumulative frequency array of each move.
		for (int i=1; i<n; i++) {
			for (int j=0; j<3; j++) {
				cumfreq[i][j] += cumfreq[i-1][j];
				dp[0][i][j] = cumfreq[i][j];
				res = Math.max(res, dp[0][i][j]);
			}
		}

		// Go through each number of switches.
		for (int i=1; i<=numSwitch; i++) {

			// Go through the DP array space.
			for (int j=i; j<n; j++) {
				for (int k=0; k<3; k++) {

					// Get other two players.
					int p1 = (k+1)%3;
					int p2 = (k+2)%3;

					// What we add for this particular move if we are currently beating k.
					int add = moves[j] == k ? 1 : 0;

					// dp[i][j][k] stores best score upto index j ending with beating k on exactly i switches.
					// So, if we stay with k, we build off i switches. If we switch from p1 or p2 to k, we build off i-1 switches.
					// We always build off of the sequence ending at index j-1.
					dp[i][j][k] = add + max(dp[i][j-1][k], dp[i-1][j-1][p1], dp[i-1][j-1][p2]);
					res = Math.max(res, dp[i][j][k]);
				}
			}
		}


		// Here is our result, the larger of these two.
		PrintWriter out = new PrintWriter(new FileWriter("hps.out"));
		out.println(res);

		out.close();
		stdin.close();
	}

	public static int max(int a, int b, int c) {
		return Math.max(Math.max(a, b), c);
	}

	public static int convert(String s) {
		if (s.equals("H")) return 0;
		if (s.equals("P")) return 1;
		return 2;
	}
}