All problems

Problem 231

Digital Root

Easy · 10 pts · Math

Problem

You are given a non-negative integer N. Replace N with the sum of its digits, and keep doing this until only a single digit remains. Print that digit, called the digital root of N. You can simulate the process, which takes only a few rounds. There is also a formula: the digital root of 0 is 0, and for N > 0 it is 1 + (N - 1) mod 9, because a number and its digit sum always leave the same remainder when divided by 9. The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).

Input

A single integer N.

Output

Print the digital root.

Constraints

0 <= N <= 10^18

Example input

38

Example output

2

Explanation

For 38: the digit sum is 3 + 8 = 11, which still has two digits, so continue: 1 + 1 = 2. The digital root is 2. The formula agrees: 1 + 37 mod 9 = 1 + 1 = 2.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Sum of First N Natural Numbers Next: Collatz Steps