edit_distance implementation