x = int(raw_input()) y = int(raw_input()) l = int(raw_input()) c = (x + l - 1) % l + (y + l - 1) % l + 1 if y < 0: c += 1 print c