def permute(x):
    """
    permute returns a number in the range 0 to 9999
    that is unique for each x
    """
    prime = 9887
    offset = 453
    maximum = 9999
    x = (x + offset) % maximum
    if x >= prime:
        return x
    else:
        residue = x * x % prime
        if x <= prime / 2:
            return residue
        else:
            return prime - residue
