Aronson's Sequence

No attempts yetTime limit2sMemory limit128 MB

Problem

Aronson's sequence $a_k$ is defined by the self-referential sentence:

"T is the first, fourth, eleventh, ... letter of this sentence."

The blanks (...) are filled in so that the sentence describes itself truthfully. Read the sentence and count only letters (spaces, punctuation, and digits are ignored, and case is ignored). Then $a_k$ is the position of the $k$-th occurrence of the letter T. The first few values are:

$$1,\ 4,\ 11,\ 16,\ 24,\ 29,\ 33,\ 35,\ 39,\ \dots$$

For $k \le 100000$ it can be shown that $a_k \le 1000000$.

To build the sentence you must spell ordinal numbers in English. Ordinals (first, second, third, …) are defined from the cardinals (one, two, three, …), so the cardinals are described first.

  • A cardinal below twenty is a single word (3 → three, 17 → seventeen).
  • A cardinal from twenty to ninety-nine is the tens word, followed by the nonzero ones word (40 → forty, 56 → fifty six).
  • A cardinal from one hundred to nine hundred ninety-nine is the hundreds part, followed by the nonzero remainder (100 → one hundred, 117 → one hundred seventeen, 640 → six hundred forty, 999 → nine hundred ninety nine).
  • A cardinal from one thousand to nine hundred ninety-nine thousand nine hundred ninety-nine is the thousands part, followed by the nonzero remainder (12345 → twelve thousand three hundred forty five).

An ordinal is written like its cardinal, but the last word is turned into its ordinal form:

3rd → third, 56th → fifty sixth, 100th → one hundredth, 12345th → twelve thousand three hundred forty fifth.

Input

The input contains several queries. Each query is a positive integer $k$ on its own line ($1 \le k \le 100000$). The queries are given in non-decreasing order. The input ends with a line containing a single 0.

Output

For each query $k$, print $a_k$ on its own line. Every $a_k$ is at most $1000000$.