This Excel formula will deliver the constant for any 4-digit value entered in cell N1 (that cell is formatted as text so as to support leading zeroes):
=UNIQUE(SCAN(N1,N10:N16,LAMBDA(a,b,IF(a=6174,a,IF(a=999,"9990",CONCAT(SORT(REGEXEXTRACT(a,".",1),,-1,1))-CONCAT(SORT(REGEXEXTRACT(a,".",1),,,1)))))))