Anton and Fairy Tale
Contest: Codeforces Round 404 (Div. 2)
Difficulty rating: 1600
Tags: binary search, math

Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:

"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."

More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:

- m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
- Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.

Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!

Input format:
The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.

Output format:
Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:

- At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
- At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
- At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
- At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
- At the beginning of the third day two grains are brought. The barn becomes full again.
- At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
- At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
- At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.

So the answer is 4, because by the end of the fourth day the barn becomes empty.

Example input:
5 2

Example output:
4

Example input:
8 1

Example output:
5

Editorial:
At first, let's make the following assumption: if a sparrow cannot eat a grain because the barn is empty, the number of grains in the barn becomes negative. It's easy to see that the answer doesn't change because of this.
Now, let's observe the number of grains before sparrows come. At first, the barn remains full for m + 1 days (because sparrows eat less grains than it's added to the barn). Then the number of grains is decreased by one, by two and so on. So, on the m + 1 + i-th day there are $$n = \frac{i(i+1)}{2}$$ grains in the barn before sparrows come (remember that for any positive integer x the equality $$1 + 2 + 3 + \cdots + x = \frac{x(x+1)}{2}$$ is always true).
How can we determine if the barn is empty? It's reasonable that if there are q grains on the k-th day after grain is brought, then at the end of the k - 1-th day there are q - m grains in the barn. So, if on the k - 1-th day the barn becomes empty (q - m ≤ 0), then there must be q ≤ m grains on the k-th day after grain is brought.
So, we must find such minimal day m + 1 + k, in which there are m or less grains after grain is brought. That is, using the formula above, we must find such minimal k that
$$n - \frac{k(k+1)}{2} \leq m$$
It can be easily done using binary search. It's not hard to observe that the answer in this case is m + k (if in the m + 1 + k-th day before sparrows come there are less or equal than m grains, then in the m + 1 + k - 1 = m + k-th day the barn is empty).
The corner case in this problem is m ≥ n. In this case the barn becomes full every day and it becomes empty only in the n-th day when sparrows eat all the grain.
Also notice that k can be found using a formula, but such solutions could fail by accuracy, because the formula is using the square root function.
Time complexity is $${\mathcal {O}}(\log n)$$.