Distance evaluating function