calculate nearest distance