Under construction

Still being built. Things may change

It doesn't get simpler!

Time limit6sMemory limit1024 MB

Summary
Given n up to 10^500000, find the n-th element of the sequence built by concatenating blocks 1, 1 2 1, 1 2 3 2 1, and so on.
Level

Medium4 of 10

Topics
Math, Implementation
Solved
No attempts yet

Problem

Returning home from school, Ivan thought for a long time about that day's math club session, where the teacher talked about infinite number sequences. As one example, the teacher showed the following interesting sequence of positive integers: ,1⏟,1,2,1⏟,1,2,3,2,1⏟,1,2,3,4,3,2,1⏟,...\underbrace{\vphantom{,}1}, \underbrace{1, 2, 1}, \underbrace{1, 2, 3, 2, 1}, \underbrace{1, 2, 3, 4, 3, 2, 1}, ...

The teacher explained that every positive integer appears infinitely many times in this sequence. Ivan, however, was curious about another question: how can you find the number at position nn of the sequence? The teacher said that this is very simple and suggested that Ivan work out the problem on his own.

Ivan likes programming as well as math, so he wants to implement an algorithm that answers this question quickly for very large values of nn. Please help him.

Input

The first line contains one integer nn (1≤n≤10500 0001 \leq n \leq 10^{500\,000}).

Output

Print one integer without spaces and without leading zeros: the nn-th element of the given sequence.

Hint

The jury does not guarantee that a solution exists that runs within twice the time limit on all tests.

Examples1

  1. Example 1

    Input
    7
    
    Expected output
    3