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



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 bits = new BinaryIndexedTree[ p ];
	foreach( ref bit; bits )
	{
		bit = BinaryIndexedTree( ss.length );
	}
	
	auto m = 0L;
	foreach( i, s; ss )
	{
		m = ( m * 10 + s ) % p;
		bits[ m ].add( i, 1 );
	}
	
	auto ans = 0L;
	auto r = 0L;
	foreach( i, s; ss )
	{
		ans += bits[ r ].sum( i, ss.length );
		r = ( r * 10 + s ) % p;
	}
	return ans;
}

struct BinaryIndexedTree
{
	private long[] ps;
	
	public this( long n )
	{
		ps.length = 1 + n;
	}
	
	public void add( long i, long v )
	{
		for( auto j = i + 1; j < ps.length; j += j & -j ) ps[ j ] += v;
	}
	
	public auto sum( long l, long r )
	{
		auto lt = 0L;
		auto rt = 0L;
		for( auto j = l; 0 < j; j -= j & -j ) lt += ps[ j ];
		for( auto j = r; 0 < j; j -= j & -j ) rt += ps[ j ];
		return rt - lt;
	}
}
