The short answer in Python is
lambda a, b, size: min((a - b) % size, (b - a) % size)
where a and b are inputs and size is the number of elements (24 for this case).
The slightly longer answer is
SIZE = 24
def dis(a, b):
d1 = (a - b) % size
d2 = (b - a) % size
return min(d1, d2)
And, if you don't like modular arithmetic,
SIZE = 24
def dis2(a, b):
high = max(a, b)
low = min(a, b)
dis1 = high - low
dis2 = SIZE - dis1
return min(dis1, dis2)
That one only works for values of a and b in range(0, 24), but it saves you the trouble of having to understand modular arithmetic. ;)