Understanding these two algorithms requires a basic understanding of modular arithmetic. When we do arithmetic modulo m, it means we can freely add and subtract multiples of m without changing the answer. For example, arithmetic modulo 12 is frequently referred to as clock arithmetic: 13 o'clock is the same as 1 o'clock, so 13 and 1 are modulo 12 congruent. When we work modulo 12, 5 is also congruent to 17, which is also congruent to 29. We can also go backward: subtracting 12 shows that 5 is congruent to -7. (This should be self-evident: if it is currently 12 o'clock, it will be 5 o'clock in 5 hours, and it was also 5 o'clock 7 hours ago.)

Source: