unittest
{
	assert( [ "3", "ABBAA" ].parse.expand.solve == 16 );
	assert( [ "4", "ABBAAABBAA" ].parse.expand.solve == 46 );
	assert( [ "100", "AAABAAAABAAAAABBBBBBBBBAABBBBBBAAAAABBB" ].parse.expand.solve == 4900 );
	
	assert( [ "3", "A" ].parse.expand.solve == 6 );		// 追加ケース
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = input.front.to!( long );
	input.popFront;
	auto s = input.front.to!( dchar[] );
	return tuple( n, s );
}

auto solve( long n, dchar[] s )
{
	auto ans = 0L;
	auto gs = s.group.array;
	if( gs.length == 1 )
	{
		if( gs.front[ 0 ] == 'A' )
		{
			auto f = gs.front[ 1 ] * n;
			ans += f * ( f + 1L ) / 2;
		}
	}
	else if( gs.front[ 0 ] == 'B' || gs.back[ 0 ] == 'B' )
	{
		foreach( g; gs )
		{
			if( g[ 0 ] == 'A' ) ans += g[ 1 ] * ( g[ 1 ] + 1L ) / 2;
		}
		ans *= n;
	}
	else
	{
		foreach( g; gs[ 1 .. $ - 1 ] )
		{
			if( g[ 0 ] == 'A' ) ans += g[ 1 ] * ( g[ 1 ] + 1L ) / 2;
		}
		ans *= n;
		
		auto f = gs.front[ 1 ];
		auto b = gs.back[ 1 ];
		auto c = f + b;
		ans += f * ( f + 1L ) / 2;
		ans += b * ( b + 1L ) / 2;
		ans += c * ( c + 1L ) / 2 * ( n - 1 );
	}
	return ans;
}
