Generalize counting of different elements.
Fix largest s-distance bug.