unittest
{
	assert( [ "3", "3 5", "2 7", "9 4" ].parse.expand.solve == 7 );
	assert( [ "7", "3 1", "4 1", "5 9", "2 6", "5 3", "5 8", "9 7" ].parse.expand.solve == 22 );
}



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 );
	auto abs = new long[ 2 ][ n ];
	foreach( ref ab; abs )
	{
		input.popFront;
		ab = input.front.split.to!( long[] );
	}
	return tuple( abs );
}

auto solve( long[ 2 ][] abs )
{
	auto ans = 0L;
	foreach_reverse( ab; abs )
	{
		auto r = ( ans + ab[ 0 ] ) % ab[ 1 ];
		if( 0 < r ) ans += ab[ 1 ] - r;
	}
	return ans;
}
