Попробуй, может сработает)
program num32;var
nums, i, min, max, a, i2, i3, sum: integer;begin
min := 7;
max := 0;
sum := 0;
readln(nums);
while nums <> 0 do
begin
if (((nums mod 10) > max)) then begin
max := nums mod 10;
end;
if (((nums mod 10) < min)) then begin
min := nums mod 10;
end;
nums := nums div 10;
end;
if (min > max) then begin
for i2 := max to min do
sum := sum + i2;
end;
if (max > min) then begin
for i2 := min to max do
sum := sum + i2;
end;
writeln(sum);
end.