Rewriting distance measure function