unittest
{
	assert( [ "4 3", "3553" ].parse.expand.solve == 2 );		// 追加ケース
	
	assert( [ "4 3", "3543" ].parse.expand.solve == 6 );
	assert( [ "4 2", "2020" ].parse.expand.solve == 10 );
	assert( [ "20 11", "33883322005544116655" ].parse.expand.solve == 68 );
}



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 np = input.front.split.to!( long[] );
	input.popFront;
	auto ss = input.front.map!( ( a ) => ( a - '0' ).to!long ).array;
	return tuple( np[ 1 ], ss );
}

auto solve( long p, long[] ss )
{
	auto ts = new long[ p ];
	auto m = 0L;
	foreach( i, s; ss )
	{
		m = ( m * 10 + s ) % p;
		ts[ m ]++;
	}
	
	auto ans = 0L;
	m = 0L;
	foreach( i, s; ss )
	{
		ans += ts[ m ];
		m = ( m * 10 + s ) % p;
		ts[ m ]--;
	}
	return ans;
}
