Regular Cost Functions, Part I: Logic and Algebra over Words (Logical Methods in Computer Science, 2013)