Код:
var
        a : array[1..101] of longint;
        x, y, d : int64;
        n, n1 : string;
        i, k, f : longint;
begin
        readln(n1);
        n := copy(n1, 1, pos(' ', n1) - 1);
        n1 := copy(n1, pos(' ', n1) + 1, length(n1) - length(n) - 1);
        val(n1, d, f);
        k := length(n);
        for i := k downto 1 do
                val(n[i], a[k - i + 1], f);
        y := 1;
        for i := 1 to k do
                begin
                x := (x + a[i] * y) mod d;
                y := (y * 10) mod d;
                end;
        writeln(x);
end.