unittest
{
	assert( [ "1817181712114" ].parse.expand.solve == 3 );
	assert( [ "14282668646" ].parse.expand.solve == 2 );
	assert( [ "2119" ].parse.expand.solve == 0 );
}



import std.algorithm;
import std.conv;
import std.functional;
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 s = input.front.map!( ( a ) => ( a - '0' ).to!( long ) ).array;
	return tuple( s );
}

auto solve( long[] s )
{
	const M = 2019L;
	
	long[ long ] cs;
	cs[ 0 ]++;
	{
		auto r = 0L;
		foreach_reverse( i, d; s )
		{
			r = ( r + memoize!solve2( d, s.length.signed - i, M ) ) % M;
			cs[ r ]++;
		}
	}
	
	auto ans = 0L;
	foreach( r, c; cs )
	{
		ans += c * ( c - 1 ) / 2;
	}
	return ans;
}

long solve2( long d, long c, long m )
{
	if( c <= 1 ) return d;
	else         return memoize!solve2( d * 10 % m, c - 1, m );
}
