Темы:
Remains
Arithmetic Algorithms
At the children's holiday, the children led round dances. Once the music had finished playing, the children were still standing in the circle. Then Lena remembered that her parents had given her a box of k Wilky May sweets. Lena is not greedy, so she decided to give all her sweets to friends from the round dance. Lena knows that some of her friends have a sweet tooth and some don't. Those with a sweet tooth take two candies from the box if there are at least two candies in the box, otherwise they take one. Lena's other friends always take exactly one candy from the box.
To start handing out sweets, Lena left the round dance, after which n of her friends remained in the round dance. To make it easier to hand out sweets, Lena assigned a number to each friend in the round dance, in clockwise order, starting with her best friend Roma, who received number 1.
Lena gave the box to a friend who received number l, after which each friend of Lena, starting with friend number l, took sweets from the box and passed the box to the next friend in clockwise order. After the friend with the number r took the candies, there were no candies left in the box. Please note that it is possible that some of Lena's friends took candies from the box several times, that is, the box could go several full circles in a round dance.
Lena does not know which of her friends have a sweet tooth, but she is interested in how many of her friends can have a sweet tooth. If such a situation could not happen, and Lena made a mistake in her observations, tell her about it.
Input
A single line contains four integers n, l, r, k (1 ≤ n, k ≤ 1011 , 1 ≤ l, r ≤ n ) — the number of children in the round dance, the number of the friend to whom Lena gave the box of chocolates, the number of the friend who took the last candy, and the number of candies in the box, respectively.
Imprint
Print a single integer — the maximum possible number of sweet teeth among Lena's friends or « -1 " (without quotes) if Lena made a mistake in her observations.
Examples
# |
Input |
Output |
Explanation |
1 |
4 1 4 12 |
2 |
Any two friends can have a sweet tooth, in which case each friend will receive a box of chocolates twice and the last person to take the candy will be the fourth person. |
2 |
5 3 4 10 |
3 |
Any three friends can be Sweet Tooth, except for the friend in third place. |
3 |
10 5 5 1 |
10 |
Only one friend will take one candy, but he can have a sweet tooth, he just can't take two candies. Everyone else in the circle can also have a sweet tooth, but they can't take a single piece of candy. |
4 |
5 4 5 6 |
-1 |
Lena made a mistake and this situation could not have happened. |
|