const
	MaxN = 1000100;
	
var
	inFile, outFile : text;
	n, m, sol, i : longint;
	s : array[0..MaxN] of char;
	p, d, rightmost : array[0..MaxN] of longint;

BEGIN

	assign(inFile, 'muzej.in');
	assign(outFile, 'muzej.out');
	reset(inFile); rewrite(outFile);
	
	readln(inFile, n, m);
	for i := 1 to n do
		read(inFile, s[i]);

	p[0] := 0;
	for i := 1 to n do
		if (s[i] = 'a') then
			p[i] := p[i - 1] + 1
		else
			p[i] := p[i - 1] - 1;

	for i := 0 to n do
		rightmost[i] := 0;

	d[0] := 0;
	for i := 1 to n do begin

		if (p[i] >= 0) then begin
		
			d[i] := d[ rightmost[ p[i] ] ] + 1;
			if ((p[i - 1] < p[i]) AND (d[i - 1] + 1 > d[i])) then
				d[i] := d[i - 1] + 1;

			rightmost[ p[i] ] := i;
			
		end
		else
			d[i] := -1;
	end;

	sol := 0;
	for i := 0 to m - 1 do
		if ((d[i] > sol) AND (p[n] - p[i] >= 0)) then
			sol := d[i];

	writeln(outFile, sol);
	close(inFile);
	close(outFile);

END.
