Code to calculate edit distances