// Arup Guha
// 2/1/2017
// Solution to 2017 January USACO Silver problem: Hoof, Paper Scissors!

import java.util.*;
import java.io.*;

public class hps_silver {

	public static void main(String[] args) throws Exception {

		// Read # of games.
		BufferedReader stdin = new BufferedReader(new FileReader("hps.in"));
		int n = Integer.parseInt(stdin.readLine());
		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[3][n];
		for (int i=0; i<n; i++)
			cumfreq[moves[i]][i]++;

		// Calculate a cumulative frequency array of each move.
		for (int i=0; i<3; i++)
			for (int j=1; j<n; j++)
				cumfreq[i][j] += cumfreq[i][j-1];

		// Try each split point, just play the best in each range.
		int best = 0;
		for (int i=0; i<=n; i++)
			best = Math.max(best, max(cumfreq, 0, i-1) + max(cumfreq, i, n-1));

		// Here is our result, the larger of these two.
		PrintWriter out = new PrintWriter(new FileWriter("hps.out"));
		out.println(best);

		out.close();
		stdin.close();
	}

	public static int max(int[][] arr, int start, int end) {

		// This isn't possible.
		if (start > end) return 0;

		int res = 0;

		// Go through all three moves.
		for (int i=0; i<3; i++) {

			// See if we need to subtract something out for frequency of this range.
			int sub = start > 0 ? arr[i][start-1] : 0;

			// Take the best of this move or the others.
			res = Math.max(res, arr[i][end]-sub);
		}
		return res;
	}

	public static int convert(String s) {
		if (s.equals("H")) return 0;
		if (s.equals("P")) return 1;
		return 2;
	}
}