def kap(n):
  """
  Execute and return result of a Kaprekar's routine step for number n.

  >>> kap(6174)
  6174
  >>> kap(2341)
  3087
  >>> kap(3087)
  8352
  >>> kap(8352)
  6174
  """
  n = format(n, '04d')
  small = int(''.join(sorted(n)))
  big = int(''.join(sorted(n, reverse=True)))
  return big - small